oracle pl sql基本語法

2022-08-19 07:36:12 字數 4085 閱讀 4004

--

-pl/sql程式語言

---pl/sql程式語言是對sql語言的擴充套件,使得sql語言具有過程化程式設計的特性。

---pl/sql程式語言比一般的過程化程式語言,更加靈活高效。

---pl/sql程式語言主要用來編寫儲存過程和儲存函式等。

---宣告方法

---賦值操作可以使用:= 也可以使用into查詢語句賦值

declare

i number(2) :=10;

s varchar2(10) :='小明

';ena emp.ename

%type;--

-引用型變數

emprow emp%rowtype;--

-記錄型變數

begin

dbms_output.put_line(i);

--10

dbms_output.put_line(s); --

小明select ename into ena from emp where empno =

7788

; dbms_output.put_line(ena);

--scott

select

*into emprow from emp where empno =

7788

; dbms_output.put_line(emprow.ename ||'

的工作為:

'|| emprow.job); --

scott的工作為:analyst

end;

--

- pl/sql中的if判斷

--範例 1:如果從控制台輸入 1 則輸出我是 1

declare

pnum

number :=

#begin

if pnum =

1then

dbms_output.put_line(

'我是1');

endif;

end;

--範例 2:如果從控制台輸入 1 則輸出我是 1否則輸出我不是 1

declare

mynum

number :=

#begin

if mynum =

1then

dbms_output.put_line(

'我是1');

else

dbms_output.put_line(

'我不是1');

endif;

end;

--範例 3:判斷人的不同年齡段 18歲以下是未成年人,18歲以上 40以下是成年人,40以上是老年人

declare

mynum

number :=

#begin

if mynum <

18then

dbms_output.put_line(

'未成年人');

elsif mynum

>=

18and mynum <

40then

dbms_output.put_line(

'中年人');

elsif mynum

>=

40then

dbms_output.put_line(

'老年人');

endif;

end;

--

-pl/sql中的loop迴圈

---用三種方式輸出1到10個數字

---while迴圈

declare

i number(2) :=1;

begin

while i<

11loop

dbms_output.put_line(i);

i := i+1;

endloop;

end;

---exit迴圈

declare

i number(2) :=1;

begin

loop

exit

when i>10;

dbms_output.put_line(i);

i := i+1;

endloop;

end;

---for迴圈

declare

begin

for i in

1..10

loop

dbms_output.put_line(i);

endloop;

end;

游標 cursor

語法:cursor 游標名 [ (引數名 資料型別,引數名 資料型別,...)] is select 語句;

例如: cursor c1 is select ename from emp;

游標的使用步驟:

開啟游標: open c1; (開啟游標執行查詢)

取一行游標的值:fetch c1 into pjob; (取一行到變數中)

關閉游標: close c1;(關閉游標釋放資源)

游標的結束方式 exit when c1%notfound

注意: 上面的 pjob 必須與 emp表中的 job 列型別一致:

定義:pjob emp.empjob%type;

--

-游標:可以存放多個物件,多行記錄。

---輸出emp表中所有員工的姓名

declare

cursor c1 is

select

*from

emp;

emprow emp

%rowtype;

begin

open

c1; loop

fetch c1 into

emprow;

exit

when c1%

notfound;

dbms_output.put_line(emprow.ename);

endloop;

close

c1;end;--

---給指定部門員工漲工資

declare

cursor c2(eno emp.deptno%

type)

isselect empno from emp where deptno =

eno;

en emp.empno

%type;

begin

open c2(10

); loop

fetch c2 into

en;

exit

when c2%

notfound;

update emp set sal=sal+

100where empno=

en;

commit

;

endloop;

close

c2;end;--

--查詢10號部門員工資訊

select

*from emp where deptno =

10;

for 迴圈提取游標值

我們每次提取游標,需要開啟游標 關閉游標 迴圈游標 提取游標 控制迴圈的退出等等,好麻煩!有沒有更簡單的寫法呢?有!用 for 迴圈一切都那麼簡單

declare

cursor cur_pricetable(v_ownertypeid number)

isselect

* from t_pricetable where ownertypeid=v_ownertypeid;begin

for v_pricetable in cur_pricetable(3

)  loop

dbms_output.put_line(

'**:

'||v_pricetable.price ||

'噸位:

'||v_pricetable.minnum||'-

'||v_pricetable.maxnum );

endloop;

end ;

Oracle PL SQL語法格式

dba基礎課程 oracle pl sql語法格式 pl sql是沒命名的儲存過程,函式,觸發器,pl sql塊的語法格式如下 declare 宣告部分,可選 begin 執行部分,必須 exception 異常處理部分,可選 endpl sql塊每條語句必須用分號結束,單行注釋 多行注釋 下面是乙...

ORACLE PL SQL語法總結

定義陣列型別 declare type array var is table of t t student.name type index by binary integer v name array var begin v name 1 張伯倫 dbms output.put line v nam...

Oracle PLSQL基本操作

declare 應用型變數 emp.sal是什麼型別,那麼通過emp.sal type就可以指明vsal的型別 vsal emp.sal type begin 將值賦給vsal select sal into vsal from emp where empno 7369 列印 dbms output...