Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kashyap81
Starting Member
9 Posts |
Posted - 2008-05-17 : 09:14:43
|
I feel like there is a better way to do the following but I cannot figure it out:declare @INPUT_PARAMETER as intset @INPUT_PARAMETER = nulldeclare @INPUT_PARAMETER as intset @INPUT_PARAMETER = nullselect * from TableA inner join ((select TableB.ID1, (TableB.Number1*TableC.Number2) as calculated from TableB, TableC where TableB.ID2 = TableC.ID2 and ((@INPUT_PARAMETER is null) OR (TableB.ID2 = @INPUT_PARAMETER))) union (select TableA.ID1, 0 from TableA where (@INPUT_PARAMETER is null) and TableA.ID1 not in (select TableB.ID1 from TableB))) as TableX on TableA.ID1 = TableX.ID1... basically i need to know how i can make an input parameter that will decide whether i should do an inner join or a left joinThank you.KashyapDiaTree.comD.I.P. Pvt Ltd |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-17 : 15:18:00
|
Didnt get what you're trying to do here. Can you explain your requirement please? |
 |
|
kashyap81
Starting Member
9 Posts |
Posted - 2008-05-19 : 09:01:34
|
basically i am wondering if there is way to do this without building a query at runtime. I have tried the following but it does not work.ALTER PROCEDURE [dbo].[Proc1] ( @USELEFT int)ASBEGIN SET NOCOUNT ON; SELECT * FROM TABLEA.ID, TABLEB.NAMEIF (@USELEFT = 0)BEGININNER JOINENDELSE IF (@USELEFT = 1)BEGINLEFT JOINENDTABLEB ON TABLEA.ID = TABLEB.IDENDquote: Originally posted by visakh16 Didnt get what you're trying to do here. Can you explain your requirement please?
DiaTree.comD.I.P. Pvt Ltd |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-05-19 : 10:38:52
|
Always do a LEFT JOIN. Then, in your condition, if you filter out NULL results, that left join becomes an INNER JOIN.For example:create procedure test @useleft bitasselect ....from aleft outer join b on ....where (@useleft = 1 or b.id is not null)Logically, that says "if @useleft is 1, then don't worry about NULL values in table B. Otherwise, do NOT allow null values in Table B -- which is the same as if you had done an INNER JOIN."- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
kashyap81
Starting Member
9 Posts |
Posted - 2008-05-20 : 07:29:30
|
Oh.. Excellent!.. that worked and made my query about 15% faster than it originally was.Thank you jsmith.quote: Originally posted by jsmith8858 Always do a LEFT JOIN. Then, in your condition, if you filter out NULL results, that left join becomes an INNER JOIN.For example:create procedure test @useleft bitasselect ....from aleft outer join b on ....where (@useleft = 1 or b.id is not null)Logically, that says "if @useleft is 1, then don't worry about NULL values in table B. Otherwise, do NOT allow null values in Table B -- which is the same as if you had done an INNER JOIN."- Jeffhttp://weblogs.sqlteam.com/JeffS
DiaTree.comD.I.P. Pvt Ltd |
 |
|
|
|
|
|
|