You must Sign In to post a response.
  • Category: SQL Server

    Execure join on certain condition only

    In my query i want inner join of on 'table3' to be executed based on 'statecode' and 'type' condition. To achieve that the below query1 is correct? or i need use IF condition as mentioned in Query2

    Query1:
    select * from table1 t1
    inner join table2 t2 on t1.id=t2.id
    inner join table3 on t2.id=t3.id and statecod='A' and type=1

    Query2:
    if(@statecode='A' and type=1)
    BEGIN
    select * from table1 t1
    inner join table2 t2 on t1.id=t2.id
    inner join table3 on t2.id=t3.id
    END
    ELSE
    BEGIN
    select * from table1 t1
    inner join table2 t2 on t1.id=t2.id
    END
  • #769489
    yes, Query 1 is correct....
    It will joins two tables table2 and table3 and display the records matching the Id's for further validating with statecode and type.


  • Sign In to post your comments