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
Ïà¹ØÎĵµ£º
SQLµÄ²Ù×÷£º
E.g.
judging-±íÃû
userid, name, password-±íÖеĹؼü×Ö
Ìí¼Ó£º
string sql = "insert into judging (userid,name,password) values('" + userid.Text.Trim() + "','" + name.Text.Trim() + "','" + password.Text.Trim() + "')";
ɾ³ý£º
string sql = "delete from judging where userid=" + ......
ʹÓà ESCAPE ¹Ø¼ü×Ö¶¨ÒåתÒå·û¡£ ÔÚģʽÖУ¬µ±×ªÒå·ûÖÃÓÚͨÅä·û֮ǰʱ£¬¸ÃͨÅä·û¾Í½âÊÍΪÆÕͨ×Ö·û¡£ÀýÈ磬ҪËÑË÷ÔÚÈÎÒâλÖðüº¬×Ö·û´® 5% µÄ×Ö·û´®£¬ÇëʹÓ㺠WHERE ColumnA LIKE '%5/%%' ESCAPE '/'
2.ESCAPE 'escape_character' ÔÊÐíÔÚ×Ö·û´®ÖÐËÑË÷ͨÅä·û¶ø²»Êǽ«Æä×÷ΪͨÅä·ûʹÓᣠescape_character ÊÇ·ÅÔÚͨÅä·ûÇ°±í ......
SQL ServertimestampÊý¾ÝÀàÐÍÓëʱ¼äºÍÈÕÆÚÎ޹ء£SQL ServertimestampÊǶþ½øÖÆÊý×Ö£¬Ëü±íÃ÷Êý¾Ý¿âÖÐÊý¾ÝÐ޸ķ¢ÉúµÄÏà¶Ô˳Ðò¡£ÊµÏÖtimestampÊý¾ÝÀàÐÍ×î³õÊÇΪÁËÖ§³Ö SQL Server »Ö¸´Ëã·¨¡£Ã¿´ÎÐÞ¸Äҳʱ£¬¶¼»áʹÓõ±Ç°µÄ @@DBTS Öµ¶ÔÆä×öÒ»´Î±ê¼Ç£¬È»ºó @@DBTS ¼Ó1¡£ÕâÑù×ö×ãÒÔ°ïÖú»Ö¸´¹ý³ÌÈ·¶¨Ò³Ð޸ĵÄÏà¶Ô´ÎÐò£¬µ«ÊÇtimest ......
Æäʵɾ³ýÊý¾Ý¿âÖÐÊý¾ÝµÄ·½·¨²¢²»¸´ÔÓ£¬ÎªÊ²Ã´ÎÒ»¹Òª¶à´ËÒ»¾ÙÄØ£¬Ò»ÊÇÎÒÕâÀï½éÉܵÄÊÇɾ³ýÊý¾Ý¿âµÄËùÓÐÊý¾Ý£¬ÒòΪÊý¾ÝÖ®¼ä¿ÉÄÜÐγÉÏ໥ԼÊø¹Øϵ£¬É¾³ý²Ù×÷¿ÉÄÜÏÝÈëËÀÑ»·£¬¶þÊÇÕâÀïʹÓÃÁË΢ÈíδÕýʽ¹«¿ªµÄsp_MSForEachTable´æ´¢¹ý³Ì¡£
Ò²ÐíºÜ¶à¶ÁÕßÅóÓѶ¼¾Àú¹ýÕâÑùµÄÊÂÇ飺ҪÔÚ¿ª·¢Êý¾Ý¿â»ù´¡ÉÏÇåÀíÒ»¸ö¿Õ¿â£¬µ«ÓÉÓÚ¶ÔÊý¾Ý¿ ......