1.function
1)创建
SQL> create or replace function f_score(p_stud varchar2,p_sub varchar2) -- 这里是定义了两个数分别为p_stud和p_sub
return varchar2
as -- AS后面的两个数v_score 与 v_rslt与p_stud和p_sub是对应还是一个什么意思?还赋值=0;还有后后一个10是什么意思?
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score -- 这个也是插进数据
where stud_no=p_stud
and sub_no=p_sub
and rownum<2; --为什么这里会限制一行?
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
/
2)使用
SQL> select f_score('201201010103','00003') from dual; -- 这样看函数主要是用于查询?
2.procedure
1)创建
SQL> create or replace procedure p_score(p_sub_no varchar2,p_stud_no in
varchar2,p_score out number) --p_score out number这个是什么意思?
as
begin
select score into p_score from t_score --怎么感觉过程和函数比较类似,看不出区别;都是插进数据;
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
/
2)使用
SQL> var v_score number; -- 这个是显示者分数?
SQL> exec p_score(‘00001’,’201201010101’,:v_score);
SQL> print v_score;
3.trigger
1)创建
SQL> create or replace trigger bir_t_stud
before insert on t_stud
for each row
declare
v_cnt int;
begin
select count(*) into v_cnt --任然是插进一个数据?与函数和过程类似?
from t_teach
where teach_no=:new.teach_no
and rownum<2;
if v_cnt=0 then
raise_application_error(-20000,'没找到班主任信息!');
return;
end if; --表示V_CNT不等于0时,然后插入值?
end bir_t_stud;
/
2)使用(触发)
SQL> insert into t_stud
values('201201010104','rose',17,'2012010105','00013'); -- 可以随便插入很多个值?
4.package
1)创建
SQL> create or replace package pkg_score is -- 包其实包含了函数和过程?
v_glbl number;
function f_score(p_stud varchar2,p_sub varchar2) return varchar2;
PROCEDURE p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score
out number);
end pkg_score;
/
SQL> create or replace package body pkg_score is
function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
procedure p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score out
number)
as
begin
select score into p_score
from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
end pkg_score;
/
2)使用
SQL> select pkg_score.f_score('201201010101','00001') from dual;
5.type
SQL> declare
type tp_stud is table of t_stud%rowtype;
tb_stud tp_stud; --这里为什么有两个tp_stud[/quote]
不好意思,最近没来,才看到。
楼主看错了,两个比较相像,tb_stud为定义的变量,tp_stud为变量类型。就是定义一个tp_stud类型的变量tb_stud。[/quote]
谢谢。[/quote]
不好意思,我只看到了最后一个问题,前面的没看到,才看到,似乎还不少,有时间了,我回答下。
1.function
1)创建
SQL> create or replace function f_score(p_stud varchar2,p_sub varchar2) -- 这里是定义了两个数分别为p_stud和p_sub
return varchar2
as -- AS后面的两个数v_score 与 v_rslt与p_stud和p_sub是对应还是一个什么意思?还赋值=0;还有后后一个10是什么意思?
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score -- 这个也是插进数据
where stud_no=p_stud
and sub_no=p_sub
and rownum<2; --为什么这里会限制一行?
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
/
2)使用
SQL> select f_score('201201010103','00003') from dual; -- 这样看函数主要是用于查询?
2.procedure
1)创建
SQL> create or replace procedure p_score(p_sub_no varchar2,p_stud_no in
varchar2,p_score out number) --p_score out number这个是什么意思?
as
begin
select score into p_score from t_score --怎么感觉过程和函数比较类似,看不出区别;都是插进数据;
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
/
2)使用
SQL> var v_score number; -- 这个是显示者分数?
SQL> exec p_score(‘00001’,’201201010101’,:v_score);
SQL> print v_score;
3.trigger
1)创建
SQL> create or replace trigger bir_t_stud
before insert on t_stud
for each row
declare
v_cnt int;
begin
select count(*) into v_cnt --任然是插进一个数据?与函数和过程类似?
from t_teach
where teach_no=:new.teach_no
and rownum<2;
if v_cnt=0 then
raise_application_error(-20000,'没找到班主任信息!');
return;
end if; --表示V_CNT不等于0时,然后插入值?
end bir_t_stud;
/
2)使用(触发)
SQL> insert into t_stud
values('201201010104','rose',17,'2012010105','00013'); -- 可以随便插入很多个值?
4.package
1)创建
SQL> create or replace package pkg_score is -- 包其实包含了函数和过程?
v_glbl number;
function f_score(p_stud varchar2,p_sub varchar2) return varchar2;
PROCEDURE p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score
out number);
end pkg_score;
/
SQL> create or replace package body pkg_score is
function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
procedure p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score out
number)
as
begin
select score into p_score
from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
end pkg_score;
/
2)使用
SQL> select pkg_score.f_score('201201010101','00001') from dual;
5.type
SQL> declare
type tp_stud is table of t_stud%rowtype;
tb_stud tp_stud; --这里为什么有两个tp_stud[/quote]
不好意思,最近没来,才看到。
楼主看错了,两个比较相像,tb_stud为定义的变量,tp_stud为变量类型。就是定义一个tp_stud类型的变量tb_stud。[/quote]
谢谢。
1.function
1)创建
SQL> create or replace function f_score(p_stud varchar2,p_sub varchar2) -- 这里是定义了两个数分别为p_stud和p_sub
return varchar2
as -- AS后面的两个数v_score 与 v_rslt与p_stud和p_sub是对应还是一个什么意思?还赋值=0;还有后后一个10是什么意思?
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score -- 这个也是插进数据
where stud_no=p_stud
and sub_no=p_sub
and rownum<2; --为什么这里会限制一行?
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
/
2)使用
SQL> select f_score('201201010103','00003') from dual; -- 这样看函数主要是用于查询?
2.procedure
1)创建
SQL> create or replace procedure p_score(p_sub_no varchar2,p_stud_no in
varchar2,p_score out number) --p_score out number这个是什么意思?
as
begin
select score into p_score from t_score --怎么感觉过程和函数比较类似,看不出区别;都是插进数据;
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
/
2)使用
SQL> var v_score number; -- 这个是显示者分数?
SQL> exec p_score(‘00001’,’201201010101’,:v_score);
SQL> print v_score;
3.trigger
1)创建
SQL> create or replace trigger bir_t_stud
before insert on t_stud
for each row
declare
v_cnt int;
begin
select count(*) into v_cnt --任然是插进一个数据?与函数和过程类似?
from t_teach
where teach_no=:new.teach_no
and rownum<2;
if v_cnt=0 then
raise_application_error(-20000,'没找到班主任信息!');
return;
end if; --表示V_CNT不等于0时,然后插入值?
end bir_t_stud;
/
2)使用(触发)
SQL> insert into t_stud
values('201201010104','rose',17,'2012010105','00013'); -- 可以随便插入很多个值?
4.package
1)创建
SQL> create or replace package pkg_score is -- 包其实包含了函数和过程?
v_glbl number;
function f_score(p_stud varchar2,p_sub varchar2) return varchar2;
PROCEDURE p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score
out number);
end pkg_score;
/
SQL> create or replace package body pkg_score is
function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
procedure p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score out
number)
as
begin
select score into p_score
from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
end pkg_score;
/
2)使用
SQL> select pkg_score.f_score('201201010101','00001') from dual;
5.type
SQL> declare
type tp_stud is table of t_stud%rowtype;
tb_stud tp_stud; --这里为什么有两个tp_stud[/quote]
1.function
1)创建
SQL> create or replace function f_score(p_stud varchar2,p_sub varchar2) -- 这里是定义了两个数分别为p_stud和p_sub
return varchar2
as -- AS后面的两个数v_score 与 v_rslt与p_stud和p_sub是对应还是一个什么意思?还赋值=0;还有后后一个10是什么意思?
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score -- 这个也是插进数据
where stud_no=p_stud
and sub_no=p_sub
and rownum<2; --为什么这里会限制一行?
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
/
2)使用
SQL> select f_score('201201010103','00003') from dual; -- 这样看函数主要是用于查询?
2.procedure
1)创建
SQL> create or replace procedure p_score(p_sub_no varchar2,p_stud_no in
varchar2,p_score out number) --p_score out number这个是什么意思?
as
begin
select score into p_score from t_score --怎么感觉过程和函数比较类似,看不出区别;都是插进数据;
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
/
2)使用
SQL> var v_score number; -- 这个是显示者分数?
SQL> exec p_score(‘00001’,’201201010101’,:v_score);
SQL> print v_score;
3.trigger
1)创建
SQL> create or replace trigger bir_t_stud
before insert on t_stud
for each row
declare
v_cnt int;
begin
select count(*) into v_cnt --任然是插进一个数据?与函数和过程类似?
from t_teach
where teach_no=:new.teach_no
and rownum<2;
if v_cnt=0 then
raise_application_error(-20000,'没找到班主任信息!');
return;
end if; --表示V_CNT不等于0时,然后插入值?
end bir_t_stud;
/
2)使用(触发)
SQL> insert into t_stud
values('201201010104','rose',17,'2012010105','00013'); -- 可以随便插入很多个值?
4.package
1)创建
SQL> create or replace package pkg_score is -- 包其实包含了函数和过程?
v_glbl number;
function f_score(p_stud varchar2,p_sub varchar2) return varchar2;
PROCEDURE p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score
out number);
end pkg_score;
/
SQL> create or replace package body pkg_score is
function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
procedure p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score out
number)
as
begin
select score into p_score
from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
end pkg_score;
/
2)使用
SQL> select pkg_score.f_score('201201010101','00001') from dual;
5.type
SQL> declare
type tp_stud is table of t_stud%rowtype;
tb_stud tp_stud; --这里为什么有两个tp_stud
就给你贴上我写的一本书中的例子吧。
1.function
1)创建
SQL> create or replace function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
/
2)使用
SQL> select f_score('201201010103','00003') from dual;
2.procedure
1)创建
SQL> create or replace procedure p_score(p_sub_no varchar2,p_stud_no in
varchar2,p_score out number)
as
begin
select score into p_score from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
/
2)使用
SQL> var v_score number;
SQL> exec p_score(‘00001’,’201201010101’,:v_score);
SQL> print v_score;
3.trigger
1)创建
SQL> create or replace trigger bir_t_stud
before insert on t_stud
for each row
declare
v_cnt int;
begin
select count(*) into v_cnt
from t_teach
where teach_no=:new.teach_no
and rownum<2;
if v_cnt=0 then
raise_application_error(-20000,'没找到班主任信息!');
return;
end if;
end bir_t_stud;
/
2)使用(触发)
SQL> insert into t_stud
values('201201010104','rose',17,'2012010105','00013');
4.package
1)创建
SQL> create or replace package pkg_score is
v_glbl number;
function f_score(p_stud varchar2,p_sub varchar2) return varchar2;
PROCEDURE p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score
out number);
end pkg_score;
/
SQL> create or replace package body pkg_score is
function f_score(p_stud varchar2,p_sub varchar2)
return varchar2
as
v_score number :=0;
v_rslt varchar2(10);
begin
select score into v_score from t_score
where stud_no=p_stud
and sub_no=p_sub
and rownum<2;
if v_score>=60 then
v_rslt:='及格';
else
v_rslt:='不及格';
end if;
return v_rslt;
end;
procedure p_score(p_sub_no varchar2,p_stud_no in varchar2,p_score out
number)
as
begin
select score into p_score
from t_score
where sub_no=p_sub_no and stud_no=p_stud_no;
end;
end pkg_score;
/
2)使用
SQL> select pkg_score.f_score('201201010101','00001') from dual;
5.type
SQL> declare
type tp_stud is table of t_stud%rowtype;
tb_stud tp_stud;