-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/trblsql/tr_syserrors1_6m5z.asp -- And btw, the latest update (July 2006) of SQL Server 2005 Books Online contains the error message topics too. See below links: -- http://msdn2.microsoft.com/en-us/library/aa337472.aspx -- http://msdn2.microsoft.com/en-us/library/ms365262.aspx -- http://msdn2.microsoft.com/en-us/library/ms165761.aspx ---- http://www.sqlservercentral.com/articles/News/exceptionhandlinginsqlserver2005/2237/ ---- Using TRY...CATCH in Transact-SQL http://msdn.microsoft.com/en-us/library/ms179296.aspx ---- 00) sys.messages pre @@ERROR ---- 01) RAISERROR <--> PRINT: ---- a) Old 2000: @@ERROR, @@ROWCOUNT; ---- b1) Novy sposob: TRY - CATCH ---- b2) S uzivatelskou procedurou ---- 00) sys.messages pre @@ERROR select * from sys.messages where message_id = 547 ---- 01) RAISERROR <--> PRINT: --severity: 0-10, 11-25: -- The severity level associated with the error. The valid values are 0–25. -- Severity levels 0–18 can be used by any user, but 19–25 are only available to members of the fixed-server role sysadmin. -- When levels 19–25 are used, the WITH LOG option is required. --state: 0-127 -- A value that indicates the invocation state of the error. The valid values are 0–127. -- This value is not used by SQL Server. RAISERROR('Haha', 10, 1) RAISERROR('Haha', 11, 1) ---- a)---- 00) Old 2000: ---- @@ERROR, ---- @@ROWCOUNT; USE Poliklinika GO DECLARE @ErrorVar INT; DECLARE @RowCountVar INT; DELETE FROM Lekari WHERE idL = 1; -- ULOZ!!! SELECT @ErrorVar = @@ERROR, @RowCountVar = @@ROWCOUNT; IF (@ErrorVar <> 0) BEGIN PRINT N' - Cislo chyby = ' + CAST(@ErrorVar AS NVARCHAR(8)); PRINT N' - Pocet vymazanych riadkov = ' + CAST(@RowCountVar AS NVARCHAR(8)); END GO ---- b1) Novy sposob: TRY - CATCH USE Poliklinika GO BEGIN TRY DELETE FROM Lekari WHERE idL = 1; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; RAISERROR ('Chyba ...', 11, 1) END CATCH ---- b2) S uzivatelskou procedurou USE tempDB; GO IF OBJECT_ID ( 'Chyba', 'P' ) IS NOT NULL DROP PROCEDURE Chyba; GO CREATE PROCEDURE Chyba AS SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; GO BEGIN TRY SELECT GETDATE() SELECT 1/0 END TRY BEGIN CATCH EXEC Chyba END CATCH; ------------------------------ ------ -- Error numbers for user-defined error messages should be greater than 50000. -- When msg_id is not specified, RAISERROR raises an error message with an error number of 50000. -- Is a user-defined error message number stored in the sys.messages catalog view using sp_addmessage. EXEC sp_addmessage @msgnum = 60000, @severity = 16, @msgtext = N'The item named % s already exists in %s.', @lang = 'us_english'; RAISERROR (60000, -- Message id. 10, -- Severity, 1, -- State, N' C H Y B A ... '); -- This statement will drop the message. EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'; GO ------------ sp_addmessage @msgnum = 50005, @severity = 10, @msgtext = N'<<%7.3s>>'; GO RAISERROR (50005, -- Message id. 10, -- Severity, 1, -- State, N'abcde'); -- First argument supplies the string. -- The message text returned is: << abc>>. GO sp_dropmessage @msgnum = 50005; GO --DECLARE @StringVariable NVARCHAR(50); --SET @StringVariable = N'<<%7.3s>>'; -- --RAISERROR (@StringVariable, -- Message text. -- 10, -- Severity, -- 1, -- State, -- N'abcde'); -- First argument supplies the string. ---- The message text returned is: << abc>>. --GO