SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Best way to check record existence and get its data


Best way to check record existence and get its data

Author
Message
PiMané
PiMané
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2904 Visits: 1351
Hi,

What the best way to check if a record exists and at get a column from it to use?

DECLARE @order INT = '1'
DECLARE @col INT

IF EXISTS (SELECT 1 FROM Orders WHERE NumOrder = @order)
BEGIN
SELECT @col = col FROM Orders WHERE NumOrder = @order
......
END


Or

DECLARE @order INT = '1'
DECLARE @col INT

SELECT @col = col FROM Orders WHERE NumOrder = @order
IF @@ROWCOUNT > 0
BEGIN
......
END


Or is there another way?
IF EXISTS (SELECT @col = col FROM Orders WHERE NumOrder = @order) 

doesn't work :-(

Thanks,
Pedro



If you need to work better, try working less...
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62461 Visits: 17955
I would use EXISTS. Using exists looks for the actual row in the table, the @@ROWCOUNT version is examining the results of query to see if the row exists. That works but is far more complicated than needed imho.

There may be other ways to handle the existence of the row but from this short snippet it is hard to tell.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
PiMané
PiMané
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2904 Visits: 1351
I just want to see if a certain record exists, get a value from a column that's going to be used on a procedure call and do some other stuff inside the IF.



If you need to work better, try working less...
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62461 Visits: 17955
Then EXISTS is a good choice. Another option might be something like this.


DECLARE @order INT = '1'
DECLARE @col INT

SELECT @col = col FROM Orders WHERE NumOrder = @order
IF @col IS NOT NULL
BEGIN

END



That might be a little simpler because you don't have to read the table more than once.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
PiMané
PiMané
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2904 Visits: 1351
Sean Lange (9/27/2013)
Then EXISTS is a good choice. Another option might be something like this.


DECLARE @order INT = '1'
DECLARE @col INT

SELECT @col = col FROM Orders WHERE NumOrder = @order
IF @col IS NOT NULL
BEGIN

END



That might be a little simpler because you don't have to read the table more than once.


Doh!!! :-D



If you need to work better, try working less...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search