SQL函数不返回任何值

时间:2016-07-17 05:13:15

标签: sql-server

我的SQL Server函数没有返回任何值。 下面是我的脚本。请帮忙。 提前谢谢。

CREATE FUNCTION [dbo].[PreviousService](@Order int)
RETURNS @rtTable TABLE
(
        OrderId Int,
        PreviousService Int,
        CurrentService Int
)
AS
BEGIN
Declare @PreviousService Int,
        @CurrentService Int

DECLARE @resultTbl table (OrderId Int,PreviousService Int,CurrentService Int)

Select Top 2 @PreviousService=II.ServiceId
        From InvoiceItems II 
        Inner Join Invoices I On I.Id = II.InvoiceId 
        Inner Join Orders O On II.OrderId=O.OrderId
        where II.ServiceId is not null And O.OrderId= @Order And I.InvoiceStatus = 20
        Order By I.CreateDate Desc
Select Top 1 @CurrentService=II.ServiceId
From InvoiceItems II 
Inner Join Invoices I On I.Id = II.InvoiceId 
Inner Join Orders O On II.OrderId=O.OrderId
where II.ServiceId is not null And O.OrderId=@Order And I.InvoiceStatus = 20
Order By I.CreateDate Desc

insert into @resultTbl
Select O.OrderId,@CurrentService,@PreviousService
From Orders O
Where OrderId=@Order
return;
END

1 个答案:

答案 0 :(得分:0)

你不需要@resultTbl,你可以直接插入@rtTable

将最后一个插入语句更改为

insert into @rtTable 
Select O.OrderId,@CurrentService,@PreviousService
From Orders O
Where OrderId=@Order