代码之家  ›  专栏  ›  技术社区  ›  jayjaypg22

在pl/sql中创建表

  •  0
  • jayjaypg22  · 技术社区  · 15 年前

    我想合并一个创建表的SQL脚本和一个在该表中插入数据的PL/SQL脚本。客户的愿望

    我的想法是用pl-sql创建表。但它不起作用。开始部分不允许创建。我看到的解决方案是在执行立即语句中执行此操作。实际上,我尝试过:

        SET serveroutput ON
        spool 03_CREATE_CATEGORIEDECL.log
    
        BEGIN
         execute immediate 'create table CATEGORIEDECLARATION ( 
         nIdCategorieDeclaration  NUMBER(10)   not null,
         ...
         constraint PK_CATDECLA primary key (nIdCategorieDeclaration)
        )';
    
    select c.nidcalendrier into millesime from calendrier c where c.smillesime = '2010';
    
    -- Lignes relatives au formulaire CA3
    INSERT into CATEGORIEDECLARATION (nIdCategorieDeclaration,nIdTypeFormulaire,sLibelle,sType,sAide,sTexte,sTexte2,sTypeAffichage,bAffichage,sInterval,nIdCalendrier) 
    values (seq_CATEGORIEDECLARATION.nextval,'5','Autres cas  (zone de saisie libre)', 'SOMME_A_DEDUIRE','','',NULL,'CAT_AUTRE_CAS', 1, 'POSITIF',millesime);
    
    COMMIT;
        END; 
        /
        spool off
    

    我在end关键字上得到一个错误,这不是预期的。所以我的问题是如何在pl/sql脚本中创建一个表?我是否必须将这两个动作保留在两个不同的脚本中?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Tony Andrews    15 年前

    您有两个语法错误,都与分号有关。试试这个:

    BEGIN
     execute immediate 'create table CATEGORIEDECLARATION (
     nIdCategorieDeclaration  NUMBER(10)   not null,
     ...
     constraint PK_CATDECLA primary key (nIdCategorieDeclaration)
    )';
    END;
    /
    
        2
  •  3
  •   mulander    15 年前

    在检查这些代码片段之前,请记住,在撰写本文的时候,我没有任何权限访问Oracle数据库来测试它们。接下来的一切都是从记忆中写出来的。

    我假设您正在使用sqlplus运行脚本。不能简单地将create语句和pl/sql块放在文件中吗?

    
    SET serveroutput ON
    
    spool 03_CREATE_CATEGORIEDECL.log
    
    create table CATEGORIEDECLARATION ( 
         nIdCategorieDeclaration  NUMBER(10)   not null,
         ...
    constraint PK_CATDECLA primary key (nIdCategorieDeclaration)
    /
    
    
    BEGIN
      select c.nidcalendrier into millesime from calendrier c where c.smillesime = '2010';
      -- Lignes relatives au formulaire CA3
      INSERT into CATEGORIEDECLARATION (nIdCategorieDeclaration,nIdTypeFormulaire,sLibelle,sType,sAide,sTexte,sTexte2,sTypeAffichage,bAffichage,sInterval,nIdCalendrier) 
      values (seq_CATEGORIEDECLARATION.nextval,'5','Autres cas  (zone de saisie libre)', 'SOMME_A_DEDUIRE','','',NULL,'CAT_AUTRE_CAS', 1, 'POSITIF',millesime);
    
      COMMIT;
    END; 
    /
    spool off
    
    

    另一种方法是动态生成脚本并调用它

    SET serveroutput ON
    SET FEEDBACK OFF
    SET HEADING OFF
    SET LINESIZE 800
    SET PAGESIZE 0
    SET ECHO OFF

    spool gen_cr_table_script.sql 选择“创建表分类声明”( nidcategoriedClaration编号(10)不为空, … 约束pk_catdecla主键(nidcategoriedClaration) “” 来自双系统 / 线轴关闭

    @gen_cr_table_script.sql

    --如果需要,可以在此处生成插入脚本 --spool gen_ins_script.sql脚本 --选择… --线轴关闭 --spool 03_创建分类cl.log --@gen_-ins_脚本.sql --线轴关闭 --适当时添加提交

    或者可以使用不带pl/sql块的普通sqlplus方法

    create table CATEGORIEDECLARATION ( 
         nIdCategorieDeclaration  NUMBER(10)   not null,
         ...
    constraint PK_CATDECLA primary key (nIdCategorieDeclaration)
    /

    插入分类声明(nidcategoriedeclaration、nidtypeformulaire、slibelle、stype、saide、stexte、stexte2、stypeafficehage、baffichage、sinterval、nidcalendrier) 选择(顺序“categoriedeclaration.nextvall”,“5”,“autres cas(zone de saisie libre)”,“somme_a_deduire”,“,”,空值,“cat_autre cas”,1,“positif”,c.nidcalendrier); 来自日历C 其中c.smillesime='2010' / 提交 /