DECLARE @query as varchar(200);
SET @query = 'SELECT COUNT(*) FROM table';
如何执行@query
,另外,在分配变量时是否可以直接存储查询结果?
答案 0 :(得分:29)
您可以使用sp_executesql
和output
参数来检索标量结果。
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]
答案 1 :(得分:12)
答案 2 :(得分:-1)
尝试一下:
declare @query as varchar(200)
select @query = COUNT(*) from table