SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Exists Keyword
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

macca
Posting Yak Master

Ireland
146 Posts

Posted - 06/15/2005 :  09:08:48  Show Profile  Reply with Quote
I am using the below code within a Stored Procedure to see if a number exists in a column in a table and depending on whether it does or not I am doing an Insert.
Problem is I keep getting this error message when I try to save the Stored Procedure "Incorrect syntax near '=', Incorrect syntax near the keyword 'Else'".

Anyone any ideas what the problem is?
Thanks macca.

IF EXISTS (SELECT @tempNum = Num FROM GenNum WHERE @serOff = SO)
BEGIN
SET @tempNumTwo = @tempNum + 1
INSERT INTO GenNum(SO, Num)
VALUES(@serOff,@tempNumTwo)

END
ELSE
BEGIN
INSERT INTO GenNum(SO, Num)
VALUES(@serOff,@tempNum)
END

madhivanan
Premature Yak Congratulator

India
22742 Posts

Posted - 06/15/2005 :  09:27:20  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Replace
@tempNum = Num by Num =@tempNum
and @serOff = SO by SO=@serOff


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

RM
Yak Posting Veteran

65 Posts

Posted - 06/15/2005 :  09:28:45  Show Profile  Reply with Quote
You cannot assign value to a variable while using EXISTS. May be something like this..

DECLARE @tempNum INT

SELECT @tempNum = Num FROM GenNum WHERE SO = @serOff
INSERT GenNum(SO, Num)
SELECT @serOff, CASE WHEN @tempNum IS NULL THEN NULL ELSE @tempNum + 1 END
Go to Top of Page

macca
Posting Yak Master

Ireland
146 Posts

Posted - 06/15/2005 :  09:51:20  Show Profile  Reply with Quote
Thanks Madhivanan.
That worked.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000