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

Stored Procedure check for recordcount 2

Status
Not open for further replies.

Westond

Programmer
Jan 19, 2005
43
US
I have a stored proc and what I would like to do is run a select query then based on the recordcount insert or edit.

SELECT * FROM Table

if recordcount = 1
UPDATE Table set x = 1
WHERE...
else
INSERT INTO Table ()VALUES()

THANKS

Wes
 
If Exists(Select * from Table Where SomeField = @SomeValue)
Begin
-- Update Record
End
Else
Begin
-- Insert Record
End



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Be careful with that syntax. If @RecordCount = 2, You'll end up doing another insert. If that's what you want, then cool. Go for it.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
hi guys, i have a doubt here in effeciency,

aint count more effecient than Exists (which has a query that selects all records)???

Known is handfull, Unknown is worldfull
 
Exists is faster. Think about it. To get a count, you have to examine all the records to selectively include in the count. With exists, as soon as the first match is found, processing the records ends and it returns a true. Don't believe me. Do a little googling to see for yourself. Here's one I found.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
thanx dude...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top