1、 PL/SQL的块结构
- Declare
- /*
- Declare section-PL/SQL variables,types,cursors,and local subprograms go here.
- */
- Begin
- /*
- Executable section-procedural and SQL statements go here.
- This is ths main section of the bloack and the only one that is required.
- */
- Exception
- /*
- Exception-handling section-error-handing statements go here
- */
- End;
Declare
/*
Declare section-PL/SQL variables,types,cursors,and local subprograms go here.
*/
Begin
/*
Executable section-procedural and SQL statements go here.
This is ths main section of the bloack and the only one that is required.
*/
Exception
/*
Exception-handling section-error-handing statements go here
*/
End;
注: 每个块的结尾都要用分号结束
如下
- declare
- fmajor varchar(50):='Chemistary';
- ffirstName varchar(50):='Fff';
- flastName varchar(50):='LB';
- begin
- update students
- set major=fmajor
- where first_name=ffirstName
- and last_name=flastName;
- if SQL%NOTFOUND THEN
- insert into students
- values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
- end if;
- end;
declare
fmajor varchar(50):='Chemistary';
ffirstName varchar(50):='Fff';
flastName varchar(50):='LB';
begin
update students
set major=fmajor
where first_name=ffirstName
and last_name=flastName;
if SQL%NOTFOUND THEN
insert into students
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
end;
2、 错误处理
- /**错误处理开始**/
- Declare
- v_ErrorCode number; --code for the error
- v_ErrorMsg varchar2(200); --Message text for the error
- v_CurrentUser varchar2(20) --Current database user
- v_Information varchar2(100) --Information about the error
- Begin
- /*Code that processes some data here*/
- Exception
- when others then
- --Assign values to the log variables,using built-in function
- v_ErrorCode:=SQLCODE;
- v_ErrorMsg:=SQLERRM;
- v_currentUser:=USER;
- V_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user' || v_CurrentUser;
- --Insert the log message into log_table.
- Insert into log_table(code,message,infro) values(v_ErrorCode,v_ErrorMsg,v_ErrorInfomation);
- end;
- /**错误处理结束**/
/**错误处理开始**/
Declare
v_ErrorCode number; --code for the error
v_ErrorMsg varchar2(200); --Message text for the error
v_CurrentUser varchar2(20) --Current database user
v_Information varchar2(100) --Information about the error
Begin
/*Code that processes some data here*/
Exception
when others then
--Assign values to the log variables,using built-in function
v_ErrorCode:=SQLCODE;
v_ErrorMsg:=SQLERRM;
v_currentUser:=USER;
V_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user' || v_CurrentUser;
--Insert the log message into log_table.
Insert into log_table(code,message,infro) values(v_ErrorCode,v_ErrorMsg,v_ErrorInfomation);
end;
/**错误处理结束**/
3、变量及类型
自带基本类型
用户自定义类型:
- Declare
- type t_StudentRecord is record(
- FirstName varchar2(50);
- LastName varchar2(50);
- CurrentCredits number(3);
- );
Declare
type t_StudentRecord is record(
FirstName varchar2(50);
LastName varchar2(50);
CurrentCredits number(3);
);
定义:v_Student t_StudentRecord;
4、过程
1)这里先介绍一个错误过程,也是很多初学者都会犯的问题
错误过程 1:
- CREATE or replace PROCEDURE changOrInsert AS
- Declare --<出错误之处。原因,受块结构的影响>
- fmajor students.major%type:='Chemistary';
- ffirstName students.first_name%type:='Fff';
- flastName students.last_name%type:='LB';
- begin
- update students set major=fmajor where first_name=ffirstName and last_name=flastName;
- end changOrInsert;
CREATE or replace PROCEDURE changOrInsert AS
Declare --<出错误之处。原因,受块结构的影响>
fmajor students.major%type:='Chemistary';
ffirstName students.first_name%type:='Fff';
flastName students.last_name%type:='LB';
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;
end changOrInsert;
错误过程 2:
- CREATE or replace PROCEDURE changOrInsert AS
- fmajor students.major%type:='Chemistary';
- ffirstName students.first_name%type:='Fff';
- flastName students.last_name%type:='LB';
- begin
- update students set major=fmajor where first_name=ffirstName and last_name=flastName;
- if SQL%NOTFOUND THEN
- insert into students
- values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
- end if;
- exception
- ; --<问题处。原因: 受其它开发语言如JAVA的影响>
- end changOrInsert;
CREATE or replace PROCEDURE changOrInsert AS
fmajor students.major%type:='Chemistary';
ffirstName students.first_name%type:='Fff';
flastName students.last_name%type:='LB';
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;
if SQL%NOTFOUND THEN
insert into students
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
exception
; --<问题处。原因: 受其它开发语言如JAVA的影响>
end changOrInsert;
错误过程3:
- create or replace procedure addNewColumn
- as
- firstID int:=10140;
- beginMark int:=65;
- loopTime int:=1;
- begin
create or replace procedure addNewColumn
as
firstID int:=10140;
beginMark int:=65;
loopTime int:=1;
begin
--仔细看下面这一条诏句。没错呀! 真的吗?( 就错在这里)
- alter table students add mark int;
- --execute immediate 'alter table students add mark1 int';( 这句才是对的)
- loop
- update students set mark=beginMark where id=firstID;
- firstID:=firstID+1;
- beginMark:=beginMark+1;
- loopTime:=loopTime+1;
- exit when loopTime>20;
- end loop;
- end addNewColumn;
alter table students add mark int;
--execute immediate 'alter table students add mark1 int';( 这句才是对的)
loop
update students set mark=beginMark where id=firstID;
firstID:=firstID+1;
beginMark:=beginMark+1;
loopTime:=loopTime+1;
exit when loopTime>20;
end loop;
end addNewColumn;
2)过程的基本结构:
- Create [or replace] procedure procedure_name [parameter_list] as
- /*Declarative section is here*/
- Begin
- /*Executable section is here*/
- Exception
- /*Exception is here*/
- End [procedure_name];
Create [or replace] procedure procedure_name [parameter_list] as
/*Declarative section is here*/
Begin
/*Executable section is here*/
Exception
/*Exception is here*/
End [procedure_name];
3) 一个完整的过程
- /*Create a Procedure*/
- CREATE or replace PROCEDURE changOrInsert(
- fmajor students.major%type,
- ffirstName students.first_name%type,
- flastName students.last_name%type) AS
- v_ErrorCode number;
- v_ErrorMsg varchar(200);
- v_currentUser varchar(50);
- v_Information varchar(500);
- begin
- update students set major=fmajor where first_name=ffirstName and last_name=flastName;
- if SQL%NOTFOUND THEN --if not found the record
- insert into students --then insert a new one
- values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
- end if;
- exception
- --write exception into table log_table
- when others then
- --Assign values to the log variables,using built-in function
- v_ErrorCode:=SQLCODE;
- v_ErrorMsg:=SQLERRM;
- v_currentUser:=USER;
- v_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user ' || v_CurrentUser;
- --Insert the log message into log_table.
- Insert into log_table(code,message,info) values(v_ErrorCode,v_ErrorMsg,v_Information);
- end changOrInsert;
/*Create a Procedure*/
CREATE or replace PROCEDURE changOrInsert(
fmajor students.major%type,
ffirstName students.first_name%type,
flastName students.last_name%type) AS
v_ErrorCode number;
v_ErrorMsg varchar(200);
v_currentUser varchar(50);
v_Information varchar(500);
begin
update students set major=fmajor where first_name=ffirstName and last_name=flastName;
if SQL%NOTFOUND THEN --if not found the record
insert into students --then insert a new one
values(student_sequence.NEXTVAL,ffirstName,flastName,fmajor);
end if;
exception
--write exception into table log_table
when others then
--Assign values to the log variables,using built-in function
v_ErrorCode:=SQLCODE;
v_ErrorMsg:=SQLERRM;
v_currentUser:=USER;
v_Information:='Error encountered on' || to_char(SYSDATE) || ' by database user ' || v_CurrentUser;
--Insert the log message into log_table.
Insert into log_table(code,message,info) values(v_ErrorCode,v_ErrorMsg,v_Information);
end changOrInsert;
4) 执行存储过程
- begin
- changOrInsert('Feng','LiBin','Computer');
- end;
begin
changOrInsert('Feng','LiBin','Computer');
end;
而不是像在SQL SERVER中的Call来调用
5、函数
--
6、包
1)写的第一个包, 花了不少的检查时间(有点类似于JAVA中的接口)
表的结构如下图:
- create or replace package manageStu as
- procedure addStu(firstname students.first_name%type,
- lastname students.last_name%type,
- majorm students.major%type,
- current_credits students.current_credits%type,
- markm students.mark%type);
- procedure delStu(stuID int);
- procedure modifyStu(idm int,
- firstname students.first_name%type,
- lastname students.last_name%type,
- majorm students.major%type,
- current_creditsm students.current_credits%type,
- markm students.major%type);
- end manageStu;
create or replace package manageStu as
procedure addStu(firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_credits students.current_credits%type,
markm students.mark%type);
procedure delStu(stuID int);
procedure modifyStu(idm int,
firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_creditsm students.current_credits%type,
markm students.major%type);
end manageStu;
- create or replace package body manageStu as
- procedure addStu(firstname students.first_name%type,
- lastname students.last_name%type,
- majorm students.major%type,
- current_credits students.current_credits%type,
- markm students.mark%type)
- as
- begin
- insert into students values(student_sequence.NEXTVAL,
- firstname,
- lastname,
- majorm,
- current_credits,
- markm);
- end addStu;
create or replace package body manageStu as
procedure addStu(firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_credits students.current_credits%type,
markm students.mark%type)
as
begin
insert into students values(student_sequence.NEXTVAL,
firstname,
lastname,
majorm,
current_credits,
markm);
end addStu;
- procedure delStu(stuID int)
- as
- begin
- delete students where id=stuID;
- end delStu;
- procedure modifyStu(idm int,
- firstname students.first_name%type,
- lastname students.last_name%type,
- majorm students.major%type,
- current_creditsm students.current_credits%type,
- markm students.major%type)
- as
- begin
- update students set
- first_name=firstname,
- last_name=lastname,
- major=majorm,
- current_credits=current_creditsm,
- mark=markm
- where id=idm;
- end modifyStu;
- manageStu;
procedure delStu(stuID int)
as
begin
delete students where id=stuID;
end delStu;
procedure modifyStu(idm int,
firstname students.first_name%type,
lastname students.last_name%type,
majorm students.major%type,
current_creditsm students.current_credits%type,
markm students.major%type)
as
begin
update students set
first_name=firstname,
last_name=lastname,
major=majorm,
current_credits=current_creditsm,
mark=markm
where id=idm;
end modifyStu;
end manageStu;
2)执行:
- begin
- manageStu.addstu('F1','LB','computer',12,90);
- end;
begin
manageStu.addstu('F1','LB','computer',12,90);
end;
3) 包内过程、方法可以重载
4) 包的初使化
当第一次调用打包程序时,该包将进行初使化。也就是说将该包从硬盘读入到内存并启动调用的子程序的编译代码开始运行。这时,系统为该包中定义的所有变量分配内存单元。每个会话都有其打包变量的副本,以确保一包子程序的两个对话使用不同的内存单元。
在大多数情况下,初使化代码要在包第一次初使化时运行。为了实现这种功能,我们可以在包体中所有对象之后加入一个初使化部分,其语法如下:
- CREATE OR REPLACE PACEAGE BODY package_name (IS|AS)
- . . .
- BEGIN
- Initialization_code;
- END [package_name]
CREATE OR REPLACE PACEAGE BODY package_name (IS|AS)
. . .
BEGIN
Initialization_code;
END [package_name]
7、取当前系统时间
- SELECT TO_CHAR(SYSDATE,'SSSSS') FROM sys.dual;
SELECT TO_CHAR(SYSDATE,'SSSSS') FROM sys.dual;
8、更改表结构
- ALTER TABLE STU MODIFY FIRST_NAME VARCHAR(100);