Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Best way to check record existence and get its data Expand / Collapse
Author
Message
Posted Friday, September 27, 2013 8:07 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:30 AM
Points: 513, Visits: 1,129
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...
Post #1499402
Posted Friday, September 27, 2013 8:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:49 AM
Points: 13,094, Visits: 11,926
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 Moden's 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)
Post #1499429
Posted Friday, September 27, 2013 8:47 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:30 AM
Points: 513, Visits: 1,129
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...
Post #1499438
Posted Friday, September 27, 2013 8:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:49 AM
Points: 13,094, Visits: 11,926
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 Moden's 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)
Post #1499440
Posted Friday, September 27, 2013 8:53 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, June 12, 2014 9:30 AM
Points: 513, Visits: 1,129
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!!!




If you need to work better, try working less...
Post #1499442
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse