sp_executesql的用法

问题:

declare @test varchar(1000)
declare @total Integer
SET @test= '21,23,24,25,26,27'

EXEC ('
declare @count Integer
select @count=count(*) from NeiEntityContainmentTable
WHERE myentitytype in (' + @test + ')')

我想将@count的结果返回到@total中,如何???

回答:

---------------------------------------------------------------

declare @test Nvarchar(1000)
declare @total Int
declare @sql Nvarchar(1000)

SET @test= N'21,23,24,25,26,27'

set @sql=N'
select @count=count(*) from NeiEntityContainmentTable
WHERE myentitytype in (' + @test + N')'

exec sp_executesql @sql, N'@count int output', @total output

Published At
Categories with 数据库类
comments powered by Disqus