创建可重新运行的 Oracle DDL SQL 脚本
我们的开发团队在本地计算机上进行所有开发,包括数据库。当我们对模式进行更改时,我们将 SQL 保存到一个文件中,然后将其发送到版本控制系统(如果有更好的实践,我也愿意听听)。
在 SQL Server 上工作时,我们会将更新包装在“如果存在”语句周围,以使它们可重新运行。我没有从事 Oracle 10g 项目,并且找不到任何执行相同操作的 Oracle 函数。我能够在 dbaforums.org 上找到 this 线程,但这里的答案似乎是有点笨拙。
Our development team does all of their development on their local machines, databases included. When we make changes to schema's we save the SQL to a file that is then sent to the version control system (If there is a better practice for this I'd be open to hearing about that as well).
When working on SQL Server we'd wrap our updates around "if exists" statements to make them re-runnable. I am not working on an Oracle 10g project and I can't find any oracle functions that do the same thing. I was able to find this thread on dbaforums.org but the answer here seems a bit kludgy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设这是为了某种自动化构建过程,并在出现问题时从头开始重做构建。
正如 Shannon 指出的那样,PL/SQL 对象(例如过程、函数和包)具有“创建或替换”选项,因此第二次重新编译/重新运行就可以了。补助金应该也不错。
对于表创建和 DDL,您可以采用以下方法之一。
1) 不要在脚本中添加任何删除命令,并要求您的开发团队为各个模块提供恢复脚本。
因此,对于添加到构建中的每个创建表,他们都会将等效的“DROP TABLE..”添加到脚本“build_rollback.sql”中。如果您的构建失败,您可以在从头开始运行构建之前运行此脚本。
2)第二种(也是我见过的最常用的方法)是在创建表语句之前包含 DROP 表,然后忽略构建日志中的“表或视图不存在”错误。就像......
您发布的线程有一个重大缺陷。最重要的一点是您始终以增量方式创建表。例如,您的数据库已经有 100 个表,并且您将在此版本中再添加 5 个表。该脚本对所有 100 个表进行 DROP Create 假脱机操作,然后执行它,这没有多大意义(除非您是第一次构建数据库)。
I am assuming this is for some sort of Automating the Build process and redoing the build from scratch if something fails.
As Shannon pointed out, PL/SQL objects such as Procedures, functions and Packages have the "create or replace" option, so a second recompile/re-run would be ok. Grants should be fine too.
As for Table creations and DDLs, you could take one of the following approaches.
1) Do not add any drop commands to the scripts and ask your development team to come up with the revert-script for the individual modules.
So for each create table that they add to the build, they will have an equivalent "DROP TABLE.." added to a script say."build_rollback.sql". If your build fails , you can run this script before running the build from scratch.
2)The second (and most frequently used approach I have seen) is to include the DROP table just before the create table statement and then Ignore the"Table or view does not exist" errors in the build log. Something like..
The thread you posted has a major flaw. The most important one is that you always create tables incrementally. Eg, your database already has 100 tables and you are adding 5 more as part of this release. The script spools the DROP Create for all 100 tables and then executes it which does not make a lot of sense (unless you are building your database for the first time).
除非另有配置,否则 SQL*Plus 脚本将继续过去的错误。
因此,您可以使用所有脚本:
据我所知,这是 PowerDesigner 中的一个选项。
另一种选择是编写一个 PL/SQL 脚本来清理模式,迭代模式中所有现有的表、视图、包、过程、函数、序列和同义词,发出适当的 DDL 语句来删除它们。
我会考虑分解 SQL 来创建数据库;在共享环境中维护一个包含模式所有内容的巨型脚本听起来很残忍。在架构/对象类型/名称级别进行划分可能是谨慎的做法,将完全依赖的对象类型(如表和索引)保持在一起。
An SQL*Plus script will continue past errors unless otherwise configured to.
So you could have all of your scripts use :
This is an option in PowerDesigner, I know.
Another choice would be to write a PL/SQL script which scrubs a schema, iterating over all existing tables, views, packages, procedures, functions, sequences, and synonyms in the schema, issuing the proper DDL statement to drop them.
I'd consider decomposing the SQL to create the database; one giant script containing everything for the schema sounds murderous to maintain in a shared environment. Dividing at a Schema / Object Type / Name level might be prudent, keeping fully dependent object types (like Tables and Indexes) together.