Error Message In Sql Server Stored Procedure
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more sql server stored procedure return error message about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges sql server 2005 stored procedure error handling Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each sql server stored procedure error handling best practices other. Join them; it only takes a minute: Sign up How to get sql error in stored procedure up vote 5 down vote favorite 3 I'm using SQL Server 2005. I created a stored procedure which works most of
Sql Server Stored Procedure Return Error Value
the time, but I found an instance of where it doesn't do what I want. Currently, the code does something like this if @@error <> 0 begin select @message_error = "There was a database error adding product "+ @product + " to product line end Where @message_error is an output variable. So, I can select @@error and get a number, but all I really want is the SQL error. Something like Hey, I couldn't do this because sql stored procedure try catch there is a fk constraint on this column or whatever. I found this article on msdn http://msdn.microsoft.com/en-us/library/ms178592(v=sql.90).aspx But it only goes over throwing custom exceptions with RAISERROR, I don't want to create my own error message or exception, I just want to know why stuff isn't working. I can execute the stored procedure through Management Studio and see the exact SQL error, but this is tedious trying to match data from the site and manually inserting it that way. How do I get the SQL error text into an output variable? sql sql-server tsql sql-server-2005 stored-procedures share|improve this question edited Nov 30 '12 at 14:53 marc_s 452k938651031 asked Nov 30 '12 at 14:47 Steve G 2,39452347 2 Have you looked at ERROR_MESSAGE msdn.microsoft.com/en-us/library/ms190358.aspx? –Romhein Nov 30 '12 at 14:54 add a comment| 3 Answers 3 active oldest votes up vote 8 down vote accepted Here's part of a stored procedure template I use: /* CREATE PROCEDURE... */ DECLARE @ErrorMessage varchar(2000) ,@ErrorSeverity tinyint ,@ErrorState tinyint /* Additional code */ BEGIN TRY /* Your code here */ END TRY BEGIN CATCH SET @ErrorMessage = ERROR_MESSAGE() SET @ErrorSeverity = ERROR_SEVERITY() SET @ErrorState = ERROR_STATE() RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState) BREAK END CATCH /* Further cleanup code */ Try/Catch blocks can be tricky but are much more thorough than @@error. More importantly, you can use the various error_xxx() functions within them. Here, I store the pr
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and sql stored procedure exit reference Dev centers Retired content Samples We’re sorry. The content you requested
Mysql Stored Procedure Error Handling
has been removed. You’ll be auto redirected in 1 second. SQL Server Microsoft SQL Server Language Reference Transact-SQL
Oracle Stored Procedure Error Handling
Reference (Database Engine) Transact-SQL Reference (Database Engine) RAISERROR RAISERROR RAISERROR Reserved Keywords (Transact-SQL) Transact-SQL Syntax Conventions (Transact-SQL) BACKUP and RESTORE Statements (Transact-SQL) Built-in Functions (Transact-SQL) Collation (Transact-SQL) Control-of-Flow Language (Transact-SQL) http://stackoverflow.com/questions/13647437/how-to-get-sql-error-in-stored-procedure Cursors (Transact-SQL) Data Definition Language (DDL) Statements (Transact-SQL) Data Manipulation Language (DML) Statements (Transact-SQL) Data Types (Transact-SQL) EXECUTE Expressions (Transact-SQL) Language Elements (Transact-SQL) Management Commands Operators (Transact-SQL) Predicates (Transact-SQL) PRINT (Transact-SQL) RAISERROR Security Statements Service Broker Statements SET Statements (Transact-SQL) SQL Server Utilities Statements System Stored Functions (Transact-SQL) System Stored Procedures (Transact-SQL) System Tables (Transact-SQL) System Views (Transact-SQL) Transaction Statements (Transact-SQL) https://msdn.microsoft.com/en-us/library/ms178592.aspx Variables (Transact-SQL) XML Statements (Transact-SQL) TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. RAISERROR (Transact-SQL) Other Versions SQL Server 2012 THIS TOPIC APPLIES TO: SQL Server (starting with 2008)Azure SQL DatabaseAzure SQL Data Warehouse Parallel Data Warehouse 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. New applications should use THROW instead. Transact-SQL Syntax ConventionsSyntax Copy -- Syntax for SQL Server and Azure SQL Database RAISERROR ( { msg_id | msg_str | @local_variable } { ,severity ,state } [ ,argument [ ,...n ] ] ) [ WITH option [ ,...n ] ] Copy -- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse RAISERROR ( { msg_str | @local_variable } { ,severity ,state } [ ,
| 2 Comments | Tags: Stored Procedures The following article introduces the basics of handling errors in stored procedures. http://www.sqlteam.com/article/handling-errors-in-stored-procedures If you are not familiar with the difference between fatal and non-fatal errors, the system function @@ERROR, or how to add a custom error with the system stored procedure sp_addmessage, you should find it interesting. The examples presented here are specific to stored procedures as they are the desired method of interacting with a database. When an error stored procedure is encountered within a stored procedure, the best you can do (assuming it’s a non-fatal error) is halt the sequential processing of the code and either branch to another code segment in the procedure or return processing to the calling application. Notice that the previous sentence is specific to non-fatal errors. There are two type of errors sql server stored in SQL Server: fatal and non-fatal. Fatal errors cause a procedure to abort processing and terminate the connection with the client application. Non-fatal errors do not abort processing a procedure or affect the connection with the client application. When a non-fatal error occurs within a procedure, processing continues on the line of code that follows the one that caused the error. The following example demonstrates how a fatal error affects a procedure. USE tempdb go CREATE PROCEDURE ps_FatalError_SELECT AS SELECT * FROM NonExistentTable PRINT 'Fatal Error' go EXEC ps_FatalError _SELECT --Results-- Server:Msg 208,Level 16,State 1,Procedure ps_FatalError_SELECT,Line 3 Invalid object name 'NonExistentTable'. The SELECT in the procedure references a table that does not exist, which produces a fatal error. The procedure aborts processing immediately after the error and the PRINT statement is not executed. To demonstrate how a non-fatal error is processed, I need to create the following table. USE tempdb go CREATE TABLE NonFatal ( Column1 int IDENTITY, Column2 int NOT NULL ) This example uses a procedure to INSERT a