gpt4 book ai didi

sql - 如何通过动态传递表名来获取行数

转载 作者:行者123 更新时间:2023-12-04 20:59:22 26 4
gpt4 key购买 nike

create procedure qa_cc
@tablename varchar(500)

AS

BEGIN
-- Create two integer values
DECLARE @tableOneCount varchar(50), @tableTwoCount varchar(50)

-- Get the number of rows from the first table
SELECT @tableOneCount = 'SELECT COUNT(*) FROM' + @tablename;
exec (@tableOneCount);

select @tableOneCount ;

END

exec qa_cc @tablename=table1

最佳答案

您可以使用 sp_executesql , 喜欢:

declare @sql nvarchar(max);
set @sql = N'select @cnt = count(*) from ' + @tablename;
declare @cnt int;
exec sp_executesql @sql, N'@cnt int output', @cnt = @cnt output;
select @cnt;

关于sql - 如何通过动态传递表名来获取行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com