-2

我在 sql server 中编写了一个函数,并在“return”语句中遇到错误。

我面临的错误是

Incorrect syntax near 'RETURNS'.

我的sql函数如下

 CREATE FUNCTION UTILIZATION(@START_DATE DATETIME,
                        @END_DATE DATETIME,
                        @D1 DECIMAL(5,2),
                        @D2 DECIMAL(5,2),
                        @D3 DECIMAL(5,2),
                        @D4 DECIMAL(5,2),
                        @D5 DECIMAL(5,2),
                        @D6 DECIMAL(5,2),
                        @D7 DECIMAL(5,2)) 
RETURNS DECIMAL(5,2)
AS
BEGIN
DECLARE
    @UTILIZATION DECIMAL(5,2),
    @TOTAL_HOURS DECIMAL(5,2),
    @TOTAL_HOURS_ACCRUED DECIMAL(5,2),
    @QUERY  NVARCHAR(MAX);
BEGIN
SET @TOTAL_HOURS = @D1 + @D1 + @D3 + @D4 + @D5 + @D6 + @D7

SET @QUERY = ' SELECT TOTAL_ACTUAL_HOURS AS @TOTAL_HOURS_ACCRUED FROM WEEK_MASTER WHERE' +
             ' START_DATE = ' + '''' + @START_DATE + '''' +' AND ' +
             ' END_DATE = ' + '''' + @END_DATE + ''''

EXEC SP_EXECUTESQL @QUERY

SET @UTILIZATION = (@TOTAL_HOURS/@TOTAL_HOURS_ACCRUED)*100

RETURNS @UTILIZATION
END
END
4

1 回答 1

1

使用RETURN而不是RETURNS

RETURN @UTILIZATION

这是根据手册创建函数的语法

CREATE FUNCTION [ schema_name. ] function_name 
( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type 
    [ = default ] [ READONLY ] } 
    [ ,...n ]
  ]
)
RETURNS return_data_type
    [ WITH <function_option> [ ,...n ] ]
    [ AS ]
    BEGIN 
        function_body 
        RETURN scalar_expression
    END
[ ; ]
于 2013-03-13T11:24:13.323 回答