t-sql question

  • Here is the below SQL:

    While ....

    Begin

    If Exists()

    Begin

    Insert into Table1 ....

    End

    Else --First Else Part

    Print 'Entering into First Else..'

    IF NOT Exists()

    Begin

    Insert into Table2...

    End

    Else --Second Else Part

    Begin

    Print 'Entering into Second Else..'

    Insert into third table ..

    End

    Based on the valid conditions, it enters into the first else part only if the Print statement is present. The same issue with the second else part too.

    Else --First Else Part

    Print 'Entering into First Else..'

    IF NOT Exists()

    Begin

    Insert into Table2...

    End

    If I remove the Print statement from the above SQL it does'nt enter in the Else part at all, even after valid conditions. Please let me know your thoughts. Thanks much!!

  • I'm not sure I actually understand what you are asking or trying to do, but the first issue is that your first Print is not within a BEGIN END under the ELSE so it is the only statement that will be run as part of the first ELSE.

    Do you want your second IF to be executed as part of the first ELSE or after the first IF ELSE is finished?

  • This is what I am trying to do:

    IF order EXISTS in the orders table

    Begin

    Insert that order into DuplicateOrders table

    End

    Else --Not existing in orders table, new order

    Check if the representative who booked the order is in representatives table

    If representative not existing in the representatives table

    Begin

    Insert that order into the ExceptionsOrder table

    End

    Else --that order is booked by a valid representative, so insert that order into Orders table

    Begin

    Insert that order into Orders table

    End

  • Any ideas please!! Thanks.

  • Here's one way to do it:

    [font="Courier New"]IF EXISTS(SELECT * FROM dbo.orders WHERE order_id = @order_id)

       BEGIN

           INSERT INTO DuplicateOrders

               SELECT

                   VALUES

       END

    ELSE

       BEGIN

           IF EXISTS(SELECT * FROM dbo.representatives WHERE representative_id = @representative_id)

               BEGIN

                   INSERT INTO ExceptionsOrder

                       SELECT

                           VALUES

               END

           ELSE

               BEGIN

                   INSERT INTO orders

                       SELECT

                           VALUES

               END

       END[/font]

  • Are you missing a BEGIN and and END? (See below)

    This is what I am trying to do:

    IF order EXISTS in the orders table

    Begin

    Insert that order into DuplicateOrders table

    End

    Else --Not existing in orders table, new order

    BEGIN

    Check if the representative who booked the order is in representatives table

    If representative not existing in the representatives table

    Begin

    Insert that order into the ExceptionsOrder table

    End

    Else --that order is booked by a valid representative, so insert that order into Orders table

    Begin

    Insert that order into Orders table

    End

    END



    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 6 posts - 1 through 5 (of 5 total)

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