Oracle中创建游标之前先创建表

发布于 2024-12-22 19:39:10 字数 110 浏览 2 评论 0原文

我有一个 PL/SQL 过程,它创建一个临时表,然后使用游标从该临时表中提取数据,处理数据,然后删除临时表。然而,如果数据库中不存在该表,Oracle 不允许使用游标。

请帮我处理这个问题。

I have a PL/SQL procedure which creates a temporary table and then extracts the data from this temporary table using cursors, processes the data and then drops the temporary table. However Oracle doesn't allow the usage of cursor if the table doesn't exist in the database.

Please help me handle this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清晰传感 2024-12-29 19:39:10

你的说法不太正确。您可以使用游标进行几乎任意的查询。请参阅下文:(

create or replace procedure fooproc
IS
    type acursor is ref cursor;
    mycur  acursor;
    mydate date;
BEGIN
    execute immediate 'create global temporary table footmp (bar date) on commit delete rows';
    execute immediate 'insert into footmp values (SYSDATE)';
    open mycur for 'select * from footmp';
    loop
        fetch mycur into mydate;
        exit when mycur%notfound;
        dbms_output.put_line(mydate);
    end loop;
    close mycur;
    execute immediate 'drop table footmp';
END fooproc;
/

更多详细信息此处< /a> - 特别是这个短过程根本不安全,因为表名是固定的并且不依赖于会话)。

它(相当)有点难看,我并不是建议您使用它 - 相反,您应该考虑是否需要该特定于过程的临时表。

请参阅另一篇文章 :

不要动态创建它们[临时表],请不要动态创建它们——不要动态创建它们。

不能使用全局临时表吗?你真的需要一个临时表吗? (即,在用于填充该表的 select 语句上不使用游标吗?)

Your statement is not quite correct. You can use a cursor for pretty much arbitrary queries. See below:

create or replace procedure fooproc
IS
    type acursor is ref cursor;
    mycur  acursor;
    mydate date;
BEGIN
    execute immediate 'create global temporary table footmp (bar date) on commit delete rows';
    execute immediate 'insert into footmp values (SYSDATE)';
    open mycur for 'select * from footmp';
    loop
        fetch mycur into mydate;
        exit when mycur%notfound;
        dbms_output.put_line(mydate);
    end loop;
    close mycur;
    execute immediate 'drop table footmp';
END fooproc;
/

(More details here - especially this short proc is not safe at all since the table name is fixed and not session-dependent).

It is (quite) a bit ugly, and I'm not suggesting you use that - rather, you should be thinking whether you need that procedure-specific temporary table at all.

See this other article:

DO NOT dynamically create them [temp tables], DO NOT dynamically create them, please -- do NOT dynamically create them.

Couldn't you use a global temporary table? Do you actually need a temporary table at all? (i.e. doesn't using a cursor on the select statement you'd use to fill that table work?)

笑饮青盏花 2024-12-29 19:39:10

或者,如果您希望避免全局临时表和“常规”永久表之间的差异,您可能会习惯(请参阅Oracle 文档 关于临时表数据可用性、生命周期等),只需先创建表(无日志记录)。假设没有其他人使用此表,您的过程可能会在处理之前/之后截断。

Or, if you wish to avoid differences between global temporary tables and "regular" permanent tables you may be used to (see Oracle docs on temp table data availability, lifetime etc), simply create the table first (nologging). Assuming nobody else is using this table, your procedure could truncate before/after your processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文