MYSQL入門學習之十二:存儲過程的基本操作 相關鏈接: MYSQL入門學習之一:基本操作 http://www.BkJia.com/database/201212/173868.html MYSQL入門學習之二:使用正則表達式搜索 http://www.BkJia.com/database/201212/173869.html MYSQL入門學習之三:全文本搜索 http://www.BkJia.com/database/201212/173873.html MYSQL入門學習之四:MYSQL的數據類型 http://www.BkJia.com/database/201212/175536.html MYSQL入門學習之五:MYSQL的字符集 http://www.BkJia.com/database/201212/175541.html MYSQL入門學習之六:MYSQL的運算符 http://www.BkJia.com/database/201212/175862.html MYSQL入門學習之七:MYSQL常用函數 http://www.BkJia.com/database/201212/175864.html MYSQL入門學習之八:數據庫及表的基本操作 http://www.BkJia.com/database/201212/175867.html MYSQL入門學習之九:索引的簡單操作 http://www.BkJia.com/database/201212/176772.html MYSQL入門學習之十:視圖的基本操作 http://www.BkJia.com/database/201212/176775.html MYSQL入門學習之十一:觸發器的基本操作 http://www.BkJia.com/database/201212/176781.html 存儲過程簡單來說,就是為以後的使用而保存的一條或多條MySQL語句的集合。可將其視為批文件,雖然它們的作用不僅限於批處理。 www.2cto.com 使用存儲過程需要MySQL5及以後的版本支持。 一、為什麼要使用存儲過程 通過把處理封閉在容易使用的單元中,簡化復雜的操作; 將一系列處理步驟放到同一存儲過程中,保證了數據的完整性和操作的安全性; 簡化對變更的管理; 提高性能。使用存儲過程比使用單獨的SQL語句要快; 存在一些只能用在單個請求中的MySQL元素和特性,存儲過程可以使用它們來編寫功能更強更靈活的代碼; 二、基本操作 1、創建存儲過程 CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body proc_parameter: [ IN | OUT | INOUT ] param_name type 示例: [sql] mysql>create procedure sp_test() ->begin -> select userid,username from newname where userid=215; ->end ->// 2、執行存儲過程 CALL sp_name; 示例: [sql] mysql> call sp_test(); +--------+----------+ | userid | username | +--------+----------+ | 215 | NULL | +--------+----------+ 3、刪除存儲過程 DROP PROCEDURE [ IF EXISTS ] sp_name; 示例: [sql] mysql> drop procedure if exists sp_test; 4、查看存儲過程創建信息 SHOW CREATE PROCEDURE sp_name; 示例: [sql] mysql> show create procedure sp_test; +-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+ | Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation | +-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+ | sp_test | | CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test`() begin select userid,username from newname where userid=215; end | latin1 | latin1_swedish_ci | latin1_swedish_ci | +-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+ 5、查看存儲過程狀態 SHOW PROCEDURE STATUS [ LIKE '' ]; 示例: [sql] mysql> show procedure status like 'sp_test'; +------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+- | Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | +------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+- | test | sp_test | PROCEDURE | root@localhost | 2012-12-17 23:57:38 | 2012-12-17 23:57:38 | DEFINER | | latin1 | +------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+- www.2cto.com 6、使用存儲過程參數 示例: [sql] mysql> delimiter // mysql> create procedure sp_type_cnt( -> IN in_type int, -> OUT out_cnt int -> ) -> begin -> select count(*) -> from newname -> where type = in_type -> into out_cnt; -> end; -> // mysql> delimiter ; mysql> call sp_type_cnt(0,@cnt); mysql> select @cnt; +------+ | @cnt | +------+ | 159 | +------+