没有使用函数如何调用结果集到另一个存储过程在sql server中

问题描述:

我正在使用sql server 2008 我有一个获取总量的存储过程。在执行此我得到的结果没有使用函数如何调用结果集到另一个存储过程在sql server中

我的第一个存储这样的过程:对只得到

Total 
100 

alter procedure [dbo].[jaseem_test4] 
@carid varchar(50) 
as 
begin 
Declare 
@locid integer, 
@vtid integer, 
@day varchar(20), 
@Date date, 
@startdate datetime, 
@Pdatetime datetime,@Ptime time, 
@prkdtime datetime,@pdate date, 
@Starttime time,@startloctime varchar(10), 
@currenttime varchar(10),@endtime time, 
@endloctime varchar(10) 
--finding loc_id and vtid 
set @locid = (select t.locid 
from Transaction_tbl t where [email protected]) 
set @vtid=(select t.vtid from Transaction_tbl t where [email protected]) 
---------- 
--finding parked time 
set @Pdatetime=(select t.dtime from Transaction_tbl t where [email protected]) 
--finding location end time 
select @endtime=l.EndTime from Location_tbl l where [email protected] 


    select @endloctime=convert(Varchar(8), @endtime,108) 
    select @currenttime=CONVERT(VARCHAR(8),GETDATE(),108) 
    select @Date=convert(date,getdate()) 
    select @pdate=convert(date,@Pdatetime,108) 
    select @Ptime=CONVERT(VARCHAR(8),@Pdatetime,108) 
    declare @prevousdate datetime 
    select @prevousdate=convert(date,getdate()-1) 
    declare @LT integer 

if @endloctime<@Ptime 
    begin 
    select @startdate= cast(@Date as Datetime) + cast(@endloctime as Datetime) 
    select @prkdtime=cast(@prevousdate as Datetime) + cast(@Ptime as Datetime) 
select @LT =datediff(mi,@prkdtime,@startdate) 
--select @LT as LT1 
end 
else 
begin 
select @LT=datediff(mi,@ptime,@endloctime) 
--select @LT as LT 
end 

declare @PT integer=datediff(mi,@Pdatetime,getdate()) 
--select @PT as PT 

DEclare @daycount integer 
if (@LT >= @PT) 
begin 
set @daycount=1 
--select @daycount 


end 
If (@PT > @LT) 
begin 
Declare @q Integer ,@q1 float,@modvAL float,@PT1 integer 
      set @q=1 
    set @modvAL=1440 
    set @[email protected]@LT 
-- select @PT1 as PT1 
    if @PT1>@modvAL 
    begin 
    set @q1= round((@PT1)/(@modvAL),0) 
    --select @q1 as q2 
    end 
    else 
    begin 
    set @q1=1 
    end 
    set @q=(@[email protected]) 
    set @[email protected]*@q 
    --select @daycount as daycount 
end 

------------------------------------------------------ 


declare @days integer,@s varchar(10),@Total integer, 
@n integer 
,@day1 integer, 
@checkWeekend integer, 
@Hamount integer, 
@Htotel integer, @Namount integer,@startloctime1 varchar(10),@currenttime1 varchar(10), 
@Ntotal integer 
set @days=0 
set @n=0 
set @[email protected] 
set @Htotel=0 
set @Ntotal=0 

    Select @startloctime1= convert(Varchar(8),(select l.StartTime from Location_tbl l where l.Locid=5),108) 
select @currenttime1=CONVERT(VARCHAR(8),GETDATE(),108) 

while (@days < @day1) 
begin 
if @day1=1 
begin 

if @startloctime1>@currenttime1 
begin 
set @s= datename(dw,getdate()-1) 
end 
else 
begin 
set @s= datename(dw,getdate()) 
end 
end 
else 
begin 
select @s=datename(dw,getdate()[email protected]) 
--select @s 
end 
select @checkWeekend= Weekend from weekends_tbl where [email protected] 
if @checkWeekend=1 
begin 

select @hamount= Hamount from locvtypeassign_tbl where 
[email protected] and [email protected] and active=0 

set @[email protected][email protected] 

end 
else 
begin 

select @Namount= NAmount from locvtypeassign_tbl where 
[email protected] and [email protected] and active=0 
set @[email protected]+ @Namount 
end 
set @[email protected]+1 
set @n= @n+1 
set @Total= @[email protected] 

end 
select @Total as [Total] 

    end --End main 
    Go 

如果我通过carid我得到了把这样的这我写了一个存储过程大约100行。在这个存储过程中,我只是传递一个值(Tbarcode)

我有另一个存储过程的财产以后行这样的:

Select t.dtime,t.vtid,t.locid 
    from transaction_tbl t where Tbarcode='4502' 

我出来放是这样的:

dtime       vtid  locid 
----------------------- ----------- ----------- 
2014-10-02 23:43:39.453   7   5 

我想补充这两个存储的过程,我很期待出来把

dtime       vtid  locid  Totalamount 
    ----------------------- ----------- ----------- ------------ 
    2014-10-02 23:43:39.453   7   5  100 

我知道这一点,我可以做第一个存储过程作为函数,并返回总量为第二个存储过程,但我担心执行速度。
如果我在我的存储过程中调用函数,那么我的存储过程执行会变慢? 没有使用函数是否有办法让我的存储过程结果进入第二个存储过程? 任何帮助非常appriciable

如何我可以调用我的第一个存储过程结果seconde存储过程?

+0

没有你的程序代码,很难评论任何东西。 – Rahul 2014-10-02 12:38:32

+0

你需要我的第一个存储过程代码? – user3252014 2014-10-02 12:49:51

你可以做的最简单的事情是;打电话给你的主存储过程传递Totalamount值,然后显示像

主要过程主体

exec sp_another_proc 100 

结果在sp_another_proc

create procedure sp_another_proc (@total int) 
as 
begin 
Select t.dtime, 
     t.vtid, 
     t.locid, 
     @total as TotalAmount 
    from transaction_tbl t where Tbarcode='4502' 
end 

编辑:

在第一个步骤[dbo].[jaseem_test4]而不是最后一行

select @Total as [Total] 

调用第二程序等

exec my_second_procedure @Total 

修改您的第二个过程来接受Integer参数如上所示,然后就显示它。

+0

你可以在这个时候详细说明你的探索请 – user3252014 2014-10-02 13:07:06

+0

这个存储过程如何得到@total值 – user3252014 2014-10-02 13:10:33

+0

先生我怎么能把我的第一个存储过程调用结果存入seconde存储过程 – user3252014 2014-10-02 13:41:07

我会使用与Rahul类似的方法,但略有不同。为了确保您不会破坏现有的可能已经在使用这两个过程的代码,我将执行以下操作:

第1步 - 使用可选的输出参数扩展第一个过程,如下所示(请参阅我已经添加了行)结束:

alter procedure [dbo].[jaseem_test4] 
@carid varchar(50), 
@totalAmount int = null OUTPUT /* the new output parameter which returns the total value */ 
as 
begin 
.... 

set @totalAmount = @Total /* setting the output parameter to the return total value */ 
select @Total as [Total] 

end --End main 
Go 

第2步 - 调用的第一个程序从第二程序的输出参数 - 我借了拉胡尔的代码,这

请注意,您刚刚必须添加到您的第二程序只有行评论:

create procedure sp_another_proc 
as 
begin 

... 
declare @totalAmount int /* the total amount variable */ 
exec jaseem_test4 'InputCaridValue', @totalAmount OUT /* call procedure to get total amount */ 
.... 

Select t.dtime, 
    t.vtid, 
    t.locid, 
    @totalAmount as TotalAmount /* add to your final select the @totalAmount variable */ 
    from transaction_tbl t where Tbarcode='4502' 
end 

GO 

所有应该工作得很好。

+0

这个方法不错(OUTPUT参数很好),但只有在原始'jaseem_test4' proc做了其他更改时才有效。在该过程结束时,'select @Total as [Total]'需要用'IF'条件包装,否则结果将在结果集中。在不添加新输入参数的情况下,您可以使用@totalAmount输出参数将值传递为各种类型的“触发器”。意思是把最后的'SELECT'包装在'IF(@totalAmount IS NULL)BEGIN SELECT ... END;'中。然后在'sp_another_proc'中,在'DECLARE'和'EXEC'之间添加'SET @totalAmount = 0;'。 – 2014-10-02 14:44:08

假设第二个过程也只返回一行,一种方法是创建第三个过程,该过程将调用两个现有过程,捕获每个过程的输出,然后为实际输出选择组合。这样做的好处是,您无需以任何方式修改现有的两个程序。如果这些现有存储过程中的任何一个或两个都被另一个代码调用,并且因此行为更改可能具有不利影响,或者至少会创建其他复杂性和代码路径来测试,这非常有用。

例如:

DECLARE @JaseemTest4Result TABLE (TotalAmount INT); 
DECLARE @Proc2Result TABLE (dtime DATETIME, vtid INT, locid INT); 

INSERT INTO @JaseemTest4Result (TotalAmount) 
    EXEC jaseem_test4 @carid = 25; 

INSERT INTO @Proc2Result (dtime, vtid, locid) 
    EXEC proc2 @Tbarcode = '4502'; 

SELECT p2r.dtime, p2r.vtid, p2r.locid, jt4.TotalAmount 
FROM @JaseemTest4Result jt4 
CROSS JOIN @Proc2Result p2r; 

或者,如果第二个存储过程只是为了这个目的,你可以更新它从例子只做一部分上面捕获TotalAmount再加入真正的表和表变量一起:

DECLARE @JaseemTest4Result TABLE (TotalAmount INT); 

INSERT INTO @JaseemTest4Result (TotalAmount) 
    EXEC jaseem_test4 @carid = 25; 

Select t.dtime, t.vtid, t.locid, jt4.TotalAmount 
from transaction_tbl t 
CROSS JOIN @JaseemTest4Result jt4 
where t.Tbarcode='4502'