SQL Server´æ´¢¹ý³ÌÖÐraiserrorµÄʹÓÃ
Ò» ϵͳԤ¶¨Òå´íÎó´úÂë
SQL Server ÓÐ3831¸öÔ¤¶¨Òå´íÎó´úÂ룬ÓÉmaster.dbo.sysmessages±íά»¤¡£Ã¿Ò»¸ö´íÎó´úÂ붼ÓÐÏàÓ¦µÄ¼¶±ðºÍÃèÊö¡£
´íÎó¶¨ÒåµÄ¼¶±ð´Ó0µ½25¡£20ÒÔÉϵĴíÎó´ú±íÖØ´ó´íÎó£¬Í¨³£ÒâζןôíÎó»áµ¼Ö´洢½ø³ÌÁ¢¿ÌÖÕÖ¹£¬²¢ÇÒËùÓеĿͻ§Á¬½Ó¶¼ÒªÖØгõʼ»¯¡£
·Ç¹Ø¼üÐÔ´íÎóÖ»ÊǽûÖ¹µôµ±Ç°ÔËÐеijÌÐòÐУ¬²¢¼ÌÐøÖ´ÐС£
ÅжϴíÎóÊÇ·ñ·¢ÉúºÍµÃµ½´íÎóÃèÊöÐÅÏ¢µÄ·½·¨£º
IF @@ERROR = 0
begin
SELECT @ERRORMESSAGETXT = description from master.dbo.sysmessages WHERE error = @@ERROR
end
¶þ raiserrorÓï·¨
msg_id
¶¨ÖÆÏûÏ¢µÄ´íÎó´úÂë¡£. RAISERROR ½ÓÊÜÈκδóÓÚ13000µÄÊý×Ö, µ«ÊǶ¨ÖÆÐÅÏ¢msg_idÒª´óÓÚµÈÓÚ50000¡£
msg_str
¶¨ÖÆÐÅÏ¢µÄÎı¾¡£
severity
¶¨ÖÆÐÅÏ¢µÄ¼¶±ð¡£´Ó 0 to 25£¬ 19-25 ÊÇÖØ´ó´íÎó´úÂë¡£
state
³ÊÏÖµ¼Ö´íÎóµÄ״̬£¬²»ÔÚSQLÄÚ²¿Ê¹Óá£
argument
¶¨ÒåÔÚ´íÎóÐÅÏ¢ÖеĿÉÒÔÌæ»»µÄÖµ¡£
WITH…
ÓÐÈý¸öÑ¡Ïî:
· WITH LOG ¼Í¼´íÎó¡£Ö»ÄÜÓÃÓÚ¼¶±ð¸ßÓÚ19µÄ´íÎó¡£
· WITH NOWAIT ½«´íÎóÁ¢¿Ì·¢Ë͵½¿Í»§¶Ë
· WITH SETERROR sets @@ERROR to the value specified by msg_id, regardless of severity level.
Óï·¨
RAISERROR ( { msg_id ¦ msg_str } { , severity , state }
[ , argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
²ÎÊý
msg_id
´æ´¢ÓÚ sysmessages ±íÖеÄÓû§¶¨ÒåµÄ´íÎóÐÅÏ¢¡£Óû§¶¨Òå´íÎóÐÅÏ¢µÄ´íÎóºÅÓ¦´óÓÚ 50,000¡£ÓÉÌØÊâÏûÏ¢²úÉúµÄ´íÎóÊÇµÚ 50,000 ºÅ¡£
msg_str
ÊÇÒ»ÌõÌØÊâÏûÏ¢£¬Æä¸ñʽÓë C ÓïÑÔÖÐʹÓÃµÄ PRINTF ¸ñʽÑùʽÏàËÆ¡£´Ë´íÎóÐÅÏ¢×î¶à¿É°üº¬ 400 ¸ö×Ö·û¡£Èç¹û¸ÃÐÅÏ¢°üº¬µÄ×Ö·û³¬¹ý 400 ¸ö£¬
ÔòÖ»ÄÜÏÔʾǰ 397 ¸ö²¢½«Ìí¼ÓÒ»¸öÊ¡ÂÔºÅÒÔ±íʾ¸ÃÐÅÏ¢Òѱ»½Ø¶Ï¡£ËùÓÐÌض¨ÏûÏ¢µÄ±ê×¼ÏûÏ¢ ID ÊÇ 14,000¡£
msg_str Ö§³ÖÏÂÃæµÄ¸ñʽ£º
% [[flag] [width] [precision] [{h ¦ l}]] type
¿ÉÔÚ msg_str ÖÐʹÓõIJÎÊý°üÀ¨£º
flag
ÓÃÓÚÈ·¶¨Óû§¶¨ÒåµÄ´íÎóÐÅÏ¢µÄ¼ä¾àºÍ¶ÔÆëµÄ´úÂë¡£
{h | l} type
Óë×Ö·ûÀàÐÍ d¡¢i¡¢o¡¢x¡¢X »ò u Ò»ÆðʹÓã¬ÓÃÓÚ´´½¨ short int (h) »ò long int (l) ÀàÐ͵ÄÖµ¡£
×Ö·ûÀàÐÍ
±íʾ
d »ò I
´ø·ûºÅµÄÕûÊý
o
²»´ø·ûºÅµÄ°Ë½øÖÆÊý
p
Ö¸ÕëÐÍ
s
String
Ïà¹ØÎĵµ£º
ͬ²½Ò»ÏÂÎÒbaiduµÄblog£ºhttp://hi.baidu.com/ncheng/blog/item/bc65f82a1a6a7c335343c11c.html
Sql×¢ÈëÊÇÒ»ÖÖÈëÃż«µÍÆÆ»µ¼«´óµÄ¹¥»÷·½Ê½¡£Èç¹ûsqlÊÇÓÃ×Ö·û´®Æ´½Ó³öÀ´µÄ»°£¬ÄÇô¿Ï¶¨»á±»×¢Èë¹¥»÷£¬Ç°¶Îʱ¼ä»¹´«³öÁËij¹úÍâ´óÐÍÉç½»ÍøÕ¾±»SQL×¢Èë¹¥»÷¡£
Sql×¢Èë¹¥»÷µÄ·½Ê½£¬À´ÕâÀï¿´µÄͬ־ÃÇÓ¦¸ÃºÜÇå³þÁË£¬¾ÍÊÇÔÚÆ´½Ó×Ö·û´ ......
SQL Server ϵͳȫ¾Ö±äÁ¿
@@CONNECTIONS
·µ»Ø×ÔÉÏ´ÎÆô¶¯ÒÔÀ´Á¬½Ó»òÊÔͼÁ¬½ÓµÄ´ÎÊý¡£
@@CURSOR_ROWS
·µ»ØÁ¬½ÓÉÏ×îºó´ò¿ªµÄÓαêÖе±Ç°´æÔڵĺϸñÐеÄÊýÁ¿(·µ»Ø±»´ò¿ªµÄÓαêÖл¹Î´±»¶ÁÈ¡µÄÓÐЧÊý¾ÝÐеÄÐÐÊý)
@@DATEFIRST
·µ»ØÿÖܵÚÒ»ÌìµÄÊý×Ö
@@ERROR
·µ»Ø×îºóÖ´ÐеÄSQL Óï¾äµÄ´íÎó´úÂë¡£
@@FETCH_STATUS
·µ»Ø ......
ÔÚ°²×°Visual Studio+SQL ServerµÄ¿ª·¢»·¾³µÄʱºòÍùÍù»áÓкܶà´íÎó£¬ÆäÖÐ×îÂé·³µÄÒ»Ìõ¾ÍËÀ°²×°Íê³ÉÖ®ºóûÓÐÊý¾Ý¿â¹ÜÀí¹¤¾ß£¬¼´Ã»ÓÐSQL Server 2005 Management Studio¡£³öÏÖÕâÑùµÄÎÊÌâÖ÷ÒªÓÐÁ½¸öÔÒò£¬Ò»ÊÇϵͳÒѾ´æÔÚÆäËû°æ±¾µÄSQLÊý¾Ý¿â»òÕßExpress°æ±¾£¬¶þÊÇ°²×°»·¾³µÄʱºòÏñÎÒÒ»ÑùÏÈ°²×°ÁËVisual Studio 2005/2008£¬È» ......
Ë®»·¾³¼à²âÊý¾Ý´æ´¢½á¹¹ÖÐÓÐÒ»ÖÖģʽ½Ð×ö"Êú±íģʽ"£¬¼´ÔÚ¼à²âÊý¾Ý±íÖУ¬Ä³¸öµãλÔÚij¸öʱ¼äµãÉϸ÷¼à²âÏîÄ¿µÄŨ¶È²â¶¨ÖµÔÚÎïÀí±íÖд洢ÔÚ¶àÌõ¼Ç¼ÖУ¬³ÊÊú×´·Ö²¼¡£¼à²âÊý¾Ý±íÖаüº¬²âµã´úÂë¡¢¼à²âʱ¼ä¡¢¼à²âÏîÄ¿´úÂ롢Ũ¶È²â¶¨ÖµµÈ£¬ËùÓмà²âÏîÄ¿µÄŨ¶È²â¶¨Öµ¶¼´æ´¢ÔÚͬһ¸ö×Ö¶ÎÖУ¬¼ÓÒÔ¼à²âÏîÄ¿´úÂë×÷ΪÇø·Ö×ֶΡ£
"Êú ......