tryxu 发表于 2008-8-29 18:58

求救 SQL中動態獲取字段名字,查詢出它的值並賦給變量

exec('select  '+@size_qty+'='+@name+' from sc_colour_kids where key_seq ='+ @link_key_Seq)

其中@name為sc_colour_kids中的一個字段的名字 動態獲取的,我要查詢@name的值存入變量@size_qty中,我上面寫的會報錯,如下錯誤信息:Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.請各位幫忙修改一下,謝謝。

hw1024 发表于 2008-8-30 09:06

当然报错啦,
C#技术交流,数据库交流群:64913828

ting4763 发表于 2008-8-30 15:51

declare @sql nvarchar(50)
set @sql = 'select @size_qty = ' + @name + ' from sc_colour_kids where key_seq ='+ @link_key_Seq
exec sp_executesql @sql, N'@size_qty nvarchar output', @size_qty output

tryxu 发表于 2008-8-30 23:03

回复 2# hw1024 的帖子

谢谢,我自己搞定了,我多加了个临时表

tryxu 发表于 2008-8-30 23:52

回复 1# tryxu 的帖子

公司不让上QQ啊,没办法咯

yi1018 发表于 2008-9-1 13:50

回复 5# tryxu 的帖子

我们也是

ting4763 发表于 2008-9-1 14:10

看来都是一样的啊[em12]

页: [1]

编程论坛