2014-09-30 65 views
0

我有兩個存儲過程。第一個存儲過程值在第二個存儲過程中使用。所以我想將兩個存儲過程合併爲一個。存儲過程從SQL Server中的存儲過程本身獲取值

Create procedure [carcallD] 
    @carid varchar(10) 
as 
Begin 
    select 
     t.dtime, t.locid, t.vtid 
    from 
     Transaction_tbl t 
    where 
     Tbarcode = @carid 
End 

如果我的413carid執行此我會得到了把這樣的:

dtime      locid  vtid 
-----------------------  ----------- ----------- 
2014-06-09 14:59:47   5    8 

我的其他存儲過程是這樣的:

ALTER procedure [dbo].[Weekend] 
    @wday varchar(50), 
    @yr varchar(50), 
    @vtid integer, 
    @locid integer 
as 
begin 
    set nocount on 

    DECLARE @todaysdate date 
    Declare @checkWeekend integer 

    select 
     @todaysdate = CONVERT(varchar(10), GETDATE(), 111) 

    select 
     @checkWeekend = Weekend 
    from 
     weekends_tbl 
    where 
     weekdays = @wday 

    if @checkWeekend = 1 
    begin 
     select Hamount as amount 
     from locvtypeassign_tbl 
     where vtid = @vtid and locid = @locid and active = 0 
    end 
    else 
    begin 
     if @todaysdate in (select Hdate from Pholidays_tbl where year = @yr) 
     begin 
     select Hamount as amount 
     from locvtypeassign_tbl 
     where vtid = @vtid and locid = @locid and active = 0 
     end 
     else 
     begin 
     select Namount as amount 
     from locvtypeassign_tbl 
     where vtid = @vtid and locid = @locid and active = 0 
     end 
    end 
end 

我在這裏使用的參數

  • @wday =我想從我的輸出傳遞DTIME的某一天
  • @yr =通過DTIME的特定年份從我的輸出
  • @vtid =通VTID從我的輸出
  • @locid =通LOCID從我的輸出

所以我可以將這兩個存儲過程合併爲一個嗎?

如果有人能夠幫助我,我會很感激。

在此先感謝

我想輸出是這樣的:

dtime      locid  vtid   amount 
-----------------------  ----------- ----------- --------- 
2014-06-09 14:59:47   5    8   100 

回答

0

試試這個

ALTER procedure [dbo].[Weekend] 
@carid varchar(50) 
as 
begin 
Declare 
@wday datetime, 
@yr varchar(50), 
@vtid integer, 
@locid integer, 
@day varchar(10), 
@year integer 
-- taking parameter value 
select @wday = t.dtime 
from Transaction_tbl t where [email protected] 
set @day=datename(Weekday,@wday) 
set @year=year(@wday) 
set @vtid = (select t.vtid 
from Transaction_tbl t where [email protected]); 
set @locid = (select t.locid 
from Transaction_tbl t where [email protected]); 
set nocount on 
DECLARE @todaysdate date 
Declare @checkWeekend integer 
select @todaysdate = CONVERT(varchar(10), GETDATE(), 111) 
--End 

--check current day is holiday(Weeknd) 
select @checkWeekend= Weekend from weekends_tbl where [email protected] 
if @checkWeekend= 1 
begin 
Select t.dtime, 
k.HBarcode, m.make,t.Compl, 
t.plateno,t.self,t.dtime, v.vtype, l.locname,case when l.edt is null or l.edt =0 then l.minEdt 
+l.BuffrEDT else l.edt + l.BuffrEDT end as EDT, t.locid,t.vtid,t.lsttic, 
c.Colname, te.UniqueName,DATEDIFF(minute,t.dtime,getdate()) as 
Duration,pl.PS,pc.PlateCode,t.Paid,t.Status,t.DelDate,t.vtid,t.Locid, Hamount as amount 


from Transaction_tbl t 
left JOIN KHanger_tbl k ON t.transactID = k.transactID 
left JOIN make_tbl m ON t.mkid = m.mkid 
left join PlateSource_tbl pl on t.PSID=pl.PSID 
left join PlateCode_tbl pc on t.PCdID=pc.PCdID 
left JOIN vtype_tbl v ON v.vtid = t.vtid 
left JOIN Location_tbl l ON t.locid = l.locid 

left JOIN Color_tbl C ON t.colid = c.colid 
left JOIN Terminals_tbl te ON k.tid = te.tid 
left join locvtypeassign_tbl loc on t.Locid=loc.locid 
where [email protected] and [email protected] and loc.active=0 and [email protected] 
end 
else 
--Check current day belongs to any public holiday 
begin 
if @todaysdate in(select Hdate from Pholidays_tbl where [email protected]) 
begin 

Select t.dtime, 
k.HBarcode, m.make,t.Compl, 
t.plateno,t.self,t.dtime, v.vtype, l.locname,case when l.edt is null or l.edt =0 then l.minEdt  
+l.BuffrEDT else l.edt + l.BuffrEDT end as EDT, t.locid,t.vtid,t.lsttic, 
c.Colname, te.UniqueName,DATEDIFF(minute,t.dtime,getdate()) as 
Duration,pl.PS,pc.PlateCode,t.Paid,t.Status,t.DelDate,t.vtid,t.Locid, Hamount as amount 


from Transaction_tbl t 
left JOIN KHanger_tbl k ON t.transactID = k.transactID 
left JOIN make_tbl m ON t.mkid = m.mkid 
left join PlateSource_tbl pl on t.PSID=pl.PSID 
left join PlateCode_tbl pc on t.PCdID=pc.PCdID 
left JOIN vtype_tbl v ON v.vtid = t.vtid 
left JOIN Location_tbl l ON t.locid = l.locid 

left JOIN Color_tbl C ON t.colid = c.colid 
left JOIN Terminals_tbl te ON k.tid = te.tid 

left join locvtypeassign_tbl loc on t.Locid=loc.locid 
where [email protected] and [email protected] and loc.active=0 and [email protected] 
end 
-- so calculating normal day amount 
else 
begin 

Select t.dtime, 
k.HBarcode, m.make,t.Compl, 
t.plateno,t.self,t.dtime, v.vtype, l.locname,case when l.edt is null or l.edt =0 then l.minEdt 
+l.BuffrEDT else l.edt + l.BuffrEDT end as EDT, t.locid,t.vtid,t.lsttic, 
c.Colname, te.UniqueName,DATEDIFF(minute,t.dtime,getdate()) as  
Duration,pl.PS,pc.PlateCode,t.Paid,t.Status,t.DelDate,t.vtid,t.Locid, Namount as amount 


from Transaction_tbl t 
left JOIN KHanger_tbl k ON t.transactID = k.transactID 
left JOIN make_tbl m ON t.mkid = m.mkid 
left join PlateSource_tbl pl on t.PSID=pl.PSID 
left join PlateCode_tbl pc on t.PCdID=pc.PCdID 
left JOIN vtype_tbl v ON v.vtid = t.vtid 
left JOIN Location_tbl l ON t.locid = l.locid 

left JOIN Color_tbl C ON t.colid = c.colid 
left JOIN Terminals_tbl te ON k.tid = te.tid 

left join locvtypeassign_tbl loc on t.Locid=loc.locid 
where [email protected] and [email protected] and loc.active=0 and [email protected] 
end 
end 
--fetching amount nd details part over--- 
--Checking corresponding barcde complimentry or not.if compl taking deltails 
if(select COUNT(t1.Compl) from dbo.Transaction_tbl t1 where [email protected])=1 
begin 
declare @compl integer =null, 
@transid integer=null, 
@complid integer=null 
select @transid=t.transactID from dbo.Transaction_tbl t where [email protected] 
Select @compl=co.Cmplid from dbo.ComplimentTransactAssign_tbl co where [email protected] 
select c.CompName,c1.Remarks from Complimentary_tbl c 
inner join ComplimentTransactAssign_tbl c1 on c.CmplID=c1.Cmplid where [email protected] and  
[email protected] 
end 
--End Compl Checking--- 
declare @locatnid integer, 
@location nvarchar(100) 
begin 
select @locatnid= t.Locid from dbo.Transaction_tbl t where [email protected] 
select l1.StartTime,l1.EndTime from dbo.Location_tbl l1 where [email protected] 
end 
end 
+0

出現錯誤:當子查詢未與EXISTS一起引入時,只能在選擇列表中指定一個表達式。 – user3252014 2014-09-30 07:32:38

+0

再試一次,因爲我編輯了它 – 2014-09-30 07:33:27

+0

先生我編輯了我的問題。我期待這樣的 – user3252014 2014-09-30 07:37:29

0

你有兩個存儲過程,主要目的是用一個存儲過程。既然你已經寫了兩個。改變第二個程序添加@carid,添加另一個參數@option。如果您的選項爲0,則執行第一個過程,否則執行第二個過程。

ALTER procedure [dbo].[Weekend] 
    @wday varchar(50), 
    @yr varchar(50), 
    @vtid integer, 
    @locid integer, 
    @option = 0, 
    @carid varchar(10) 
as 
begin 
set nocount on 
DECLARE @todaysdate date 
Declare @checkWeekend integer 
    select @todaysdate = CONVERT(varchar(10), GETDATE(), 111) 

select @checkWeekend= Weekend from weekends_tbl where [email protected] 
if @option = 1 
beging 
    exec [carcallD] @carid 
end 
else 
begin 
if @checkWeekend= 1 
begin 
    select Hamount as amount from locvtypeassign_tbl where 
    [email protected] and [email protected] and active=0 
end 
else 
begin 
if @todaysdate in(select Hdate from Pholidays_tbl where [email protected]) 
begin 
    select Hamount as amount from locvtypeassign_tbl where 
    [email protected] and [email protected] and active=0 
end 
else 
begin 
    select Namount as amount from locvtypeassign_tbl where 
    [email protected] and [email protected] and active=0 
end 
end 
end 
end 

你可以通過刪除它來複制你的第一個過程,也可以在那裏複製你的第一個過程。像

if @option = 1 
begin 
    select t.dtime,t.locid,t.vtid 
from Transaction_tbl t where [email protected] 
end 

哪裏有你想要你的第一個程序被執行,然後設置通過@option爲1到所有程序。或者讓它假定默認的0值來執行sec過程。

+0

先生,我給我的預期出來..請你檢查 – user3252014 2014-09-30 07:39:30

+0

哦,對不起,我明白了。在這種情況下,您可以使用臨時表。執行第一個過程,然後將結果插入tempTable,然後將臨時表加入到第二個過程中的select語句中。 btw什麼列是常見的btw Transaction_tbl和你的locvtypeassign_tbl?我的意思是FK。有沒有 ? – 2014-09-30 07:47:14