如何仅执行 SQL 文件中的选择性语句?
我有一个 .sql 文件,其中包含一系列创建表语句,如下所示:
crtab.sql
define ll='&1';
define ul='&2';
create table TAB1 (...);
create table TAB2 (...);
create table TAB3 (...);
...
create table TAB1000 (...);
用户传递两个参数作为参数 - 下限 ll
和上限 ul
,它们表示表编号的范围被创建。
例如,
sqlplus crtab.sql 3 67
应仅创建表 TAB3
到 TAB67
。
如何在我的 .sql
文件中实现此逻辑?
I have a .sql file that has a sequence of create table statements, like this:
crtab.sql
define ll='&1';
define ul='&2';
create table TAB1 (...);
create table TAB2 (...);
create table TAB3 (...);
...
create table TAB1000 (...);
The user passes as arguments two parameters - the lower limit ll
and the upper limit ul
, which signify the range of table numbers to be created.
For example,
sqlplus crtab.sql 3 67
should create only tables TAB3
to TAB67
.
How can I implement this logic in my .sql
file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
并按以下方式插入该数组中的所有查询:
myArray(1) := 'CREATE TAB 1 ...';
...
myArray( 1000) := '创建选项卡 1000 ...';
2.
循环 ll 到 ul 并执行这些查询,
即
我对任何语法错误感到抱歉。
and insert all the queries in that array in the following manner:
myArray(1) := 'CREATE TAB 1 ...';
...
myArray(1000) := 'CREATE TAB 1000 ...';
2.
Loop through ll to ul and execute those queries,
i.e.
I am sorry for any of the syntax errors.
也许像这些
Maybe like these
假设表结构相同,您最好创建 &根据您的 ll 和 ul 在循环中执行动态 sql 语句。例如(在 sql server 语法中)
如果表结构不同,则只需在每个创建周围放置一个 if 语句。
Assuming the table structures are the same, you'd be better off creating & executing dynamic sql statements in a loop, based on your ll and ul. e.g. (in sql server syntax)
If the table structures are different then just put an if statement around each create.
有几种方法可以做到这一点。首先,您可以将匿名块与语句的嵌套表或语句的关联数组一起使用。嵌套表方法需要对现有脚本进行稍微较少的更改,但存在数字乱序的风险。请注意,我使用的是替代引用机制,例如 q'[character]... [character]',以防万一您的 DDL 包含一些撇号。
此代码与其他一些答案类似,但使用 Oracle 语法,并且不需要创建其他对象。
嵌套表脚本:
关联数组脚本:
如果您正在寻找一种使脚本与其当前形式几乎相同的方法,另一种方法是运行整个脚本,然后在末尾添加一个匿名块来删除不需要的表。这使脚本的顶部非常简单,但显然可能会出现一些自动删除表的问题。
There are several ways to do this. First, you can use an anonymous block with either a nested table of the statements or an associative array of the statements. The nested table approach requires slightly less change to your existing script but there is a risk that the numbers will get out of sequence. Note that I'm using the alternative quoting mechanism, e.g. q'[character]... [character]', just in case your DDL contains some apostrophes.
This code is similar to some of the other answers, but with Oracle syntax and doesn't require creating additional objects.
Nested table script:
Associative array script:
If you're looking for a way to keep your script almost identical to its current form, another approach would be to run the entire script and then have an anonymous block at the end that drops the unwanted tables. This keeps the top of your script really simple, but obviously there may be some concerns with automatically dropping tables.
一种破解方法是使用 C 预处理器和 #if 指令来包含或不包含基于您在命令行上定义的某些宏的每个语句。然后运行预处理的文件而不是原始文件。
One hacked way to do it would be to use the C preprocessor and
#if
directives to include or not each statement baed on some macros you define on the command line. Then run the preprocessed file rather than the original.