/*
--建表
create table student(
recordid number(38),
sid int not null ,
sname varchar2(50),
sdate date,
sage number(3)
);
*/
-- 删除表
-- drop table student;
-- 插入数据
/*
set serveroutput on --允许服务器输出
declare
maxrecords constant int:=100;
i int:=1;
begin
for i in 1..maxrecords
loop
insert into student(sid,sdate)values(i,sysdate);
end loop
--dbms_output.put_line('成功录入数据!');
commit;
end;
*/
-- select * from student;
-- 声明一个变量
/*
declare
pi constant number(9):=3.1415926;
begin
commit;
end;
*/
-- 复合数据类型(常见的五种)
-- 1 .使用 %type 定义变量
-- 为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。
-- 这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改.
/*
Declare
mydate student.sdate%type;
begin
commit;
end;
*/
-- 2. 定义记录类型变量
-- 将多个基本数据类型捆绑在一起的记录数据类型。
/*
set serveroutput on
declare
type myrecord is record(
sid int,
sdate date);
srecord myrecord; --声明一个自定义记录类型变量的实例
begin
select sid,sdate into srecord from student where sid=68;
dbms_output.put_line('ID: '|| srecord.sid ||'Date:'|| srecord.sdate); --'||': 它是字符串连接符.
end;
*/
-- 3.使用 %rowtype 变量
-- 使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。
-- 比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。
/*
set serveroutput on
Declare
mytableRow student%rowtype;
begin
select * into mytableRow
from student
where sid=88;
dbms_output.put_line(mytableRow.sid || mytableRow.sdate);
end;
*/
-- 4.定义一维表类型变量
-- 表类型变量和数据表是有区别的,定义表类型变量的语法如下:
-- ―――――――――――――――――――――――――――――――――――――
-- type 表类型 is table of 类型 index by binary_integer;
-- 表变量名 表类型;
-- ―――――――――――――――――――――――――――――――――――――
-- 类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
-- 这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
/*
Declare
type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组
type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组
table1 tabletype1; --实例声明
table2 tabletype2; --实例声明
begin
table1(1):='学生';
table1(2):='职员';
table2(1):=88;
table2(2):=89;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
*/
-- 5.定义多维类型表变量
-- 相当于定义多维数组.
-- 注意在运行下面的语句前要在数据库中插入数据.
Declare
type tabletype1 is table of student % rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1( 60 )
from student
where sid = 60 ;
dbms_output.put_line(table1( 60 ).sid || table1( 60 ).sdate);
end ;
--建表
create table student(
recordid number(38),
sid int not null ,
sname varchar2(50),
sdate date,
sage number(3)
);
*/
-- 删除表
-- drop table student;
-- 插入数据
/*
set serveroutput on --允许服务器输出
declare
maxrecords constant int:=100;
i int:=1;
begin
for i in 1..maxrecords
loop
insert into student(sid,sdate)values(i,sysdate);
end loop
--dbms_output.put_line('成功录入数据!');
commit;
end;
*/
-- select * from student;
-- 声明一个变量
/*
declare
pi constant number(9):=3.1415926;
begin
commit;
end;
*/
-- 复合数据类型(常见的五种)
-- 1 .使用 %type 定义变量
-- 为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。
-- 这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改.
/*
Declare
mydate student.sdate%type;
begin
commit;
end;
*/
-- 2. 定义记录类型变量
-- 将多个基本数据类型捆绑在一起的记录数据类型。
/*
set serveroutput on
declare
type myrecord is record(
sid int,
sdate date);
srecord myrecord; --声明一个自定义记录类型变量的实例
begin
select sid,sdate into srecord from student where sid=68;
dbms_output.put_line('ID: '|| srecord.sid ||'Date:'|| srecord.sdate); --'||': 它是字符串连接符.
end;
*/
-- 3.使用 %rowtype 变量
-- 使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。
-- 比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。
/*
set serveroutput on
Declare
mytableRow student%rowtype;
begin
select * into mytableRow
from student
where sid=88;
dbms_output.put_line(mytableRow.sid || mytableRow.sdate);
end;
*/
-- 4.定义一维表类型变量
-- 表类型变量和数据表是有区别的,定义表类型变量的语法如下:
-- ―――――――――――――――――――――――――――――――――――――
-- type 表类型 is table of 类型 index by binary_integer;
-- 表变量名 表类型;
-- ―――――――――――――――――――――――――――――――――――――
-- 类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
-- 这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
/*
Declare
type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组
type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组
table1 tabletype1; --实例声明
table2 tabletype2; --实例声明
begin
table1(1):='学生';
table1(2):='职员';
table2(1):=88;
table2(2):=89;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
*/
-- 5.定义多维类型表变量
-- 相当于定义多维数组.
-- 注意在运行下面的语句前要在数据库中插入数据.
Declare
type tabletype1 is table of student % rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1( 60 )
from student
where sid = 60 ;
dbms_output.put_line(table1( 60 ).sid || table1( 60 ).sdate);
end ;
--
流程控制
-- 1. if..then..end if
set serveroutput on
declare
i integer : = 60 ;
j integer : = 30 ;
begin
if i >= j then
dbms_output.put_line(j);
end if ;
end ;
-- 2.if嵌套条件控制
if 条件1 then
if 条件2 then
语句段1;
else
语句段2;
end if ;
else
语句段3;
end if ;
-- 3.循环控制
-- loopexitend loop
set serveroutput on
declare
i integer : = 10 ;
j integer : = 20 ;
w integer : = 10 ;
begin
loop
i: = i + 1 ;
if (j = i) then
begin
dbms_output.put_line( ' 我哥俩现在一样多了. ' || to_char(j)) ;
exit ;
end ;
else
w: = w - 1 ;
end if ;
end loop;
dbms_output.put_line( ' w还剩多少: ' || to_char(w)) ;
end ;
-- 4.whileloopend loop
while 条件 loop
执行语句段;
end loop;
-- 5.forinloopend
for 循环变量 in [ reverse ] 循环下界循环上界 loop
循环处理语句;
end loop;
set serveroutput on
declare
i int : = 0 ;
j int : = 100 ;
begin
for j in 1 .. 6 loop
i: = i + 1 ;
end loop;
dbms_output.put_line( ' 循环次数: ' || to_char(i)) ;
end ;
-- 5.事务处理
-- 在pl/sql中,可以使用三个事务处理控制命令.
-- 在PL/SQL程序打开自动提交功能。这样每次执行PL/SQL程序都会自动进行事务提交。
-- 语句: set auto on;
-- 相应的,取消自动提交功能的PL/SQL程序为:
-- set auto off;
-- commit,rollback,savepoint
-- select * from student;
-- commit;
-- delete from student;
-- rollback; --回滚刚才的delete 操作.
-- select * from student;
savepoint insertpoint;
insert into student(sid,sname) values ( 900 , ' mrfu ' );
-- 创建保存点 : savepoint 保存点名;
select sid,sname from student where sid = 900 ;
rollback to insertpoint; -- 回滚保存点: rollbacd to 保存点名;
-- dbms_output.put_line('回滚保存点') ;
select sid,sname from student where sid = 900 ;
-- 1. if..then..end if
set serveroutput on
declare
i integer : = 60 ;
j integer : = 30 ;
begin
if i >= j then
dbms_output.put_line(j);
end if ;
end ;
-- 2.if嵌套条件控制
if 条件1 then
if 条件2 then
语句段1;
else
语句段2;
end if ;
else
语句段3;
end if ;
-- 3.循环控制
-- loopexitend loop
set serveroutput on
declare
i integer : = 10 ;
j integer : = 20 ;
w integer : = 10 ;
begin
loop
i: = i + 1 ;
if (j = i) then
begin
dbms_output.put_line( ' 我哥俩现在一样多了. ' || to_char(j)) ;
exit ;
end ;
else
w: = w - 1 ;
end if ;
end loop;
dbms_output.put_line( ' w还剩多少: ' || to_char(w)) ;
end ;
-- 4.whileloopend loop
while 条件 loop
执行语句段;
end loop;
-- 5.forinloopend
for 循环变量 in [ reverse ] 循环下界循环上界 loop
循环处理语句;
end loop;
set serveroutput on
declare
i int : = 0 ;
j int : = 100 ;
begin
for j in 1 .. 6 loop
i: = i + 1 ;
end loop;
dbms_output.put_line( ' 循环次数: ' || to_char(i)) ;
end ;
-- 5.事务处理
-- 在pl/sql中,可以使用三个事务处理控制命令.
-- 在PL/SQL程序打开自动提交功能。这样每次执行PL/SQL程序都会自动进行事务提交。
-- 语句: set auto on;
-- 相应的,取消自动提交功能的PL/SQL程序为:
-- set auto off;
-- commit,rollback,savepoint
-- select * from student;
-- commit;
-- delete from student;
-- rollback; --回滚刚才的delete 操作.
-- select * from student;
savepoint insertpoint;
insert into student(sid,sname) values ( 900 , ' mrfu ' );
-- 创建保存点 : savepoint 保存点名;
select sid,sname from student where sid = 900 ;
rollback to insertpoint; -- 回滚保存点: rollbacd to 保存点名;
-- dbms_output.put_line('回滚保存点') ;
select sid,sname from student where sid = 900 ;
--
存储过程
/*
--1.过程的语法结构
--参见:http://newland.cnblogs.com/archive/2006/04/05/367531.html
--2.执行存储过程
begin
存储过程名;
end;
--创建好的存储过程可以被任何程序调用
*/
-- 3.带参数的存储过程
/*
参数类型
在PL/SQL过程中,可以有3种类型的参数。
in参数:读入参数,主程序向过程传递参数值。
out参数:读出参数,过程向主程序传递参数值。
in out 参数:双向参数,过程与主程序双向交流数据。
*/
-- rownum
-- SELECT * FROM STUDENT WHERE sid=2 and ROWNUM<=1;
/
-- SELECT * FROM STUDENT WHERE ROWNUM<=5;
/
CREATE OR REPLACE PROCEDURE MRFUTESTPRO
( MYID student.sid % type)
AS
MYNAME student.sname % type;
BEGIN
EXECUTE IMMEDIATE ' SELECT SNAME FROM STUDENT WHERE SID=:1 and ROWNUM<2 '
USING MYID
RETURNING INTO MYNAME;
DBMS_OUTPUT.PUT_LINE( ' 我的名字: ' || MYNAME);
exception
when others then
dbms_output.put_line( ' 没有符合条件的数据 ' );
end MRFUTESTPRO;
/
BEGIN
MRFUTESTPRO( 2 );
END ;
/
-- 创建一个存储过程实现输入一个学生的sid编号,查询出拥有该sid号的第一个学生的名字.
create or replace procedure mrfuproc
( tempid in student.sid % type ,
tempName out student.sname % type
)
as
myName nvarchar2( 50 );
begin
select sname into tempName from student where sid = tempid and rownum < 2 ;
myName: = ' 姓名 ' || tempName;
end mrfuproc;
/
declare
myid student.sid % type;
myName student.sname % type;
begin
myid: = 10 ;
myName: = '' ;
mrfuproc(myid,myName);
dbms_output.put_line(myid);
dbms_output.put_line(myName);
end ;
/
-- 创建队列示例
CREATE SEQUENCE idseqSample
START WITH 1 -- 从1开始计数
INCREMENT BY 1 -- 每次加几个
NOMAXVALUE -- 不设置最大值
MINVALUE 1
CACHE 10 -- 缓冲区大小
NOCYCLE -- 一直累加,不循环
-- NOCACHE -- 不建缓冲区
/
-- 创建索引
CREATE UNIQUE INDEX system.idindex ON system.student(recordid);
/
-- 创建队列
CREATE SEQUENCE system.idseq start with 1 increment by 1 maxvalue 987654321 minvalue 1 cache 10 ;
/
-- 创建触发器 ,实现插入student表中纪录时,自动插入recordid字段数据,并实现自动增量.
CREATE OR REPLACE TRIGGER system.idtrigger
BEFORE INSERT ON system.student REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
BEGIN
SELECT system.idseq.NEXTVAL INTO :NEW.recordid FROM DUAL;
END ;
/
-- 测试 ,首先删除表中的所有记录,然后插入30条纪录.最后检查效果.
delete from system.student;
/
declare
i int : = 0 ;
begin
for i in 1 .. 30 loop
insert into system.student (sid,sname,sage) values (i, ' frj ' || to_char(i), ' 26 ' );
end loop;
end ;
/
select * from system.student;
/
/*
--1.过程的语法结构
--参见:http://newland.cnblogs.com/archive/2006/04/05/367531.html
--2.执行存储过程
begin
存储过程名;
end;
--创建好的存储过程可以被任何程序调用
*/
-- 3.带参数的存储过程
/*
参数类型
在PL/SQL过程中,可以有3种类型的参数。
in参数:读入参数,主程序向过程传递参数值。
out参数:读出参数,过程向主程序传递参数值。
in out 参数:双向参数,过程与主程序双向交流数据。
*/
-- rownum
-- SELECT * FROM STUDENT WHERE sid=2 and ROWNUM<=1;
/
-- SELECT * FROM STUDENT WHERE ROWNUM<=5;
/
CREATE OR REPLACE PROCEDURE MRFUTESTPRO
( MYID student.sid % type)
AS
MYNAME student.sname % type;
BEGIN
EXECUTE IMMEDIATE ' SELECT SNAME FROM STUDENT WHERE SID=:1 and ROWNUM<2 '
USING MYID
RETURNING INTO MYNAME;
DBMS_OUTPUT.PUT_LINE( ' 我的名字: ' || MYNAME);
exception
when others then
dbms_output.put_line( ' 没有符合条件的数据 ' );
end MRFUTESTPRO;
/
BEGIN
MRFUTESTPRO( 2 );
END ;
/
-- 创建一个存储过程实现输入一个学生的sid编号,查询出拥有该sid号的第一个学生的名字.
create or replace procedure mrfuproc
( tempid in student.sid % type ,
tempName out student.sname % type
)
as
myName nvarchar2( 50 );
begin
select sname into tempName from student where sid = tempid and rownum < 2 ;
myName: = ' 姓名 ' || tempName;
end mrfuproc;
/
declare
myid student.sid % type;
myName student.sname % type;
begin
myid: = 10 ;
myName: = '' ;
mrfuproc(myid,myName);
dbms_output.put_line(myid);
dbms_output.put_line(myName);
end ;
/
-- 创建队列示例
CREATE SEQUENCE idseqSample
START WITH 1 -- 从1开始计数
INCREMENT BY 1 -- 每次加几个
NOMAXVALUE -- 不设置最大值
MINVALUE 1
CACHE 10 -- 缓冲区大小
NOCYCLE -- 一直累加,不循环
-- NOCACHE -- 不建缓冲区
/
-- 创建索引
CREATE UNIQUE INDEX system.idindex ON system.student(recordid);
/
-- 创建队列
CREATE SEQUENCE system.idseq start with 1 increment by 1 maxvalue 987654321 minvalue 1 cache 10 ;
/
-- 创建触发器 ,实现插入student表中纪录时,自动插入recordid字段数据,并实现自动增量.
CREATE OR REPLACE TRIGGER system.idtrigger
BEFORE INSERT ON system.student REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
BEGIN
SELECT system.idseq.NEXTVAL INTO :NEW.recordid FROM DUAL;
END ;
/
-- 测试 ,首先删除表中的所有记录,然后插入30条纪录.最后检查效果.
delete from system.student;
/
declare
i int : = 0 ;
begin
for i in 1 .. 30 loop
insert into system.student (sid,sname,sage) values (i, ' frj ' || to_char(i), ' 26 ' );
end loop;
end ;
/
select * from system.student;
/