Solved

Errors with SQL syntax

Posted on 2013-02-07
5
324 Views
Last Modified: 2013-02-07
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 ')'.
0
Comment
Question by:JessyRobinson1234
5 Comments
 
LVL 12

Expert Comment

by:Jared_S
Comment Utility
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)
0
 
LVL 11

Accepted Solution

by:
Simone B earned 350 total points
Comment Utility
Try this:

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
0
 
LVL 12

Expert Comment

by:Jared_S
Comment Utility
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)
0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
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.
0
 

Author Closing Comment

by:JessyRobinson1234
Comment Utility
This was the only one working for me. Thank you everyone.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now