September 27, 2013 at 8:07 am
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
September 27, 2013 at 8:39 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 27, 2013 at 8:47 am
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.
September 27, 2013 at 8:50 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 27, 2013 at 8:53 am
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!!! 😀
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply