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
Solved

Why the conditions not meet but the SP still execute?

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

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…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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