Sql Sever 表遍历,Exec动态执行sql语句返回变量
Posted 唯爱金生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql Sever 表遍历,Exec动态执行sql语句返回变量相关的知识,希望对你有一定的参考价值。
DECLARE @id AS INT,
@table_name AS NVARCHAR(200),
@number_column_name AS NVARCHAR(200),
@sql NVARCHAR(MAX),
@cnt INT;
WHILE EXISTS (SELECT id FROM @temp)
BEGIN
-- 也可以使用top 1
SET ROWCOUNT 1;
SELECT @id = id,
@table_name = table_name,
@number_column_name = number_column_name
FROM @temp;
SELECT @table_name = table_name,
@number_column_name = number_column_name
FROM dbo.sed_filing_doc
WHERE id = @id;
SET @sql
= N‘ select @cnt=count(*) from ‘ + @table_name + N‘ where filing_id=‘ + CAST(@filing_id AS NVARCHAR(200));
EXEC sp_executesql @sql, N‘@cnt int out‘, @cnt OUT;
UPDATE dbo.sed_filing_doc
SET count = @cnt
WHERE id = @id;
SET ROWCOUNT 0;
DELETE FROM @temp
WHERE id = @id;
END;
以上是关于Sql Sever 表遍历,Exec动态执行sql语句返回变量的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?