Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Exists or just Count?

Status
Not open for further replies.

jfrost10

Programmer
Jun 3, 2001
2,004
CA
hey guys,

We have a single Save stored proc for many of our business objects. In this proc, we determine whether its an insert or update. We're converting our SP's from having to pass in a bit saying if its inserting or updating, to doing it a bit smarter. We have two options, both that appear to work the same, but I'm curious as to the best one performane and best-practice wise.

Option 1: Exists
Code:
If Exists(Select ComboID1, ComboID2, ComboID3 From Table Where ComboID1 = @ID1 And ComboID2 = @ID2 And ComboID3 = @ID3)

begin
Insert...
end

Else

begin
Update...
end

Option 2: Count

Code:
Delcare @Counter as int
Select @Counter = Count(ComboID1)From Table Where ComboID1 = @ID1 And ComboID2 = @ID2 And ComboID3 = @ID3)

If @Counter = 0
begin
Insert...
end

Else

begin
Update...
end

Looking forward to your thoughts. Thanks!

D'Arcy
 
IMHO, the EXISTS is more to the point and may perform a little better unless you need the COUNT for anything else.

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
AFAIK in some cases a bit better. Unlike COUNT, EXISTS stops processing after first record was found.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top