关键字'ELSE'附近的语法不正确

时间:2014-05-05 22:15:34

标签: sql-server tsql

我遇到了else函数的问题。

这是我的代码

IF (Not Exists(SELECT * FROM Log_DB.dbo._LogJobSYS WHERE KillerJobID=@JKillerID AND DeadJobID=@CharID)) 
    BEGIN
        INSERT Log_DB.dbo._LogJobSYS(KillerJobID,DeadJobID,DeathTime)
        VALUES (@JKillerID, @CharID, GETDATE())
        BEGIN
            BEGIN
                IF (@JKillerJobType = 1) /*TRADER*/ 
                BEGIN
                IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobTradersys Where CharID=@JKillerID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobTradersys(CharID,TraderPoints,Kills,Deaths,LastKill,Date)
                VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE Begin
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobTradersys SET TraderPoints = TraderPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END
                END
            END
            ELSE 
                IF (@JKillerJobType = 2) /*THIEF*/ 
                BEGIN
                IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobThiefsys Where CharID=@JKillerID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobThiefsys(CharID,ThiefPoints,Kills,Deaths,LastKill,Date)
                VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE Begin
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobThiefsys SET ThiefPoints = ThiefPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END

            END
            ELSE 
                IF (@JKillerJobType = 3) /*HUNTER*/ 
                BEGIN
                IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobHuntersys Where CharID=@JKillerID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobHuntersys(CharID,HunterPoints,Kills,Deaths,LastKill,Date)
                VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE Begin
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobHuntersys SET HunterPoints = HunterPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END

            END 
            BEGIN
                IF (@JDeadJobType = 1) /*TRADER*/ 
                BEGIN
                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobTradersys WHERE CharID=@CharID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobTradersys(CharID,TraderPoints,Kills,Deaths,LastKill,Date)
                VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                END
                ELSE BEGIN
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobTradersys SET TraderPoints = TraderPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                END
                END
            END 
            ELSE  
                IF (@JDeadJobType = 2) /*THIEF*/ 
                BEGIN
                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobThiefsys WHERE CharID=@CharID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobThiefsys(CharID,ThiefPoints,Kills,Deaths,LastKill,Date)
                VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                END
                ELSE BEGIN
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobThiefsys SET ThiefPoints = ThiefPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                END

            END
            ELSE 
                IF (@JDeadJobType = 3) /*HUNTER*/ 
                BEGIN
                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobHuntersys WHERE CharID=@CharID))
                BEGIN
                INSERT SRO_VT_SHARD_INIT.dbo._CharJobHuntersys(CharID,HunterPoints,Kills,Deaths,LastKill,Date)
                VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                END
                ELSE BEGIN
                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobHuntersys SET HunterPoints = HunterPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                END

            END
        END
    END 

我收到这些错误:

  

Msg 156,Level 15,State 1,Procedure _AddLogChar,Line 155
  关键字' ELSE'附近的语法不正确。

     

消息156,级别15,状态1,过程_AddLogChar,第194行   关键字' ELSE'附近的语法不正确。

旁注:这是第一个Else命令,但第二个没有给出错误(IF之前的那些(@JKillerJobType = 2)和IF(@JDeadJobType = 2)! 谢谢!

3 个答案:

答案 0 :(得分:4)

我已经注释掉了一些可疑的END现在看看,有了这么复杂的查询,缩进确实让事情变得更容易。

IF (Not Exists(SELECT * FROM Log_DB.dbo._LogJobSYS WHERE KillerJobID=@JKillerID AND DeadJobID=@CharID)) 
    BEGIN
        INSERT Log_DB.dbo._LogJobSYS(KillerJobID,DeadJobID,DeathTime)
        VALUES (@JKillerID, @CharID, GETDATE())
        BEGIN

                IF (@JKillerJobType = 1) /*TRADER*/ 
                    BEGIN
                        IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobTradersys Where CharID=@JKillerID))
                          BEGIN
                             INSERT SRO_VT_SHARD_INIT.dbo._CharJobTradersys(CharID,TraderPoints,Kills,Deaths,LastKill,Date)
                             VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                           END
                        ELSE 
                          Begin
                            UPDATE SRO_VT_SHARD_INIT.dbo._CharJobTradersys SET TraderPoints = TraderPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                          END
                    END
                    --END  --<-- This End

                    ELSE IF (@JKillerJobType = 2) /*THIEF*/ 
                      BEGIN
                             IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobThiefsys Where CharID=@JKillerID))
                                BEGIN
                                    INSERT SRO_VT_SHARD_INIT.dbo._CharJobThiefsys(CharID,ThiefPoints,Kills,Deaths,LastKill,Date)
                                    VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                                END
                        ELSE 
                            Begin
                                UPDATE SRO_VT_SHARD_INIT.dbo._CharJobThiefsys SET ThiefPoints = ThiefPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                            END

                      END

                    ELSE IF (@JKillerJobType = 3) /*HUNTER*/ 
                      BEGIN
                            IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobHuntersys Where CharID=@JKillerID))
                                BEGIN
                                    INSERT SRO_VT_SHARD_INIT.dbo._CharJobHuntersys(CharID,HunterPoints,Kills,Deaths,LastKill,Date)
                                    VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                                END
                            ELSE 
                                Begin
                                    UPDATE SRO_VT_SHARD_INIT.dbo._CharJobHuntersys SET HunterPoints = HunterPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                            END

                      END 
                    BEGIN IF (@JDeadJobType = 1) /*TRADER*/ 
                        BEGIN
                                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobTradersys WHERE CharID=@CharID))
                                    BEGIN
                                        INSERT SRO_VT_SHARD_INIT.dbo._CharJobTradersys(CharID,TraderPoints,Kills,Deaths,LastKill,Date)
                                        VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                                    END
                                ELSE 
                                    BEGIN
                                        UPDATE SRO_VT_SHARD_INIT.dbo._CharJobTradersys SET TraderPoints = TraderPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                                    END
                        END
                    --END   --<--  this End  
                    ELSE  IF (@JDeadJobType = 2) /*THIEF*/ 
                        BEGIN
                                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobThiefsys WHERE CharID=@CharID))
                                    BEGIN
                                        INSERT SRO_VT_SHARD_INIT.dbo._CharJobThiefsys(CharID,ThiefPoints,Kills,Deaths,LastKill,Date)
                                        VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                                    END
                                ELSE 
                                    BEGIN
                                        UPDATE SRO_VT_SHARD_INIT.dbo._CharJobThiefsys SET ThiefPoints = ThiefPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                                END

                        END
                    ELSE IF (@JDeadJobType = 3) /*HUNTER*/ 
                        BEGIN
                                IF (NOT EXISTS(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobHuntersys WHERE CharID=@CharID))
                                    BEGIN
                                        INSERT SRO_VT_SHARD_INIT.dbo._CharJobHuntersys(CharID,HunterPoints,Kills,Deaths,LastKill,Date)
                                        VALUES (@CharID, 0, 0, 1, 'NONE', GETDATE())
                                    END
                                ELSE 
                                    BEGIN
                                        UPDATE SRO_VT_SHARD_INIT.dbo._CharJobHuntersys SET HunterPoints = HunterPoints -0.5, Deaths = Deaths +1, Date = GETDATE() WHERE CharID = @CharID
                             END

                      END
               END  
          END
   END

答案 1 :(得分:1)

看起来你可能会错过一些&#39; END&#39; END。除非你的意思是更深入。对于TRADER类型,你一直关闭,但对于THIEF,你不会。

BEGIN
        BEGIN
            IF (@JKillerJobType = 1) /*TRADER*/ 
            BEGIN
                IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobTradersys Where CharID=@JKillerID))
                BEGIN
                    INSERT SRO_VT_SHARD_INIT.dbo._CharJobTradersys(CharID,TraderPoints,Kills,Deaths,LastKill,Date)
                    VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE 
                Begin
                    UPDATE SRO_VT_SHARD_INIT.dbo._CharJobTradersys SET TraderPoints = TraderPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END
            END
        END
        ELSE 
            IF (@JKillerJobType = 2) /*THIEF*/ 
            BEGIN
                IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobThiefsys Where CharID=@JKillerID))
                BEGIN
                    INSERT SRO_VT_SHARD_INIT.dbo._CharJobThiefsys(CharID,ThiefPoints,Kills,Deaths,LastKill,Date)
                    VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE 
                Begin
                    UPDATE SRO_VT_SHARD_INIT.dbo._CharJobThiefsys SET ThiefPoints = ThiefPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END
            END
            ELSE 
                IF (@JKillerJobType = 3) /*HUNTER*/ 
                BEGIN
                    IF (Not Exists(SELECT * FROM SRO_VT_SHARD_INIT.dbo._CharJobHuntersys Where CharID=@JKillerID))
                    BEGIN
                    INSERT SRO_VT_SHARD_INIT.dbo._CharJobHuntersys(CharID,HunterPoints,Kills,Deaths,LastKill,Date)
                    VALUES (@JKillerID, 1, 1, 0, @JDeadNick, GETDATE())
                END
                ELSE 
                Begin
                    UPDATE SRO_VT_SHARD_INIT.dbo._CharJobHuntersys SET HunterPoints = HunterPoints +1, Kills = Kills +1, LastKill = @JDeadNick, Date = GETDATE() WHERE CharID = @JKillerID
                END

答案 2 :(得分:0)

在_AddLogChar周围加上方括号。 [_AddLogChar]