Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help with Stored Procedure
Message
From
02/02/2011 12:14:36
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01498262
Message ID:
01498339
Views:
33
An Aha moment. Thank you
Tim

>The statement you posted is valid. If you fill more comfortable with it than use it.
>
>Now back to EXISTS subquery. It's so called 'correlated query'. It means that one or more colums of the subquery match columns from the main query.
>
>In plain language the query reads: Return all records from Household table WHERE ApplicantID = @PersonId OR any record in HouseholdMembers table with corresponded HouseholdID has PersonId = @PersonId.
>
>
>SELECT *
>  FROM Household 
>  WHERE ApplicantID = @PersonId  
>    OR EXISTS (SELECT 1 FROM HouseholdMembers WHERE HouseholdID = Household.HouseholdID AND PersonId =  @PersonId)
>
>
>>
>>The puzzling part for me was how the primary Select statement gets a match to the subquery. While this probably isn't valid SQL, I would have expected something like this where we specify we are looking for HouseholdID in the subquery to match the HouseholdID in the main query.
>>
>>SELECT *
>>  FROM Household 
>>  WHERE ApplicantID = @PersonId  
>>    OR HouseholdID = (SELECT HouseholdID FROM HouseholdMembers WHERE PersonId =  @PersonId)
>>
>>In that case where trying to specifically select HouseholdID from the subquery.
>>
>>I guess it is this part of the subquery that does that magic. WHERE HouseholdID = Household.HouseholdID AND PersonId = @PersonId
>>
>>Basically it works so I am happy, but just trying to figure out so I understand.
>>Thanks a bunch, I think I am making sense of it now.
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform