2014-09-10 164 views
1

我有以下SQL函數定義和測試:SQL函數返回任何結果,但功能內容返回結果

IF EXISTS (SELECT * 
     FROM sys.objects 
     WHERE object_id = OBJECT_ID(N'[dbo].[udf_Assessment_Timelines]') 
       AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT')) 
    DROP FUNCTION [dbo].udf_Assessment_Timelines 
GO 
CREATE FUNCTION dbo.udf_Assessment_Timelines 
(
    @startDate datetime, 
    @endDate datetime, 
    @assessmentId int = 14, 
    @outIds nvarchar(max)='3,9,10' 
) 
RETURNS @result TABLE(case_id int, [TOTAL DAYS] int) 
AS 
BEGIN 
    DECLARE @in TimeLineReportList; 
    DECLARE @out TimeLineReportList; 

    INSERT INTO @in 
     SELECT * FROM dbo.udf_First_Timeline_Entries_Of_Status(@assessmentId) 

    INSERT INTO @out 
     SELECT * FROM dbo.udf_First_Timeline_Entries_Of_Any_Statuses(@outIds) 

    INSERT INTO @result 
     SELECT * FROM dbo.udf_Generic_Timelines(@startDate, @endDate, @in, @out,null) 

    RETURN 
END 
GO 

SELECT * FROM dbo.udf_Assessment_Timelines('2013-01-01T00:00:00.000', '2014-01-01T00:00:00.000',null,null) 

調用該函數返回0行的最後一行。但是,下面的SQL直接執行時會返回行。有什麼不同?

DECLARE @result TABLE(case_id int, [TOTAL DAYS] int)  
DECLARE @in TimeLineReportList; 
DECLARE @out TimeLineReportList; 

INSERT INTO @in 
    SELECT * FROM dbo.udf_First_Timeline_Entries_Of_Status(14) 

INSERT INTO @out 
    SELECT * FROM dbo.udf_First_Timeline_Entries_Of_Any_Statuses('3,9,10') 

INSERT INTO @result 
    SELECT * FROM dbo.udf_Generic_Timelines('2013-01-01T00:00:00.000', '2014-01-01T00:00:00.000', @in, @out,null) 

SELECT * FROM @result 

回答

4

你覆蓋@assessmentId@outIds是在不工作的版本null

SELECT * FROM dbo.udf_Assessment_Timelines('2013-01-01T00:00:00.000', 
    '2014-01-01T00:00:00.000', null, null) 
          ^ ^

如果你想使用的默認值的參數,使用default關鍵字來代替:

SELECT * FROM dbo.udf_Assessment_Timelines('2013-01-01T00:00:00.000', 
    '2014-01-01T00:00:00.000', default, default) 
          ^ ^
+0

謝謝!我是一個將C#邏輯應用於SQL的C#-er。 :( – hofnarwillie 2014-09-10 12:46:07