March 25, 2019 at 2:23 am
In the ye olde AdventureWorks2016 database if I run the following query it returns Exists, even though the sub-query returns no rows, I'm trying to under why.
SELECT CASE WHEN EXISTS
(
SELECT COUNT(*)
FROM Production.Product
WHERE ProductID = 9999
) THEN 'Exists' ELSE 'Does not Exist' END
I have tried a few things including checking the @@Rowcount before and after the query e.g.
SELECT @@Rowcount -- Returns 0
SELECT COUNT(*)
FROM Production.Product
WHERE ProductID = 9999 -- Returns 0
SELECT @@Rowcount -- Returns 1
Is the reason it returns Exists when using the case statement because it returned a row count of 1 to the outer query?
Any help appreciated.
March 25, 2019 at 4:55 am
It will exist with a count of zero.
You need to replace 'COUNT(*)' with '*'
March 25, 2019 at 5:08 am
According to BOL, EXISTS "Returns TRUE if a subquery contains any rows."
Your subquery will always return one row, where the integer column value will be 0 or greater:
SELECT COUNT(*)
FROM Production.Product
WHERE ProductID = 9999
Note that COUNT will count all qualifying rows. The EXISTS operator will stop when a qualifying row is first found, regardless of how many there are. Change the syntax of your query only slightly to benefit from this optimization, and to return zero rows if no qualifying rows exist:
EXISTS(
SELECT 1 FROM Production.Product
WHERE ProductID = 9999
)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 25, 2019 at 10:52 am
'ye' in this context is a misunderstanding of 'ðe', where 'ð' is the old spelling of 'th' as in 'the'. In fact, 'ðe' IS the old spelling of 'the', so you are essentially saying 'in the the olde....'
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
March 25, 2019 at 10:56 am
ReamerXXVI - Monday, March 25, 2019 2:23 AMIn the ye olde
'ye' in this context is a misunderstanding of 'ðe', where 'ð' is the old spelling of 'th' as in 'the'. In fact, 'ðe' IS the old spelling of 'the', so you are essentially saying 'in the the olde....'
Drew
Playing on the fringe of various medieval societies in England, I've come across this statement from time to time. Makes good sense to me.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 25, 2019 at 11:06 am
Yes. You can say either:
In the old AdventureWorks2016 database
In ye olde AdventureWorks2016 database
but I don't think they should be mixed 😉
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply