I am getting Dead Lock exceptions in my C# code when I call my stored procedure to update/insert into my Configuration table.
The SP_Update_Configuration stored procedure will either insert a new record or update an existing record.
The Triggers are setup to keep a history of previous records in a history table. If the Configuration table has an update or an insert, then it should add that record to the Configuration_History table.
I believe the triggers are causing the deadlock? I did not have any problems previous to adding triggers…. Any Ideas?
I am using SQL Server 2012 Express.
Here is an example of my SQL:
CREATE PROCEDURE SP_Update_Configuration
(
--Input variables
)
AS
BEGIN TRANSACTION
DECLARE @RetCode INT
DECLARE @RowCnt INT
--Standard Update Logic
SELECT @RowCnt = @@ROWCOUNT
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
SET @RetCode = 5
RETURN @RetCode
END
IF @RowCnt = 0
BEGIN
--Standard Insert Logic
END
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
SET @RetCode = 5
RETURN @RetCode
END
COMMIT TRANSACTION
GO
create trigger dbo.Configuration_Log_Insert
on dbo.Configuration
for insert
as
set nocount on
insert into Configuration_History
select *
from Configuration
go
exec sp_settriggerorder @triggername = 'Configuration_Log_Insert', @order = 'last', @stmttype = 'insert'
create trigger dbo.Configuration_Log_Update
on dbo.Configuration
for update
as
set nocount on
insert into Configuration_History
select *
from Configuration
go
exec sp_settriggerorder @triggername = 'Configuration_Log_Update', @order = 'last', @stmttype = 'update'
here you have trouble, because
@@ERRORis error code ofYou can do this as:
In triggers you have
but is must be