SQL语句作为参数

时间:2013-03-29 14:46:18

标签: sql

是否可以在SQL中执行类似的操作?

DECLARE @t Nvarchar(50)

SET @t = 'SELECT * FROM KIN_PHON'

execute @t  --??

1 个答案:

答案 0 :(得分:1)

使用exec

DECLARE @t Nvarchar(50)
SET @t = 'SELECT * FROM KIN_PHON'
exec (@t)

或使用参数sp_executesql

declare @sql nvarchar(max)
set @sql = 'select * from YourTable where ID = @ID'
execute sp_executesql @sql, N'@ID int', @ID = 42;