mysql 存储过程

存储过程可以没有参数,也可以有参数:in,out,当参数为out是,out只能接受一行的数据,有多行数据时会报错。

参数为in:

drop PROCEDURE if exists p1;
create procedure p1(in names VARCHAR(10))
begin
select * from abc where s_name=names;
end;

call p1('张四');

当定义 names类型时为:char时,调用p1,查询结果为null;或者报错:[Err] 1406 - Data too long for column 'na' at row 342

 

有时通过直接使用select语句查询有数据,而通过调用存储过程却没有数据,并且有中文字符时,考虑database,table,字段的编码格式问题,可以通过下列语句查询修改:

show full columns from abc#查看每个字段的编码格式

show create database abc#查看数据库的编码格式
show create table abc#查看表的编码格式
alter table abc charset=utf8
alter database abc charset=utf8

 

参数为out:

drop PROCEDURE if exists p1;
create procedure p1(out na CHAR)
begin
select s_id into na from abc where s_name='张四' limit 1;
end;

call p1(@res);
select @res;

 

posted @ 2022-04-02 15:55  张测试  阅读(44)  评论(0)    收藏  举报