Re: try catch ok, what about finally?
- From: "Mike C#" <xyz@xxxxxxx>
- Date: Tue, 14 Nov 2006 00:31:23 -0500
"JCollum" <jcollum@xxxxxxxxx> wrote in message
news:1163441509.251607.233440@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
What error are you trying to trap? Is it possible the error you're trying
to catch is being caught before the trigger is fired? Try these samples.
I'm not sure exactly what you're trying to do, but TRY...CATCH doesn't catch
all errors. Post some sample code and expected results, so we can try to
pinpoint the problem.
-- This one raises an error in the trigger, catches the error and inserts
-- a row in another table
CREATE TABLE test1 (i INT)
GO
CREATE TABLE test2 (msg VARCHAR(255) NOT NULL PRIMARY KEY)
GO
CREATE TRIGGER test_trig
ON test1
AFTER INSERT
AS
BEGIN
BEGIN TRY
RAISERROR ('Error', 15, 127)
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO test2(msg)
VALUES ('Message')
END CATCH
END
GO
INSERT INTO test1 (i)
VALUES (1)
GO
SELECT * FROM test2
GO
DROP TABLE test1
GO
DROP TABLE test2
GO
-- This example results in a PK constraint violation, and logs a message in
the catch block
-- The result is two messages in the test2 table
CREATE TABLE test1 (i INT)
GO
CREATE TABLE test2 (msg VARCHAR(255) NOT NULL PRIMARY KEY)
GO
CREATE TRIGGER test_trig
ON test1
AFTER INSERT
AS
BEGIN
BEGIN TRY
INSERT INTO test2(msg)
VALUES ('Message')
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
INSERT INTO test2(msg)
VALUES ('Message2')
END CATCH
END
GO
INSERT INTO test1 (i)
VALUES (1)
GO
INSERT INTO test1 (i)
VALUES (1)
GO
SELECT * FROM test2
GO
DROP TABLE test1
GO
DROP TABLE test2
GO
.
- References:
- try catch ok, what about finally?
- From: JCollum
- Re: try catch ok, what about finally?
- From: Mike C#
- Re: try catch ok, what about finally?
- From: JCollum
- Re: try catch ok, what about finally?
- From: Mike C#
- Re: try catch ok, what about finally?
- From: JCollum
- Re: try catch ok, what about finally?
- From: JCollum
- try catch ok, what about finally?
- Prev by Date: Re: try catch ok, what about finally?
- Next by Date: Re: Managing Databases on Multiple Servers
- Previous by thread: Re: try catch ok, what about finally?
- Next by thread: Sqlconnection failure
- Index(es):
Relevant Pages
|