我现在正想实现
declare @id char(10)
declare @tablename sysname
set @tblename = outputtbale
set @id = 'storageid'
exec('declare cur1 cursor scroll for select ' + @id + ' from' + @tablename)
但是, 在执行的时候长长报错误: @id列名无效但是,exec('declare cur1 cursor scroll for select storageid from' + @tablename)
就可以通过了.
是不是SQL SERVER里面不能够实现字段名的动态实现的.还是有办法只是我的思路有有问题
请指教
谢谢
declare @id char(10)
declare @tablename sysname
set @tblename = outputtbale
set @id = 'storageid'
exec('declare cur1 cursor scroll for select ' + @id + ' from' + @tablename)
但是, 在执行的时候长长报错误: @id列名无效但是,exec('declare cur1 cursor scroll for select storageid from' + @tablename)
就可以通过了.
是不是SQL SERVER里面不能够实现字段名的动态实现的.还是有办法只是我的思路有有问题
请指教
谢谢
set @tblename = outputtbale --拼写错误 @tablename
declare @tablename sysname
set @tblename = 'outputtbale'
set @id = 'storageid'
exec('declare cur1 cursor scroll for select [' + @id + '] from [' + @tablename+']')
declare @tablename sysname
set @tblename = outputtbale
// 改为 set @tablename = 'outputtable'
set @id = 'storageid'
exec('declare cur1 cursor scroll for select ' + @id + ' from' + @tablename)
//改为 exec('declare cur1 cursor scroll for select ' + @id + ' from ' + @tablename)
或者 exec('declare cur1 cursor scroll for select [' + @id + '] from [' + @tablename + ']')