【问题标题】:Passing DataTable to Oracle Procedure将 DataTable 传递给 Oracle 过程
【发布时间】:2019-09-04 00:42:53
【问题描述】:

我想将 DataTable 传递给创建临时表的 Oracle 过程。 甚至我的程序也需要更改以接受数据表作为参数。 我想要 datatable 代替 existing_table。

以下是程序:

CREATE OR REPLACE procedure temptable
is
begin
EXECUTE IMMEDIATE'CREATE GLOBAL TEMPORARY TABLE TEMP_TABLE 
ON COMMIT PRESERVE ROWS
AS
select * from existing_table';
End;

我该如何解决这个问题。请帮忙!

【问题讨论】:

  • 这和C#有关系吗?如果是这样,上下文是什么?
  • DataTable 是从 C# 传递过来的
  • 我建议搜索有关如何构建使用数据库的应用程序的入门教程

标签: oracle datatable procedure


【解决方案1】:

以下是您可以如何做到的示例:

SQL> create or replace procedure temptable (par_table_name in varchar2)
  2  is
  3    l_cnt number;
  4    l_str varchar2(200);
  5  begin
  6    -- if TEMP_TABLE already exists, drop it
  7    select count(*)
  8      into l_cnt
  9      from user_tables
 10      where table_name = 'TEMP_TABLE';
 11
 12    if l_cnt > 0 then
 13       execute immediate 'drop table temp_table';
 14    end if;
 15
 16    -- create a new TEMP_TABLE
 17    l_str := 'create global temporary table temp_table on commit preserve rows ' ||
 18             'as select * from ' || par_table_name;
 19    execute immediate (l_str);
 20  end;
 21  /

Procedure created.

SQL> exec temptable('dept');

PL/SQL procedure successfully completed.

SQL> select * from temp_table;

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK
        20 RESEARCH             DALLAS
        30 SALES                CHICAGO
        40 OPERATIONS           BOSTON

SQL>

但是,在 Oracle 中,您并没有真正动态地创建/删除表 - 您创建它们一次并多次使用它们。对于全局临时表,您只需创建一次并根据需要在不同会话中填充。

可能有您的要求(即对不同的数据源使用相同的表名),所以 - 如果是这种情况,请查看上面的代码是否有帮助。

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 2011-07-09
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多