[英]How to pass table name as parameter in select statement in SQL Sever
Table: 表:
Col
------
Table1
table2
table3
Query: 查询:
select count(*)
from @tablename
I wanted to pass table1
, table2
, table3
as parameters for @tablename
in the select query and get the count for each table 我想通过
table1
, table2
, table3
的参数@tablename
在选择查询和获取计数为每个表
Desired output: 所需的输出:
2 (table 1 count) 3 (table 2 count) 4 (table 3 count)
you can use dynamic sql and a cursor to run through them: 您可以使用动态sql和游标来运行它们:
Create temp table for testing: 创建用于测试的临时表:
DECLARE @tablenametable TABLE(tablename VARCHAR(100));
INSERT INTO @tablenametable
VALUES('table1'), ('table2'), ('table3');
Use a cursor to run through all tablenames in the table 使用游标遍历表中的所有表名
DECLARE @tablename VARCHAR(100);
DECLARE dbcursor CURSOR
FOR
SELECT tablename
FROM @tablenametable;
OPEN dbcursor;
FETCH NEXT FROM dbcursor INTO @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @sql VARCHAR(MAX);
SET @sql = 'select count(*) from '+@tablename;
PRINT(@sql);
FETCH NEXT FROM dbcursor INTO @tablename;
END;
CLOSE dbcursor;
DEALLOCATE dbcursor;
Give the following results: 给出以下结果:
select count(*) from table1
select count(*) from table2
select count(*) from table3
Just change PRINT(@SQL)
to EXEC(@SQL)
when your happy with it 满意时将
PRINT(@SQL)
更改为EXEC(@SQL)
You can use dynamic sql query. 您可以使用动态sql查询。
Query 询问
declare @sql as varchar(max);
select @sql = stuff((
select ' union all '
+ 'select cast(count(*) as varchar(100))
+ ' + char(39) + '(' + [Col] +' Count)' + char(39)
+ ' as [table_counts] '
+ ' from ' + [col]
from [your_table_name]
for xml path('')
)
, 1, 11, ''
);
exec(@sql);
Find a demo here
As mentioned here, you have to use dynamic SQL. 如此处所述,您必须使用动态SQL。
First approach is where you specify table name yourself: 第一种方法是您自己指定表名:
declare @tablename varchar(30), @SQL varchar(30)
set @tablename = 'Table1' --here you specify the name
set @SQL = concat('SELECT COUNT(*) FROM ', @tablename) --here you build the query
EXEC(@SQL)
Second approach lets you use table with names of tables: 第二种方法允许您使用带有表名的表:
declare @SQL varchar(8000)
set @SQL = ''
declare @TableNames table(name varchar(30))
insert into @TableNames values ('Table1'), ('Table2'), ('Table3')
--here you build the query
select @SQL = @SQL + ' SELECT ''' + name + ''' AS [TableName], COUNT(*) AS [Count] FROM ' + name + ' UNION ALL' from @TableNames
-- get rid of last "UNION ALL"
set @SQL = LEFT(@SQL, LEN(@SQL) - 10)
--execute the query
EXEC(@SQL)
The result of it will be: 结果将是:
TableName Count
Table1 3
Table2 6
Table3 4
You can use sys.dm_db_partition_stats
like this [1] : 您可以像这样[1]使用
sys.dm_db_partition_stats
:
select
t.col tableName, sum(s.row_count) tableCount
from
yourTable t
join
sys.dm_db_partition_stats s
on
(object_name(s.object_id) = t.col )
and
(s.index_id < 2)
group by
t.col;
One line output version will be: 一行输出版本将是:
select
sum(s.row_count), ' ('+t.col +' count) '
from
yourTable t
join
sys.dm_db_partition_stats s
on
(object_name(s.object_id) = t.col )
and
(s.index_id < 2)
group by
t.col
for xml path('');
output: 输出:
2 (Table1 count) 3 (table2 count) 4 (table3 count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.