oracle學習_基本語法


1.創建存儲

create or replace procedure test(var_name_1 in type,var_name_2 out ty
pe) as
--聲明變量(變量名 變量類型)
begin
--存儲過程的執行體
end test;
打印出輸入的時間信息
E.g:
create or replace procedure test(workDate in Date) is
begin
dbms_output.putline(The input date is:||to_date(workDate, yyyy-mm-d
d));
end test;

  

2.變量賦值

變量名 := 值;
E.g:
create or replace procedure test(workDate in Date) is
x number(4,2);
begin
x := 1;
end test;  

3.判斷語句

if 比較式 then begin end; end if;
E.g
create or replace procedure test(x in number) is
begin
if x >0 then

x := 0 - x;
elsif =0 then
x:=x;
else 
x:=x+1;
end if;
if x = 0 then
begin
x: = 1;
end;
end if;
end test; 

4.FOR

For ... in ... LOOP
-- 執行語句
end LOOP;
(1) 循環遍歷游標
create or replace procedure test() as
Cursor cursor is select name from student;
name varchar(20);
begin
for name in cursor LOOP
begin
dbms_output.putline(name);
end;
end LOOP;
end test;
2)循環遍歷數組
create or replace procedure test(varArray in myPackage.TestArray) as
--(輸入參數 varArray 是自定義的數組類型,定義方式見標題 6)
i number;
begin
i := 1; --存儲過程數組是起始位置是從 1 開始的,與 java、C、C++等語言
不同。因為在 Oracle 中本是沒有數組的概念的,數組其實就是一張
--表(Table),每個數組元素就是表中的一個記錄,所以遍歷數組時就相當於從表
中的第一條記錄開始遍歷
for i in 1..varArray.count LOOP
dbms_output.putline(The No. || i ||record in varArray is: ||varArray
(i));
end LOOP;
end test;

  

5.while循環

while 條件語句 LOOP
begin
end;
end LOOP;
E.g
create or replace procedure test(i in number) as
begin
while i < 10 LOOP
begin
i:= i + 1;
end;
end LOOP;
end test; 

6.數組

首先明確一個概念:Oracle 中本是沒有數組的概念的,數組其實就是一張表(Ta
ble),每個數組元素就是表中的一個記錄。
使用數組時,用戶可以使用 Oracle 已經定義好的數組類型,或可根據自己的需
要定義數組類型。
(1)使用 Oracle 自帶的數組類型
x array; --使用時需要需要進行初始化
e.g:
create or replace procedure test(y out array) is
x array;
begin
x := new array();
y := x;
end test;
(2)自定義的數組類型 (自定義數據類型時,建議通過創建 Package 的方式實現 ,
以便於管理)
E.g (自定義使用參見標題 4.2) create or replace package myPackage is
-- Public type declarations type info is record( name va
rchar(20), y number);
type TestArray is table of info index by binary_integer; --此處
聲明了一個 TestArray 的類型數據,其實其為一張存儲 Info 數據類型的 Table
而已,及 TestArray 就是一張表,有兩個字段,一個是
name,一個是 y。需要注意的是此處使用了 Index by binary_integer 編制該 T
able 的索引項,也可以不寫,直接寫成:type TestArray is
table of info,如果不寫的話使用數組時就需要進行初始化:varArray myPac
kage.TestArray; varArray := new myPackage.TestArray();
end TestArray;

7.游標的使用

游標的使用 Oracle 中 Cursor 是非常有用的,用於遍歷臨時表中的查詢結果 。
其相關方法和屬性也很多,現僅就常用的用法做一二介紹:
(1)Cursor 型游標(不能用於參數傳遞)
create or replace procedure test() is
cusor_1 Cursor is select std_name from student where ...; --Curso
r 的使用方式 1 cursor_2 Cursor;
begin
select class_name into cursor_2 from class where ...; --Cursor 的使
用方式 2
可使用 For x in cursor LOOP .... end LOOP; 來實現對 Cursor 的遍歷
end test;
(2)SYS_REFCURSOR 型游標,該游標是 Oracle 以預先定義的游標,可作出參數進
行傳遞
create or replace procedure test(rsCursor out SYS_REFCURSOR) is
cursor SYS_REFCURSOR; name varhcar(20);
begin
OPEN cursor FOR select name from student where ... --SYS_REFCURSOR 只
能通過 OPEN 方法來打開和賦值
LOOP
fetch cursor into name --SYS_REFCURSOR 只能通過 fetch into 來打
開和遍歷 exit when cursor%NOTFOUND; --SYS_R
EFCURSOR 中可使用三個狀態屬性:
---%NOTFOUND(未找到記錄信息) %FOUND(找到記錄信息)
---%ROWCOUNT(然后當前游標所指向的行位置)
dbms_output.putline(name);
end LOOP;
rsCursor := cursor;
end test;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM