在pl/sql中创建表
我想合并一个创建表的 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 脚本中创建表?我是否必须将这两个操作保留在两个不同的脚本中?
I want to merge a sql script which create a table and a pl/sql script which insert data in that table. Customer wishes
My idea was to do the table creation in pl sql. But it doesn't work. Create is not allowed in the begin part. A solution I've seen is to do this in an execute immediate statement. In practice I 've tried this :
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
I get an error on the end keyword, it wasn't expected. So my question is how to create a table in a pl/sql script? Do I have to keep those 2 actions in 2 differents scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有 2 个语法错误,均与分号有关。试试这个:
You have 2 syntax errors, both concerning semi-colons. Try this:
在查看这些片段之前,请记住,在撰写本文时,我无法访问 Oracle 数据库来以任何方式测试它们。接下来的一切都是凭记忆写的。
我假设您正在使用 sqlplus 来运行脚本。不能简单地将 create 语句和 PL/SQL 块放入文件中吗?
另一种方法是动态生成脚本并调用它
或者您可以使用没有 pl/sql 块的普通 sqlplus 方法
Before checking out the snippets please keep in mind that at the moment of this writing I didn't have access to a Oracle database to test them in any way. Everything that follows is written from memory.
I assume you are using sqlplus to run your scripts. Can't you simply put the create statement and a PL/SQL block in the file?
Another approach is to generate the script dynamically and call it
Or you can use a plain sqlplus approach without pl/sql blocks