1、创建Create or Replace Procedure pName(praram in out type,..) is
示例:
Create Or Replace Procedure pTest(name in varchar2) is
Begin
语句...
End pTest;
2、游标使用Cursor
Create Or Replace Procedure pTest(name in varchar2) is
Cursor cursor1 is select id from pTest where ...;
Begin
语句...
End pTest;
3、遍历Cursor方法
For tempCrs in Cursor1 Loop
tempCrs.id
End Loop;
4、Cursor使用方法2
Create Or Replace Procedure pTest(name in varchar2) is
Cursor cursor1;
Begin
select id into cursor1 from pTest where ..;
other..
End pTest;
5、带参数Cursor使用
Create Or Replace Procedure pTest(name in varchar2) is
Cursor cursor1(t_id number) is select id from pTest where typeid=t_id;
Begin
For tempCrs in cursor1(10) Loop
other..
End pTest;
6、SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into
变量1,变量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
xxxx;
END;
7、基本语句
if 比较式 then
begin
end;
end if;
while i<100 Loop
begin
...
end ;
end Loop;
for x in y loop
begin
..
end ;
end loop;
赋值 :=
create or replace procedure test(varArray in myPackage.TestArray) as
--(输入参数varArray 是自定义的数组类型)
i number;
begin
i := 1;
--存储过程数组是起始位置是从1开始的,与java、C、C++等语言不同。因为在Oracle中本是没有数组的概念的,数组其实就是一张
--表(Table),每个数组元素就是表中的一个记录,所以遍历数组时就相当于从表中的第一条记录开始遍历
For i in varArray.count LOOP
dbms_output.putline('The No.'|| i ||
'record in varArray is:'||varArray(i));
end LOOP;
end test;
(2)自定义的数组类型 (自定义数据类型时,建议通过创建Package的方式实现,以便于管理)
create or replace package
myPackage is
-- Public type declarations type info is record( name
varchar(20), y number);
type TestArray is table of info index by
binary_integer;
--此处声明了一个TestArray的类型数据,其实其为一张存储Info数据类型的Table而已,及TestArray
就是一张表,有两个字段,一个是name,一个是y。需要注意的是此处使用了Index by binary_integer
编制该Table的索引项,也可以不写,直接写成:type TestArray is table of
info,如果不写的话使用数组时就需要进行初始化:varArray myPackage.TestArray; varArray := new
myPackage.TestArray();
end TestArray;