好记性不如烂笔头。
首先看看表student
注意每个操作是分开执行的,全部一起执行会出问题的。
drop procedure if exists myproc //如果存在名为myproc 的存储过程就删掉
create procedure myproc()//创建名为myproc 的存储过程
begin//开始
select * from student; //逻辑操作
end//结束
call myproc();//调用 myproc(),查询出所有的学生信息
添加一个学生
drop procedure if exists addStudent1
//参数有 in/输入 out/输出 inout/既可以输入也可以输出
create procedure addStudent1(in theIndex int ,in theName VARCHAR(10))
begin
declare v1 int ;
declare v2 VARCHAR(10) ;
set v1 = theIndex ;
set v2 = theName ;
select * from student where id = v1 and name = v2;
end
call addStudent1(1,'FX')
创建存储函数
CREATE FUNCTION NameByT()//创建名为NameByT的存储函数
RETURNS CHAR(20) //这个是指定的返回类型
RETURN (SELECT name FROM student WHERE id=1); //返回查出来的name
select NameByT()//调用存储函数
变量的使用
drop procedure if exists selectStudent
create procedure selectStudent(in theIndex int)
begin
declare v1 int ;
declare v2 VARCHAR(10);
set v1 = theIndex ;
set v1 = v1 + v1 ;
select * from student where id = v1 ;
end
call selectStudent(1)
调用存储函数
drop procedure if exists CountStudent
CREATE PROCEDURE CountStudent(OUT sname VARCHAR(50))
BEGIN
SELECT name INTO sname FROM student ;
END
call CountStudent(@snamea)
SELECT @sname