日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

oracle游标语法举例,PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)

發布時間:2023/12/18 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle游标语法举例,PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、PL/SQL語句塊

PL/SQL語句塊只適用于Oracle數據庫,使用時臨時保存在客戶端,而不是保存在數據庫。

基本語法:

declare

變量聲明、初始化

begin

業務處理、邏輯代碼

exception

異常捕獲

end;

變量聲明:

[:=]

例:v_name

varchar2(20):=’張三’;

例:見第3節

2、循環語句

loop循環語法:

loop

exit

when表達式

end

loop;

while循環語法:

while 表達式

loop

end

loop;

for循環語法:

for

in

loop

end

loop;

for循環的變量可不做聲明及初始化。

例:見第3節

3、if判斷語句

基本語法:

if

then

else

if

then

else

end

if;

end

if;

例:

declare

v_identity number(4):=0;

begin

loop

if

v_identity=1 then

dbms_output.put_line('v_identity=1');

else

if v_identity=3 then

dbms_output.put_line('v_identity=3');

else

if v_identity=6 then

exit;

else

dbms_output.put_line('v_identity

is not 1 or 3');

end

if;

end

if;

end

if; --

注意,有多少個if就要有多少個end

if結束標志。

v_identity:=v_identity+1;

end

loop;

exception

when

others

then dbms_output.put_line('error!');

end;

/

4、分支case

基本語法:

case

when常量

then

when常量

then

else

end

case;

例:

declare

v_number number(4):=3;

v_string varchar(20):='abc';

begin

case

v_number

when

1 then

dbms_output.put_line('v_number

is '||1);

when

2 then

dbms_output.put_line('v_number

is '||2);

when

3 then

dbms_output.put_line('v_number

is '||3);

end

case;

case

v_string

when

'ab' then

dbms_output.put_line('v_string

is '||'ab');

when

'bc' then

dbms_output.put_line('v_string

is '||'bc');

else

--

缺省匹配

dbms_output.put_line('v_string

is other value');

end

case;

exception

when

others

then dbms_output.put_line('error!');

end;

/

5、異常(exception)

聲明異常語法:

exception;

拋出異常語法:raise

;

捕獲異常語法:when

then異常處理語句;

例:

declare

v_input varchar2(1):='&throw';--

動態輸入

v_exception_1 exception; --

自定義異常

v_exception_2 exception;

others exception; --

系統異常

begin

if

v_input='1' then

raise

v_exception_1;

--

拋出異常

else

if v_input='2' then

raise

v_exception_2;

else

raise

others;

end

if;

end

if;

exception

--

捕獲異常

when

v_exception_1

then dbms_output.put_line('throw

exception: v_exception_1');

when

v_exception_2

then dbms_output.put_line('throw

exception: v_exception_2');

when

others

then dbms_output.put_line('throw

exception: others');

end;

/

6、游標(cursor)

聲明游標語法:cursor

is

select語句;

聲明ref游標語法:

is ref cursor;

打開游標語法:open

;

移動游標并獲取數據語法:fetch

into

;

關閉游標語法:close

;

游標屬性(游標的屬性必須在關閉游標之前):

%isopen:

判斷游標是否打開

%notfound:

找不到數據時

%found:

%rowcount:

返回當前游標已掃描的數據行數量

游標分類:1、顯示游標(自定義游標);2、隱示游標(系統游標);3、REF游標

例:

declare

v_row

t_test%rowtype; --

匹配t_test表中一行所有的數據類型

cursor v_cur is select * from t_test;--

聲明游標

begin

open

v_cur;--

打開游標

loop

fetch

v_cur

into v_row;--

將游標所在行的數據轉存到v_row中

exit

when v_cur%notfound; --

當游標到最后一行時跳出

dbms_output.put_line('id

= '||v_row.t_id||' name = '||v_row.t_name||' msg = '||v_row.t_msg);

end

loop;

close

v_cur;--

關閉游標

exception

when

others

then dbms_output.put_line('throw

exception: others');

end;

/

--

REF游標 --

create or replace package upk_select_test

as

type uc_test is ref cursor; --

聲明ref游標

end

upk_select_test;

/

--

存儲過程中調用ref游標,并將查詢結果以游標的方式返回

create or replace procedure up_select_test_2

(uc_result out upk_select_test.uc_test)

is

begin

open

uc_result

for select * from t_test;

end

up_select_test_2;

/

7、通配類型操作符

%type:

通配某行某列數據類型,如v_name

t_test.t_name%type;通配表t_test中的t_name。

%rowtype:

通配一行所有列的數據類型,如 v_row

t_test%rowtype;匹配t_test表中一行

所有的數據類型。

8、存儲過程(procedure)

基本語法:

create procedure

()

as|is

變量聲明、初始化

begin

業務處理、邏輯代碼

exception

異常捕獲、容錯處理

end

;

參數:

in|out|in out

,如:v_name

varchar2

in:入參

out:出參

in

out:出入參

注:as|is表示as或is

調用語法:

1)、exec

;

2)、execute

;

3)、在PL/SQL語句塊中直接調用。

例:

create or replace procedure up_wap(v_param1 in out varchar2,v_param2 in out varchar2)

is

v_temp varchar2(20);

begin

dbms_output.put_line('交換前參數1:'||v_param1||'參數2:'||v_param2);

v_temp:=v_param1;

v_param1:=v_param2;

v_param2:=v_temp;

dbms_output.put_line('交換后參數1:'||v_param1||'參數2:'||v_param2);

exception

when

others

then dbms_output.put_line('There

is a error when the procedure up_wap executing!');

end

up_wap;

/

--

調用存儲過程

declare

v_param1 varchar2(20):='param1';

v_param2 varchar2(20):='param2';

begin

up_wap(v_param1 =>

v_param1,v_param2 => v_param2);

end;

/

9、自定義函數(function)

基本語法:

create function

()

return

as|is

變量聲明、初始化

begin

業務處理、邏輯代碼

return

;

exception

異常捕獲、容錯處理

end

;

參數:in入參

注:只有入參的類型。

在存儲過程和自定義函數中的參數的傳遞(入參和出參)不能使用%type或%rowtype匹配,不能使用空值null,但是存儲過程可以返回空值。

例:

create function uf_select_name_by_id_test(v_id

in number)

return varchar2

is

v_name

t_test.t_name%type;

begin

select t_name into v_name from t_test where t_id=v_id;

return v_name;

exception

when

others

then dbms_output.put_line('error');

end

uf_select_name_by_id_test;

/

select uf_select_name_by_id_test(1)

姓名 from dual;--

select調用

declare --pl/sql語句塊調用

v_name varchar2(20);

begin

v_name:=uf_select_name_by_id_test(1);

dbms_output.put_line('name

= '||v_name);

end;

/

10、包(package)

封裝,可以封裝過程(procedure)、函數(function)和變量。

注意,在包(package)中聲明的過程(procedure)和函數(function)必須在包的實現體

(package

body)中定義實現。

基本語法:

create

package

as|is

變量聲明

存儲過程聲明

自定義函數聲明

end

;

/

create

package

as|is

存儲過程的代碼實現

自定義函數的代碼實現

end

;

/

例:

--

創建包upk_hello

create or replace package upk_hello

is

v_hello_world varchar2(20):='hello world'; --

聲明變量

procedure up_hello_world(v_name

in varchar2);--

聲明過程

function uf_hello_world(v_name

in varchar2) return varchar2;--

聲明函數

end

upk_hello;

/

--

實現包(upk_hello)里聲明的方法

create or replace package body upk_hello

is

procedure up_hello_world(v_name

in varchar2)

is

v_string varchar2(100);

begin

v_string:=v_name||'

say hello world!';

dbms_output.put_line(v_string);

exception

when

others

then dbms_output.put_line('error');

end

up_hello_world;

function uf_hello_world(v_name

in varchar2) return varchar2

is

v_string varchar2(100);

begin

v_string:=v_name||'

say hello world!';

return v_string;

exception

when

others

then dbms_output.put_line('error');

end

uf_hello_world;

end

upk_hello;

/

--

包的調用

declare

v_msg

varchar2(100);

begin

upk_hello.up_hello_world('bing');

v_msg:=upk_hello.uf_hello_world('admin');

dbms_output.put_line(v_msg);

dbms_output.put_line(upk_hello.v_hello_world);

end;

/

總結

以上是生活随笔為你收集整理的oracle游标语法举例,PL/SQL语句块基本语法(ORACLE存储过程,函数,包,游标)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。