站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter 20. Stored Procedures and Functions - MySQL 5.1参考手册中文版

绗20绔狅細瀛樺偍绋嬪簭鍜屽嚱鏁

鐩綍

20.1. 瀛樺偍绋嬪簭鍜屾巿鏉冭〃

20.2. 瀛樺偍绋嬪簭鐨勮娉

20.2.1. CREATE PROCEDURE鍜孋REATE FUNCTION

20.2.2. ALTER PROCEDURE鍜孉LTER FUNCTION

20.2.3. DROP PROCEDURE鍜屽拰DROP FUNCTION

20.2.4. SHOW CREATE PROCEDURE鍜孲HOW CREATE FUNCTION

20.2.5. SHOW PROCEDURE STATUS鍜孲HOW FUNCTION STATUS

20.2.6. CALL璇彞

20.2.7. BEGIN ... END澶嶅悎璇彞

20.2.8. DECLARE璇彞

20.2.9. 瀛樺偍绋嬪簭涓殑鍙橀噺

20.2.10. 鏉′欢鍜屽鐞嗙▼搴

20.2.11. 鍏夋爣

20.2.12. 娴佺▼鎺у埗鏋勯

20.3. 瀛樺偍绋嬪簭銆佸嚱鏁般佽Е鍙戠▼搴忓拰澶嶅埗锛氬父瑙侀棶棰

20.4. 瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘

MySQL 5.1鐗堟敮鎸佸瓨鍌ㄧ▼搴忓拰鍑芥暟銆備竴涓瓨鍌ㄧ▼搴忔槸鍙互琚瓨鍌ㄥ湪鏈嶅姟鍣ㄤ腑鐨勪竴濂SQL璇彞銆備竴鏃﹀畠琚瓨鍌ㄤ簡锛屽鎴风涓嶉渶瑕佸啀閲嶆柊鍙戝竷鍗曠嫭鐨勮鍙ワ紝鑰屾槸鍙互寮曠敤瀛樺偍绋嬪簭鏉ユ浛浠

涓嬮潰涓浜涙儏鍐典笅瀛樺偍绋嬪簭灏ゅ叾鏈夌敤锛

·         褰撶敤涓嶅悓璇█缂栧啓澶氬鎴峰簲鐢ㄧ▼搴忥紝鎴栧瀹㈡埛搴旂敤绋嬪簭鍦ㄤ笉鍚屽钩鍙颁笂杩愯涓旈渶瑕佹墽琛岀浉鍚岀殑鏁版嵁搴撴搷浣滀箣鏃躲

·         瀹夊叏鏋佷负閲嶈涔嬫椂銆傛瘮濡傦紝閾惰瀵规墍鏈夋櫘閫氭搷浣滀娇鐢ㄥ瓨鍌ㄧ▼搴忋傝繖鎻愪緵涓涓潥鍥鸿屽畨鍏ㄧ殑鐜锛岀▼搴忓彲浠ョ‘淇濇瘡涓涓搷浣滈兘琚Ε鍠勮鍏ユ棩蹇椼傚湪杩欐牱涓涓缃腑锛屽簲鐢ㄧ▼搴忓拰鐢ㄦ埛涓嶅彲鑳界洿鎺ヨ闂暟鎹簱琛紝浣嗘槸浠呭彲浠ユ墽琛屾寚瀹氱殑瀛樺偍绋嬪簭

鍌ㄧ▼搴忓彲浠ユ彁渚涙敼鑹悗鐨勬ц兘锛屽洜涓哄彧鏈夎緝灏戠殑淇℃伅闇瑕佸湪鏈嶅姟鍣ㄥ拰瀹㈡埛绠椾箣闂翠紶閫併備唬浠锋槸澧炲姞鏁版嵁搴撴湇鍔″櫒绯荤粺鐨勮礋鑽凤紝鍥犱负鏇村鐨勫伐浣滃湪鏈嶅姟鍣ㄨ繖杈瑰畬鎴愶紝鏇村皯鐨勫湪瀹㈡埛绔紙搴旂敤绋嬪簭锛夐偅杈瑰畬鎴愪笂銆傚鏋滆澶氬鎴风鏈哄櫒锛堟瘮濡傜綉椤垫湇鍔″櫒锛夊彧鐢变竴涓垨灏戞暟鍑犱釜鏁版嵁搴撴湇鍔″櫒鎻愪緵鏈嶅姟锛屽彲浠ヨ冭檻涓涓嬪瓨鍌ㄧ▼搴忋

瀛樺偍绋嬪簭涔熷厑璁镐綘鍦ㄦ暟鎹簱鏈嶅姟鍣ㄤ笂鏈夊嚱鏁板簱銆傝繖鏄竴涓鐜颁唬搴旂敤绋嬪簭璇█鍏变韩鐨勭壒寰侊紝瀹冨厑璁歌繖鏍风殑鍐呴儴璁捐锛屾瘮濡傞氳繃浣跨敤绫汇備娇鐢ㄨ繖浜涘鎴风搴旂敤绋嬪簭璇█鐗瑰緛瀵圭敋鑷充簬鏁版嵁搴撲娇鐢ㄨ寖鍥翠互澶栫殑缂栫▼浜哄憳閮芥湁濂藉銆

MySQL涓哄瓨鍌ㄧ▼搴忛伒寰猄QL:2003璇硶锛岃繖涓娉曚篃琚敤鍦IBM鐨凞B2鏁版嵁搴撲笂銆

MySQL瀵瑰瓨鍌ㄧ▼搴忕殑瀹炵幇杩樺湪杩涘害涓傛墍鏈夋湰绔犲彊杩扮殑璇硶閮借鏀寔锛屽湪鏈夐檺鍒舵垨鎵╁睍鐨勫湴鏂逛細鎭板綋鍦版寚鍑烘潵銆傛湁鍏充娇鐢 瀛樺偍绋嬪簭鐨勯檺鍒剁殑鏇村璁ㄨ鍦闄勫綍 I, 鐗规ч檺鍒閲屾彁鍒般

20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”閲屾墍璇寸殑锛 瀛樺偍瀛愮▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳藉凡缁忓畬鎴愩

20.1. 瀛樺偍绋嬪簭鍜屾巿鏉冭〃

瀛樺偍绋嬪簭闇瑕佸湪mysql鏁版嵁搴撲腑鏈塸roc琛ㄣ傝繖涓〃鍦∕ySQL 5.1瀹夎杩囩▼涓垱寤恒傚鏋滀綘浠庢棭鏈熺殑鐗堟湰鍗囩骇鍒癕ySQL 5.1 锛岃纭畾鏇存柊浣犵殑鎺堟潈琛ㄤ互纭繚proc琛ㄧ殑瀛樺湪銆傝鍙傞槄2.10.2鑺 “鍗囩骇鎺堟潈琛”

鍦∕ySQL 5.1涓紝鎺堟潈绯荤粺濡備笅鑰冭檻瀛樺偍瀛愮▼搴忥細

·         鍒涘缓瀛樺偍瀛愮▼搴忛渶瑕丆REATE ROUTINE鏉冮檺銆

·         鎻愰啋鎴栫Щ闄ゅ瓨鍌ㄥ瓙绋嬪簭闇瑕丄LTER ROUTINE鏉冮檺銆傝繖涓潈闄愯嚜鍔ㄦ巿浜堝瓙绋嬪簭鐨勫垱寤鸿呫

·         鎵ц瀛愮▼搴忛渶瑕丒XECUTE鏉冮檺銆傜劧鑰岋紝杩欎釜鏉冮檺鑷姩鎺堜簣 瀛愮▼搴忕殑鍒涘缓鑰呫傚悓鏍凤紝瀛愮▼搴忛粯璁ょ殑SQL SECURITY 鐗瑰緛鏄疍EFINER锛屽畠鍏佽鐢ㄨ瀛愮▼搴忚闂暟鎹簱鐨勭敤鎴蜂笌鎵ц瀛愮▼搴忚仈绯诲埌涓璧枫

20.2. 瀛樺偍绋嬪簭鐨勮娉

瀛樺偍绋嬪簭鍜屽嚱鏁版槸鐢–REATE PROCEDURE鍜孋REATE FUNCTION璇彞鍒涘缓鐨勫瓙绋嬪簭銆備竴涓瓙绋嬪簭瑕佷箞鏄竴涓▼搴忚涔堟槸涓涓嚱鏁般備娇鐢–ALL璇彞鏉ヨ皟鐢 绋嬪簭锛岀▼搴忓彧鑳界敤杈撳嚭鍙橀噺浼犲洖鍊笺傚氨鍍忓埆鍏跺畠鍑芥暟璋冪敤涓鏍凤紝鍑芥暟鍙互琚粠璇彞澶栬皟鐢紙鍗抽氳繃寮曠敤鍑芥暟鍚嶏級锛屽嚱鏁拌兘杩斿洖鏍囬噺鍊笺 瀛樺偍瀛愮▼搴忎篃鍙互璋冪敤鍏跺畠瀛樺偍瀛愮▼搴忋

鍦∕ySQL 5.1涓紝涓涓瓨鍌ㄥ瓙绋嬪簭鎴栧嚱鏁颁笌鐗瑰畾鐨勬暟鎹簱鐩歌仈绯汇傝繖閲屾湁鍑犱釜鎰忔濓細

·         褰撲竴涓瓙绋嬪簭琚皟鐢ㄦ椂锛屼竴涓殣鍚殑USE db_name 琚墽琛岋紙褰撳瓙绋嬪簭缁堟鏃跺仠姝㈡墽琛岋級銆瀛樺偍瀛愮▼搴忓唴鐨USE璇彞鏃朵笉鍏佽鐨勩

·         浣犲彲浠ヤ娇鐢ㄦ暟鎹簱鍚嶉檺瀹氬瓙绋嬪簭鍚嶃傝繖鍙互琚敤鏉ュ紩鐢ㄤ竴涓笉鍦ㄥ綋鍓嶆暟鎹簱涓殑瀛愮▼搴忋傛瘮濡傦紝瑕佸紩鐢ㄤ竴涓笌test鏁版嵁搴撳叧鑱旂殑瀛樺偍绋嬪簭p鎴栧嚱鏁癴锛屼綘鍙互璇碈ALL test.p()鎴杢est.f()銆

·         鏁版嵁搴撶Щ闄ょ殑鏃跺欙紝涓庡畠鍏宠仈鐨勬墍鏈夊瓨鍌ㄥ瓙绋嬪簭涔熼兘琚Щ闄ゃ

MySQL 鏀寔闈炲父鏈夌敤鐨勬墿灞曪紝鍗冲畠鍏佽鍦ㄥ瓨鍌ㄧ▼搴忎腑浣跨敤甯歌鐨凷ELECT璇彞锛堥偅灏辨槸璇达紝涓嶄娇鐢ㄥ厜鏍囨垨 灞閮ㄥ彉閲忥級銆傝繖涓竴涓煡璇㈢殑缁撴灉鍖呰绠鍗曞湴鐩存帴閫佸埌瀹㈡埛绔傚SELECT璇彞鐢熸垚澶氫釜缁撴灉鍖咃紝鎵浠ュ鎴风蹇呴』浣跨敤鏀寔澶氱粨鏋滃寘鐨凪ySQL瀹㈡埛绔簱銆傝繖鎰忓懗杩欏鎴风蹇呴』 浣跨敤鑷冲皯MySQL 4.1浠ユ潵鐨勮繎鏈熺増鏈笂鐨勫鎴风搴撱

涓嬮潰涓鑺傛弿杩扮敤鏉ュ垱寤猴紝鏀瑰彉锛岀Щ闄ゅ拰鏌ヨ瀛樺偍绋嬪簭鍜屽嚱鏁扮殑璇硶銆

20.2.1CREATE PROCEDURECREATE FUNCTION

CREATE PROCEDURE sp_name ([proc_parameter[,...]])
    [characteristic ...] routine_body
 
CREATE FUNCTION sp_name ([func_parameter[,...]])
    RETURNS type
    [characteristic ...] routine_body
    
    proc_parameter:
    [ IN | OUT | INOUT ] param_name type
    
    func_parameter:
    param_name type
 
type:
    Any valid MySQL data type
 
characteristic:
    LANGUAGE SQL
  | [NOT] DETERMINISTIC
  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }
  | COMMENT 'string'
 
routine_body:
    Valid SQL procedure statement or statements

杩欎簺璇彞鍒涘缓瀛樺偍瀛愮▼搴忋傝鍦MySQL 5.1涓垱寤哄瓙绋嬪簭锛屽繀椤诲叿鏈CREATE ROUTINE鏉冮檺锛屽苟涓擜LTER ROUTINE鍜孍XECUTE鏉冮檺琚嚜鍔ㄦ巿浜堝畠鐨勫垱寤鸿呫傚鏋滀簩杩涘埗鏃ュ織鍔熻兘琚厑璁革紝浣犱篃鍙兘闇瑕SUPER鏉冮檺锛岃鍙傞槄20.4鑺鈥滃瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戠▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳”

榛樿鍦帮紝瀛愮▼搴忎笌褰撳墠鏁版嵁搴撳叧鑱斻傝鏄庣‘鍦版妸瀛愮▼搴忎笌涓涓粰瀹氭暟鎹簱鍏宠仈璧锋潵锛屽彲浠ュ湪鍒涘缓瀛愮▼搴忕殑鏃跺欐寚瀹氬叾鍚嶅瓧涓db_name.sp_name

濡傛灉瀛愮▼搴忓悕鍜屽唴寤虹殑SQL鍑芥暟鍚嶄竴鏍凤紝瀹氫箟瀛愮▼搴忔椂锛屼綘闇瑕佸湪杩欎釜鍚嶅瓧鍜岄殢鍚庢嫭鍙蜂腑闂存彃鍏ヤ竴涓┖鏍硷紝鍚﹀垯鍙戠敓璇硶閿欒銆傚綋浣犻殢鍚庤皟鐢ㄥ瓙绋嬪簭鐨勬椂鍊欎篃瑕佹彃鍏ャ備负姝わ紝鍗充娇鏈夊彲鑳藉嚭鐜拌繖绉嶆儏鍐碉紝鎴戜滑杩樻槸寤鸿鏈濂介伩鍏嶇粰浣犺嚜宸辩殑 瀛樺偍瀛愮▼搴忓彇涓庡瓨鍦ㄧ殑SQL鍑芥暟涓鏍风殑鍚嶅瓧銆

鐢辨嫭鍙峰寘鍥寸殑鍙傛暟鍒楀繀椤绘绘槸瀛樺湪銆傚鏋滄病鏈夊弬鏁帮紝涔熻浣跨敤涓涓┖鍙傛暟鍒()銆傛瘡涓弬鏁 榛樿閮芥槸涓涓狪N鍙傛暟銆傝鎸囧畾涓哄叾瀹冨弬鏁帮紝鍙湪鍙傛暟鍚嶄箣鍓嶄娇鐢ㄥ叧閿瘝 OUT鎴朓NOUT

娉ㄦ剰: 鎸囧畾鍙傛暟涓篒N, OUT, 鎴朓NOUT 鍙PROCEDURE鏄悎娉曠殑銆傦紙FUNCTION鍙傛暟鎬绘槸琚涓烘槸IN鍙傛暟锛

RETURNS瀛楀彞鍙兘瀵笷UNCTION鎸囧畾锛屽鍑芥暟鑰岃█杩欐槸寮哄埗鐨勩傚畠鐢ㄦ潵鎸囧畾鍑芥暟鐨勮繑鍥炵被鍨嬶紝鑰屼笖鍑芥暟浣撳繀椤诲寘鍚竴涓RETURN value璇彞銆

routine_body 鍖呭惈鍚堟硶鐨凷QL杩囩▼璇彞銆傚彲浠ヤ娇鐢ㄥ鍚堣鍙ヨ娉曪紝璇峰弬闃20.2.7鑺傦紝鈥淏EGIN ... END澶嶅悎璇彞銆傚鍚堣鍙ュ彲浠ュ寘鍚 澹版槑锛屽惊鐜拰鍏跺畠鎺у埗缁撴瀯璇彞銆傝繖浜涜鍙ョ殑璇硶鍦ㄦ湰绔犲悗鍏嶄粙缁嶏紝涓句緥锛岃鍙傞槄20.2.8鑺傦紝鈥淒ECLARE璇彞20.2.12鑺傦紝鈥娴佺▼鎺у埗鏋勯犫

CREATE FUNCTION璇彞琚敤鍦ㄦ洿鏃╃殑MySQL鐗堟湰涓鏀寔UDF 锛堣嚜瀹氫箟鍑芥暟锛夈傝鍙傞槄27.2鑺傦紝鈥缁橫ySQL娣诲姞鏂板嚱鏁”銆 UDF缁х画琚敮鎸侊紝鍗充娇鐜板湪 鏈変簡瀛樺偍鍑芥暟銆俇DF浼氳璁や负涓涓閮ㄥ瓨鍌ㄥ嚱鏁般傜劧鑰岋紝涓嶈璁╁瓨鍌ㄥ嚱鏁颁笌UDF鍑芥暟鍏变韩鍚嶅瓧绌洪棿銆

澶栭儴瀛樺偍绋嬪簭鐨勬鏋跺皢鍦ㄤ笉涔呯殑灏嗘潵寮曞叆銆傝繖灏嗗厑璁镐綘鐢SQL涔嬪鐨勮瑷缂栧啓瀛樺偍绋嬪簭銆傛渶鍙兘鐨勬槸锛岀涓涓鏀寔璇█鏄疨HP锛屽洜涓烘牳蹇働HP寮曟搸寰堝皬锛岀嚎绋嬪畨鍏紝涓斿彲浠ヨ鏂逛究鍦板祵鍏ャ傚洜涓烘鏋舵槸鍏紑鐨勶紝瀹冨笇鏈涜澶氬叾瀹冭瑷涔熻兘琚敮鎸併

濡傛灉绋嬪簭鎴栫嚎绋嬫绘槸瀵瑰悓鏍风殑杈撳叆鍙傛暟浜х敓鍚屾牱鐨勭粨鏋滐紝鍒欒璁や负瀹冩槸“纭畾鐨”锛屽惁鍒欏氨鏄“闈炵‘瀹”鐨勩傚鏋滄棦娌℃湁缁欏畾DETERMINISTIC涔熸病鏈夌粰瀹歂OT DETERMINISTIC锛岄粯璁ょ殑灏辨槸NOT DETERMINISTIC銆

涓鸿繘琛屽鍒讹紝浣跨敤NOW()鍑芥暟锛堟垨瀹冪殑鍚屼箟璇嶏級鎴RAND()鍑芥暟浼氫笉蹇呰鍦颁娇寰椾竴涓瓙绋嬪簭闈炵‘瀹氥傚NOW()鑰岃█锛屼簩杩涘埗鏃ュ織鍖呮嫭鏃堕棿鎴冲苟琚纭鍒躲RAND() 鍙鍦ㄤ竴涓瓙绋嬪簭琚唴搴旂敤涓娆′篃浼氳姝g‘澶嶅埗銆傦紙浣犲彲浠ユ妸瀛愮▼搴忔墽琛屾椂闂存埑鍜岄殢鏈烘暟绉嶅瓙璁や负寮哄埗杈撳叆锛屽畠浠湪涓讳粠涓婃槸鍚屾牱鐨勩傦級

褰撳墠鏉ヨ锛孌ETERMINISTIC鐗瑰緛琚帴鍙楋紝浣嗚繕娌℃湁琚紭鍖栫▼搴忔墍浣跨敤銆傜劧鑰屽鏋滀簩杩涘埗鏃ュ織鍔熻兘琚厑璁镐簡锛岃繖涓壒寰佸奖鍝嶅埌MySQL鏄惁浼氭帴鍙楀瓙绋嬪簭瀹氫箟銆璇峰弬闃20.4锛屸瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”

涓浜涚壒寰佹彁渚涘瓙绋嬪簭浣跨敤鏁版嵁鐨勫唴鍦ㄤ俊鎭侰ONTAINS SQL琛ㄧず瀛愮▼搴忎笉鍖呭惈璇绘垨鍐欐暟鎹殑璇彞銆侼O SQL琛ㄧず瀛愮▼搴忎笉鍖呭惈SQL璇彞銆俁EADS SQL DATA琛ㄧず瀛愮▼搴忓寘鍚鏁版嵁鐨勮鍙ワ紝浣嗕笉鍖呭惈鍐欐暟鎹殑璇彞銆侻ODIFIES SQL DATA琛ㄧず瀛愮▼搴忓寘鍚啓鏁版嵁鐨勮鍙ャ傚鏋杩欎簺鐗瑰緛娌℃湁鏄庣‘缁欏畾锛榛樿鐨勬槸CONTAINS SQL銆

SQL SECURITY鐗瑰緛鍙互鐢ㄦ潵鎸囧畾 瀛愮▼搴忚鐢ㄥ垱寤哄瓙绋嬪簭鑰呯殑璁稿彲鏉ユ墽琛岋紝杩樻槸浣跨敤璋冪敤鑰呯殑璁稿彲鏉ユ墽琛屻傞粯璁ゅ兼槸DEFINER銆傚湪SQL:2003涓呮槸涓涓柊鐗规с傚垱寤鸿呮垨璋冪敤鑰呭繀椤荤敱璁块棶 瀛愮▼搴忓叧鑱旂殑鏁版嵁搴撶殑璁稿彲銆傚湪MySQL 5.1涓紝蹇呴』鏈塃XECUTE鏉冮檺鎵嶈兘鎵ц瀛愮▼搴忋傚繀椤绘嫢鏈夎繖涓潈闄愮殑鐢ㄦ埛瑕佷箞鏄畾涔夎咃紝瑕佷箞鏄皟鐢ㄨ咃紝杩欏彇鍐充簬SQL SECURITY鐗瑰緛鏄浣曡缃殑銆

MySQL瀛樺偍sql_mode绯荤粺鍙橀噺璁剧疆锛岃繖涓缃湪瀛愮▼搴忚鍒涘缓鐨勬椂鍊欒捣浣滅敤锛孧ySQL鎬绘槸寮哄埗浣跨敤杩欎釜璁剧疆鏉ユ墽琛 瀛愮▼搴忋

COMMENT瀛愬彞鏄竴涓狹ySQL鐨勬墿灞曪紝瀹冨彲浠ヨ鐢ㄦ潵鎻忚堪 瀛樺偍绋嬪簭銆傝繖涓俊鎭SHOW CREATE PROCEDURE鍜 SHOW CREATE FUNCTION璇彞鏉ユ樉绀恒

MySQL鍏佽瀛愮▼搴忓寘鍚獶DL璇彞锛屽CREATE鍜孌ROP銆侻ySQL涔熷厑璁稿瓨鍌ㄧ▼搴忥紙浣嗕笉鏄 瀛樺偍鍑芥暟锛夊寘鍚玈QL 浜や簰璇彞锛屽COMMIT銆傚瓨鍌ㄥ嚱鏁颁笉鍙互鍖呭惈閭d簺鍋氭槑纭殑鍜岀粷瀵圭殑鎻愪氦鎴栬呭仛鍥炴粴鐨勮銆係QL鏍囧噯涓嶈姹傚杩欎簺璇彞鐨勬敮鎸侊紝SQL鏍囧噯澹版槑姣忎釜DBMS鎻愪緵鍟嗗彲浠ュ喅瀹氭槸鍚﹀厑璁告敮鎸佽繖浜涜鍙ャ

瀛樺偍瀛愮▼搴忎笉鑳戒娇鐢↙OAD DATA INFILE銆

杩斿洖缁撴灉鍖呯殑璇彞涓嶈兘琚敤鍦ㄥ瓨鍌ㄥ嚱鏁扮銆傝繖鍖呮嫭涓嶄娇鐢↖NTO缁欏彉閲忚鍙 鍒楀肩殑SELECT璇彞锛孲HOW 璇彞锛屽強鍏跺畠璇稿EXPLAIN杩欐牱鐨勮鍙ャ傚浜庡彲鍦ㄥ嚱鏁板畾涔夋椂闂磋鍐冲畾瑕佽繑鍥炰竴涓粨鏋滃寘鐨勮鍙ワ紝鍙戠敓涓涓厑璁镐粠鍑芥暟閿欒杩斿洖缁撴灉鍖呯殑Not锛圗R_SP_NO_RETSET_IN_FUNC锛夈傚浜庡彧鍙湪杩愯鏃跺喅瀹氳杩斿洖涓涓粨鏋滃寘鐨勮鍙ワ紝 鍙戠敓涓涓笉鑳藉湪缁欏畾涓婁笅鏂囬敊璇繑鍥炵粨鏋滃寘鐨凱ROCEDURE %s 锛圗R_SP_BADSELECT)銆

涓嬮潰鏄竴涓娇鐢∣UT鍙傛暟鐨勭畝鍗曠殑瀛樺偍绋嬪簭鐨勪緥瀛愩備緥瀛愪负锛屽湪 绋嬪簭琚畾涔夌殑鏃跺欙紝鐢mysql瀹㈡埛绔痙elimiter鍛戒护鏉ユ妸璇彞瀹氱晫绗︿粠 ;鍙樹负//銆傝繖灏卞厑璁哥敤鍦 绋嬪簭浣撲腑鐨;瀹氱晫绗﹁浼犻掑埌鏈嶅姟鍣ㄨ屼笉鏄mysql鑷繁鏉ヨВ閲娿

mysql> delimiter //
 
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
    -> BEGIN
    ->   SELECT COUNT(*) INTO param1 FROM t;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> delimiter ;
 
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT @a;
+------+
| @a   |
+------+
| 3    |
+------+
1 row in set (0.00 sec)

褰撲娇鐢╠elimiter鍛戒护鏃讹紝浣犲簲璇ラ伩鍏嶄娇鐢ㄥ弽鏂滄潬(\)瀛楃锛屽洜涓洪偅鏄疢ySQL鐨 杞箟瀛楃銆

涓嬪垪鏄竴涓緥瀛愶紝涓涓噰鐢ㄥ弬鏁扮殑鍑芥暟浣跨敤涓涓猄QL鍑芥暟鎵ц涓涓搷浣滐紝骞惰繑鍥炵粨鏋滐細

mysql> delimiter //
 
mysql> CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50)
    -> RETURN CONCAT('Hello, ',s,'!');
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> delimiter ;
 
mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world!  |
+----------------+
1 row in set (0.00 sec)

濡傛灉鍦ㄥ瓨鍌ㄥ嚱鏁颁腑鐨凴ETURN璇彞杩斿洖涓涓被鍨嬩笉鍚屼簬鍦ㄥ嚱鏁扮殑RETURNS瀛愬彞涓寚瀹氱被鍨嬬殑鍊硷紝杩斿洖鍊艰寮哄埗涓烘伆褰撶殑绫诲瀷銆傛瘮濡傦紝濡傛灉涓涓嚱鏁拌繑鍥炰竴涓狤NUM鎴朣ET鍊硷紝浣嗘槸RETURN璇彞杩斿洖涓涓暣鏁帮紝瀵逛簬SET鎴愬憳闆嗙殑鐩稿簲鐨凟NUM鎴愬憳锛屼粠鍑芥暟杩斿洖鐨勫兼槸瀛楃涓层

20.2.2. ALTER PROCEDUREALTER FUNCTION

ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]
 
characteristic:
    { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
  | SQL SECURITY { DEFINER | INVOKER }
  | COMMENT 'string'

杩欎釜璇彞鍙互琚敤鏉ユ敼鍙樹竴涓瓨鍌ㄧ▼搴忔垨鍑芥暟鐨勭壒寰併傚湪MySQL 5.1涓紝浣犲繀椤荤敤ALTER ROUTINE鏉冮檺鎵嶅彲鐢ㄦ瀛愮▼搴忋傝繖涓潈闄愯鑷姩鎺堜簣瀛愮▼搴忕殑鍒涘缓鑰呫傚20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘”涓墍杩帮紝 濡傛灉浜岃繘鍒舵棩蹇楀姛鑳借鍏佽浜嗭紝浣犲彲鑳戒篃闇瑕丼UPER鏉冮檺銆

鍦ˋLTER PROCEDURE鍜孉LTER FUNCTION璇彞涓紝鍙互鎸囧畾瓒呰繃涓涓殑鏀瑰彉銆

20.2.3. DROP PROCEDUREDROP FUNCTION

DROP {PROCEDURE | FUNCTION} [IF EXISTS] sp_name

杩欎釜璇彞琚敤鏉ョЩ闄や竴涓瓨鍌ㄧ▼搴忔垨鍑芥暟銆傚嵆锛屼粠鏈嶅姟鍣ㄧЩ闄や竴涓埗瀹氱殑瀛愮▼搴忋傚湪MySQL 5.1涓紝浣犲繀椤绘湁ALTER ROUTINE鏉冮檺鎵嶅彲鐢ㄦ瀛愮▼搴忋傝繖涓潈闄愯鑷姩鎺堜簣瀛愮▼搴忕殑鍒涘缓鑰呫

IF EXISTS 瀛愬彞鏄竴涓狹ySQL鐨勬墿灞曘傚鏋滅▼搴忔垨鍑芥暟涓嶅瓨鍌紝瀹冮槻姝㈠彂鐢熼敊璇備骇鐢熶竴涓彲浠ョ敤SHOW WARNINGS鏌ョ湅鐨勮鍛娿

20.2.4. SHOW CREATE PROCEDURESHOW CREATE FUNCTION

SHOW CREATE {PROCEDURE | FUNCTION} sp_name

杩欎釜璇彞鏄竴涓狹ySQL鐨勬墿灞曘傜被浼间簬SHOW CREATE TABLE锛屽畠杩斿洖涓涓彲鐢ㄦ潵閲嶆柊鍒涘缓宸插懡鍚 瀛愮▼搴忕殑纭垏瀛楃涓层

mysql> SHOW CREATE FUNCTION test.hello\G
*************************** 1. row ***************************
       Function: hello
       sql_mode:
Create Function: CREATE FUNCTION `test`.`hello`(s CHAR(20)) RETURNS CHAR(50)
RETURN CONCAT('Hello, ',s,'!')

20.2.5. SHOW PROCEDURE STATUSSHOW FUNCTION STATUS

SHOW {PROCEDURE | FUNCTION} STATUS [LIKE 'pattern']

杩欎釜璇彞鏄竴涓狹ySQL鐨勬墿灞曘傚畠杩斿洖瀛愮▼搴忕殑鐗瑰緛锛屽鏁版嵁搴擄紝鍚嶅瓧锛岀被鍨嬶紝鍒涘缓鑰呭強鍒涘缓鍜屼慨鏀规棩鏈熴傚鏋滄病鏈夋寚瀹氭牱寮忥紝鏍规嵁浣犱娇鐢ㄧ殑璇彞锛屾墍鏈 瀛樺偍绋嬪簭鍜屾墍鏈夊瓨鍌ㄥ嚱鏁扮殑淇℃伅閮借鍒楀嚭銆

mysql> SHOW FUNCTION STATUS LIKE 'hello'\G
*************************** 1. row ***************************
           Db: test
         Name: hello
         Type: FUNCTION
      Definer: testuser@localhost
     Modified: 2004-08-03 15:29:37
      Created: 2004-08-03 15:29:37
Security_type: DEFINER
      Comment:

浣犲彲浠ヤ粠INFORMATION_SCHEMA涓殑ROUTINES琛ㄨ幏寰楁湁鍏冲瓨鍌ㄥ瓙绋嬪簭鐨勪俊鎭傝鍙傞槄23.1.14鑺傦紝鈥淚NFORMATION_SCHEMA ROUTINES 琛

20.2.6. CALL璇彞

CALL sp_name([parameter[,...]])

CALL璇彞璋冪敤涓涓厛鍓嶇敤CREATE PROCEDURE鍒涘缓鐨勭▼搴忋

CALL璇彞鍙互鐢 澹版槑涓篛UT鎴栫殑INOUT鍙傛暟鐨勫弬鏁扮粰瀹冪殑璋冪敤鑰呬紶鍥炲笺傚畠涔“杩斿洖”鍙楀奖鍝嶇殑琛屾暟锛屽鎴风绋嬪簭鍙互鍦SQL绾у埆閫氳繃璋冪敤ROW_COUNT()鍑芥暟鑾峰緱杩欎釜鏁帮紝浠嶤涓槸璋冪敤the mysql_affected_rows() C API鍑芥暟鑾峰緱銆

20.2.7. BEGIN ... END澶嶅悎璇彞

[begin_label:] BEGIN
    [statement_list]
END [end_label]

瀛樺偍瀛愮▼搴忓彲浠ヤ娇鐢BEGIN ... END澶嶅悎璇彞鏉ュ寘鍚涓鍙ャstatement_list 浠h〃涓涓垨澶氫釜璇彞鐨勫垪琛ㄣstatement_list涔嬪唴姣忎釜璇彞閮藉繀椤荤敤鍒嗗彿锛堬紱锛夋潵缁撳熬銆

澶嶅悎璇彞鍙互琚爣璁般傞櫎闈begin_label瀛樺湪,鍚﹀垯end_label涓嶈兘琚粰鍑,骞朵笖濡傛灉浜岃呴兘瀛樺湪,浠栦滑蹇呴』鏄悓鏍风殑銆

璇锋敞鎰忥紝鍙夌殑[NOT] ATOMIC瀛愬彞鐜板湪杩樹笉琚敮鎸併傝繖鎰忓懗鐫鍦ㄦ寚浠ゅ潡鐨勫紑濮嬫病鏈変氦浜掔殑瀛樺偍鐐硅璁剧疆锛屽苟涓斿湪涓婁笅鏂囦腑鐢ㄥ埌鐨凚EGIN瀛愬彞瀵瑰綋鍓嶄氦浜掑姩浣滄病鏈夊奖鍝嶃

浣跨敤澶氶噸璇彞闇瑕佸鎴风鑳藉彂閫佸寘鍚鍙ュ畾鐣岀;鐨勬煡璇㈠瓧绗︿覆銆傝繖涓鍙峰湪鍛戒护琛屽鎴风琚敤delimiter鍛戒护鏉ュ鐞嗐傛敼鍙樻煡璇㈢粨灏惧畾鐣岀;锛堟瘮濡傛敼鍙樹负//锛変娇寰; 鍙鐢ㄥ湪瀛愮▼搴忎綋涓

20.2.8. DECLARE璇彞

DECLARE璇彞琚敤鏉ユ妸涓嶅悓椤圭洰灞鍩熷埌涓涓 瀛愮▼搴忥細灞閮ㄥ彉閲忥紙璇峰弬闃20.2.9鑺傦紝鈥瀛樺偍绋嬪簭涓殑鍙橀噺”)锛屾潯浠跺拰 澶勭悊绋嬪簭锛堣鍙傞槄20.2.10鑺傦紝鈥鏉′欢鍜屽鐞嗙▼搴”) 鍙婂厜鏍囷紙璇峰弬闃20.2.11鑺傦紝鈥鍏夋爣”)銆係IGNAL鍜孯ESIGNAL璇彞褰撳墠杩樹笉琚敮鎸併

DECLARE浠呰鐢ㄥ湪BEGIN ... END澶嶅悎璇彞閲岋紝骞朵笖蹇呴』鍦ㄥ鍚堣鍙ョ殑寮澶达紝鍦ㄤ换浣曞叾瀹冭鍙ヤ箣鍓嶃

鍏夋爣蹇呴』鍦ㄥ0鏄庡鐞嗙▼搴忎箣鍓嶈澹版槑锛屽苟涓斿彉閲忓拰鏉′欢蹇呴』鍦ㄥ0鏄庡厜鏍囨垨澶勭悊绋嬪簭涔嬪墠琚0鏄庛

20.2.9. 瀛樺偍绋嬪簭涓殑鍙橀噺

浣犲彲浠ュ湪瀛愮▼搴忎腑澹版槑骞朵娇鐢ㄥ彉閲忋

20.2.9.1. DECLARE灞閮ㄥ彉閲

DECLARE var_name[,...] type [DEFAULT value]

杩欎釜璇彞琚敤鏉ュ0鏄庡眬閮ㄥ彉閲忋傝缁欏彉閲忔彁渚涗竴涓粯璁ゅ硷紝璇峰寘鍚竴涓狣EFAULT瀛愬彞銆傚煎彲浠ヨ鎸囧畾涓轰竴涓〃杈惧紡锛屼笉闇瑕佷负涓涓父鏁般傚鏋滄病鏈塂EFAULT瀛愬彞锛屽垵濮嬪间负NULL銆

灞閮ㄥ彉閲忕殑浣滅敤鑼冨洿鍦ㄥ畠琚0鏄庣殑BEGIN ... END鍧楀唴銆傚畠鍙互琚敤鍦ㄥ祵濂楃殑鍧椾腑锛岄櫎浜嗛偅浜涚敤鐩稿悓鍚嶅瓧 澹版槑鍙橀噺鐨勫潡銆

20.2.9.2. 鍙橀噺SET璇彞

SET var_name = expr [, var_name = expr] ...

鍦ㄥ瓨鍌ㄧ▼搴忎腑鐨凷ET璇彞鏄竴鑸琒ET璇彞鐨勬墿灞曠増鏈傝鍙傝冨彉閲忓彲鑳芥槸瀛愮▼搴忓唴澹版槑鐨勫彉閲忥紝鎴栬呮槸鍏ㄥ眬鏈嶅姟鍣ㄥ彉閲忋

鍦ㄥ瓨鍌ㄧ▼搴忎腑鐨凷ET璇彞浣滀负棰勫厛瀛樺湪鐨凷ET璇硶鐨勪竴閮ㄥ垎鏉ュ疄鐜般傝繖鍏佽SET a=x, b=y, ...杩欐牱鐨勬墿灞曡娉曘傚叾涓笉鍚岀殑鍙橀噺绫诲瀷锛堝眬鍩 澹版槑鍙橀噺鍙婂叏灞鍜岄泦浣撳彉閲忥級鍙互琚贩鍚堣捣鏉ャ傝繖涔熷厑璁告妸灞閮ㄥ彉閲忓拰涓浜涘彧瀵圭郴缁熷彉閲忔湁鎰忎箟鐨勯夐」鍚堝苟璧锋潵銆傚湪閭g鎯呭喌涓嬶紝姝ら夐」琚瘑鍒紝浣嗘槸琚拷鐣ヤ簡銆

20.2.9.3. SELECT ... INTO璇彞

SELECT col_name[,...] INTO var_name[,...] table_expr

杩欎釜SELECT璇硶鎶婇夊畾鐨勫垪鐩存帴瀛樺偍鍒板彉閲忋傚洜姝わ紝鍙湁鍗曚竴鐨勮鍙互琚彇鍥炪

SELECT id,data INTO x,y FROM test.t1 LIMIT 1;

娉ㄦ剰锛岀敤鎴峰彉閲忓悕鍦∕ySQL 5.1涓槸瀵瑰ぇ灏忓啓涓嶆晱鎰熺殑銆傝鍙傞槄9.3鑺傦紝鈥鐢ㄦ埛鍙橀噺”

閲嶈: SQL鍙橀噺鍚嶄笉鑳藉拰鍒楀悕涓鏍枫傚鏋淪ELECT ... INTO杩欐牱鐨凷QL璇彞鍖呭惈涓涓鍒楃殑鍙傝冿紝骞跺寘鍚竴涓笌鍒楃浉鍚屽悕瀛楃殑 灞閮ㄥ彉閲忥紝MySQL褰撳墠鎶婂弬鑰冭В閲婁负涓涓彉閲忕殑鍚嶅瓧銆備緥濡傦紝鍦ㄤ笅闈㈢殑璇彞涓紝xname 琚В閲婁负鍒皒name variable 鐨勫弬鑰冭屼笉鏄埌xname column鐨勶細

CREATE PROCEDURE sp1 (x VARCHAR(5))
  BEGIN
    DECLARE xname VARCHAR(5) DEFAULT 'bob';
    DECLARE newname VARCHAR(5);
    DECLARE xid INT;
    
    SELECT xname,id INTO newname,xid 
      FROM table1 WHERE xname = xname;
    SELECT newname;
  END;

褰撹繖涓▼搴忚璋冪敤鐨勬椂鍊欙紝鏃犺table.xname鍒楃殑鍊兼槸浠涔堬紝鍙橀噺newname灏嗚繑鍥炲‘bob’銆

璇峰弬闃I.1鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勯檺鍒”

20.2.10. 鏉′欢鍜屽鐞嗙▼搴

鐗瑰畾鏉′欢闇瑕佺壒瀹氬鐞嗐傝繖浜涙潯浠跺彲浠ヨ仈绯诲埌閿欒锛屼互鍙婂瓙绋嬪簭涓殑涓鑸祦绋嬫帶鍒躲

20.2.10.1. DECLARE鏉′欢

DECLARE condition_name CONDITION FOR condition_value
 
condition_value:
    SQLSTATE [VALUE] sqlstate_value
  | mysql_error_code

杩欎釜璇彞鎸囧畾闇瑕佺壒娈婂鐞嗙殑鏉′欢銆傚畠灏嗕竴涓悕瀛楀拰鎸囧畾鐨勯敊璇潯浠跺叧鑱旇捣鏉ャ傝繖涓悕瀛楀彲浠ラ殢鍚庤鐢ㄥ湪DECLARE HANDLER璇彞涓傝鍙傞槄20.2.10.2鑺傦紝鈥淒ECLARE澶勭悊绋嬪簭

闄や簡SQLSTATE鍊硷紝涔熸敮鎸丮ySQL閿欒浠g爜銆

20.2.10.2. DECLARE澶勭悊绋嬪簭

DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
 
handler_type:
    CONTINUE
  | EXIT
  | UNDO
 
condition_value:
    SQLSTATE [VALUE] sqlstate_value
  | condition_name
  | SQLWARNING
  | NOT FOUND
  | SQLEXCEPTION
  | mysql_error_code

杩欎釜璇彞鎸囧畾姣忎釜鍙互澶勭悊涓涓垨澶氫釜鏉′欢鐨勫鐞嗙▼搴忋傚鏋滀骇鐢熶竴涓垨澶氫釜鏉′欢锛屾寚瀹氱殑璇彞琚墽琛屻

瀵逛竴涓狢ONTINUE澶勭悊绋嬪簭锛屽綋鍓嶅瓙绋嬪簭鐨勬墽琛屽湪鎵ц 澶勭悊绋嬪簭璇彞涔嬪悗缁х画銆傚浜嶦XIT澶勭悊绋嬪簭锛屽綋鍓岯EGIN...END澶嶅悎璇彞鐨勬墽琛岃缁堟銆俇NDO 澶勭悊绋嬪簭绫诲瀷璇彞杩樹笉琚敮鎸併

·         SQLWARNING鏄鎵鏈変互01寮澶寸殑SQLSTATE浠g爜鐨勯熻銆

·         NOT FOUND鏄鎵鏈変互02寮澶寸殑SQLSTATE浠g爜鐨勯熻銆

·         SQLEXCEPTION鏄鎵鏈夋病鏈夎SQLWARNING鎴朜OT FOUND鎹曡幏鐨凷QLSTATE浠g爜鐨勯熻銆

闄や簡SQLSTATE鍊硷紝MySQL閿欒浠g爜涔熶笉琚敮鎸併

渚嬪:

mysql> CREATE TABLE test.t (s1 int,primary key (s1));
Query OK, 0 rows affected (0.00 sec)
 
mysql> delimiter //
 
mysql> CREATE PROCEDURE handlerdemo ()
    -> BEGIN
    ->   DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
    ->   SET @x = 1;
    ->   INSERT INTO test.t VALUES (1);
    ->   SET @x = 2;
    ->   INSERT INTO test.t VALUES (1);
    ->   SET @x = 3;
    -> END;
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> CALL handlerdemo()//
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT @x//
    +------+
    | @x   |
    +------+
    | 3    |
    +------+
    1 row in set (0.00 sec)

娉ㄦ剰鍒帮紝@x鏄3锛岃繖琛ㄦ槑MySQL琚墽琛屽埌绋嬪簭鐨勬湯灏俱傚鏋淒ECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1; 杩欎竴琛屼笉鍦紝绗簩涓狪NSERT鍥燩RIMARY KEY寮哄埗鑰屽け璐ヤ箣鍚,MySQL鍙兘宸茬粡閲囧彇 榛樿(EXIT)璺緞锛屽苟涓擲ELECT @x鍙兘宸茬粡杩斿洖2銆

20.2.11. 鍏夋爣

绠鍗曞厜鏍囧湪瀛樺偍绋嬪簭鍜屽嚱鏁板唴琚敮鎸併傝娉曞鍚屽湪宓屽叆鐨凷QL涓傚厜鏍囧綋鍓嶆槸涓嶆晱鎰熺殑锛屽彧璇荤殑鍙婁笉婊氬姩鐨勩備笉鏁忔劅鎰忎负鏈嶅姟鍣ㄥ彲浠ユ椿涓嶅彲浠ュ鍒跺畠鐨勭粨鏋滆〃銆

鍏夋爣蹇呴』鍦ㄥ0鏄庡鐞嗙▼搴忎箣鍓嶈澹版槑锛屽苟涓斿彉閲忓拰鏉′欢蹇呴』鍦ㄥ0鏄庡厜鏍囨垨澶勭悊绋嬪簭涔嬪墠琚0鏄庛

渚嬪锛

CREATE PROCEDURE curdemo()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE a CHAR(16);
  DECLARE b,c INT;
  DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
  DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
 
  OPEN cur1;
  OPEN cur2;
 
  REPEAT
    FETCH cur1 INTO a, b;
    FETCH cur2 INTO c;
    IF NOT done THEN
       IF b < c THEN
          INSERT INTO test.t3 VALUES (a,b);
       ELSE
          INSERT INTO test.t3 VALUES (a,c);
       END IF;
    END IF;
  UNTIL done END REPEAT;
 
  CLOSE cur1;
  CLOSE cur2;
END

20.2.11.1.澹版槑鍏夋爣

DECLARE cursor_name CURSOR FOR select_statement

杩欎釜璇彞澹版槑涓涓厜鏍囥備篃鍙互鍦ㄥ瓙绋嬪簭涓畾涔夊涓厜鏍囷紝浣嗘槸涓涓潡涓殑姣忎竴涓厜鏍囧繀椤绘湁鍞竴鐨勫悕瀛椼

SELECT璇彞涓嶈兘鏈塈NTO瀛愬彞銆

20.2.11.2. 鍏夋爣OPEN璇彞

OPEN cursor_name

杩欎釜璇彞鎵撳紑鍏堝墠澹版槑鐨勫厜鏍囥

20.2.11.3. 鍏夋爣FETCH璇彞

FETCH cursor_name INTO var_name [, var_name] ...

杩欎釜璇彞鐢ㄦ寚瀹氱殑鎵撳紑鍏夋爣璇诲彇涓嬩竴琛岋紙濡傛灉鏈変笅涓琛岀殑璇濓級锛屽苟涓斿墠杩涘厜鏍囨寚閽堛

20.2.11.4. 鍏夋爣CLOSE璇彞

CLOSE cursor_name

杩欎釜璇彞鍏抽棴鍏堝墠鎵撳紑鐨勫厜鏍囥

濡傛灉鏈鏄庣‘鍦板叧闂紝鍏夋爣鍦ㄥ畠琚0鏄庣殑澶嶅悎璇彞鐨勬湯灏捐鍏抽棴銆

20.2.12. 娴佺▼鎺у埗鏋勯

IF, CASE, LOOP, WHILE, ITERATE, 鍙 LEAVE 鏋勯犺瀹屽叏瀹炵幇銆

杩欎簺鏋勯犲彲鑳芥瘡涓寘鍚涔堜竴涓崟鐙鍙ワ紝瑕佷箞鏄娇鐢˙EGIN ... END澶嶅悎璇彞鐨勪竴鍧楄鍙ャ傛瀯閫犲彲浠ヨ宓屽銆

鐩墠杩樹笉鏀寔FOR寰幆銆

20.2.12.1. IF璇彞

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

IF瀹炵幇浜嗕竴涓熀鏈殑鏉′欢鏋勯犮傚鏋search_condition姹傚间负鐪燂紝鐩稿簲鐨凷QL璇彞鍒楄〃琚墽琛屻傚鏋滄病鏈search_condition鍖归厤锛屽湪ELSE瀛愬彞閲岀殑璇彞鍒楄〃琚墽琛屻statement_list鍙互鍖呮嫭涓涓垨澶氫釜璇彞銆

璇锋敞鎰忥紝涔熸湁涓涓狪F() 鍑芥暟锛屽畠涓嶅悓浜庤繖閲屾弿杩扮殑IF璇彞銆傝鍙傞槄12.2鑺傦紝鈥鎺у埗娴佺▼鍑芥暟”

20.2.12.2. CASE璇彞

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

Or:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

瀛樺偍绋嬪簭鐨凜ASE璇彞瀹炵幇涓涓鏉傜殑鏉′欢鏋勯犮傚鏋search_condition 姹傚间负鐪燂紝鐩稿簲鐨凷QL琚墽琛屻傚鏋滄病鏈夋悳绱㈡潯浠跺尮閰嶏紝鍦‥LSE瀛愬彞閲岀殑璇彞琚墽琛屻

娉ㄦ剰锛杩欓噷浠嬬粛鐨勭敤鍦 瀛樺偍绋嬪簭閲岀殑CASE璇彞涓12.2鑺傦紝鈥鎺у埗娴佺▼鍑芥暟鈥閲屾弿杩扮殑SQL CASE琛ㄨ揪寮忕殑CASE璇彞鏈夎交寰笉鍚屻傝繖閲岀殑CASE璇彞涓嶈兘鏈塃LSE NULL瀛愬彞锛屽苟涓旂敤END CASE鏇夸唬END鏉ョ粓姝€

20.2.12.3. LOOP璇彞

[begin_label:] LOOP
    statement_list
END LOOP [end_label]

LOOP鍏佽鏌愮壒瀹氳鍙ユ垨璇彞缇ょ殑閲嶅鎵ц锛屽疄鐜颁竴涓畝鍗曠殑寰幆鏋勯犮傚湪寰幆鍐呯殑璇彞涓鐩撮噸澶嶇洿寰幆琚鍑猴紝閫鍑洪氬父浼撮殢鐫涓涓LEAVE 璇彞銆

LOOP璇彞鍙互琚爣娉ㄣ傞櫎闈begin_label瀛樺湪锛屽惁鍒end_label涓嶈兘琚粰鍑猴紝骞朵笖濡傛灉涓よ呴兘鍑虹幇锛屽畠浠繀椤绘槸鍚屾牱鐨勩

20.2.12.4. LEAVE璇彞

LEAVE label

杩欎釜璇彞琚敤鏉ラ鍑轰换浣曡鏍囨敞鐨勬祦绋嬫帶鍒舵瀯閫犮傚畠鍜孊EGIN ... END鎴栧惊鐜竴璧疯浣跨敤銆

20.2.12.5. ITERATE璇彞

ITERATE label

ITERATE鍙彲浠ュ嚭鐜板湪LOOP, REPEAT, 鍜學HILE璇彞鍐呫侷TERATE鎰忔濅负锛“鍐嶆寰幆銆”

渚嬪锛

CREATE PROCEDURE doiterate(p1 INT)
BEGIN
  label1: LOOP
    SET p1 = p1 + 1;
    IF p1 < 10 THEN ITERATE label1; END IF;
    LEAVE label1;
  END LOOP label1;
  SET @x = p1;
END

20.2.12.6. REPEAT璇彞

[begin_label:] REPEAT
    statement_list
UNTIL search_condition
END REPEAT [end_label]

REPEAT璇彞鍐呯殑璇彞鎴栬鍙ョ兢琚噸澶嶏紝鐩磋嚦search_condition 涓虹湡銆

REPEAT 璇彞鍙互琚爣娉ㄣ 闄ら潪begin_label涔熷瓨鍦紝end_label鎵嶈兘琚敤锛屽鏋滀袱鑰呴兘瀛樺湪锛屽畠浠繀椤绘槸涓鏍风殑銆

渚嬪

mysql> delimiter //
 
mysql> CREATE PROCEDURE dorepeat(p1 INT)
    -> BEGIN
    ->   SET @x = 0;
    ->   REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> CALL dorepeat(1000)//
Query OK, 0 rows affected (0.00 sec)
 
mysql> SELECT @x//
+------+
| @x   |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)

20.2.12.7. WHILE璇彞

[begin_label:] WHILE search_condition DO
    statement_list
END WHILE [end_label]

WHILE璇彞鍐呯殑璇彞鎴栬鍙ョ兢琚噸澶嶏紝鐩磋嚦search_condition 涓虹湡銆

WHILE璇彞鍙互琚爣娉ㄣ 闄ら潪begin_label涔熷瓨鍦紝end_label鎵嶈兘琚敤锛屽鏋滀袱鑰呴兘瀛樺湪锛屽畠浠繀椤绘槸涓鏍风殑銆

渚嬪锛

CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;
 
  WHILE v1 > 0 DO
    ...
    SET v1 = v1 - 1;
  END WHILE;
END

20.3. 瀛樺偍绋嬪簭 銆佸嚱鏁般佽Е鍙戠▼搴忓強澶嶅埗锛氬父瑙侀棶棰

  • MySQL 5.1瀛樺偍绋嬪簭鍜屽嚱鏁板澶嶅埗璧蜂綔鐢ㄥ悧锛 

    鏄殑锛屽湪瀛樺偍绋嬪簭鍜屽嚱鏁颁腑琚墽琛屾爣鍑嗚涓鸿浠庝富MySQL鏈嶅姟鍣ㄥ鍒跺埌浠庢湇鍔″櫒銆傛湁灏戞暟闄愬埗锛屽畠浠湪20.4鑺傦紝鈥瀛樺偍瀛愮▼搴忓拰 瑙﹀彂绋嬪簭浜岃繘鍒舵棩蹇楀姛鑳解涓杩般

  • 鍦ㄤ富鏈嶅姟鍣ㄤ笂鍒涘缓鐨勫瓨鍌ㄧ▼搴忓拰鍑芥暟鍙互琚鍒跺埌浠庢湇鍔″櫒涓婁箞锛

    鏄殑锛岄氳繃涓鑸珼DL璇彞鎵ц鐨勫瓨鍌ㄧ▼搴忓拰鍑芥暟锛屽叾鍦ㄤ富鏈嶅姟鍣ㄤ笂鐨勫垱寤鸿澶嶅埗鍒颁粠鏈嶅姟鍣紝鎵浠ョ洰鏍囧皢瀛樺湪涓や釜鏈嶅姟鍣ㄤ笂銆傚瀛樺偍绋嬪簭鍜屽嚱鏁扮殑ALTER 鍜孌ROP璇彞涔熻澶嶅埗銆

  • 琛屼负濡備綍鍦ㄥ凡澶嶅埗鐨勫瓨鍌ㄧ▼搴忓拰鍑芥暟閲屽彂鐢燂紵

    MySQL绾綍姣忎釜鍙戠敓鍦ㄥ瓨鍌ㄧ▼搴忓拰鍑芥暟閲岀殑DML浜嬩欢锛屽苟澶嶅埗杩欎簺鍗曠嫭鐨勮涓哄埌浠庢湇鍔″櫒銆傛墽琛屽瓨鍌ㄧ▼搴忓拰鍑芥暟鐨勫垏瀹炶皟鐢ㄤ笉琚鍒躲

  • 瀵逛竴璧蜂娇鐢ㄥ瓨鍌ㄧ▼搴忥紝鍑芥暟鍜屽鍒舵湁浠涔堢壒鍒殑瀹夊叏瑕佹眰涔堬紵

    鏄殑锛屽洜涓轰竴涓粠鏈嶅姟鍣ㄦ湁鏉冮檺鏉ユ墽琛屼换浣曡鑷富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇楃殑璇彞锛屾寚瀹氱殑瀹夊叏绾︽潫鍥犱笌澶嶅埗涓璧蜂娇鐢ㄧ殑瀛樺偍绋嬪簭鍜屽嚱鏁拌屽瓨鍦ㄣ傚鏋滃鍒舵垨浜岃繘鍒舵棩蹇楀ぇ浣撲笂鏄縺娲荤殑锛堜负point-in-time鎭㈠鐨勭洰鐨勶級锛岄偅涔圡ySQL DBA 鏈変袱涓畨鍏ㄩ夐」鍙夛細

    • 浠讳綍鎯冲垱寤哄瓨鍌ㄧ▼搴忕殑鐢ㄦ埛蹇呴』琚祴浜圫UPER鏉冮檺銆
    • 浣滀负閫夋嫨锛屼竴涓狣BA鍙互璁剧疆log_bin_trust_routine_creators绯荤粺鍙橀噺涓1锛屽畠灏嗕細鍏佽鏈夋爣鍑咰REATE ROUTINE鏉冮檺鐨勪汉鏉ュ垱寤轰竴涓瓨鍌ㄧ▼搴忓拰鍑芥暟銆
     
  • 瀵瑰鍒跺瓨鍌ㄧ▼搴忓拰鍑芥暟鐨勮涓烘湁浠涔堥檺鍒讹紵

    宓屽叆鍒板瓨鍌ㄧ▼搴忎腑鐨勪笉纭畾锛堥殢鏈猴級鎴栨椂鍩鸿涓嶈兘閫傚綋鍦板鍒躲傞殢鏈轰骇鐢熺殑缁撴灉锛屼粎鍥犲叾鏈э紝鏄綘鍙娴嬬殑鍜屼笉鑳借纭疄鍏嬮殕鐨勩傚洜姝わ紝澶嶅埗鍒颁粠鏈嶅姟鍣ㄧ殑闅忔満琛屼负灏嗕笉浼氶暅鍍忛偅浜涗骇鐢熷湪涓绘湇鍔″櫒涓婄殑銆傛敞鎰忥紝 澹版槑瀛樺偍绋嬪簭鎴栧嚱鏁颁负DETERMINISTIC鎴栬呭湪log_bin_trust_routine_creators涓缃郴缁熷彉閲忎负0 灏嗕細鍏佽闅忓嵆鍊兼搷浣滆璋冪敤銆

    姝ゅ锛屾椂鍩鸿涓轰笉鑳藉湪浠庢湇鍔″櫒涓婇噸鏂颁骇鐢燂紝鍥犱负鍦ㄥ瓨鍌ㄧ▼搴忎腑閫氳繃瀵瑰鍒朵娇鐢ㄧ殑浜岃繘鍒舵棩蹇楁潵璁℃椂杩欐牱鐨勬椂鍩鸿涓烘槸涓嶅彲閲嶆柊浜х敓鐨勶紝鍥犱负璇ヤ簩杩涘埗鏃ュ織浠呯邯褰旸ML浜嬩欢涓斾笉鍖呮嫭璁℃椂绾︽潫銆

    鏈鍚庯紝鍦ㄥぇ鍨婦ML琛屼负锛堝澶ф壒鎻掑叆锛変腑闈炰氦浜掕〃鍙戠敓閿欒锛岃闈炰氦浜掕〃鍙兘缁忓巻澶嶅埗锛屽湪澶嶅埗鐗堢殑闈炰氦浜掕〃涓富鏈嶅姟鍣ㄥ彲浠ヨ閮ㄥ垎鍦颁粠DML琛屼负鏇存柊銆備絾鏄洜涓哄彂鐢熺殑閭d釜閿欒锛屽浠庢湇鍔″櫒娌℃湁鏇存柊銆 瀵瑰嚱鏁扮殑DML琛屼负锛屽伐浣滃尯灏嗚鐢↖GNORE鍏抽敭璇嶆潵鎵ц锛屼互渚夸簬鍦ㄤ富鏈嶅姟鍣ㄤ笂瀵艰嚧閿欒鐨勬洿鏂拌蹇界暐锛屽苟涓斾笉浼氬鑷撮敊璇殑鏇存柊琚鍒跺埌浠庢湇鍔″櫒銆

     

  • 涓婅堪鐨勯檺鍒朵細褰卞搷MySQL浣 point-in-time鎭㈠鐨勮兘鍔涘悧锛

    褰卞搷澶嶅埗鐨勫悓涓闄愬埗浼氬奖鍝峱oint-in-time鎭㈠銆

  •  MySQL瑕佸仛浠涔堟潵鏀规鍓嶈堪鐨勯檺鍒跺憿锛

    灏嗘潵鍙戣鐨凪ySQL棰勬湡鏈変竴涓姛鑳藉幓閫夋嫨澶嶅埗璇ュ浣曡澶勭悊锛

    •  鍩轰簬璇彞鐨勫鍒讹紙褰撳墠瀹炵幇锛夈
    • 琛岀骇鍒鍒讹紙瀹冨皢瑙e喅鎵鏈夋棭鍏堟弿杩扮殑闄愬埗锛夈
  • 瑙﹀彂绋嬪簭瀵瑰鍒惰捣浣滅敤涔堬紵

    MySQL 5.1涓殑瑙﹀彂绋嬪簭鍜屽鍒惰薄鍦ㄥぇ澶氭暟鍏跺畠鏁版嵁搴撳紩鎿庝腑涓鏍峰伐浣滐紝鍦ㄩ偅浜涘紩鎿庝腑锛岄氳繃瑙﹀彂绋嬪簭鍦ㄤ富鏈嶅姟鍣ㄤ笂鎵ц鐨勮涓轰笉琚鍒跺埌浠庢湇鍔″櫒銆傚彇鑰屼唬涔嬬殑鏄紝浣嶄簬涓籑ySQL鏈嶅姟鍣ㄧ殑琛ㄤ腑鐨 瑙﹀彂绋嬪簭闇瑕佸湪閭d簺瀛樺湪浜庝换浣昅ySQL浠庢湇鍔″櫒涓婄殑琛ㄥ唴琚垱寤猴紝浠ヤ究浜庤Е鍙戠▼搴忓彲浠ヤ篃鍙互鍦ㄤ粠鏈嶅姟鍣ㄤ笂琚縺娲汇

     

  •  涓涓涓哄浣曢氳繃浠庝富鏈嶅姟鍣ㄤ笂澶嶅埗鍒颁粠鏈嶅姟鍣ㄤ笂鐨勮Е鍙戠▼搴忔潵鎵ц鍛紵

    棣栧厛锛屼富鏈嶅姟鍣ㄤ笂鐨勮Е鍙戠▼搴忓繀椤诲湪浠庢湇鍔″櫒涓婇噸寤恒備竴鏃﹂噸寤轰簡锛屽鍒舵祦绋嬪氨璞″叾瀹冨弬涓庡埌澶嶅埗涓殑鏍囧噯DML璇彞涓鏍峰伐浣溿備緥濡傦細鑰冭檻涓涓凡缁忔彃鍏ヨЕ鍙戠▼搴廇FTER鐨凟MP琛紝瀹冧綅浜庝富MySQL鏈嶅姟鍣ㄤ笂銆傚悓鏍风殑EMP琛ㄥ拰AFTER鎻掑叆 瑙﹀彂绋嬪簭涔熷瓨鍦ㄤ簬浠庢湇鍔″櫒涓娿傚鍒舵祦绋嬪彲鑳芥槸锛

1.    瀵笶MP鍋氫竴涓狪NSERT璇彞銆

2.   EMP涓婄殑AFTER瑙﹀彂绋嬪簭婵娲汇

3.    INSERT璇彞琚啓杩涗簩杩涘埗鏃ュ織銆

4.    浠庢湇鍔″櫒涓婄殑澶嶅埗鎷捐捣INSERT璇彞缁橢MP琛紝骞跺湪浠庢湇鍔″櫒涓婃墽琛屽畠銆

5.    浣嶄簬浠庢湇鍔″櫒EMP涓婄殑AFTER瑙﹀彂绋嬪簭婵娲汇

20.4. 瀛樺偍瀛愮▼搴忓拰瑙﹀彂绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘

锛岃繖涓鑺備粙缁峂ySQL 5.1濡備綍鑰冭檻浜岃繘鍒舵棩蹇楀姛鑳芥潵澶勭悊瀛樺偍瀛愮▼搴忥紙绋嬪簭鍜屽嚱鏁帮級 銆傝繖涓鑺備篃閫傜敤浜庤Е鍙戠▼搴忋

浜岃繘鍒舵棩蹇楀寘鍚慨鏀规暟鎹簱鍐呭鐨凷QL璇彞鐨勪俊鎭傝繖涓俊鎭互鎻忚堪淇敼鐨勪簨浠剁殑褰㈠紡淇濆瓨璧锋潵銆

浜岃繘鍒舵棩蹇楁湁涓や釜閲嶈鐩殑锛

·         澶嶅埗鐨勫熀纭鏄富鏈嶅姟鍣ㄥ彂閫佸寘鍚湪浜岃繘鍒舵棩蹇楅噷鐨勪簨浠跺埌浠庢湇鍔″櫒锛屼粠鏈嶅姟鍣ㄦ墽琛岃繖浜涗簨浠舵潵閫犳垚涓庡涓绘湇鍔″櫒閫犳垚鐨勫悓鏍风殑鏁版嵁鏀瑰彉锛岃鍙傞槄6.2鑺傦紝鈥澶嶅埗姒傝堪”

·         鐗瑰畾鐨勬暟鎹仮澶嶆搷浣滆瑕佷娇鐢ㄤ簩杩涘埗鏃ュ織銆傚浠界殑鏂囦欢琚仮澶嶄箣鍚庯紝澶囦唤鍚庣邯褰曠殑浜岃繘鍒舵棩蹇楅噷鐨勪簨浠惰閲嶆柊鎵ц銆傝繖浜涗簨浠舵妸鏁版嵁搴撳甫浠庡浠界偣鐨勬棩瀛愬甫鍒板綋鍓嶃傝鍙傞槄5.9.2.2鑺傦紝鈥浣跨敤澶囦唤鎭㈠”

MySQL涓紝浠ュ瓨鍌ㄥ瓙绋嬪簭鐨勪簩杩涘埗鏃ュ織鍔熻兘寮曞彂浜嗗緢澶氶棶棰橈紝杩欎簺鍦ㄤ笅闈㈣璁轰腑鍒楀嚭锛屼綔涓哄弬鑰冧俊鎭

闄や簡瑕佸彟澶栨敞鎰忕殑涔嬪锛岃繖浜涜皥璁哄亣璁句綘宸茬粡閫氳繃鐢--log-bin閫夐」鍚姩鏈嶅姟鍣ㄥ厑璁镐簡浜岃繘鍒舵棩蹇楀姛鑳姐傦紙濡傛灉浜岃繘鍒舵棩蹇楀姛鑳戒笉琚厑璁革紝澶嶅埗灏嗕笉鍙兘锛屼负鏁版嵁鎭㈠鐨勪簩杩涘埗鏃ュ織涔熶笉瀛樺湪銆傦級璇峰弬闃5.11.3鑺傦紝鈥浜岃繘鍒舵棩蹇”

瀵瑰瓨鍌ㄥ瓙绋嬪簭璇彞鐨勪簩杩涘埗鏃ュ織鍔熻兘鐨勭壒寰佸湪涓嬮潰鍒楄〃涓弿杩般備竴浜涙潯鐩寚鍑轰綘搴旇娉ㄦ剰鍒扮殑闂銆備絾鏄湪涓浜涙儏鍐典笅锛屾湁浣犲彲浠ユ洿鏀圭殑濡囦簲涓冭缃垨浣犲彲浠ョ敤鏉ュ鐞嗗畠浠殑宸ヤ綔鍖恒

·         CREATE PROCEDURE, CREATE FUNCTION, ALTER PROCEDURE,鍜孉LTER FUNCTION 璇彞琚啓杩涗簩杩涘埗鏃ュ織锛孋ALL, DROP PROCEDURE, 鍜孌ROP FUNCTION 涔熶竴鏍枫

灏界濡傛锛屽澶嶅埗鏈変竴涓畨鍏ㄦ殫绀猴細瑕佸垱寤轰竴涓瓙绋嬪簭锛岀敤鎴峰繀椤绘湁CREATE ROUTINE鏉冮檺,浣嗘湁杩欎釜鏉冮檺鐨勭敤鎴蜂笉鑳藉啓涓涓 瀛愮▼搴忓湪浠庢湇鍔″櫒涓婃墽琛屼换浣曟搷浣溿傚洜涓哄湪浠庢湇鍔″櫒涓婄殑SQL绾跨▼鐢ㄥ畬鍏ㄦ潈闄愭潵杩愯銆備緥濡傦紝濡傛灉涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄥ垎鍒湁鏈嶅姟鍣↖D鍊1鍜2锛屽湪涓绘湇鍔″櫒涓婄殑鐢ㄦ埛鍙兘鍒涘缓骞惰皟鐢ㄥ涓嬩竴涓 绋嬪簭锛

mysql> delimiter //
mysql> CREATE PROCEDURE mysp ()
    -> BEGIN
    ->   IF @@server_id=2 THEN DROP DATABASE accounting; END IF;
    -> END;
    -> //
mysql> delimiter ;
mysql> CALL mysp();

CREATE PROCEDURE鍜孋ALL璇彞灏嗚鍐欒繘浜岃繘鍒舵棩蹇楋紝鎵浠ヤ粠鏈嶅姟鍣ㄥ皢鎵ц瀹冧滑銆傚洜涓轰粠SQL绾跨▼鏈夊畬鍏ㄦ潈闄愶紝瀹冨皢绉婚櫎accounting鏁版嵁搴撱

瑕佷娇鍏佽浜岃繘鍒舵棩蹇楀姛鑳界殑鏈嶅姟鍣ㄩ伩鍏嶈繖涓嵄闄╋紝MySQL 5.1宸茬粡瑕佹眰 瀛樺偍绋嬪簭鍜屽嚱鏁扮殑鍒涘缓鑰呴櫎浜嗛氬父闇瑕佺殑CREATE ROUTINE鐨勬潈闄愬锛岃繕蹇呴』鏈塖UPER 鏉冮檺銆傜被浼煎湴锛岃浣跨敤ALTER PROCEDURE鎴朅LTER FUNCTION锛岄櫎浜咥LTER ROUTINE鏉冮檺澶栦綘蹇呴』鏈塖UPER鏉冮檺銆傛病鏈塖UPER鏉冮檺锛屽皢浼氬彂鐢熶竴涓敊璇細

ERROR 1419 (HY000): You do not have the SUPER privilege and
binary logging is enabled (you *might* want to use the less safe
log_bin_trust_routine_creators variable)

浣犲彲鑳戒笉鎯冲己鍒惰姹傚瓙绋嬪簭鍒涘缓鑰呭繀椤绘湁SUPER鏉冮檺銆備緥濡傦紝浣犵郴缁熶笂鎵鏈夋湁CREATE ROUTINE鏉冮檺鐨勭敤鎴峰彲鑳芥槸鏈夌粡楠岀殑搴旂敤绋嬪簭寮鍙戣呫傝绂佹鎺夊SUPER鏉冮檺鐨勮姹傦紝璁剧疆log_bin_trust_routine_creators 鍏ㄥ眬绯荤粺鍙橀噺涓1銆傞粯璁ゅ湴锛岃繖涓彉閲忓间负0锛屼絾浣犲彲浠ヨ薄杩欐牱鏀瑰彉杩欐牱锛

mysql> SET GLOBAL log_bin_trust_routine_creators = 1;

浣犱篃鍙互鍦ㄥ惎鍔ㄦ湇鍔″櫒涔嬫椂鐢--log-bin-trust-routine-creators閫夐」鏉ヨ缃厑璁歌繖涓彉閲忋

濡傛灉浜岃繘鍒舵棩蹇楀姛鑳戒笉琚厑璁革紝log_bin_trust_routine_creators 娌℃湁琚敤涓婏紝瀛愮▼搴忓垱寤洪渶瑕丼UPER鏉冮檺銆

·         涓涓墽琛屾洿鏂扮殑闈炵‘瀹氬瓙绋嬪簭鏄笉鍙噸澶嶇殑锛屽畠鑳芥湁涓や釜涓嶅鎰忕殑褰卞搷锛

o        瀹冧細浣垮緱浠庢湇鍔″櫒涓嶅悓浜庝富鏈嶅姟鍣

-        鎭㈠鐨勬暟鎹笌鍘熷鏁版嵁涓嶅悓銆

瑕佽В鍐宠繖浜涢棶棰橈紝MySQL寮哄埗鍋氫笅闈㈣姹傦細鍦ㄤ富鏈嶅姟鍣ㄤ笂锛岄櫎闈炲瓙绋嬪簭琚0鏄庝负纭畾鎬х殑鎴栬呬笉鏇存敼鏁版嵁锛屽惁鍒欏垱寤烘垨鑰呮浛鎹㈠瓙绋嬪簭灏嗚鎷掔粷銆傝繖鎰忓懗鐫褰撲綘鍒涘缓涓涓瓙绋嬪簭鐨勬椂鍊欙紝浣犲繀椤昏涔堝0鏄庡畠鏄‘瀹氭х殑锛岃涔堝畠涓嶆敼鍙樻暟鎹備袱濂楀瓙绋嬪簭鐗瑰緛鍦ㄨ繖閲岄傜敤锛

-        DETERMINISTIC鍜孨OT DETERMINISTIC鎸囧嚭涓涓瓙绋嬪簭鏄惁瀵圭粰瀹氱殑杈撳叆鎬绘槸浜х敓鍚屾牱鐨勭粨鏋溿傚鏋滄病鏈夌粰瀹氫换涓鐗瑰緛锛岄粯璁ゆ槸NOT DETERMINISTIC锛屾墍浠ヤ綘蹇呴』鏄庣‘鎸囧畾DETERMINISTIC鏉 澹版槑涓涓 瀛愮▼搴忔槸纭畾鎬х殑銆

浣跨敤NOW() 鍑芥暟锛堟垨瀹冪殑鍚屼箟锛夋垨鑰匯AND() 鍑芥暟涓嶆槸蹇呰鍦颁娇涔熶竴涓瓙绋嬪簭闈炵‘瀹氭с傚NOW()鑰岃█锛屼簩杩涘埗鏃ュ織鍖呮嫭鏃堕棿鎴冲苟姝g‘澶嶅埗銆俁AND()鍙鍦ㄤ竴涓 瀛愮▼搴忓唴琚皟鐢ㄤ竴娆′篃鍙互姝g‘澶嶅埗銆傦紙浣犲彲浠ヨ涓哄瓙绋嬪簭鎵ц鏃堕棿鎴冲拰闅忔満鏁扮瀛愪綔涓烘鏃犵枒闂湴杈撳叆锛屽畠浠湪涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂鏄竴鏍风殑銆傦級

-        CONTAINS SQL, NO SQL, READS SQL DATA, 鍜 MODIFIES SQL鏁版嵁鎻愪緵瀛愮▼搴忔槸璇昏繕鏄啓鏁版嵁鐨勪俊鎭傛棤璁篘O SQL 杩樻槸READS SQL DATA i閮芥寚鍑猴紝瀛愮▼搴忔病鏈夋敼鍙樻暟鎹紝浣嗕綘蹇呴』鏄庣櫧鍦版寚鏄庤繖浜涗腑鐨勪竴涓紝鍥犱负濡傛灉浠讳綍杩欎簺鐗瑰緛娌℃湁琚粰鍑猴紝 榛樿鐨勭壒寰佹槸CONTAINS SQL銆

榛樿鍦帮紝瑕佷竴涓狢REATE PROCEDURE 鎴 CREATE FUNCTION 璇彞琚帴鍙楋紝DETERMINISTIC 鎴 NO SQL涓嶳EADS SQL DATA 涓殑涓涓繀椤绘槑鐧藉湴鎸囧畾锛屽惁鍒欎細浜х敓濡備笅閿欒锛

ERROR 1418 (HY000): This routine has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled
(you *might* want to use the less safe log_bin_trust_routine_creators
variable)

濡傛灉璁剧疆log_bin_trust_routine_creators 涓1, 绉婚櫎瀵瑰瓙绋嬪簭蹇呴』鏄‘瀹氱殑鎴栦笉淇敼鏁版嵁鐨勮姹傘

娉ㄦ剰锛屽瓙绋嬪簭鏈х殑璇勪及鏄熀浜庡垱寤鸿呯殑“璇氬疄搴” 锛歁ySQL涓嶆鏌ュ0鏄庝负纭畾鎬х殑瀛愮▼搴忔槸鍚︿笉鍚骇鐢熼潪纭畾鎬х粨鏋滅殑璇彞銆

·         濡傛灉瀛愮▼搴忚繑鍥炴棤閿欙紝CALL璇彞琚啓杩涗簩杩涘埗鏃ュ織锛屽惁鍒欏氨涓嶅啓銆傚綋涓涓瓙绋嬪簭淇敼鏁版嵁澶辫触浜嗭紝浣犱細寰楀埌杩欐牱鐨勮鍛婏細

                ERROR 1417 (HY000): A routine failed and has neither NO SQL nor
                READS SQL DATA in its declaration and binary logging is enabled; if
                non-transactional tables were updated, the binary log will miss their
                changes

杩欎釜璁版棩蹇楄涓烘綔鍦ㄥ湴瀵艰嚧闂.濡傛灉涓涓瓙绋嬪簭閮ㄥ垎鍦颁慨鏀逛竴涓潪浜や簰琛紙姣斿涓涓狹yISAM琛╝ble锛夊苟涓旇繑鍥炰竴涓敊璇紝浜岃繘鍒舵棩蹇楀皢鍙嶆槧杩欎簺鍙樺寲銆傝闃叉杩欑鎯呭喌锛屼綘搴旇鍦 瀛愮▼搴忎腑浣跨敤浜や簰琛ㄥ苟涓斿湪浜や簰鍔ㄤ綔鍐呬慨鏀硅〃銆

鍦ㄤ竴涓瓙绋嬪簭鍐咃紝濡傛灉浣犲湪INSERT, DELETE, 鎴栬匲PDATE閲屼娇鐢↖GNORE鍏抽敭璇嶆潵蹇界暐閿欒锛屽彲鑳藉彂鐢熶竴涓儴鍒嗘洿鏂帮紝浣嗘病鏈夐敊璇骇鐢熴傝繖鏍风殑璇彞琚褰曟棩蹇楋紝涓旀甯稿鍒躲

·         濡傛灉涓涓瓨鍌ㄥ嚱鏁板湪涓涓SELECT杩欐牱涓嶄慨鏀规暟鎹殑璇彞鍐呰璋冪敤锛屽嵆浣垮嚱鏁版湰韬洿鏀规暟鎹紝鍑芥暟鐨勬墽琛屼篃灏嗕笉琚啓杩涗簩杩涘埗鏃ュ織閲屻傝繖涓褰曟棩蹇楃殑琛屼负娼滃湪鍦板鑷撮棶棰樸傚亣璁惧嚱鏁癿yfunc()濡備笅瀹氫箟锛

                CREATE FUNCTION myfunc () RETURNS INT
                BEGIN
                  INSERT INTO t (i) VALUES(1);
                  RETURN 0;
                END;

鎸夌収涓婇潰瀹氫箟锛涓嬮潰鐨勮鍙ヤ慨鏀琛╰锛屽洜涓簃yfunc()淇敼琛╰, 浣嗘槸璇彞涓嶈鍐欒繘浜岃繘鍒舵棩蹇楋紝鍥犱负瀹冩槸涓涓猄ELECT璇彞锛

SELECT myfunc();

瀵硅繖涓棶棰樼殑宸ヤ綔鍖哄皢璋冪敤鍦ㄥ仛鏇存柊鐨勮鍙ラ噷鍋氭洿鏂扮殑鍑芥暟銆傛敞鎰忥紝铏界劧DO璇彞鏈夋椂涓轰簡鍏朵及绠楄〃杈惧紡鐨勫壇鏁堝簲鑰岃鎵ц锛孌O鍦ㄨ繖閲屼笉鏄竴涓伐浣滃尯锛屽洜涓哄畠涓嶈鍐欒繘浜岃繘鍒舵棩蹇椼

·         鍦ㄤ竴涓瓙绋嬪簭鍐呮墽琛岀殑璇彞涓嶈鍐欒繘浜岃繘鍒舵棩蹇椼傚亣濡備綘鍙戝竷涓嬪垪璇彞锛

                CREATE PROCEDURE mysp INSERT INTO t VALUES(1);
                CALL mysp;

瀵逛簬杩欎釜渚嬪瓙鏉ヨ锛孋REATE PROCEDURE 鍜孋ALL璇彞鍑虹幇鍦ㄤ簩杩涘埗鏃ュ織閲岋紝浣咺NSERT璇彞骞舵湭鍑虹幇銆

·         鍦ㄤ粠鏈嶅姟鍣ㄤ笂锛屽綋鍐冲畾澶嶅埗鍝釜鏉ヨ嚜涓绘湇鍔″櫒鐨勪簨浠舵椂锛屼笅鍒楅檺鍒惰搴旂敤锛--replicate-*-table瑙勫垯涓嶉傜敤浜嶤ALL璇彞鎴栧瓙绋嬪簭鍐呯殑璇彞锛氬湪杩欎簺鎯呭喌涓嬶紝鎬绘槸杩斿洖鈥滃鍒讹紒”

瑙﹀彂绋嬪簭绫讳技浜庡瓨鍌ㄥ嚱鏁帮紝鎵浠ュ墠杩扮殑璇勮涔熼傜敤浜庤Е鍙戠▼搴忥紝闄や簡涓嬪垪鎯呭喌锛 CREATE TRIGGER娌℃湁鍙夌殑DETERMINISTIC鐗瑰緛锛屾墍浠ヨЕ鍙戠▼搴忚鍋囧畾涓烘绘槸纭畾鎬х殑銆傜劧鑰岋紝杩欎釜鍋囪鍦ㄤ竴浜涙儏鍐典笅鏄潪娉曠殑銆傛瘮濡傦紝UUID()鍑芥暟鏄潪纭畾鎬х殑锛堜笉鑳藉鍒讹級銆備綘搴旇灏忓績鍦 瑙﹀彂绋嬪簭涓娇鐢ㄨ繖涓嚱鏁般

瑙﹀彂绋嬪簭鐩墠涓嶈兘鏇存柊琛紝浣嗘槸鍦ㄥ皢鏉ヤ細鏀寔銆傚洜涓鸿繖涓師鍥狅紝濡傛灉浣犳病鏈塖UPER鏉冮檺涓攍og_bin_trust_routine_creators 琚涓0锛屽緱鍒扮殑閿欒淇℃伅绫讳技浜庡瓨鍌ㄥ瓙绋嬪簭涓嶤REATE TRIGGER浜х敓鐨勯敊璇俊鎭

鍦ㄦ湰鑺備腑鍙欒堪鐨勯棶棰樻潵鑷彂鐢熷湪SQL璇彞绾у埆鐨勪簩杩涘埗鏃ュ織璁板綍鐨勪簨瀹炪傛湭鏉ュ彂琛岀殑MySQL鏈熸湜鑳藉疄鐜拌绾х殑浜岃繘鍒舵棩蹇楄褰曪紝璁板綍鍙戠敓鍦ㄦ洿 缁嗚嚧鐨勭骇鍒苟涓旀寚鍑哄摢涓敼鍙樹綔涓烘墽琛孲QL鐨勭粨鏋滃鍗曚釜璁板綍鑰屽仛銆


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆