能不能把两个存储过程出来的表连接一下呢比如如下的代码。运行后会出来2个表格。能不能把这两个表格当作普通的查询 来进行 union 或者join 操作呢注:我这里只是个例子。如果直接一行代码的我就不给分了。我想要:“把两个存储过程出来的表连接一下”--建立表
Create Table Test
(id Int,name nvarchar(20))
--插入数据Insert Test Select 1,'a'
Union all Select 1,'b'
Union all Select 2,'c'
Union all Select 3,'d'
Union all Select 4,'e'
GO
--建立存储过程
Create ProceDure P_1
As
select * from Test where id<2
GOCreate ProceDure P_2
As
select * from Test where id>2
GO
--运行
exec P_1
exec P_2
Go
--删除
Drop Table Test
Drop ProceDure P_1
Drop ProceDure P_2

解决方案 »

  1.   

    通常都是借用臨時表的--建立表
    Create Table Test
    (id Int,name nvarchar(20))
    --插入数据Insert Test Select 1,'a'
    Union all Select 1,'b'
    Union all Select 2,'c'
    Union all Select 3,'d'
    Union all Select 4,'e'
    GO
    --建立存储过程
    Create ProceDure P_1
    As
    select * from Test where id<2
    GOCreate ProceDure P_2
    As
    select * from Test where id>2
    GO
    --运行
    Create Table #T
    (id Int,name nvarchar(20))
    Insert #T exec P_1
    Insert #T exec P_2Select * From #TDrop Table #T
    Go
    --删除
    Drop Table Test
    Drop ProceDure P_1
    Drop ProceDure P_2
      

  2.   

    不過不用臨時表,也有辦法實現
    --建立表
    Create Table Test
    (id Int,name nvarchar(20))
    --插入数据Insert Test Select 1,'a'
    Union all Select 1,'b'
    Union all Select 2,'c'
    Union all Select 3,'d'
    Union all Select 4,'e'
    GO
    --建立存储过程
    Create ProceDure P_1
    As
    select * from Test where id<2
    GOCreate ProceDure P_2
    As
    select * from Test where id>2
    GO
    --运行
    Select * From OpenRowSet('sqloledb','Trusted_Connection=yes','exec Test.dbo.P_1') 
    Union All
    Select * From OpenRowSet('sqloledb','Trusted_Connection=yes','exec Test.dbo.P_2') 
    Go
    --删除
    Drop Table Test
    Drop ProceDure P_1
    Drop ProceDure P_2
    --將數據庫名TEST改為你自己的數據庫名