急:存储过程遍历调用存储过程* (赏100分)
存储过程(proc_GetFltAriLineByOrderId名称):是根据订单号(OrderID)得到路程(Tpm)只有一条。
现在要写一个存储过程调用上面那个存储过程
根据用户UID得到订单号 订单号可能为多行,返回的订单OrderID可能是个集合。
CREATE PROCEDURE [dbo].[proc_GetFltAriLineByOrderId2]
@UID varchar(10)
AS
begin
select fltorder.OrderID from
[FltOrderDB].[dbo].[FltPassenger] fltpass
inner join [FltOrderDB].[dbo].[FltOrder] fltorder
on fltpass.OrderID=fltorder.ID
inner JOin [UserDB].[dbo].[Member_Corp] corp on fltpass.CorpUID=corp.UID
where corp.UID=@UID and fltorder.Status=1
end
CREATE PROCEDURE [dbo].[proc_GetFltAriLineByOrderId2] @UID VARCHAR(10)
AS
BEGIN
DECLARE @table TABLE
(
OrderID VARCHAR(10),
Tpm NVARCHAR(MAX)
)
DECLARE @temp TABLE
(
Tpm NVARCHAR(MAX)
)
DECLARE @OrderID VARCHAR(10)
DECLARE cursor_order CURSOR STATIC LOCAL FORWARD_ONLY READ_ONLY--定义游标
FOR
SELECT fltorder.OrderID
FROM [FltOrderDB].[dbo].[FltPassenger] fltpass
INNER JOIN [FltOrderDB].[dbo].[FltOrder] fltorder ON fltpass.OrderID = fltorder.ID
INNER JOIN [UserDB].[dbo].[Member_Corp] corp ON fltpass.CorpUID = corp.UID
WHERE corp.UID = @UID
AND fltorder.Status = 1
OPEN cursor_order--打开游标
FETCH NEXT FROM cursor_order INTO @OrderID--指针指向第一行
WHILE (@@FETCH_STATUS = 0)--如果指针没到行尾,就继续
BEGIN
DELETE @temp
INSERT INTO @temp--得到一个Order的tpm
(
Tpm
)
EXEC proc_GetFltAriLineByOrderID @OrderID
INSERT @table ( OrderID, Tpm )--缓存一对Order, tpm
SELECT TOP(1)
@OrderID,
Tpm
FROM @temp
FETCH NEXT FROM cursor_order INTO @OrderID--指针指向下一行
END
CLOSE cursor_order
DEALLOCATE cursor_order
SELECT * FROM @table--返回最终的结果
END
create proc dbo.[存储过程名]
(@UID varchar(10))
as
begin
set nocount on
create table #Orders(OrderID varchar(10))
create table #return(Tpm varchar(6000))
insert into #Orders(OrderID) exec dbo.proc_GetFltAriLineByOrderId2 @UID
declare @oid varchar(10)
declare ap scroll cursor for select OrderID from #Orders
open ap
fetch first from ap into @oid
while(@@fetch_status<>-1)
begin
insert into #return(Tpm)
exec dbo.proc_GetFltAriLineByOrderId @oid
fetch next from ap into @oid
end
close ap
deallocate ap
select @UID 'UID',Tpm from #return
end