[英]Stored procedure take value from stored procedure itself in SQL Server
I have two stored procedures. 我有两个存储过程。 The first stored procedure value's using in second stored procedure. 第一个存储过程的值用于第二个存储过程。 So I want to combine the two stored procedures into a single one. 所以我想将两个存储过程合并为一个。
Create procedure [carcallD]
@carid varchar(10)
as
Begin
select
t.dtime, t.locid, t.vtid
from
Transaction_tbl t
where
Tbarcode = @carid
End
If I execute this with carid
of 413
I will get out put like this: 如果我用413
carid
执行这个动作,我将像这样被放出去:
dtime locid vtid
----------------------- ----------- -----------
2014-06-09 14:59:47 5 8
My other stored procedure looks like this: 我的其他存储过程如下所示:
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
Here am using parameter 这里正在使用参数
@wday =
I want to pass particular day of dtime from my output @wday =
我想从输出中传递@wday =
特定日期 @yr =
pass particular year of dtime from my output @yr =
从我的输出中经过特定的@yr =
年 @vtid =
pass vtid from my output @vtid =
从我的输出中传递vtid @locid =
pass locid from my output @locid =
从我的输出传递locid So can I combine these two stored procedures into a single one? 那么我可以将这两个存储过程合并为一个吗?
If anyone is able to help me, I'd appreciate it. 如果有人能够帮助我,我将不胜感激。
Thanks in advance 提前致谢
I want to get output like this: 我想要这样的输出:
dtime locid vtid amount
----------------------- ----------- ----------- ---------
2014-06-09 14:59:47 5 8 100
You can use output parameteres: 您可以使用输出参数:
CREATE PROCEDURE GetImmediateManager
@employeeID INT,
@managerID INT OUTPUT
AS
BEGIN
SELECT @managerID = ManagerID
FROM HumanResources.Employee
WHERE EmployeeID = @employeeID
END
Get a look in this link: 在此链接中查找:
http://technet.microsoft.com/en-us/library/ms378108(v=sql.110).aspx http://technet.microsoft.com/zh-CN/library/ms378108(v=sql.110).aspx
Try this 尝试这个
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 Tbarcode=@carid
set @day=datename(Weekday,@wday)
set @year=year(@wday)
set @vtid = (select t.vtid
from Transaction_tbl t where Tbarcode=@carid);
set @locid = (select t.locid
from Transaction_tbl t where Tbarcode=@carid);
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 weekdays=@day
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 loc.vtid=@vtid and loc.locid=@locid and loc.active=0 and t.TBarcode=@carid
end
else
--Check current day belongs to any public holiday
begin
if @todaysdate in(select Hdate from Pholidays_tbl where year=@year)
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 loc.vtid=@vtid and loc.locid=@locid and loc.active=0 and t.TBarcode=@carid
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 loc.vtid=@vtid and loc.locid=@locid and loc.active=0 and t.TBarcode=@carid
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 T1.TBarcode=@Carid)=1
begin
declare @compl integer =null,
@transid integer=null,
@complid integer=null
select @transid=t.transactID from dbo.Transaction_tbl t where t.TBarcode=@carid
Select @compl=co.Cmplid from dbo.ComplimentTransactAssign_tbl co where co.TransactID=@transid
select c.CompName,c1.Remarks from Complimentary_tbl c
inner join ComplimentTransactAssign_tbl c1 on c.CmplID=c1.Cmplid where c.CmplID=@compl and
c1.TransactID=@transid
end
--End Compl Checking---
declare @locatnid integer,
@location nvarchar(100)
begin
select @locatnid= t.Locid from dbo.Transaction_tbl t where t.TBarcode=@carid
select l1.StartTime,l1.EndTime from dbo.Location_tbl l1 where l1.Locid=@locatnid
end
end
You have two stored procedures, the main aim is to use one stored procedure. 您有两个存储过程,主要目的是使用一个存储过程。 Since you have two written already. 由于您已经写了两个。 Alter the second procedure add the @carid
, add another parameter @option
. 在第二个过程中,添加@carid
,添加另一个参数@option
。 If your option is 0 then execute the first procedure, else execute the second. 如果您的选项为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 weekdays=@wday
if @option = 1
beging
exec [carcallD] @carid
end
else
begin
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
end
You can copy your first procedure also and past in the place of exec there by eliminating it. 您也可以复制第一个程序,并通过删除它来代替exec。 like 喜欢
if @option = 1
begin
select t.dtime,t.locid,t.vtid
from Transaction_tbl t where Tbarcode=@carid
end
where ever you want your first procedure to be executed, then set pass @option as 1 to the procedure that's all. 在您希望执行第一个过程的任何地方,然后将pass @option设置为1即可。 OR leave it to assume the default 0 value to execute the sec procedure. 或保留默认值为0来执行sec过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.