Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why the conditions not meet but the SP still execute?

Posted on 2009-04-08
1
Medium Priority
?
204 Views
Last Modified: 2012-05-06
Hi

I need to compare 2 tables' 2 values, if they are equal, then change the status of 2 tables.

However, even they are not equal, the update command still run.

How can I fix it?

I use MSSQL
ALTER PROCEDURE [dbo].[SP_Complete_Transaction] 
@id int
AS
BEGIN 
BEGIN TRAN Complete_Transaction
	SET NOCOUNT ON;
If (
(select sum(amount) as amount from subtransaction where transactionsid=@id and status=2) = 
(select amount from transactions where id=@id and status=2) 
and 
(select sum(payoutamount) as payoutamount from subtransaction where transactionsid=@id and status=2) =
(select countervalue from transactions where id=@id and status=2)
)
 
update transactions set status = 3 where id =@id
update subtransaction set status = 3 where transactionsid =@id
 
ELSE
 
select id from transactions where id=@id
 
IF @@ERROR <> 0
	ROLLBACK TRAN Complete_Transaction
ELSE
	COMMIT TRAN Complete_Transaction
END

Open in new window

0
Comment
Question by:techques
1 Comment
 
LVL 11

Accepted Solution

by:
aaronakin earned 150 total points
ID: 24099371
You need to wrap it in a BEGIN and END if you have more than one line in the IF and ELSE clauses.

IF ...
BEGIN
update transactions set status = 3 where id =@id
update subtransaction set status = 3 where transactionsid =@id
END
ELSE
BEGIN
...
END
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question