Solved

Errors with SQL syntax

Posted on 2013-02-07
5
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Expert Comment

by:Jared_S
ID: 38863979
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
ID: 38863982
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
ID: 38863983
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
ID: 38864093
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
ID: 38864249
This was the only one working for me. Thank you everyone.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…

749 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