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 vs SELECT

Author  Topic 

SQL_Stinger
Starting Member

40 Posts

Posted - 2001-01-18 : 10:28:04
I have stored procedures written to insert data into my tables. The first part of the script uses EXISTS to determine if there is a duplicate row already in the table and returns -1 if its a dupe:

IF EXISTS(SELECT AllergenID FROM LstAllergens
WHERE Allergen = @vchrAllergen AND ActiveFlag = 1)
BEGIN
Set @iReturnVal = -1
RETURN
END


Now I want to return the negative value of the ID column (denoting the ID and the fact that the record existed).

DECLARE @ID int
SELECT @ID = AllergenID FROM LstAllergens
WHERE Allergen = @vchrAllergen AND ActiveFlag = 1

IF @ID IS NOT NULL
BEGIN
Set @iReturnVal = -1 * @ID
RETURN
END


One of the developers is arguing that the SELECT requires more resources than the EXISTS. What do the gurus say?


--------------------
SQL Stinger
   

- Advertisement -