Mysql的儲存過程的使用方法

2021-10-24 16:44:25 字數 2631 閱讀 1405

create

procedure 儲存過程名(引數列表)

begin

儲存過程體(一組合法的sql語句)

end

in stuname varchar(20

)

delimiter 結束標記
delimiter $
call 儲存過程名(實參列表)

;

select

*from admin;

delimiter $

create

procedure myp1(

)begin

insert

into admin(username,

`password`

)values

('john1'

,'0000'),

('lily'

,'0000'),

('rose'

,'0000'),

('jack'

,'0000'),

('tom'

,'0000');

end $

#呼叫call myp1(

)$

create

procedure myp2(

in beautyname varchar(20

))begin

select bo.

*from boys bo

right

join beauty b on bo.id = b.boyfriend_id

where b.name=beautyname;

end $

#呼叫call myp2(

'柳岩'

)$

create

procedure myp4(

in username varchar(20

),in password varchar(20

))begin

declare result int

default0;

#宣告並初始化

select

count(*

)into result#賦值

from admin

where admin.username = username

and admin.password = password;

select

if(result>0,

'成功'

,'失敗');

#使用end $

#呼叫call myp3(

'張飛'

,'8888'

)$

create

procedure myp6(

in beautyname varchar(20

),out boyname varchar(20

))begin

select bo.boyname into boyname

from boys bo

right

join

beauty b on b.boyfriend_id = bo.id

where b.name=beautyname ;

end $

create

procedure myp7(

in beautyname varchar(20

),out boyname varchar(20

),out usercp int

)begin

select boys.boyname ,boys.usercp into boyname,usercp

from boys

right

join

beauty b on b.boyfriend_id = boys.id

where b.name=beautyname ;

end $

#呼叫call myp7(

'小昭'

,@name

,@cp

)$select

@name

,@cp$

create

procedure myp8(

inout a int

,inout b int

)begin

set a=a*2;

set b=b*2;

end $

#呼叫set@m=

10$set@n=

20$call myp8(@m,

@n)$

select@m,

@n$

drop

procedure p1;

drop

procedure p2,p3;

desc myp2;

show

create

procedure myp2;

使用mysql編寫儲存過程的使用方法

1.建立儲存過程 建立無參儲存過程。create procedure p cp begin select from user end 呼叫無參儲存過程 call p cp 在mybatis中呼叫儲存過程 建立有參 create procedure p cp in p unitid varchar 2...

儲存過程的使用方法

建表 create table tab cj id int identity 1,1 not null primary key,name varchar 50 age int null info varchar 200 建儲存過程 create procedure queryinfobyname n...

儲存過程 MySQL儲存過程的使用總結

案例所涉及到的表如下 create table t goods g id varchar 20 not null comment 商品編號 g name varchar 30 default null comment 商品名稱 g price float default null comment 商...