2014-09-30 1 views
0

두 개의 저장 프로 시저가 있습니다. 첫 번째 저장 프로 시저 값은 두 번째 저장 프로 시저에서 사용됩니다. 그래서 두 개의 저장 프로 시저를 하나의 단일 프로 시저에 결합하려고합니다. 내가 413carid 이것을 실행하면저장 프로 시저 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 

나는 밖으로 같이 넣어 얻을 것이다 :

내 다른 저장 프로 시저는 다음과 같습니다
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 = 나는
  • @yr = 내 출력에서 ​​내 출력
  • @locid = 패스 locid에서 내 출력
  • @vtid = 패스 VTID에서

그래서를 dtime의 특정 연도를 전달 내 출력에서 ​​dtime의 특정 일을 전달하려면 이 두 가지 저장 프로 시저를 하나의 저장 프로 시저로 결합 할 수 있습니까?

누구든지 나를 도울 수 있다면, 고맙겠습니다. 나는이 같은 출력을 얻을 할

사전에

감사 :

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

점점 오류 : 존재합니다 하나의 표현이 선택 목록에 지정할 수있는 하위 쿼리가 도입되지 않은 경우. – user3252014

+0

내가 편집 한대로 다시 시도하십시오. –

+0

선생님이 내 퀴 스턴을 편집했습니다.나는 이런 식으로 넣을 것을 기대하고있다 – user3252014

0

당신은 출력 parameteres를 사용할 수 있습니다

:

CREATE PROCEDURE GetImmediateManager 
    @employeeID INT, 
    @managerID INT OUTPUT 
AS 
BEGIN 
    SELECT @managerID = ManagerID 
    FROM HumanResources.Employee 
    WHERE EmployeeID = @employeeID 
END 

이 링크에 모습을 받기 http://technet.microsoft.com/en-us/library/ms378108(v=sql.110).aspx

0

이 주요 목표는 하나의 저장 프로 시저를 사용하는 것입니다보십시오. 당신은 이미 두 가지를 썼습니다. 두 번째 절차는 다른 매개 변수 @option를 추가 @carid을 추가 변경합니다. 옵션이 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 

첫 번째 절차도 복사하여 삭제 한 다음 exec 장소로 복사 할 수 있습니다. like

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

첫 번째 절차를 실행하려는 곳에서 @option을 1로 설정하면 모든 절차가 완료됩니다. 또는 sec 프로 시저를 실행하려면 기본 0 값으로 가정하십시오.

+0

선생님. 내 기대치를 내었습니다. 확인해주세요. – user3252014

+0

오, 죄송합니다. 알겠습니다. 이 경우 임시 테이블을 사용할 수 있습니다. 첫 번째 proc을 실행 한 다음 결과를 tempTable에 삽입 한 다음 두 번째 프로 시저의 select 문에 임시 테이블을 조인합니다. 어떤 열이 Transaction_tbl과 locvtypeassign_tbl에서 공통인가? 나는 FK를 의미한다. 있어요 ? –