面临错误的语法错误

时间:2013-03-13 11:22:52

标签: sql sql-server

我在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

1 个答案:

答案 0 :(得分:1)

使用RETURN代替RETURNS

RETURN @UTILIZATION

This is the syntax for creating a function according to the manual

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
[ ; ]