0
declare @value varchar(500)
declare @Whrcol varchar(500)
declare @Whrvalue varchar(500)
set @Whrcol='Product Cat3'
set @Whrvalue='IR DOME CAMERA'
set @value=('select [Table Name] 
             from tblProductsCatalog 
             where ['+ @Whrcol+ '] ='''+@Whrvalue+'''')
execute (@value)

我想通过 Execute 语句保存返回的值,以便我可以在下一个语句中使用,即

execute('select * from '+@value+ ' where IsActive =1')

但我收到一个错误。

我该怎么做?

我已经尝试了几个小时但无法解决。

4

2 回答 2

0

使用 sp_executesql。

请注意,@value 需要更改为 nvarchar。此外,我分离了返回的值,而不是覆盖 @value 变量。

declare @value nvarchar(500)
declare @Whrcol varchar(500)
declare @Whrvalue varchar(500)
declare @returnValue varchar(500)  

set @Whrcol='Product Cat3'
set @Whrvalue='IR DOME CAMERA'
set @value='select TOP 1 @returnValue = [Table Name] 
             from tblProductsCatalog 
             where ['+ @Whrcol+ '] ='''+@Whrvalue+''''
exec sp_executesql @value, N'@returnValue varchar(500) output', @returnValue output

execute('select * from '+@returnValue+ ' where IsActive =1')
于 2013-03-05T13:55:45.513 回答
0

试试这个方法:

declare @value varchar(500)
declare @Whrcol varchar(500)
declare @Whrvalue varchar(500)
set @Whrcol='Product Cat3'
set @Whrvalue='IR DOME CAMERA'
execute('select @value = [Table Name] 
             from tblProductsCatalog 
             where ['+ @Whrcol+ '] ='''+@Whrvalue+'''')
execute('select * from '+@value+ ' where IsActive =1')
于 2013-03-05T11:23:23.713 回答