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;