Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect syntax near the keyword 'ELSE'

I got a problem with else function.

Here is my code

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 

I get these errors:

Msg 156, Level 15, State 1, Procedure _AddLogChar, Line 155
Incorrect syntax near the keyword 'ELSE'.

Msg 156, Level 15, State 1, Procedure _AddLogChar, Line 194
Incorrect syntax near the keyword 'ELSE'.

Side note : this is for the first Else command but the second one doesn't give error (the ones which are before IF (@JKillerJobType = 2) AND IF (@JDeadJobType = 2) ! thanks !

like image 761
Muhab Avatar asked May 05 '14 22:05

Muhab


People also ask

How do you resolve Incorrect syntax near?

You can try to directly run your query to database and check if syntax is correct. There can be multiple () where your query might be failing. For example in my case the argument passed for IN clause was empty creating my query something like select * from tableName where id in () and hence was getting error.

What does Incorrect syntax near the keyword mean?

This indicates there is an incorrect symbol in the criteria of the query.


1 Answers

I have commented out some of the suspicious ENDs have a look now, With such a complex query Indentation really make things much easier.

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
like image 113
M.Ali Avatar answered Oct 24 '22 22:10

M.Ali