Solved

Why the conditions not meet but the SP still execute?

Posted on 2009-04-08
1
197 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

24 Experts available now in Live!

Get 1:1 Help Now