【问题标题】:Create a table using plsql使用 plsql 创建表
【发布时间】:2016-11-29 01:01:47
【问题描述】:

问题:在执行此查询时,它会抛出以下错误ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 5 01422. 00000 - "exact fetch returns more than requested number of row

其他问题是我试图在一个位置获取所有主键列,但它正在为同一主键列中的另一列创建单独的 sql 行。

DECLARE 
  v_create VARCHAR2(10000);
  v_alter          varchar2(10000);
  v_index          varchar2(10000);
BEGIN
  SELECT 'create table owner_new.' 
                ||table_name 
                ||' as select * from ' 
                ||owner 
                ||'.' 
                ||table_name 
                ||';', 
         'alter table owner_new.' 
                ||table_name 
                ||' add (Time timestamp not null default systimestamp, Action varchar2(10) not null default ''I'');', 
         'create index ' 
                ||cc.owner 
                ||'.' 
                ||cc.constraint_name 
                ||' on ' 
                ||cc.owner 
                ||'.' 
                ||cc.table_name 
                ||'(' 
                ||cc.column_name 
                ||' ASC ' 
                ||')' 
                ||';' 
  INTO   v_create, 
         v_alter, 
         v_index 
  FROM   all_tables, 
         all_cons_columns cc, 
         all_constraints pk 
  WHERE  cc.owner = pk.owner 
  AND    cc.constraint_name = pk.constraint_name 
  AND    pk.constraint_type = 'P' 
  AND    cc.owner = owner 
  AND    owner IN ('Old_owner', 
                   ) 
  EXECUTE immediate 
    v_create; 
  EXECUTE immediate 
    v_alter; 
  EXECUTE immediate 
    v_index; 
END;

【问题讨论】:

  • 不要试图发明温水 :)。使用 dbms_metadata.get_ddl stackoverflow.com/questions/10886450/…
  • SELECT INTO 查询必须准确返回一行。你的返回很多行。
  • @Motor: DBMS_METADATA.GET_DDL 不会产生 CREATE TABLE xxxx AS SELECT * FROM ...
  • @BobJarvis 为什么需要选择 *。 get_ddl 将生成具有必要语法的有效 DDL,之后可以对其进行更改。 OP想要什么?
  • @Motor: 查看 OP 代码生成的语句。显示的代码会生成一个CREATE TABLE AS SELECT *... 语句,所以我不相信 GET_DDL 调用会做他需要做的事情。

标签: oracle plsql


【解决方案1】:

您正在尝试执行将多行返回到 3 个 sqls 变量中的选择。它不会工作。

为此查询创建一个游标并立即循环执行

【讨论】:

    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多