mysql儲存過程procedure、函式function的用法

阿飛_程式設計師發表於2021-01-03

一、儲存過程的用法

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語句的基本結構

  1. while(表示式) do 

  2.    ......  

  3. 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()

 

 

 

相關文章