tsql - T-SQL - How to write a conditional join -


I have a stored procedure with several parameters I would like to write in my query so that she can join some tables, but only If a specific parameter is a value. Take the following example: I have an individual table with an address table that contains the person's address and a group table that holds the individual group. Both men are one of the many relationships with the table. My stored procedure has an @ AddressID parameter and @GroupID parameter.

The query always returns a field from the person table, if there is no parameter value then the query should return all records from the person table. If the @ Address ID parameter is supplied then it should only return the records which will match the address table and ignore the group table. If the @GroupID parameter is supplied, then it should return only those records which have a matching record in the group table and ignore the address table. If both parameters are supplied, then only those records that match in both tables should be shown. Understandable

Is there an easy way of doing what I am missing?

Thank you, blank

If I understand correctly, then it seems That your joining status will be equal to ON (but not @addressID) and (alias.column = @AddressID)) and so on to join the group

I sometimes add this condition.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -