drop procedure if exists dcs;#增强健壮性,如果dcs这个存储过程存在则删除
create procedure dcs(n int) #创建 一个存储过程,名字叫dcs,n就是形式参数,数据类型为int整型
begin #存储过程的开始
#存储过程的函数体,也可以理解为SQL语句的集合
declare i int(20); #default (select count(*) from qq); #declare 定义一个变量,数据类型为int
drop table if exists qq; #当qq表已经存在,先删除qq这个表
create table qq(id int(20) primary key auto_increment,score int(20));
insert into qq values(1,99),(2,65);
insert into qq values(3,23),(4,98);
insert into qq values(5,66),(6,77);
insert into qq values(7,88),(8,49);
select * from qq where id=n; #带参数进行查询操作
set i= (select count(*) from qq);
/*if n=0 then
select count(*) from qq;
else
select max(score) from qq;
end if;*/
/*if n=0 then
select count(*) from qq;
else if n>0 && n<i then
select max(score) from qq;
else if n>=i then
select * from qq order by score desc;
else
select * from qq;
end if;
end if;
end if;
end
call dcs(8)*/
if n=0 then
select count(*) from qq;
else if n>0 && n<i then
select max(score) from qq;
else
while n>i do
insert into qq(score) values(66);
set i=i+1;
end while;
end if;
end if;
select * from qq;
end #存储过程的结束
call dcs(150);
call dcs(2) #调用dcs()这个存储过程,注意,创建跟调用不能同时选中执行,先执行创建在执行调用
#在创建存储过程的时候设置了参数,调用是必须传实参,否则会报错
create table user(id int(20) primary key ,user_name varchar(20),user_pwd int(20),verify varchar(10));
create procedure dcs1() #创建 一个存储过程,名字叫dcs
begin # 存储过程的开始
#存储过程的函数体,也可以理解为SQL语句的集合
declare i int(20) default (select count(*) from user);
declare g varchar(20) default '';
declare h varchar(20) default '';
declare a varchar(20) default '123456';
if i>=1000 then
select count(*) from user;
else
while i<1000 do
set g=(select concat('user',i));
set h=(select substring(md5(rand()),1,6));
insert into user values(i,g,a,h);
set i=i+1;
end while;
end if;
end # 存储过程的结束
call dcs1() 调用dcs()这个存储过程,注意,创建跟调用不能同时选中执行,先执行创建在执行调用
delete from user
select * from user
|
|