每当我执行函数获取错误时,子查询返回的值超过1。子查询遵循=,!=,<,< =,>,> =

时间:2016-02-03 07:04:51

标签: sql-server tsql

我使用T-SQL编写了下面的函数,但每当我执行它时,我都会收到错误

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

ALTER FUNCTION [dbo].[get_lapsed_days]
(
    @month INT,
    @year INT,
    @siteid INT
)
RETURNS INT
BEGIN
    DECLARE @lapsed_days AS INT;
    SELECT @lapsed_days = (
        SELECT CASE
                WHEN eil.RULE_TYPE = 'Remuneration'
                    THEN (
                        SELECT (SUM(etts.GENERAL_SHIFT + etts.FIRST_SHIFT + etts.SECOND_SHIFT + etts.THIRD_SHIFT))
                        FROM dbo.EMP_FPONM_EMP_TIME_SHEET etts
                        WHERE etts.emp_id = eil.emp_code
                            AND etts.month = @month
                            AND etts.year = @year
                            AND etts.SITE_ID = @siteid
                    )
                WHEN eil.RULE_TYPE = 'Comp-Off'
                    THEN (
                        SELECT (LAPSED_DAYS) * 8
                        FROM EMP_FPONM_COMP_OFF_BALANCE
                        WHERE EMP_FPONM_COMP_OFF_BALANCE.EMP_ID = eil.EMP_CODE
                            AND EMP_FPONM_COMP_OFF_BALANCE.month = @month
                            AND EMP_FPONM_COMP_OFF_BALANCE.year = @year
                            AND EMP_FPONM_COMP_OFF_BALANCE.SITE_ID = @siteid
                    )
                ELSE 0
            END
        FROM dbo.EMP_FPONM_EMPLOYEE_INFORMATION eil
    )
    RETURN @lapsed_days

END

1 个答案:

答案 0 :(得分:1)

ALTER FUNCTION [dbo].[get_lapsed_days]
(
    @month INT,
    @year INT,
    @siteid INT
)
RETURNS INT
BEGIN

    RETURN (
        SELECT SUM(
                CASE
                    WHEN eil.RULE_TYPE = 'Remuneration'
                        THEN ETS.GENERAL_SHIFT + ETS.FIRST_SHIFT + ETS.SECOND_SHIFT + ETS.THIRD_SHIFT
                    ELSE 0
                END)
            FROM dbo.EMP_FPONM_EMPLOYEE_INFORMATION eil
            JOIN dbo.EMP_FPONM_EMP_TIME_SHEET ETS ON eil.emp_code = ETS.emp_id
            WHERE ETS.site_id = @siteid
                AND ETS.month = @month
                AND ETS.year = @year
    )

END

更新

ALTER FUNCTION [dbo].[get_lapsed_days]
(
    @month INT,
    @year INT,
    @siteid INT
)
RETURNS INT
BEGIN
    DECLARE @lapsed_days INT
    SELECT @lapsed_days = (
        SELECT SUM(
            CASE
                WHEN eil.RULE_TYPE = 'Remuneration' THEN etts.GENERAL_SHIFT + etts.FIRST_SHIFT + etts.SECOND_SHIFT + etts.THIRD_SHIFT
                WHEN eil.RULE_TYPE = 'Comp-Off' THEN LAPSED_DAYS * 8
            END
        )
        FROM dbo.EMP_FPONM_EMPLOYEE_INFORMATION eil
        LEFT JOIN dbo.EMP_FPONM_COMP_OFF_BALANCE b ON b.EMP_ID = eil.EMP_CODE
            AND b.[month] = @month
            AND b.[year] = @year
            AND b.SITE_ID = @siteid
            AND eil.RULE_TYPE = 'Comp-Off'
        LEFT JOIN dbo.EMP_FPONM_EMP_TIME_SHEET etts ON etts.emp_id = eil.emp_code
            AND etts.month = @month
            AND etts.year = @year
            AND etts.SITE_ID = @siteid
            AND eil.RULE_TYPE = 'Remuneration'
        WHERE eil.RULE_TYPE IN ('Comp-Off', 'Remuneration')
    )
    RETURN ISNULL(@lapsed_days, 0)

END