【学习的点点滴滴】学了一段时间的pl/sql 这里Sinpoal将一些东西记录在这里头。首先我要写的是关于pl/sql 语句块的结构。所有的pl/sql块都会按这种结构来写:
-----pl/sql structure
declare
v_fenshu t8.fenshu%type;
v_name t8.name%type;
v_score t8.fenshu%type;
v_number constant number(3,2):=5.25;
begin
--执行体
select fenshu,name into v_fenshu ,v_name from t8 where name='&v_name'and chengji='&v_chengji';
v_score:=v_fenshu*v_number;
dbms_output.put_line(v_name||';'||v_fenshu);
dbms_output.put_line(v_score);
--异常处理
exception
when no_data_found then
dbms_output.put_line('please input the right values');
end;
--------接下来写一哈如何定义使用package--
--其实对它的理解我们可以用对c#中接口来理解的方式来理解package
我们在package中定义function 和 procedure ,在 package body 中实现。这一点接口有点共通之处。
----package-----------
create or replace package test_package
is
monthh number:=2;
function validate (v_month number) return boolean;
procedure add_data(monthh number,price number);
procedure fire_date(monthh number);
end test_package;
--------package body------
create or replace package body test_package
is
function validate (v_month number)
return boolean
is
v_int number;
begin
select 1 into v_int from test where month=v_month;
return false;
exception
when no_data_found then
return true;
end;
procedure add_data(monthh number,price number)
is
begin
if validate(monthh) then
insert into test values (monthh,price);
else
raise_application_error(-20010,'该月份已存在');
end if;
end;
procedure fire_date(monthh number)
is
begin
delete from test where month=monthh;
if sql%notfound then
raise_application_error(-20010,'数据不存在');
end if ;
end;
end test_package;
-----------call the package--------
--我们在调用package中的function 和procedures的时候。在 sql command的模式下用exec 调用
如:exec package_name.function_name or procedure_name.