方案一:
create or replace procedure goods1(goodsname goodstype.typename%type)
as
p_typeid goodstype.typeid%type;
cursor mycur is select gid,gname,price,stock from goods where typeid=p_typeid;
goodsall mycur%rowtype;
begin
select typeid into p_typeid from goodstype where typename=goodsname;
open mycur;
loop
fetch mycur into goodsall;
exit when mycur%notfound;
dbms_output.put_line(goodsall.gid||' '||goodsall.gname);
end loop;
close mycur;
end;
--方法的调用:
declare
pname goodstype.typename%type:='&a';
begin
goods1(pname);
end;
方案二:
create or replace procedure my(v_name goodstype.typename%type)
is
cursor mycur is select g.* from goods g join goodstype gt on g.typeid=gt.typeid and typename=v_name;
message mycur%rowtype;
begin
open mycur;
loop
fetch mycur into message;
exit when mycur%notfound;
dbms_output.put_line(message.gid|| ' ' || message.gname);
end loop;
close mycur;
end;
--存储过程的调用
declare
n_name goodstype%type := '&a'; --因为读入的是字符串,所以这个读入的地方要加一对单引号
begin
my(n_name);
end;
方案三:(for循环)
create or replace procedure mydure(v_name goodstype.typename%type)
is
cursor mycur is select g.* from goods g join goodstype gt on d.typeid= gt.typeid and typename=v_name;
begin
for my in mycur loop
dbms_output.put_line(my.gid|| ' ' || my.gname);
end loop;
end;
我个人是特别喜欢使用for循环的,因为for循环不需要打开关闭游标,不需要声明记录类型的变量,也不需要fetch也不需要exit,是及其的方便。
create or replace procedure mydure(dno student.department%type)
is
cursor mycur is select sid,sname,sex,department from student where department=dno;
message mycur%rowtype;
begin
open mycur;
loop
fetch mycur into message;
exit when mycur%notfound;
dbms_output.put_line(message.sid || ' ' || message.sname || ' ' || message.sex || ' ' || message.department);
end loop;
close mycur;
end;
declare
v_dno student.department%type :='&dno';
begin
mydure(v_dno);
end;
方案2:
create or replace procedure mydure(dno student.department%type)
is
cursor mycur is select sid,sname,sex,department from student where department=dno;
begin
for my in mycur loop
dbms_output.put_line(my.sid||' ' ||my.sname||' '|| my.sex ||' ' ||my.department);
end loop;
end;
create or replace procedure mydure
is
cursor mycur is select student.sid,sname,sex,sc.cid,cname,grade,department from student,sc,c where student.sid=sc.sid and sc.cid=c.cid and grade<60;
message mycur%rowtype;
begin
open mycur;
loop
fetch mycur into message;
exit when mycur%notfound;
dbms_output.put_line('a' || message.sid|| ' ' || message.sname || ' ' || message.cid || ' ' || message.cname || ' ' || message.grade || ' ' || message.department);
end loop;
close mycur;
end;
--调用
begin
mydure;
end;
create or replace procedure mydure(v_cid sc.cid%type,v_score out sc.grade%type)
is
begin
select sum(grade) into v_score from sc where cid=v_cid;
end;
--调用
declare
p_cid sc.cid%type := &v_cid;
p_score sc.grade%type;
begin
mydure(v_cid=>p_cid,v_score=>p_score);
dbms_output.put_line(p_cid || '这一门课程的考试总分数为:' || p_score);
end;
create or replace function myfunc (v_cid sc.cid%type) return sc.grade%type
is
score sc.grade%type;
begin
select sum(grade) into score from sc where cid=v_cid;
return score;
end;
--调用
declare
c_cid sc.cid%type := &c_cid;
begin
dbms_output.put_line( c_cid || '这一门课程的考试总分数为:' || myfunc(c_cid));
end;
方案一:
create or replace procedure mydure(v_sid sc.sid%type)
is
v_avggrade sc.grade%type;
begin
select avg(grade) into v_avggrade from sc where sid=v_sid;
case
when v_avggrade>90 then
dbms_output.put_line('该生成绩优秀');
when v_avggrade>=80 then
dbms_output.put_line('该生成绩良好');
when v_avggrade>=60 then
dbms_output.put_line('该生成绩合格');
else
dbms_output.put_line('该生成绩不合格');
end case;
end;
--调用
declare
s_sid sc.sid%type := &sid;
begin
mydure(s_sid);
end;
方案二:
create or replace procedure mydure(v_sid sc.sid%type, v_avggrade out sc.grade%type;)
is
begin
select avg(grade) into v_avggrade from sc where sid=v_sid;
end;
--调用
declare
s_sid sc.sid%type := &sid;
s_avggrade sc.grade%type;
begin
mydure(v_sid=>s_sid,v_avggrade=>s_avggrade);
case
when s_avggrade>90 then
dbms_output.put_line('该生成绩优秀');
when s_avggrade>=80 then
dbms_output.put_line('该生成绩良好');
when s_avggrade>=60 then
dbms_output.put_line('该生成绩合格');
else
dbms_output.put_line('该生成绩不合格');
end case;
end;
(DML触发器,update,after,student,行级触发-----修改的是记录 :old、:new)
create or replace trigger mytrig after update on student
begin
if :new.sid!=:old.sid then
update sc set sid=:new.sid where sid=:old.sid;
end if;
end;
--验证
update student set sid=108 where sid=100;
商品表建表,点击我,通往建表
学生表建表,点击我,通往建表