亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我總結了一下午都沒弄明白,實在是好麻煩,求高手幫解決,非常感謝!

我總結了一下午都沒弄明白,實在是好麻煩,求高手幫解決,非常感謝!

守候你守候我 2022-10-20 19:15:30
1:首先定義一個簡單的包create or replace package p1 isprocedure pro_p1;Type p1_cursor is ref cursor;end p1;2:然后編寫該包下的存儲過程create or replace package body p1 isprocedure pro_p1 is--定義s varchar2(100);p1_cursor is select ename from emp;beginfor loop_cursor in t loopdbms_output.put_line(loop_cursor.ename);end loop;exceptionwhen no_data_found thennull;end;end p1;PLS-00103: 出現符號 "IS"在需要下列之一時: constant exception <an identifier> <a double-quoted delimited-identifier> table LONG_ double ref char time timestamp interval date binary national character nchar我的問題1:哪里的錯誤?2:能總結一下游標可以定義的位置和使用方法嗎?我見過兩種:cursor 游標名 is select 語句 寫在declare內部通過函數參數傳入進來,然后使用open 游標 for select 語句
查看完整描述

3 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

Type p1_cursor is ref cursor;
是定義了一個動態游標類型叫p1_cursor ,如同number表示數字類型一樣。使用時要定義一個該類型的變量才可以,如:
varCur p1_cursor ;
然后再在程序體內部定義這個動態游標變量varCur是什么:
open varCur for 查詢語句。

這種定義 方式 通常用于查詢語句本身是個變量,比如根據不同的值確定從哪個表中來查時,表名是作為變量的,這時就必須使用動態游標了。在你這個簡單的應用中其實是不需要使用動態游標的。
你例中的游標是固定的查詢語句(select ename from emp),語句本身是固定的,即使加上查詢條件,也并不需要使用動態游標,這種情況下可以直接在程序頭declare段中定義即可:
cursor varCur(varName) is select ename from emp where ename=varname;

總結:1、動態游標需要先用定義type定義一個類型(如:Type p1_cursor is ref cursor),再使用這個類型定義一個游標變量(如:varCur p1_cursor);
2、動態游標一般用于查詢語句中有變量存在的情況,在程序體內部進行游標的查詢語句,如:
varSQL:='select ename from emp where ename=' || varName;
open varCur for varSQL;

3、直接使用cursor完全可以完成你例中的情況。這時是不需要用動態游標的。

查看完整回答
反對 回復 2022-10-24
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

--定義包頭
create or replace package p1 is
procedure pro_p1;
Type p1_cursor is ref cursor;
end p1;

--定義包體
create or replace package body p1 is
procedure pro_p1
is
s varchar2(100);
cur p1.p1_cursor;
begin
open cur for select ename from emp;

loop
fetch cur into s;
exit when cur%NOTFOUND;
dbms_output.put_line(s);
end loop;

exception
when no_data_found then
null;
end pro_p1;
end p1;

--調用存儲過程
begin
p1.pro_p1;
end;

--運行結果
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER


查看完整回答
反對 回復 2022-10-24
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

給你個例子,你看看吧,實在太多了
SQL> l
1 declare
2 cursor emp_cursor is
3 select ename from emp;
4 v_name emp.ename%type;
5 begin
6 open emp_cursor;
7 fetch emp_cursor into v_name;
8 while emp_cursor%found loop
9 dbms_output.put_line(v_name);
10 exit when emp_cursor%rowcount >= 5;
11 fetch emp_cursor into v_name;
12 end loop;
13 close emp_cursor;
14* end;
SQL> /


查看完整回答
反對 回復 2022-10-24
  • 3 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號