标签:into rop 孙悟空 row rom erro float roc 猪八戒
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.49 sec)
mysql> delimiter //
mysql> create procedure t1()
-> begin
-> declare mm int default 5;
-> select mm;
-> end //
Query OK, 0 rows affected (0.12 sec)
mysql> delimiter ;
mysql> call t1();
+------+
| mm |
+------+
| 5 |
+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> set @mm=3;
Query OK, 0 rows affected (0.00 sec)
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.39 sec)
mysql> delimiter //
mysql> create procedure t1()
-> begin
-> declare mm int;
-> select @mm;
-> set mm=5;
-> select mm;
-> end //
Query OK, 0 rows affected (0.07 sec)
mysql> delimiter ;
mysql> call t1();
+------+
| @mm |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
+------+
| mm |
+------+
| 5 |
+------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
# 注:{set @mm=3}为用户变量,用户范围内生效;{declare mm int; set mm=10}为局部变量,仅在存储过程内生效
if 条件 then
SQL语句;
elseif 条件 then
SQL语句;
else SQL语句;
end if;
mysql> desc mm2;
+-------+-------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------------------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| score | int | YES | | NULL | |
| level | enum(‘优秀‘,‘及格‘,‘不及格‘) | YES | | NULL | |
+-------+-------------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.53 sec)
mysql> delimiter //
mysql> create procedure t1(name varchar(10),sc int)
-> begin
-> if sc>100 or sc<0 then
-> select ‘请输入正确的分数‘;
-> elseif sc>=80 then
-> insert into mm2 values (name,sc,‘优秀‘);
-> elseif sc>=60 then
-> insert into mm2 values (name,sc,‘及格‘);
-> else
-> insert into mm2 values (name,sc,‘不及格‘);
-> end if;
-> end //
Query OK, 0 rows affected (0.08 sec)
mysql> delimiter ;
mysql> call t1(‘张三‘,‘85‘);
Query OK, 1 row affected, 1 warning (0.11 sec)
mysql> call t1(‘李四‘,‘77‘);
Query OK, 1 row affected (0.08 sec)
mysql> call t1(‘王五‘,‘52‘);
Query OK, 1 row affected (0.40 sec)
mysql> call t1(‘孙悟空‘,‘220‘);
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> call t1(‘猪八戒‘,‘-22‘);
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select * from mm2;
+--------+-------+-----------+
| name | score | level |
+--------+-------+-----------+
| 张三 | 85 | 优秀 |
| 李四 | 77 | 及格 |
| 王五 | 52 | 不及格 |
+--------+-------+-----------+
3 rows in set (0.00 sec)
case 变量
when 值1 then
SQL语句;
when 值2 then
SQL语句;
else
SQL语句;
end case;
case
when 条件1 then
SQL语句;
when 条件2 then
SQL语句;
···
end case;
mysql> drop procedure if exists t1;
Query OK, 0 rows affected, 1 warning (0.33 sec)
mysql> delimiter //
mysql> create procedure t1(name varchar(10),sc int)
-> begin
-> case
-> when sc>100 or sc<0 then
-> select ‘请输入正确的分数‘;
-> when sc>=80 then
-> insert into mm2 values (name,sc,‘优秀‘);
-> when sc>=60 then
-> insert into mm2 values (name,sc,‘及格‘);
-> else
-> insert into mm2 values (name,sc,‘不及格‘);
-> end case;
-> end //
Query OK, 0 rows affected (0.10 sec)
mysql> delimiter ;
mysql> call t1(‘杨过‘,‘15‘);
Query OK, 1 row affected, 1 warning (0.39 sec)
mysql> call t1(‘梅超风‘,‘75‘);
Query OK, 1 row affected (0.12 sec)
mysql> call t1(‘郭靖‘,‘99‘);
Query OK, 1 row affected (0.07 sec)
mysql> call t1(‘小李‘,‘103‘);
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> call t1(‘天天‘,‘-1‘);
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
| 请输入正确的分数 |
+--------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
mysql> select * from mm2;
+-----------+-------+-----------+
| name | score | level |
+-----------+-------+-----------+
| 杨过 | 15 | 不及格 |
| 梅超风 | 75 | 及格 |
| 郭靖 | 99 | 优秀 |
+-----------+-------+-----------+
3 rows in set (0.00 sec)
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.52 sec)
mysql> delimiter //
mysql> create procedure t1(name varchar(10),sc int)
-> begin
-> case sc
-> when ‘0‘ then
-> insert into mm2 values (name,sc,‘退学‘);
-> when ‘99‘ then
-> insert into mm2 values (name,sc,‘很棒‘);
-> when ‘100‘ then
-> insert into mm2 values (name,sc,‘最棒‘);
-> end case;
-> end //
Query OK, 0 rows affected (0.15 sec)
mysql> delimiter ;
mysql> call t1(‘大蛇丸‘,‘0‘);
Query OK, 1 row affected, 1 warning (0.09 sec)
mysql> call t1(‘自来也‘,‘99‘);
Query OK, 1 row affected (0.42 sec)
mysql> call t1(‘纲手‘,‘100‘);
Query OK, 1 row affected (0.09 sec)
mysql> call t1(‘小樱‘,‘80‘);
ERROR 1339 (20000): Case not found for CASE statement
mysql> select * from mm2;
+-----------+-------+-----------+
| name | score | level |
+-----------+-------+-----------+
| 大蛇丸 | 0 | 退学 |
| 自来也 | 99 | 很棒 |
| 纲手 | 100 | 最棒 |
+-----------+-------+-----------+
6 rows in set (0.00 sec)
while 条件 do
SQL语句;
end while;
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.12 sec)
mysql> delimiter //
mysql> create procedure t1()
-> begin
-> declare i int default 1;
-> while i<=10 do
-> insert into mm1 values (i);
-> set i=i+1;
-> end while;
-> end //
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter ;
mysql> call t1();
Query OK, 1 row affected (0.90 sec)
mysql> select * from mm1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
10 rows in set (0.00 sec)
repeat
SQL语句;
until 条件
end repeat;
mysql> drop procedure if exists t1;
Query OK, 0 rows affected (0.15 sec)
mysql> delimiter //
mysql> create procedure t1()
-> begin
-> declare i int default 1;
-> repeat
-> insert into mm1 values (i);
-> set i=i+1;
-> until i>10
-> end repeat;
-> end //
Query OK, 0 rows affected (0.22 sec)
mysql> delimiter ;
mysql> call t1();
Query OK, 1 row affected (0.62 sec)
mysql> select * from mm1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
10 rows in set (0.00 sec)
loop
标签名: loop
SQL语句;
if 条件 then
leave 标签名;
end if;
end loop 标签名;
# 注:loop循环无法跳出循环,需借助{if…then…leave}跳出
mysql> drop procedure if exists t1;
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> delimiter //
mysql> create procedure t1()
-> begin
-> declare i int default 1;
-> bbc: loop
-> insert into mm1 values (i);
-> set i=i+1;
-> if i>10 then
-> leave bbc;
-> end if;
-> end loop bbc;
-> end //
Query OK, 0 rows affected (0.07 sec)
mysql> delimiter ;
mysql> call t1();
Query OK, 1 row affected (0.68 sec)
mysql> select * from mm1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+------+
10 rows in set (0.00 sec)
mysql> delimiter //
mysql> create procedure in_param(in p_in int)
-> begin
-> select p_in;
-> set p_in=2;
-> select P_in;
-> end //
mysql> delimiter ;
mysql> set @p_in=1;
mysql> call in_param(@p_in);
+------+
| p_in |
+------+
| 1 |
+------+
+------+
| P_in |
+------+
| 2 |
+------+
mysql> select @p_in;
+-------+
| @p_in |
+-------+
| 1 |
+-------+
mysql> delimiter //
mysql> create procedure out_param(out p_out int)
-> begin
-> select p_out;
-> set p_out=2;
-> select p_out;
-> end
-> //
mysql> delimiter ;
mysql> set @p_out=1;
mysql> call out_param(@p_out);
+-------+
| p_out |
+-------+
| NULL |
+-------+
#因为out是向调用者输出参数,不接收输入的参数,所以存储过程里的p_out为null
+-------+
| p_out |
+-------+
| 2 |
+-------+
mysql> select @p_out;
+--------+
| @p_out |
+--------+
| 2 |
+--------+
mysql> delimiter //
mysql> create procedure inout_param(inout p_inout int)
-> begin
-> select p_inout;
-> set p_inout=2;
-> select p_inout;
-> end //
mysql> delimiter ;
mysql> set @p_inout=1;
mysql> call inout_param(@p_inout);
+---------+
| p_inout |
+---------+
| 1 |
+---------+
+---------+
| p_inout |
+---------+
| 2 |
+---------+
mysql> select @p_inout;
+----------+
| @p_inout |
+----------+
| 2 |
+----------+
CREATE PROCEDURE zichan(in i_type varchar(20))
begin
declare s_type varchar(20);
declare s_name varchar(20);
declare s_money float;
select t2.type,t3.name,t1.money into s_type,s_name,s_money from t_zichan t1
left join t_zichan_type t2
on t1.type = t2.id
left join t_renyuan t3
on t1.owner = t3.id
where t2.type like concat(‘%‘,i_type,‘%‘);
select s_type,s_name,s_money;
end
标签:into rop 孙悟空 row rom erro float roc 猪八戒
原文地址:https://www.cnblogs.com/ly447742/p/14176697.html