日期:2014-05-18  浏览次数:20572 次

求高分解决一个SQL中的存储过程问题!!回答者都有分拿~~
我现在想写这样一个存储过程,即我有两个表table1和table2,这两个表有一个共同的字段parentID,现在我想写一个存储过程,把这个两个表的记录数相加并返回相加后的值,比如说:
  select   count(*)   from   table1   where   parentID   =   1   (有3条数据)
  select   count(*)   from   table2   where   parentID   =   1   (有2条数据)
我现在想用一条存储过程来实现当传入一个parentID后返回这两个结果集相加后的值,在这个例子中应该是返回5,请问该怎么写这个存储过程啊??

------解决方案--------------------
select [count]=sum(tmp.[count])
from
(
select [count]=count(*) from table1 where parentID = 1
union all
select count(*) from table2 where parentID = 1
)tmp