Solved

Why the conditions not meet but the SP still execute?

Posted on 2009-04-08
1
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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