Hi friends,
I have a procedure with transaction which remove clerk from clerk table and I've written a delete trigger for this table which it has transaction.
When I want to delete a clk_Id from clerk table, transactions have interference together.
Can you help me?
ALTER Procedure [dbo].[Clerk_Delete]
@clk_Idint
As
Begin
Begin Try
Begin Tran a
Delete Clerk
Where clk_Id = @clk_Id
Commit Tran a;
End Try
Begin Catch
Rollback Tran a;
End Catch
End
Alter TRIGGER Clerk_Delete1
ON Clerk
AFTER Delete
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
begin tran a
SET NOCOUNT ON;
Declare @count int;
Select @count = Count (*) From deleted
If @count >1
Begin
Rollback tran a;
End
else
Begin
Print 'ok';
End
END
If I want to remove 2 clk_Id at the moment,
like this procedure:
ALTER Procedure [dbo].[Clerk_Delete]
@clk_Idint,
@clk_Id1 int
As
Begin
Begin Try
Begin Tran a
Delete Clerk
Where clk_Id = @clk_Id or clk_Id = @clk_Id1
Commit Tran a;
End Try
Begin Catch
Rollback Tran a;
End Catch
End
I enforce with an error.
I don't know, What should i do?
The error is:
(0 row(s) affected)
Msg 3903, Level 16, State 1, Procedure Clerk_Delete, Line 15
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
10 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Riyaz AkhtarPosted Aug 7, 2013, 10:13 AM
Check below links:
triggers:
http://www.codeproject.com/Articles/25600/Triggers-Sql-Server
Stored Procedure
http://www.codeproject.com/Articles/126898/Sql-Server-How-to-write-a-Stored-procedure-in-Sql
Views:
http://www.w3schools.com/sql/sql_view.asp
elham deljooeiPosted Aug 7, 2013, 6:52 AM
Thanks for your answer.
I don't distinguish, When i have to use Trigger in my project.
I'm confused, When I have to use Store Procedure or Trigger or View?
Please help me with an example. Thanks a million friends.
Regards,
Elham
Riyaz AkhtarPosted Aug 7, 2013, 6:37 AM
you are doing rollback in your trigger if more than 1 record is deleted.
if you see above code it will delete the record if record is only one, hence you don't have to rollback.
hope it answered your query..
elham deljooeiPosted Aug 7, 2013, 6:20 AM
Could you tell me a little more?
Thanks,
Elham
Iftikar HussainPosted Aug 7, 2013, 6:08 AM
Regards,
Iftikar
Riyaz AkhtarPosted Aug 7, 2013, 6:06 AM
you want to delete if u have only one record, then check the count before deleting,
see below code:
elham deljooeiPosted Aug 7, 2013, 5:33 AM
Thanks,
Elham
Iftikar HussainPosted Aug 7, 2013, 5:25 AM
use @@ROWCOUNT to get number of rows affected
Regards,
Iftikar
elham deljooeiPosted Aug 7, 2013, 4:56 AM
First Thanks for your answer.
I want to say If I want to remove 2 or more clk_Id from Clerk table at the moment, rollback all act.
I meant to say don't allow to remove more than one clk_Id at the momunt.
Regards,
Elham
Iftikar HussainPosted Aug 7, 2013, 4:48 AM
Can you please explain why you have declared DELETE trigger with TRANSACTION?
Regards,
Iftikar