如果存储过程中的ELSE STATEMENT

时间:2018-01-22 16:10:56

标签: sql-server tsql if-statement stored-procedures

我正在尝试编写存储过程,但是我收到错误

  

';'

附近的语法不正确

我认为IF Else陈述有问题。

CREATE PROCEDURE setSystemStaff
    @SYSTEMNAME nvarchar(50),
    @STAFFNAME nvarchar(50),
    @SYSTEMSTAFFROLE nvarchar(50)
AS
BEGIN
    SET NOCOUNT ON;

    WITH t1 (SYSTEMID) AS 
    (
        SELECT SYSTEMID 
        FROM SYSTEM 
        WHERE SYSTEMNAME = @SYSTEMNAME
    ), t2 (STAFFID) AS 
    (
        SELECT STAFFID 
        FROM STAFF 
        WHERE STAFFNAME = @STAFFNAME
    );

    IF @SYSTEMSTAFFROLE = 'Owner'
        INSERT INTO SYSTEMSTAFF ([SYSTEMID], [SYSTEMSTAFFOWNER])    
            SELECT t1.SYSTEMID, t2.STAFFID 
            FROM t1, t2
    ELSE IF @SYSTEMSTAFFROLE = 'Specialist'
        INSERT INTO SYSTEMSTAFF ([SYSTEMID], [SYSTEMSTAFSPECIALIST])    
            SELECT t1.SYSTEMID, t2.STAFFID 
            FROM t1,t2  
    ELSE 
        RETURN  
END
GO

4 个答案:

答案 0 :(得分:1)

您可以使用案例陈述重写程序,而不使用IF..ELSE

CREATE PROCEDURE setSystemStaff
-- Add the parameters for the stored procedure here
@SYSTEMNAME nvarchar(50),
@STAFFNAME nvarchar(50),
@SYSTEMSTAFFROLE nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
WITH t1 
(
    SYSTEMID
) 
AS 
(
    SELECT 
        SYSTEMID  
        FROM SYSTEM 
            WHERE SYSTEMNAME = @SYSTEMNAME
),
t2
(
    STAFFID
) 
AS 
(
    SELECT 
        STAFFID 
        FROM STAFF 
            WHERE STAFFNAME = @STAFFNAME
);
INSERT INTO SYSTEMSTAFF
(
    [SYSTEMID],
    [SYSTEMSTAFFOWNER],
    [SYSTEMSTAFSPECIALIST]
)
SELECT
    SYSTEMID = t1.SYSTEMID,
    SYSTEMSTAFFOWNER = CASE WHEN @SYSTEMSTAFFROLE = 'Owner'
                                THEN t2.STAFFID END,
    SYSTEMSTAFSPECIALIST = CASE WHEN @SYSTEMSTAFFROLE = 'Specialist'
                                THEN t2.STAFFID END
    FROM T1,T2

END
GO

你也可以简单地完成这样的程序

CREATE PROCEDURE setSystemStaff
-- Add the parameters for the stored procedure here
@SYSTEMNAME nvarchar(50),
@STAFFNAME nvarchar(50),
@SYSTEMSTAFFROLE nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here

INSERT INTO SYSTEMSTAFF
(
    [SYSTEMID],
    [SYSTEMSTAFFOWNER],
    [SYSTEMSTAFSPECIALIST]
)
SELECT
    SYSTEMID = t1.SYSTEMID,
    SYSTEMSTAFFOWNER = CASE WHEN @SYSTEMSTAFFROLE = 'Owner'
                                THEN t2.STAFFID END,
    SYSTEMSTAFSPECIALIST = CASE WHEN @SYSTEMSTAFFROLE = 'Specialist'
                                THEN t2.STAFFID END
    FROM SYSTEM T1
        INNER JOIN STAFF T2
            ON T1.SYSTEMNAME = @SYSTEMNAME
                AND T2.STAFFNAME = @STAFFNAME

END
GO

答案 1 :(得分:0)

我认为你缺少'那么'的关键字。 你应该把这个条件设置为

如果那么 - 声明(根据需要插入/更新/删除) 埃尔西夫然后 - 声明(根据需要插入/更新/删除)

结束if;

- 仅在SQL中使用的上述语法

答案 2 :(得分:0)

请试试这个 -

CREATE PROCEDURE setSystemStaff
(
    @SYSTEMNAME nvarchar(50),
    @STAFFNAME nvarchar(50),
    @SYSTEMSTAFFROLE nvarchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
    IF @SYSTEMSTAFFROLE = 'Owner'
    BEGIN
        WITH t1 AS 
        (
            SELECT SYSTEMID  FROM SYSTEM WHERE SYSTEMNAME = @SYSTEMNAME
        )
        ,t2 AS 
        (
            SELECT STAFFID FROM STAFF WHERE STAFFNAME = @STAFFNAME
        )
        INSERT INTO SYSTEMSTAFF ([SYSTEMID],[SYSTEMSTAFFOWNER]) 
        SELECT t1.SYSTEMID, t2.STAFFID FROM t1,t2
    END
    ELSE IF @SYSTEMSTAFFROLE = 'Specialist'
    BEGIN
        WITH t1 AS 
        (
            SELECT SYSTEMID  FROM SYSTEM WHERE SYSTEMNAME = @SYSTEMNAME
        )
        ,t2 AS 
        (
            SELECT STAFFID FROM STAFF WHERE STAFFNAME = @STAFFNAME
        )
        INSERT INTO SYSTEMSTAFF ([SYSTEMID],[SYSTEMSTAFSPECIALIST]) 
        SELECT t1.SYSTEMID, t2.STAFFID FROM t1,t2  
    END
END
GO

答案 3 :(得分:0)

或者:传递给临时表。

    CREATE PROCEDURE setSystemStaff
    -- Add the parameters for the stored procedure here
        @SYSTEMNAME NVARCHAR(50) ,
        @STAFFNAME NVARCHAR(50) ,
        @SYSTEMSTAFFROLE NVARCHAR(50)
    AS
        BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
            SET NOCOUNT ON;
    -- Insert statements for procedure here

            WITH    t1
                      AS ( SELECT   SYSTEMID
                           FROM     SYSTEM
                           WHERE    SYSTEMNAME = @SYSTEMNAME
                         ),
                    T2
                      AS ( SELECT   STAFFID
                           FROM     STAFF
                           WHERE    STAFFNAME = @STAFFNAME
                         ),
                    T3
                      AS ( SELECT   t1.SYSTEMID ,
                                    t2.STAFFID
                           FROM     t1 ,
                                    t2
                         )
                SELECT  *
                INTO    #temptbl
                FROM    t3


            IF @SYSTEMSTAFFROLE = 'Owner'
                BEGIN
                    INSERT  INTO SYSTEMSTAFF
                            ( [SYSTEMID] ,
                              [SYSTEMSTAFFOWNER]
                            )
                            SELECT  SYSTEMID ,
                                    STAFFID
                            FROM    #temptbl
                END 
            ELSE
                IF @SYSTEMSTAFFROLE = 'Specialist'
                    BEGIN
                        INSERT  INTO SYSTEMSTAFF
                                ( [SYSTEMID] ,
                                  [SYSTEMSTAFSPECIALIST]
                                )
                                SELECT  SYSTEMID ,
                                        STAFFID
                                FROM    #temptbl
                    END

            RETURN

        END
    GO