选择各种表中的记录数

Mos*_*026 0 sql t-sql stored-procedures

我希望在一个存储过程中获得此查询的结果.与此查询我要返回的行数table1table4

select count(*) from table1
select count(*) from table2
select count(*) from table3
select count(*) from table4
Run Code Online (Sandbox Code Playgroud)

我希望将此结果放在临时表中并选择临时表的所有列.

Tom*_*Tom 5

这是一个不太优雅的解决方案:

SELECT  'table1' as table_name, COUNT(*) as record_count from table1
UNION ALL 
SELECT  'table2' as table_name, COUNT(*) as record_count from table2
UNION ALL 
SELECT  'table3' as table_name, COUNT(*) as record_count from table3
UNION ALL 
SELECT  'table4' as table_name, COUNT(*) as record_count from table4
Run Code Online (Sandbox Code Playgroud)

  • 为什么要这样做,这个查询将结果表示为table(table_name,record_count) (2认同)