Solved

Why the conditions not meet but the SP still execute?

Posted on 2009-04-08
1
196 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 50 total points
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now