Join IF...  
Author Message
larrythediver





PostPosted: Thu Apr 07 10:54:45 CDT 2005 Top

SQL Server Developer >> Join IF...

Hello!

I am using very simple query...

Select * From Table1 Where Active = 1

Now I would like to preform Join if input parameter X is Not Null?

Could someone provide me a sample..? I tried myself, but couldnt get it
working.

Thanks!
James

SQL Server279  
 
 
David





PostPosted: Thu Apr 07 10:54:45 CDT 2005 Top

SQL Server Developer >> Join IF... Typically the purpose of a JOIN is to return columns from more than one
table. Do you mean you want to bring back a different set of columns
depending on input? If so, then use an IF statement. Avoid using SELECT
* in production code because it creates a lot of problems for
maintenance and reliability.


BEGIN
SELECT ...
FROM Table1
WHERE active = 1
END
ELSE
BEGIN
SELECT ...
FROM Table1
JOIN ...
WHERE active = 1
END

Alternatively, if the columns to be returned are the same in each case
you can just add the parameter to the ON clause:

SELECT ...
FROM Table1
JOIN ...


--
David Portas
SQL Server MVP
--