下面是一个使用OUT参数的简单的存储程序的例子。例子为,在 程序被定义的时候,用mysql客户端delimiter命令来把语句定界符从 ;变为//。这就允许用在 程序体中的;定界符被传递到服务器而不是被mysql自己来解释。
当使用delimiter命令时,你应该避免使用反斜杠(‘\’)字符,因为那是MySQL的 转义字符。
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2012/8/19 8:45:30 */
/*==============================================================*/
drop table if exists student;
/*==============================================================*/
/* Table: student */
/*==============================================================*/
create table student
(
no varchar(10) not null,
name varchar(10),
height float,
primary key (no)
);
delete from student;
insert student(no, name, height) values("123654", "zhangsan", 1.56);
insert student(no, name, height) values("129854", "lisi", 1.16);
insert student(no, name, height) values("122654", "wangwu", 1.36);
select * from student;
+--------+----------+--------+
| no | name | height |
+--------+----------+--------+
| 122654 | wangwu | 1.36 |
| 123654 | zhangsan | 1.56 |
| 129854 | lisi | 1.16 |
+--------+----------+--------+
delimiter //
create procedure simpleproc (out outParam int)
begin
select count(*) into outParam from student;
end
//
delimiter ;
call simpleproc(@a);
select @a;
+------+
| @a |
+------+
| 3 |
+------+