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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

SQL-Oracle游标

發(fā)布時(shí)間:2025/6/15 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL-Oracle游标 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

游標(biāo)提供了一種從集合性質(zhì)的結(jié)果集中提供出單條記錄的手段。初始時(shí)指向首記錄。

  • 游標(biāo)的種類(lèi)
    靜態(tài)游標(biāo)、REF游標(biāo)

    靜態(tài)游標(biāo):能夠理解為一個(gè)數(shù)據(jù)快照,打開(kāi)游標(biāo)后的結(jié)果集是數(shù)據(jù)庫(kù)表中數(shù)據(jù)的備份,數(shù)據(jù)不會(huì)對(duì)表的DML操作而改變。

    ①顯式靜態(tài)游標(biāo):是指在使用之前必須有明白的游標(biāo)定義,這樣的游標(biāo)的定義會(huì)關(guān)聯(lián)數(shù)據(jù)查詢(xún)語(yǔ)句。一般會(huì)返回一行或多行,打開(kāi)游標(biāo)后能夠利用游標(biāo)的位置對(duì)結(jié)果集進(jìn)行檢索,使之返回單一的行記錄,用戶(hù)能夠操作該記錄,關(guān)閉游標(biāo)后就不能對(duì)結(jié)果集進(jìn)行操作。

    ②隱式靜態(tài)游標(biāo):和顯式游標(biāo)不同,它被PL/SQL自己主動(dòng)管理,也被稱(chēng)為SQL游標(biāo)。

  • 顯示游標(biāo)的使用
    語(yǔ)法

cursor cursor_name[(parameter_name datatype,...)] is select_statement; 使用步驟:聲明、打開(kāi)、讀取數(shù)據(jù)、關(guān)閉①聲明游標(biāo) declare cursor cursor_name is select_statement; ②打開(kāi)游標(biāo)(游標(biāo)一旦被打開(kāi),結(jié)果就是靜態(tài)的了) open cursor_name; ③讀取數(shù)據(jù) 讀取數(shù)據(jù)要用到fetch,它能夠吧游標(biāo)指向位置的記錄讀取到pl/sql聲明的變量中。 fetch cursor_name into record_name ④關(guān)閉游標(biāo) close cursor_name;

游標(biāo)中簡(jiǎn)單的loop語(yǔ)句
eg:

declarecursor test_cursor is select * from test1;test_id test1.id%type;test_name test1.name%type;test_money test1.money%type;beginopen test_cursor;loopfetch test_cursor into test_id,test_name,test_money;exit when test_cursor%notfound;dbms_output.put_line('.....');end loop;close test_cursor; end;

須要注意的是:使用fetch…into..提取數(shù)據(jù)的時(shí)候的單條提取,數(shù)據(jù)量較大時(shí)效率比較低。

使用fetch…bulk collect into 提取大數(shù)據(jù)量的游標(biāo)數(shù)據(jù)
eg:

declarecursor emp_cursor is select * from emp;type emp_tab is table of emp%rowtype;emp_rd emp_tab; beginopen emp_cursor;loopfetch emp_cursor bulk collect into emp_rd limit 2;for i in 1...emp_rd.countloopdbms_output.put_line(......);end loop;exit when emp_cursor%notfound;end loop;close emp_cursor; end;

利用cursor … for … loop 便利游標(biāo)數(shù)據(jù)。使用簡(jiǎn)潔、方便
eg:

declarecursor test_cursor isselect * from test1; beginfor rec in test_cursorloopdbsm_output.put_line(.....);end loop; end;

帶參數(shù)的游標(biāo)
eg:

declaretest_id1 test.id%type := 1;test_id2 test.id%type := 2;cd_test test1%rowtype;cursor test_cursor(id1 number,id2 number)is select * from test1 where id in(id1,id2); beginopen test_cursor(test_id1,test_id2);loopfetch test_cursor into cd_test;exit when test_cursor%notfound;dbsm_output.put_line(...);end loop;close test_cursor; end;
  • 隱式游標(biāo)
    隱式游標(biāo)和顯式游標(biāo)有所差異,它顯沒(méi)有顯式游標(biāo)的課操作性,每當(dāng)執(zhí)行DQL或DML語(yǔ)句時(shí),PL/SQL會(huì)打開(kāi)一個(gè)隱式游標(biāo),隱式游標(biāo)不受用戶(hù)控制。


    ①隱式游標(biāo)由pl/sql自己主動(dòng)管理
    ②隱式游標(biāo)的默認(rèn)名稱(chēng)是SQL
    ③DQL和DML語(yǔ)句產(chǎn)出隱式游標(biāo)
    ④隱式游標(biāo)的屬性值是指是最新執(zhí)行的sql語(yǔ)句的。

總結(jié)

以上是生活随笔為你收集整理的SQL-Oracle游标的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。