August 27, 2008 at 3:08 pm
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!!
August 27, 2008 at 3:11 pm
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?
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 27, 2008 at 8:20 pm
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
August 28, 2008 at 7:29 am
Any ideas please!! Thanks.
August 28, 2008 at 7:35 am
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]
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 28, 2008 at 7:38 am
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
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