使用tSQL计算多个表中的行 [英] Count rows in more than one table with tSQL

查看:111
本文介绍了使用tSQL计算多个表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在SQL Server 2008中的多个表中计算行。我这样做:

  select count )from(select * from tbl1 union all select * from tbl2)

但是它给了我一个错误不正确的语法near)。为什么?



PS。实际的表数可以多于2个。

解决方案

如果表中有不同数量的列,

  SELECT count(*)
FROM(
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
)T


I need to count rows in more than one table in SQL Server 2008. I do this:

select count(*) from (select * from tbl1 union all select * from tbl2)

But it gives me an error of incorrect syntax near ). Why?

PS. The actual number of tables can be more than 2.

解决方案

In case you have different number of columns in your tables try this way

 SELECT count(*) 
 FROM (
      SELECT NULL as columnName 
      FROM tbl1 
          UNION ALL
      SELECT NULL 
      FROM tbl2
     ) T

这篇关于使用tSQL计算多个表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆