mysql儲存過程procedure、函式function的用法
一、儲存過程的用法
1.建立語法
CREATE PROCEDURE Pro_name()
BEGIN
...
END
2.定義變數
DELIMITER $$
CREATE PROCEDURE find_name1()
BEGIN
DECLARE uid VARCHAR(50) DEFAULT '';
SET uid='004';
SELECT * FROM student WHERE sid=uid;
SELECT NAME FROM test;
END
CALL find_name1();
3.傳入一個引數
DELIMITER $$
CREATE PROCEDURE find_name2(uid VARCHAR(20))
BEGIN
SELECT * FROM student WHERE sid=uid;
END
CALL find_name2('005');
4.可以多個begin—end,也可以返回多個引數
變數作用域說明:
(1)、儲存過程中變數是有作用域的,作用範圍在begin和end塊之間,end結束變數的作用範圍即結束。
(2)、需要多個塊之間傳值,可以使用全域性變數,即放在所有程式碼塊之前
(3)、傳參變數是全域性的,可以在多個塊之間起作用
DELIMITER $$
CREATE PROCEDURE find_name5()
BEGIN
BEGIN
DECLARE max_test INT DEFAULT 0;
DECLARE max_student INT DEFAULT 0;
SELECT MAX(age) INTO max_test FROM test;
SELECT MAX(age) INTO max_student FROM student;
SELECT max_test,max_student;
END;
BEGIN
DECLARE count_test INT DEFAULT 0;
DECLARE count_student INT DEFAULT 0;
SELECT COUNT(0) INTO count_test FROM test;
SELECT COUNT(0) INTO count_student FROM student;
SELECT count_test,count_student;
END;
END
CALL find_name5();
5.測試全域性變數
DELIMITER $$
CREATE PROCEDURE find_name6()
BEGIN
DECLARE max_test INT DEFAULT 0;
DECLARE max_student INT DEFAULT 0;
BEGIN
SELECT MAX(age) INTO max_test FROM test;
SELECT MAX(age) INTO max_student FROM student;
SELECT max_test;
END;
BEGIN
DECLARE count_test INT DEFAULT 0;
DECLARE count_student INT DEFAULT 0;
SELECT COUNT(0) INTO count_test FROM test;
SELECT COUNT(0) INTO count_student FROM student;
SELECT max_student,count_test,count_student;
END;
END
CALL find_name6
6.in、out引數的用法
in為傳入引數,out為輸出引數
DELIMITER $$
CREATE PROCEDURE find_name7(IN id VARCHAR(3),OUT myname VARCHAR(100))
BEGIN
SELECT sname INTO myname FROM student WHERE sid=id;
END;
CALL find_name7('001',@myname);
SELECT @myname
7.引數inoutde的使用(既能輸入一個值又能傳出來一個值)
DELIMITER $$
CREATE PROCEDURE find_name8(INOUT myage INT(20))
BEGIN
SET myage=myage+1;
SELECT age INTO myage FROM student WHERE age=myage;
END
SET @myage=20;
CALL find_name8(@myage);
SELECT @myage
也可以寫多個inout引數
DELIMITER $$
CREATE PROCEDURE find_name9(INOUT uid VARCHAR(20),INOUT uname VARCHAR(20))
BEGIN
SET uid='002';
SET uname='';
SELECT sid,sname INTO uid,uname FROM student WHERE sid=uid;
END
CALL find_name9(@uid,@uname);
SELECT @uid,@uname
8.儲存過程條件語句用法
①條件語句基本結構 if() then...else...end if;
案例:傳入一個年齡,如果年齡為偶數,查詢名字,否則查詢年齡
DELIMITER $$
CREATE PROCEDURE pro_test1(IN myage INT(20))
BEGIN
DECLARE username VARCHAR(200) DEFAULT '';
IF(myage%2=0)
THEN
SELECT sname INTO username FROM student WHERE age=myage;
SELECT username;
ELSE
SELECT myage;
END IF;
END
CALL pro_test1(20)
CALL pro_test1(21)
②多條件判斷語句:
if() then...
elseif() then...
else ...
end if;
案例:傳入id,查詢性別,如果為男人,年齡+10,女人年齡+5,中性人年齡+2
DELIMITER $$
CREATE PROCEDURE pro_test2(IN id VARCHAR(20))
BEGIN
DECLARE yousex VARCHAR(20) DEFAULT '';
DECLARE youage INT DEFAULT 0;
DECLARE yage INT DEFAULT 0;
SELECT sex ,age INTO yousex,youage FROM student WHERE sid=id;
SELECT age INTO yage FROM student WHERE sid=id;
IF(yousex='男')
THEN
SET youage=youage+10;
ELSEIF(yousex='女')
THEN
SET youage=youage+5;
ELSE
SET youage=youage+2;
END IF;
SELECT yage,yousex,youage;
END
CALL pro_test2('001')
CALL pro_test2('002')
CALL pro_test2('003')
9.儲存過程迴圈語句
①while語句的基本結構
-
while(表示式) do
-
......
-
end while;
案例:迴圈插入幾千條資料
DELIMITER $$ -- 以delimiter來標記用$表示儲存過程結束
CREATE PROCEDURE pro_insert1() -- 建立pro_insert()儲存方法
BEGIN
DECLARE i INT; -- 定義i變數
SET i=500; -- 對i賦值
WHILE 400<i DO -- 設定i的迴圈條件
INSERT INTO test (id,NAME,age)VALUES(i,'張三',i);
SET i=i-1; -- 自減迴圈
END WHILE; -- 結束while迴圈
END
CALL pro_insert1()
相關文章
- MySQL入門--儲存過程(PROCEDURE)和儲存函式(FUNCTION)MySql儲存過程儲存函式Function
- Mysql 的儲存過程和儲存函式MySql儲存過程儲存函式
- MySQL 儲存過程和函式MySql儲存過程函式
- MySQL儲存過程 (即函式)MySql儲存過程函式
- MySQL儲存過程和函式MySql儲存過程函式
- mySql 儲存過程與函式MySql儲存過程函式
- mysql 儲存過程 procedure 批次建表MySql儲存過程
- mysql和orcale的儲存過程和儲存函式MySql儲存過程儲存函式
- mysql儲存過程基本函式(轉)MySql儲存過程函式
- function.procedure函式下的過程執行問題Function函式
- mysql儲存函過程和儲存函式都屬於儲存程式MySql儲存函式
- mysql檢視儲存過程show procedure status;MySql儲存過程
- 儲存過程 函式儲存過程函式
- 儲存過程與儲存函式儲存過程儲存函式
- MySQL自定義函式與儲存過程MySql函式儲存過程
- MySQL 5.5 建立儲存過程和函式MySql儲存過程函式
- MySQL4:儲存過程和函式MySql儲存過程函式
- SQL 建立儲存過程PROCEDURESQL儲存過程
- 觸發器trigger中呼叫包package(包中含:儲存過程procedure及函式function)_plsql觸發器Package儲存過程函式FunctionSQL
- 儲存過程與函式儲存過程函式
- mysql儲存過程及日期函式實踐MySql儲存過程函式
- PL/SQL 05 儲存過程 procedureSQL儲存過程
- [Procedure]Oracle之分頁儲存過程Oracle儲存過程
- Oracle 基礎 ----procedure(儲存過程)Oracle儲存過程
- (9)mysql 中的儲存過程和自定義函式MySql儲存過程函式
- php呼叫mysql儲存過程和函式的方法(轉)PHPMySql儲存過程函式
- Oracle 過程(Procedure)、函式(Function)、包(Package)、觸發器(Trigger)Oracle函式FunctionPackage觸發器
- SQL server儲存過程函式SQLServer儲存過程函式
- MySQL儲存過程詳解 mysql 儲存過程MySql儲存過程
- 儲存過程和函式的區別儲存過程函式
- oracle父儲存過程呼叫子儲存過程procedure與輸出引數Oracle儲存過程
- 儲存過程vs.函式QM儲存過程函式
- 【MySQL】MySQL(三)儲存過程和函式、觸發器、事務MySql儲存過程函式觸發器
- mysql的儲存過程MySql儲存過程
- 深入mysql建立自定義函式與儲存過程的詳解MySql函式儲存過程
- MySQL儲存過程詳解 mysql 儲存過程linkMySql儲存過程
- MySql儲存過程—2、第一個MySql儲存過程的建立MySql儲存過程
- mysql 儲存過程MySql儲存過程