Link to home
Start Free TrialLog in
Avatar of JessyRobinson1234
JessyRobinson1234

asked on

Errors with SQL syntax

IF (Select * from Lenovo_Events where Event_Key = @Event_Key) IS NULL THEN INSERT INTO Lenovo_Events VALUES (@Event_Date,@Line_Key, @Shift_Key, @Total_Qty, 0,0) ELSE UPDATE Lenovo_Events set Total_Qty = @Total_Qty where Event_Key = @Event_Key) END IF

Error:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Incorrect syntax near the keyword 'THEN'.
Incorrect syntax near the keyword 'ELSE'.
Incorrect syntax near ')'.
Avatar of Jared_S
Jared_S

IF (Select * from Lenovo_Events where Event_Key = @Event_Key) IS NULL
INSERT INTO Lenovo_Events VALUES (@Event_Date,@Line_Key, @Shift_Key, @Total_Qty, 0,0)
ELSE
UPDATE Lenovo_Events set Total_Qty = @Total_Qty where Event_Key = @Event_Key)
ASKER CERTIFIED SOLUTION
Avatar of Simone B
Simone B
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might also try:

IF NOT EXISTS (Select * from Lenovo_Events where Event_Key = @Event_Key)
INSERT INTO Lenovo_Events VALUES (@Event_Date,@Line_Key, @Shift_Key, @Total_Qty, 0,0)
ELSE
UPDATE Lenovo_Events set Total_Qty = @Total_Qty where Event_Key = @Event_Key)
Avatar of Lowfatspread
remove the END IF

and retry

IF (Select * from Lenovo_Events where Event_Key = @Event_Key) IS NULL THEN INSERT INTO Lenovo_Events VALUES (@Event_Date,@Line_Key, @Shift_Key, @Total_Qty, 0,0) ELSE UPDATE Lenovo_Events set Total_Qty = @Total_Qty where Event_Key = @Event_Key) END IF

1 it should only be END
2 you only have an END if you also have a matching prior BEGIN

other points of concern
1. always specify the column list that you are presenting to the insert...
     insert into table (columnlist) values (1,2,3...)
2. shouldn't one of the columns in the values clause be @event_key?
3. not clear , but relationaly suspect why you can just update the total_qty without checking/updating the shift/line key values...


ps the IF not Exists (select ...) then ... is normally considered the standard syntax.
Avatar of JessyRobinson1234

ASKER

This was the only one working for me. Thank you everyone.