gpt4 book ai didi

sql - TYPE 可以声明为 ref cursor rowtype

转载 作者:行者123 更新时间:2023-12-04 22:12:43 26 4
gpt4 key购买 nike

 TYPE ref_cur IS REF CURSOR;
ref_cur_name ref_cur;
TYPE tmptbl IS TABLE OF ref_cur_name%ROWTYPE;
n_tmptbl tmptbl;

我试过这段代码,但无法通过编译器获得它。有没有办法将 ref 游标的结果存储到表中?

注意-我需要一个表,因为我需要访问 ref cursor 的列。使用 dbms_sql 访问 ref cursor 的记录对我来说有点困难。

更新:

/* Formatted on 8/1/2013 4:09:08 PM (QP5 v5.115.810.9015) */
CREATE OR REPLACE PROCEDURE proc_deduplicate (p_tblname IN VARCHAR2,
p_cname IN VARCHAR2,
p_cvalue IN VARCHAR2)
IS
v_cnt NUMBER;

TYPE ref_cur IS REF CURSOR;
ref_cur_name ref_cur;


v_str1 VARCHAR2 (4000);
v_str2 VARCHAR2 (4000);
v_str3 VARCHAR2 (4000);
BEGIN
v_str1 :=
'SELECT ROWID v_rowid FROM '
|| p_tblname
|| ' WHERE '
|| p_cname
|| '='''
|| p_cvalue
|| '''';


BEGIN
v_str2 :=
'SELECT COUNT ( * )

FROM '
|| p_tblname
|| ' WHERE '
|| p_cname
|| ' = '''
|| p_cvalue
|| '''';
logerrors ('proc_deduplicate',
'count exception',
SQLCODE,
v_str2 || SQLERRM,
'e');

EXECUTE IMMEDIATE v_str2 INTO v_cnt;
EXCEPTION
WHEN OTHERS
THEN
logerrors ('proc_deduplicate',
'count exception',
SQLCODE,
SQLERRM,
'e');
END;

IF v_cnt IS NOT NULL
THEN
OPEN ref_cur_name FOR v_str1;

LOOP
IF v_cnt = 1
THEN
EXIT;
ELSE
BEGIN
v_str3 :=
'DELETE FROM '
|| p_tblname
|| ' WHERE ROWID = v_rowid ';
-- THIS IS THE PROBLEM . i just created an alias above for rowid keyword but i guess, DBMS sql will have to be used after all .


EXECUTE IMMEDIATE v_str3;
EXCEPTION
WHEN OTHERS
THEN
logerrors (
' proc_deduplicate
',
' delete exception
',
SQLCODE,
SQLERRM,
' e
'
);
END;
END IF;

v_cnt := v_cnt - 1;
END LOOP;
END IF;
EXCEPTION
WHEN OTHERS
THEN
logerrors (
' proc_deduplicate',
' final exception
',
SQLCODE,
SQLERRM,
' e'
);
END;
/

最佳答案

通过发出 TYPE ref_cur IS REF CURSOR,您声明了一个弱游标。弱游标不返回指定类型。这意味着您不能声明 weak_cursor%rowtype 的变量,因为弱游标不返回任何类型。

declare
type t_rf is ref cursor;
l_rf t_rf;
type t_trf is table of l_rf%rowtype;
l_trf t_trf;
begin
null;
end;

ORA-06550: line 4, column 27:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 4, column 3:
PL/SQL: Item ignored

如果您为引用游标指定返回类型,使其变强,您的 PL/SQL block 将成功编译:

 SQL> declare                            -- strong cursor
2 type t_rf is ref cursor return [table_name%rowtype][structure];
3 l_rf t_rf;
4 type t_trf is table of l_rf%rowtype;
5 l_trf t_trf;
6 begin
7 null;
8 end;
9 /

PL/SQL procedure successfully completed

关于sql - TYPE 可以声明为 ref cursor rowtype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com