[sql]存储过程procedure

- 创建存储过程
create procedure proc_c1()
begin
	select * from man;
end

- 调用存储过程
call proc_c1();

- 一般不修改,比较麻烦
drop procedure if EXISTS proc_p1;
create procedure proc_c1()
begin
	select * from man;
end


创建存储过程的过程

drop procedure if EXISTS proc_p1
create procedure proc_p1(
	in i1 int
)
begin
	declare d1 int;     # 终端会报错, 因为sql遇到分号即视为sql终结.  所以用 delimiter 来包裹sql块.
	declare d2 int default 1;
	set d1=i1+d2;
	select * from relationship where nid > d1;
end
delimiter $$
drop procedure if EXISTS proc_p1
create procedure proc_p1(
	in i1 int
)
begin
	declare d1 int;
	declare d2 int default 1;
	set d1=i1+d2;
	select * from relationship where nid > d1;
end $$
delimiter $$ # 设置delimiter为$$
drop procedure if EXISTS proc_p1$$ # 存储过程从$$开始到$$结束, 这句不能算在proc里
create procedure proc_p1(
	in i1 int
)
begin
	declare d1 int;
	declare d2 int default 1;
	set d1=i1+d2;
	select * from relationship where nid > d1;
end $$ 
delimiter ; # 恢复delimiter的值
delimiter $$
drop procedure if EXISTS proc_p1 $$
create procedure proc_p1(
	in i1 int
)
begin
	declare d1 int;
	declare d2 int default 1;
	set d1=i1+d2;
	select * from relationship where nid > d1;
end $$
delimiter ;

posted @ 2018-04-17 14:29  mmaotai  阅读(166)  评论(0编辑  收藏  举报