| | |
Code inside CATCH not execute
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
Hy, you guys!
I'm trying to implement in a trigger write by-me error handling with TRY...CATCH, but I'm loosing a lot of time without success.
Here there's a simple code write for test:
I think the code between BEGIN CATCH...END CATCH are not execute, because I find table "Errors" empty.
Anyone can help me?
I'm trying to implement in a trigger write by-me error handling with TRY...CATCH, but I'm loosing a lot of time without success.
Here there's a simple code write for test:
MS SQL Syntax (Toggle Plain Text)
ALTER TRIGGER [dbo].[INS_AlarmsEvents] ON [dbo].[TEST] AFTER INSERT, UPDATE AS BEGIN TRY RAISERROR (N'This is message %s %d.', -- Message text. 10, -- Severity, 1, -- State, N'number', -- First argument. 5); -- Second argument. END TRY BEGIN CATCH INSERT INTO gestioneimpianti.dbo.Errors([ERROR_NUMBER],[ERROR_SEVERITY],[ERROR_STATE],[ERROR_PROCEDURE],[ERROR_LINE],[ERROR_MESSAGE]) VALUES( (SELECT ERROR_NUMBER() AS ErrorNumber), (SELECT ERROR_SEVERITY() AS ErrorSeverity), (SELECT ERROR_STATE() AS ErrorState), (SELECT ERROR_PROCEDURE() AS ErrorProcedure), (SELECT ERROR_LINE() AS ErrorLine), (SELECT ERROR_MESSAGE() AS ErrorMessage) ) END CATCH
Anyone can help me?
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 0
Your code in the TRY don't fire a error into the catch.
If you run this one you will se that it prints out ERROR:
But this dosn't print 'ERROR':
If you run this one you will se that it prints out ERROR:
MS SQL Syntax (Toggle Plain Text)
BEGIN TRY DECLARE @INT AS INT SET @INT=CAST(GetDate() AS VARCHAR(2)) END TRY BEGIN CATCH PRINT 'ERROR' END CATCH
But this dosn't print 'ERROR':
MS SQL Syntax (Toggle Plain Text)
BEGIN TRY RAISERROR (N'This is message %s %d.', -- Message text. 10, -- Severity, 1, -- State, N'number', -- First argument. 5); -- Second argument. END TRY BEGIN CATCH PRINT 'ERROR' END CATCH
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
Why not?
In MSDN there's this explanation of RAISERROR:
By the way, I've try also to generate an error typing...
This is an error! but code in CATCH are not execute.
any idea?
thanks for any reply!
bye
In MSDN there's this explanation of RAISERROR:•
•
•
•
Generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY…CATCH construct.
•
•
•
•
SELECT 1/0
any idea?
thanks for any reply!
bye
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 0
This gode writes ERROR:
If you click into messages insted of resoults you will see the text:
MS SQL Syntax (Toggle Plain Text)
BEGIN TRY SELECT 1/0 END TRY BEGIN CATCH PRINT 'ERROR' END CATCH
If you click into messages insted of resoults you will see the text:
MS SQL Syntax (Toggle Plain Text)
(0 row(s) affected) ERROR
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 0
If you "Include Actual Execution Plan" what does it say when you Insert or Update a record?
You could also try to change:
To:
You could also try to change:
ALTER TRIGGER [dbo].[INS_AlarmsEvents]
ON [dbo].[TEST]
AFTER INSERT, UPDATETo:
ALTER TRIGGER [dbo].[INS_AlarmsEvents]
ON [dbo].[TEST]
FOR INSERT, UPDATE![]() |
Similar Threads
- Is it possible to code inside XML file for an RSS Feed? (RSS, Web Services and SOAP)
- Problems with executeNonQuery and connection (VB.NET)
- Avoid the Page_Load after clicking dropdownlist (ASP.NET)
- Button activate other button (C++)
- Text from txt To Table in MSACCESS (Java)
- while loop (Java)
- Please help me!!! (Java)
- python mysql doesnt work?plz help (Python)
- Getting "DB_E_ERRORSINCOMMAND" even when execute is successful (C)
Other Threads in the MS SQL Forum
- Previous Thread: querry problem
- Next Thread: How to combine multiple rows's column into one column
| Thread Tools | Search this Thread |





