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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is best practice with IF Exists

Status
Not open for further replies.

mjp483

Programmer
Apr 27, 2004
42
US
I want to run an update based on whether a condition exists. To get the condition I run a query written like

IF Exists (select 1 from Table a join Table b on a.id = b.id)

Update Table A

The select query returns 300 or so results as 1, is there a better practice so the condition is met before all 300 results are retrieved...maybe something more efficient to kick off the update once the very first record is found? Or is it already? Thanks
 
Who says SELECT query returns 300 results? :X

------
"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]
 
In query analyzer I run the select statement and it returns 300 1's......
 
Yes, but EXISTS() tells optimizer to stop subquery execution after first value found.

So tehnically - even if * is used instead of 1, it shouldn't matter (for SQL2k server).



------
"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]
 
That is the assurance I was looking for...thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top