DECLARE @query as varchar(200);
SET @query = 'SELECT COUNT(*) FROM table';
如何执行@query
,另外,在分配变量时有没有办法直接存储查询结果?
您可以使用sp_executesql
withoutput
参数来检索标量结果。
DECLARE @query as nvarchar(200), @count int;
SET @query = N'SELECT @count = COUNT(*) FROM table';
EXEC sp_executesql @query,
N'@count int OUTPUT',
@count = @count OUTPUT
SELECT @count AS [@count]
试试这个 :
declare @query as varchar(200)
select @query = COUNT(*) from table