Create table student
(
Id varchar(255), #学号
Name varchar(255), #姓名
Roomid varchar(255), #班级
Sex char(1), #性别
Degree varchar(3) #成绩
);
Insert into student values('1','amy','8','0','65');
Insert into student values('2','bob','4','1','80');
Insert into student values('3','candy','6','0','70');
Insert into student values('4','danny','8','1','90');
Insert into student values('5','edward','8','1','95');
Insert into student values('6','frank','7','1','100');
1:创建一个存储过程,实现查询表student中的记录信息,并执行存储过程
delimiter //
drop procedure if exists aaaa;
CREATE procedure aaaa()
begin
SELECT * from student;
END
//
call aaaa()
2:创建一个存储过程,根据给定的学生学号返回该学生的姓名
delimiter //
drop procedure if EXISTS bbbb;
CREATE procedure bbbb(in n int)
begin
SELECT `Name` from student where Id=n;
END
//
call bbbb(1)
3:创建一个存储过程,根据所给的班级的编号,统计该班人数,并将人数以输出变量返回给用户。
delimiter//
-- drop PROCEDURE if EXISTS stu_count;
-- CREATE PROCEDURE stu_count(in rid INT,OUT cb int )
-- BEGIN
-- SELECT COUNT(room_id) INTO cb FROM student3 WHERE room_id = rid;
-- end
-- //
--
-- SET @rid = 8;
-- call stu_count(@rid,@cb);
--SELECT @cb;
4:创建一个存储过程,根据所给的学号查询该学生的考试分数是否超过了85分, 若超过则输出“ 考得好”, 否则输出 “考得不好”。
delimiter//
-- drop PROCEDURE if EXISTS kao;
-- CREATE PROCEDURE kao(in s_no INT )
-- BEGIN
-- DECLARE i int ;
-- SELECT degree INTO i FROM student3 WHERE id = s_no;
-- if i > 85 THEN
-- SELECT "考的好";
-- else
-- SELECT "考得不好";
-- end if;
-- end
-- //
call kao(2)
delimiter//
create PROCEDURE gg (in n varchar(255))
BEGIN
declare i VARCHAR(3) default 85;
declare m VARCHAR(3);
set m:=(select Degree from student where Id=n);
if m>i THEN
select "考得好";
ELSE
select "考得不好";
end IF;
END
////
-- -- delimiter //
-- -- drop procedure if exists d;
-- -- create procedure d(in n int,out m int)
-- -- begin
-- -- select Degree into m from student001 where Id=n;
-- -- if m>=85 THEN
-- -- select '考得好';
-- -- else
-- -- select '考得不好';
-- -- end if;
-- -- end
-- -- //
call d(6,@m);
-- delimiter //
-- drop procedure if exists d;
-- create procedure d(in n int,out m int)
-- begin
-- select Degree into m from student001 where Id=n;
-- select if( m>85 ,'考得好','考的不好');
-- end
-- //
call d(2,@m);
实验5:创建一个存储过程,对表student增加两条记录。
delimiter //
drop procedure if EXISTS eeee;
CREATE procedure eeee(in n int)
BEGIN
insert into student VALUES(n,n,n,n,n);
insert into student VALUES(n,n,n,n,n);
END
//
call eeee(1);
SELECT * from student
6:请撰写一个存储过程,输出各班学生的平均成绩。
delimiter //
drop procedure if EXISTS ffff;
CREATE procedure ffff()
begin
SELECT Roomid,avg(Degree) from student GROUP BY Roomid;
END
//
CALL ffff()
7:请撰写一个存储过程,能根据用户输入的学号,输出学生的姓名、性别到两个参数变量中,以供其它程序使用。
delimiter //
drop procedure if exists gg;
create procedure gg(in n int,out m varchar(255),out l varchar(255))
BEGIN
select name into m from student where id=n;
select sex into l from student where id=n;
END
//
call gg(1,@m,@l);
select @m,@l
8:撰写一个存储过程,根据用户输入的学号,输出学生的姓名、年龄两个参数到新表temp中Create table temp(Sex varchar(255),Degree varchar(255));
#create table temp(sex varchar(255),degree varchar(255));
#SELECT * FROM temp ;
-- delimiter //
-- drop procedure if exists hh;
-- create procedure hh(in n int)
-- BEGIN
-- insert into temp select sex,degree from student001 where id=n ;
-- END//
#call hh(4)
#select * from temp ;
delimiter //
drop procedure if exists hh;
create procedure hh(in n int,out a char(20), out b char(20))
BEGIN
select sex ,degree into a,b from student001 where id=n ;
insert into temp (sex,degree)values (a,b) ;
END//
call hh(2,@a,@b);
select @a,@b;
select * from temp ;
--
9:请撰写一个存储过程,求1+2+3+…x的值。
delimiter //
drop procedure if exists iiii;
create procedure iiii(in n int)
begin
declare a int;
set a=(n+1)*n/2;
SELECT a;
END
//