Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Exists Keyword

Author  Topic 

macca
Posting Yak Master

146 Posts

Posted - 2005-06-15 : 09:08:48
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

22864 Posts

Posted - 2005-06-15 : 09:27:20
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 - 2005-06-15 : 09:28:45
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

146 Posts

Posted - 2005-06-15 : 09:51:20
Thanks Madhivanan.
That worked.
Go to Top of Page
   

- Advertisement -