我想合并一个创建表的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个动作保存在2个不同的脚本中?
答案 0 :(得分:4)
你有2个语法错误,都涉及分号。试试这个:
BEGIN
execute immediate 'create table CATEGORIEDECLARATION (
nIdCategorieDeclaration NUMBER(10) not null,
...
constraint PK_CATDECLA primary key (nIdCategorieDeclaration)
)';
END;
/
答案 1 :(得分:3)
在查看代码段之前,请记住,在撰写本文时,我无法访问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 SELECT 'create table CATEGORIEDECLARATION ( nIdCategorieDeclaration NUMBER(10) not null, ... constraint PK_CATDECLA primary key (nIdCategorieDeclaration) )' FROM SYS.DUAL / SPOOL OFF
@gen_cr_table_script.sql
-- you can generate the insert script here if needed -- spool gen_ins_script.sql -- select ... -- spool off -- spool 03_CREATE_CATEGORIEDECL.log -- @gen_ins_script.sql -- spool off -- add commit where appropriate
或者你可以使用没有pl / sql块的普通sqlplus方法
create table CATEGORIEDECLARATION ( nIdCategorieDeclaration NUMBER(10) not null, ... constraint PK_CATDECLA primary key (nIdCategorieDeclaration) /
INSERT INTO CATEGORIEDECLARATION (nIdCategorieDeclaration,nIdTypeFormulaire,sLibelle,sType,sAide,sTexte,sTexte2,sTypeAffichage,bAffichage,sInterval,nIdCalendrier) SELECT (seq_CATEGORIEDECLARATION.nextval,'5','Autres cas (zone de saisie libre)', 'SOMME_A_DEDUIRE','','',NULL,'CAT_AUTRE_CAS', 1, 'POSITIF',c.nidcalendrier); FROM calendrier c WHERE c.smillesime = '2010' / COMMIT /