Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have an procedure
i am using transaction in it .......

create PROCEDURE MY_TEST_PRO
(
@ID INT
)

AS
BEGIN
BEGIN TRAN


DELETE FROM Employee_Test WHERE Emp_ID=@ID

IF(@ID<10)
BEGIN
ROLLBACK;
END
COMMIT TRAN
END


it created successfully . but when i execute it through sql query ...

EXEC MYTESTPRO 1

it execute query for infinite times and status shows that "executing query " until we not cancel it .
can any buddy tell me how to write this procedure ....?
Posted

All these solutions are stupid. Why not do this :

SQL
create PROCEDURE MY_TEST_PRO
(
@ID INT
)

AS
BEGIN

if (@ID > 10)
    DELETE FROM Employee_Test WHERE Emp_ID=@ID

END


But, if you were trying to write pointless code to understand transactions then, yes, you don't commit after a rollback.
 
Share this answer
 
Comments
GDdixit 10-Jan-14 4:01am    
yes .. the last line u wrote here , is right . i trying to use trnasaction here :)
SQL
create PROCEDURE MY_TEST_PRO
(
@ID INT
)

AS
BEGIN
BEGIN TRAN
DELETE FROM Employee_Test WHERE Emp_ID=@ID
IF(@ID<10)
BEGIN
ROLLBACK;
return --//
END
COMMIT --//

END
 
Share this answer
 
SQL
CREATE PROCEDURE MY_TEST_PRO (@ID INT)
AS
BEGIN
  BEGIN TRAN
    DELETE FROM Employee_Test WHERE Emp_ID=@ID

   IF(@ID < 10)
   BEGIN
     ROLLBACK
 
     RETURN -- !!!
   END

   COMMIT TRAN
END
 
Share this answer
 
Comments
GDdixit 9-Jan-14 6:08am    
nothing happened good on doing as u told here .. u only add rerurn ....
Kornfeld Eliyahu Peter 9-Jan-14 6:12am    
So? I can't understand you!
Adding return after rollback is essential (and I also removed ;, that important too!), otherwise also commit will hit and drop SQL into an unstable state (for this SP at least).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900