1.基本语法:call sp_name()
create procedure 存储过程名字()
(
[in|out|inout] 参数 datatype
)
begin
MySQL 语句;
end;
MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。
drop procedure sp_name//
1.区块定义,常用
begin
......
end;
也可以给区块起别名,如:
lable:begin
...........
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句
if 条件 then
statement
else
statement
end if;
3.循环语句
(1).while循环
[label:] WHILE expression DO
statements
END WHILE [label] ;
(2).loop循环
[label:] LOOP
statements
END LOOP [label];
(3).repeat until循环
[label:] REPEAT
statements
UNTIL expression
END REPEAT [label] ;
1.show procedure status
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息
附
create procedure 存储过程名字()
(
[in|out|inout] 参数 datatype
)
begin
MySQL 语句;
end;
MySQL 存储过程中的变量,不需要在变量名字前加“@”,虽然 MySQL 客户端用户变量要加个“@”。
drop procedure sp_name//
1.区块定义,常用
begin
......
end;
也可以给区块起别名,如:
lable:begin
...........
end lable;
可以用leave lable;跳出区块,执行区块以后的代码
2.条件语句
if 条件 then
statement
else
statement
end if;
3.循环语句
(1).while循环
[label:] WHILE expression DO
statements
END WHILE [label] ;
(2).loop循环
[label:] LOOP
statements
END LOOP [label];
(3).repeat until循环
[label:] REPEAT
statements
UNTIL expression
END REPEAT [label] ;
1.show procedure status
显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等
2.show create procedure sp_name
显示某一个存储过程的详细信息
附
drop procedure if exists pr_statis_rows;
create procedure pr_statis_rows(
db1 varchar(50),
db2 varchar(50)
)
begin
DECLARE done INT DEFAULT 0;
declare tempTableName varchar(50);
declare stmt varchar(2000);
DECLARE cur CURSOR FOR select table_name from INFORMATION_SCHEMA.TABLES where table_type='BASE TABLE' and table_schema=db1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
create TEMPORARY table IF NOT EXISTS ecard_row (table_name varchar(50), rows_ int, table_name2 varchar(50), rows_2 int);
-- 打开
/* select db1; */
# 打印db1
OPEN cur;
REPEAT
FETCH cur INTO tempTableName;
if tempTableName<>'ecard_row' then
IF NOT done THEN
set @c1=0;
set @c2=0;
set @sql = concat('select count(*) from ', db1 ,'.', tempTableName, ' into @c1');
set @sql2 = concat('select count(*) from ', db2 ,'.', tempTableName, ' into @c2');
prepare stmt from @sql;
execute stmt;
prepare stmt from @sql2;
execute stmt;
Insert ecard_row values (tempTableName, @c1, tempTableName, @c2);
END IF;
end if;
UNTIL done END REPEAT;
CLOSE cur;
# 不能有return
end;
delete from ecard.ecard_row;
# 不能省略参数
call ecard.pr_statis_rows('aa','bb');
select * from ecard.ecard_row
show procedure status