Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: John Couch <JohnCouch@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 6 Aug 2009 05:25:01 -0700
Um, I think I see part of my issue just from your example. If I run my tests
using a stored proc, the error is logged fine, but if I use enterprise
manager, and hand enter data, the transaction is aborted in the trigger and
no error is logged.
Thanks for the example. I hope you hadthat code lying around and didn't
create just for me. :)
"Erland Sommarskog" wrote:
John Couch (JohnCouch@xxxxxxxxxxxxxxxxxxxxxxxxx) writes:.
Well, I have tried about everything i can think of, including the CLR
and a loopback call to write to a log table. Nothing works. Even
shutting off xact_abort. I feel like I should resign myself to not
catching errors in SQL. I did go to your links and rated the posts to
very important (i think that was the highest), and hopefully someday
they will fix the issue. I appreciate all your feedback Erland. Not sure
where to go from here, but i'm sure ill figure out something.
If "nothing works", you are probably doing something you shouldn't. Here
is a simple working example. No loopbacks, no XACT_ABORT ON.
CREATE TABLE logtable (id int IDENTITY,
errno int NOT NULL,
errmsg nvarchar(2048),
errproc sysname,
errtime datetime,
CONSTRAINT pk_logtable PRIMARY KEY (id))
go
CREATE PROCEDURE error_handler_sp @procid int AS
DECLARE @errmsg nvarchar(2048),
@severity tinyint,
@state tinyint,
@errno int,
@proc sysname,
@lineno int
SELECT @errmsg = error_message(), @severity = error_severity(), -- 10
@state = error_state(), @errno = error_number(),
@proc = error_procedure(), @lineno = error_line()
IF @errmsg NOT LIKE '***%' -- 11
BEGIN
SELECT @errmsg = '*** ' + coalesce(quotename(@proc), '<dynamic SQL>') +
', ' + ltrim(str(@lineno)) + '. Errno ' +
ltrim(str(@errno)) + ': ' + @errmsg
END
INSERT logtable(errno, errmsg, errproc, errtime)
VALUES(@errno, @errmsg, object_name(@procid), getdate())
RAISERROR(@errmsg, @severity, @state)
go
CREATE TABLE testtable (somekey int NOT NULL,
somedata varchar(24) NOT NULL,
CONSTRAINT pk_testtable PRIMARY KEY(somekey)
)
go
CREATE TRIGGER testtable_tri ON testtable INSTEAD OF INSERT AS
BEGIN TRY
IF EXISTS (SELECT * FROM inserted WHERE somekey % 5 = 0)
RAISERROR('The key must not be divisible by 5', 16, 1)
IF EXISTS (SELECT * FROM inserted WHERE convert(int, somedata) > 100)
RAISERROR('somedata must not be > 100', 16, 1)
INSERT testtable(somekey, somedata)
SELECT somekey, somedata FROM inserted
END TRY
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTION
EXEC error_handler_sp @@procid
END CATCH
go
CREATE PROCEDURE insert_data @key int, @data varchar(29) AS
BEGIN TRY
INSERT testtable(somekey, somedata)
VALUES (@key, @data)
END TRY
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTION
EXEC error_handler_sp @@procid
END CATCH
go
EXEC insert_data 9, '12'
EXEC insert_data 20, 'lullull'
EXEC insert_data 21, 'ubbabubba'
go
SELECT * FROM logtable ORDER BY id
go
DROP PROCEDURE insert_data, error_handler_sp
DROP TABLE logtable, testtable
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
- Follow-Ups:
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: Erland Sommarskog
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- References:
- Transaction Abort in a trigger SQL 2008, writing to event log or t
- From: John Couch
- Re: Transaction Abort in a trigger SQL 2008, writing to event log or t
- From: Erland Sommarskog
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: John Couch
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: Erland Sommarskog
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: John Couch
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: Erland Sommarskog
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: John Couch
- Re: Transaction Abort in a trigger SQL 2008, writing to event log
- From: Erland Sommarskog
- Transaction Abort in a trigger SQL 2008, writing to event log or t
- Prev by Date: Re: I'm wondering about have two connections, one inside the other.
- Next by Date: Re: Transaction Abort in a trigger SQL 2008, writing to event log
- Previous by thread: Re: Transaction Abort in a trigger SQL 2008, writing to event log
- Next by thread: Re: Transaction Abort in a trigger SQL 2008, writing to event log
- Index(es):
Relevant Pages
|
Loading