Article View: cn.bbs.comp.database
Article #570SQL Server Errors Handling
From: Galaxy��VOLUNTEERS��Database
Date: Mon, 09 Feb 2009 08:12
Date: Mon, 09 Feb 2009 08:12
164 lines
10928 bytes
10928 bytes
��ǰ�ı��ϰ��һֱ��̫�ã�������дC��C++��C#������ݿ�ű�һ�㶼�ǵ��Բ���ͨ
��ɣ���������try/catch���쳣���?���ڿ�ʼΪ�����õı��ϰ�ߺ�һ��case����
ʼѧ�����б�Ҫ�����쳣��ʱ�������쳣������롣
SQL Server�ƺ���ǰ�����?�ܲ���ô�����ڿ϶����˺ܴ�Ľ��ҿ϶���û��
���ʸ�ָ�ֻ��š��������ܺû���ѧ�����ǵ�һ����
1. ��
1.1 Error Message
һ��SQL Server�Ĵ�����Ϣ���£�
Server: Msg 547, Level 16, State 1, Procedure error_demo_sp, Line 2
UPDATE statement conflicted with COLUMN FOREIGN KEY constraint 'fk7_acc_cur'.
The conflict occurred in database 'bos_sommar', table 'currencies', column
'curcode'.
The statement has been terminated.
�������¼������֣�
Message number������50000��Ϊ�Զ�����Ϣ��������Ϣ��master..sysmessages���
������sp_addmessage�������Ϣ��
Severity level��0-25��0-10����Ϣ�棬��̴���Ϊ11-16��17-25Ϊ��Դ����Ӳ
�������SQL Server�ڲ�����>=20�Ĵ���ᵼ��������ֹ��
State��0-127��������undocumented�����������Ƕ���OSQL��ISQL�����StateΪ127��
��ֹͣ����ERRORLEVEL����Ϊmessage number���������?װ�ű�ʱ�Ĵ���
Line�����Ϊ0˵����procedure����ʱ���?
�ͻ��˾������ֵĸ�ʽ��
��ݿ�����Ĵ�������������棺T-SQL�����Ӧ�ó��������档
1.2 T-SQL
1.2.1 TRY...CATCH
T-SQL�е�TRY/CATCH�÷���C++��C#�в�࣬������ȥû��Finally������
ע���1����CATCH�鴦�������֮����END CATCH��ĵ�һ�䡣���END CATCH��
���Ǵ洢��̻��������һ���ˣ���ô�ͷ��ص��ô洢��̻�������һ�䡣��T
RY�������г���֮��Ĵ��뱻���ӡ�
ע���2��severity�����ضȣ�>=20��������ݿ�����ر����ӵģ����ܱ�����
ע���3��severity�����ضȣ�<=10����Ϊ�������Ϣ��Ϣ�������ܱ�����
ע���4��Ҫ������λ��ر���εĴ���ʹ��sp_executesql
1.2.2 ERROR ����
T��SQL��Ҫ����쳣��Ϣ����Ҫ��TRY...CATCH�ṹ��Ȼ���ڽṹ��ʹ��ERROR_LINE,
ERROR_MESSAGE, ERROR_NUMBER, ERROR_PROCEDURE, ERROR_SEVERITY �� ERROR_STATE��
���������֪������ʲô�õ��ˡ�ע���1�������TRY...CATCH�ṹ��ʹ�þͻ᷵��NUL
L����ij��case��customer��Ϊʲô�᷵��NULL�����ƾ������ԭ������
ע���2��CATCH���ڲ��ܱ����ü��Σ��������������ã�������Ϣ��һ��
ע���3������Ƕ��CATCH�飬�IJ���쳣���?���IJ����Ϣ��
1.2.3 @@ERROR
��֮ǰ���е�T-SQL���Ĵ���ţ�����ij������һ��Ĭ��Ϊ0������11���ϵ�message
����������SQL Server 2005֮ǰ�İ汾�����Ҫ�����ϸ������Ϣ��Щ�鷳�������ӵ
��sysadminȨ�Ļ����ǿ���ʵ�ֵģ�DBCC OUTPUTBUFFER����һ�㲻������ֹSQL
Server���?
���бȽ���Ҫ��11-16����Ŵ�����Ϣ��
11 �C Specified Database Object Not Found
12 �C Unused
13 �C User Transaction Syntax Error
14 �C Insufficient Permission
15 �C Syntax Error in SQL Statements
16 �C Miscellaneous User Error
ע���1��@@ERROR������֮����Ĵ���û��ϵ��
ע���2�������TRY����?@@ERRORҪΪCATCH�ĵ�һ�䣻�����TRY����?@@ERRO
RҪ�����ų������һ�䡣
ע���3��@@ERROR���뱻���浽��ʱ�����в��ܶ�����в���
����
CREATE TABLE notnull(a int NOT NULL)
DECLARE @value int
INSERT notnull VALUES (@value)
IF @@error <> 0
PRINT '@@error is ' + ltrim(str(@@error)) + '.'
--�᷵��@@error is 0.
��ȷ��
CREATE TABLE notnull(a int NOT NULL)
DECLARE @err int,
@value int
INSERT notnull VALUES (@value)
SELECT @err = @@error
IF @err <> 0
PRINT '@err is ' + ltrim(str(@err)) + '.'
--�᷵��@err is 515.
ע���4��������صı�����@@rowcount������Ӱ����У���@@trancount������������
1.2.4 Uncommittable Transactions�������ύ������
TRY...CATCH��������ܻ�����ύ����ʹ��DDL��SET XACT_ABORT ����Ϊ ON ʱ
��������״̬��
�����漰������XACT_STATE ������CATCH���в�������״̬��XACT_STATE�����ַ���ֵ
��1Ϊ��������0Ϊû�л���û�����-1Ϊ�л�������ڴ�������
������Ҫ��ROLLBACK TRANSACTION�ع���
1.2.5 Stored Procedure����ֵ
���û�з��������ô����ֵΪ0�����������ܷ���-1��0��
1.2.6 SET XACT_ABORT
SET XACT_ABORT OFF��ӦStatement-termination
SET XACT_ABORT ON��ӦBatch-abortion
XACT_ABORTӦ����execution action aborted����д��
1.3 Handling Errors and Messages in Applications
���ODBC��ADO��ADO.NET��SqlClient��OleDb���?��Ҳ��ͬ��
2. �쳣����ı���
��Բ�ͬ����SQL Server���ܵ�Ӧ�Է���Ҳ��һ��
���ܲ�ȡ���ж���
1. �����ֹ��Statement-termination��������open transaction��Ҫ�ֶ�ROLLBACK
TRANSACTION����Ȼ֮���INSERT/UPDATE/DELETE�����Զ��ع���
2. ��Χ��ֹ��Scope-abortion����A��B��Ϊstored procedure��A����B��B���������
ֹ��A����B֮��Ĵ����ճ�ִ�С�ͨ�����ڱ������
3. Batch��ֹ��Batch-abortion�����ͻ����ύ��SQL Server�����һ����ֹ������o
pen transaction�ع���
4. ������ֹ��Connection-termination����open transaction�ع���@@ERROR���ܻ�ȡ
ֵ��ͨ�������ط������˴�����һ�����⣺sp_OAxxxxx�����Զ���stored
procedure��OLE objects��Ӧ���Ǻ�DMO�йأ���RAISERROR>=20�Ļ�Ҳ����������
�ж��������ֹ����batch��ֹ��Щ���ѣ�����������������
���µ�����������ᣨ��û�йٷ�˵��������:
1. ����ع�������ǰbatch����ִ��
2. batch��ֹ�������ع���
��һ����Ҫע����ǿͻ��˵���ȡ��batch-cancellation�����ͻ��˿�����ʱҪ��SQL
Server��ִֹ��batch��SQL Server���ܻ�������Ӧ��Ҳ���ܻ��ӳ�һ����Ӧ���Ҳ���
�ع�����INSERT/UPDATE/DELETE/Trigger�л�ع��������ij����ǿͻ���timeout
��ODBC, OLE DB, ADO �� ADO.Net ����30���Ĭ��timeout���á�����ͨ������
CommandTimeout������������⡣
SQL Server��ξ�����ȡʲô�ж�����������SET XACT_ABORT OFF�������ijλ�ͻ���
�ؼ��������ڣ��Դ�SQL Server 2005��˵�ڴ��������������ȡ���ˣ���������������
�õ�˵����������Trigger Context���Ǹ��ͻ�������ƫƫ���Ǵ������е�SET
XACT_ABORT OFF��������
����Trigger Context�����еĴ�����ֹbatch��������ڵ㿪ʼ�ع���������RAISE
RROR��error 266�����������@@trancountΪ0������Trigger Context����Ϊ1��ʱҲ��
��ֹ������Trigger�ڴ������ģ��Ӧ��Ϊ��
IF EXISTS (SELECT *
FROM inserted i
JOIN abainstallhistory inh ON i.inhid = inh.inhid
WHERE inh.ss_label <> i.ss_label
OR inh.ss_label IS NULL AND i.ss_label IS NOT NULL
OR inh.ss_label IS NOT NULL AND i.ss_label IS NULL)
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Values on ss_label does not match abainstallhistory.', 16, 1)
RETURN
END
�����:
SQL Server BOL - Handling Database Engine Errors
http://www.sommarskog.se/error-handling-I.html
http://www.sommarskog.se/error-handling-II.html
http://rusanu.com/2007/10/31/error-handling-in-service-broker-procedures/
http://www.windows-tech.info/15/397cd9af617c7a4e.php
--
Welcome to Database Board
��ӭ������ݿ��
http://bbs.sjtu.edu.cn/bbsdoc?board=database
[m[1;37m�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 207.46.55.27][m
Technical Details
Message-ID:
<4eNUck$ayh@bbs.sjtu.edu.cn>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!news.shec6.edu.cn!SJTUShow Full Headers
Complete Article Headers
X-Google-Thread: 101948,aaf2db6adb2e3a2b,start X-Google-Attributes: gid101948,domainid0,public,usenet X-Google-Language: Chinese,GB Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!news.shec6.edu.cn!SJTU From: Dem.bbs@bbs.sjtu.edu.cn (Galaxy��VOLUNTEERS��Database) Newsgroups: cn.bbs.comp.database Subject: SQL Server Errors Handling Date: 09 Feb 2009 08:12:06 GMT Organization: ��ˮ˼Դ Message-ID: <4eNUck$ayh@bbs.sjtu.edu.cn> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: database/M.1234167118.A