在另一个存储过程中加入2个存储过程

时间:2011-01-05 14:41:09

标签: sql-server-2005 stored-procedures

我有两个用于instanse test1和test2的存储过程,它们都使用相同的参数X。

X是这两个存储过程中if语句的一部分,基本上它告诉了选择哪个select语句的过程。 (这应该不重要,因为它是一个参数,但我不确定)

我希望test1和test2通过列名加入,因为它们都有列名,我希望它们并排链接而不是另一个,这是可能的以及如何

基本上,我想在我的asp.net程序将调用的另一个存储过程中加入这两个存储过程。

这是存储过程(sp_current)

@mode varchar(20)




AS
SET NOCOUNT ON

Declare @Booked Int
Set @Booked = CONVERT(int,DateAdd(year,  (year( getdate() )) - Year(getdate() + 1),                   
                DateAdd(day, DateDiff(day, 1, getdate()), 1) ) )

 If @mode = 'Sales'
      Select
           Division,
           SDESCR,
           DYYYY,

       Sum(Case When Booked <= @Booked Then NetAmount End) currentNetSales,       
       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX        



      From   dbo.B101BookingsDetails
      Where  DYYYY = (year( getdate() ))
      Group By SDESCR, DYYYY, Division           
      Order By Division, SDESCR, DYYYY

else if @mode = 'netsales'

Select Division, 
       SDESCR,        
       DYYYY,  


       Sum(Case When Booked <= @Booked Then NetAmount End) currentNetSales,        


       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX      



From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'Inssales'

Select Division, 
       SDESCR,        
       DYYYY,  

       Sum(Case When Booked <= @Booked Then InsAmount End) currentInsSales,        

       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX        

From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'CXsales'
Select Division, 
       SDESCR,        
       DYYYY,  

       Sum(Case When Booked <= @Booked Then CancelRevenue End) currentCXSales,        


       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX        


From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'othersales'
Select Division, 
       SDESCR,        
       DYYYY,  

   Sum(Case When Booked <= @Booked Then OtherAmount End) currentOtherSales,        
       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX        



From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'cxvalue'
Select Division, 
       SDESCR,        
       DYYYY,  

       Sum(Case When Booked <= @Booked Then CXVALUE End) currentCXValue,
       Sum(Case When Booked <= @Booked Then PARTY End) AS currentPAX        



From   dbo.B101BookingsDetails 
Where  DYYYY = (year( getdate() ))
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

和 这是sp_compare

@mode varchar(20),
@YearToGet int



AS
SET NOCOUNT ON

Declare @Booked Int
Set @Booked = CONVERT(int,DateAdd(year, @YearToGet - Year(getdate() + 1),                   
                DateAdd(day, DateDiff(day, 1, getdate()), 1) ) )

 If @mode = 'Sales'
      Select
           Division,
           SDESCR,
           DYYYY,

       Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,        
       SUM(NetAmount) AS YENetSales,

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


      From   dbo.B101BookingsDetails
      Where  DYYYY = @YearToGet
      Group By SDESCR, DYYYY, Division           
      Order By Division, SDESCR, DYYYY

else if @mode = 'netsales'

Select Division, 
       SDESCR,        
       DYYYY,  


       Sum(Case When Booked <= @Booked Then NetAmount End) ASofNetSales,        
       SUM(NetAmount) AS YENetSales,

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'Inssales'

Select Division, 
       SDESCR,        
       DYYYY,  

      Sum(Case When Booked <= @Booked Then InsAmount End) ASofInsSales,        
       SUM(InsAmount)  AS YEInsSales, 

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX

From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'CXsales'
Select Division, 
       SDESCR,        
       DYYYY,  

      Sum(Case When Booked <= @Booked Then CancelRevenue End) ASofCXSales,        
       SUM(CancelRevenue) AS YECXSales, 


       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX

From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'othersales'
Select Division, 
       SDESCR,        
       DYYYY,  

   Sum(Case When Booked <= @Booked Then OtherAmount End) ASofOtherSales,        
       SUM(OtherAmount) AS YEOtherSales,

       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

Else if @mode = 'cxvalue'
Select Division, 
       SDESCR,        
       DYYYY,  

       Sum(Case When Booked <= @Booked Then CXVALUE End) ASofCXValue,
       SUM(CXVALUE) AS YECXValue,


       Sum(Case When Booked <= @Booked Then PARTY End) AS ASofPAX,        
       SUM(PARTY) AS YEPAX


From   dbo.B101BookingsDetails 
Where  DYYYY = @YearToGet
Group By SDESCR, DYYYY, Division
Order By Division, SDESCR, DYYYY 

我要么想要一个程序,要么以某种方式将它们并排分开

1 个答案:

答案 0 :(得分:1)

你的问题有点大。我猜这两个程序都返回结果集,并且您希望返回这些结果集的连接。

您可以定义临时表或表变量并使用

INSERT INTO table_1 exec sp_1 ...
INSERT INTO table_2 exec sp_2 ...

您可以从这两个临时对象的连接中进行选择。

编辑: 不知道数据类型让我尝试类似

create table #tmp1 (
  Division varchar(50),
  SDESCR varchar850),
  DYYYY varchar(5),
  currentNetSales money,
  currentPax money
)

create table #tmp2 (
  Division varchar(50),
  SDESCR varchar(50),
  DYYYY varchar(5),
  ASofNetSales money,        
  YENetSales money,
  ASofPAX money,        
  YEPAX money
)

 INSERT INTO #tmp1 exec sp_current ('Sales')
 INSERT INTO #tmp2 exec sp_compare ('Sales', 2009)

select t1.Division,
 isnull(t1.SDESCR, t2.SDESCR) SDESCR,
 isnull(t1.DYYYY, t2.DYYYY) DYYYY,
 t1.currentNetSales,
 t1.currentPax,
 t2.ASofNetSales money,        
 t2.YENetSales money,
 t2.ASofPAX money,        
 t2.YEPAX money
from #tmp1 t1
  full outer join #tmp2 t2 on t1.Division = t2.Division and t1.SDESCR = t2.SDESCR
 Order By Division, SDESCR