JOIN if exists

  • Hi,

    I have standard SQL query for clients, but sometimes a client does not have a specific table.

    How do I deal with that? (creating the table is not an option)

    SELECT

    A.Col1

    ,A.Col2

    ,B.Col3

    --> Col 50

    FROM

    TableA A Join

    TableB B ON A.ColA = B.ColA

    Suppose in some cases TabelB does not exist. How do I make sure the rest of the query runs okay and does not result in an error?

    Thanks...

  • ArnolddG (6/9/2015)


    Hi,

    I have standard SQL query for clients, but sometimes a client does not have a specific table.

    How do I deal with that? (creating the table is not an option)

    SELECT

    A.Col1

    ,A.Col2

    ,B.Col3

    --> Col 50

    FROM

    TableA A Join

    TableB B ON A.ColA = B.ColA

    Suppose in some cases TabelB does not exist. How do I make sure the rest of the query runs okay and does not result in an error?

    Thanks...

    You could try something like:

    IF OBJECT_ID (N'dbo.TableB', N'U') IS NOT NULL

    BEGIN

    SELECT

    A.Col1

    ,A.Col2

    ,B.Col3

    --> Col 50

    FROM

    TableA A Join

    TableB B ON A.ColA = B.ColA

    END

    ELSE

    BEGIN

    SELECT

    A.Col1

    ,A.Col2

    --> Col 50

    FROM

    TableA A

    END

    Check if TableB exists. If it does, run the first query. If it doesn't, run the second query.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Thanks for your help Alvin,

    That works okay.

    One more thing. My query is about 140 lines long.

    Is there a way to do the If Then on one select column only and/or on one join only?

    If not this will double the length of my query.

    Thanks again.

  • ArnolddG (6/9/2015)


    Thanks for your help Alvin,

    That works okay.

    One more thing. My query is about 140 lines long.

    Is there a way to do the If Then on one select column only and/or on one join only?

    If not this will double the length of my query.

    Thanks again.

    Nope, not without using Dynamic SQL which would probably be overkill in this situation. 140 lines isn't that long, I regularly deal with queries/stored procedures with 1000's of lines.

  • Jack Corbett (6/9/2015)


    ArnolddG (6/9/2015)


    Thanks for your help Alvin,

    That works okay.

    One more thing. My query is about 140 lines long.

    Is there a way to do the If Then on one select column only and/or on one join only?

    If not this will double the length of my query.

    Thanks again.

    Nope, not without using Dynamic SQL which would probably be overkill in this situation. 140 lines isn't that long, I regularly deal with queries/stored procedures with 1000's of lines.

    I agree.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply