Multiple IF EXISTS blocks

  • How do I write multiple IF statements but only one BEGIN block? In other words, I would love to have an OR statement between the IF statements but I know that is not possible. If someone can shed some light on what I am doing wrong.

    IF EXISTS <CODE>

    IF EXISTS <CODE>

    IF EXISTS <CODE>

    BEGIN

    <CODE>

    END

    ELSE

    <CODE>

  • SQLSeTTeR (11/8/2012)


    How do I write multiple IF statements but only one BEGIN block? In other words, I would love to have an OR statement between the IF statements but I know that is not possible. If someone can shed some light on what I am doing wrong.

    IF EXISTS <CODE>

    IF EXISTS <CODE>

    IF EXISTS <CODE>

    BEGIN

    <CODE>

    END

    ELSE

    <CODE>

    Not sure I understand? This should work fine:

    IF EXISTS (SELECT 1 FROM table1 WHERE id = @a)

    OR EXISTS (SELECT 1 FROM table2 WHERE id = @a)

    OR EXISTS (SELECT 1 FROM table3 WHERE id = @a)

    BEGIN

    -- Some Code

    END

    What trouble are you running into?


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • That is what I'm looking for. Not thinking. Thank you very much.

  • That did it, Thanks again.

  • SQLSeTTeR (11/8/2012)


    That is what I'm looking for. Not thinking. Thank you very much.

    Heheh, I've had those days. 😀 Glad I could help.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

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

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