设置“开始于” Oracle 序列的动态值
我正在尝试创建一个可以部署在多个数据库上的发布脚本,但数据可以在以后重新合并在一起。处理此问题的明显方法是在后续部署中将生产数据的序列号设置得足够高,以防止冲突。
问题在于提出一个发布脚本,该脚本将接受环境编号并适当地设置序列的“开始于”值。理想情况下,我想使用这样的东西:
ACCEPT EnvironNum PROMPT 'Enter the Environment Number: '
--[more scripting]
CREATE SEQUENCE seq1 START WITH &EnvironNum*100000;
--[more scripting]
这不起作用,因为您无法计算 DDL 中的数字表达式。
另一种选择是通过 PL/SQL 使用动态 SQL 创建序列。
ACCEPT EnvironNum PROMPT 'Enter the Environment Number: '
--[more scripting]
EXEC execute immediate 'CREATE SEQUENCE seq1 START WITH ' || &EnvironNum*100000;
--[more scripting]
但是,我宁愿避免这种解决方案,因为我通常会尝试避免在 PL/SQL 中发出 DDL。
最后,我提出的第三个选项是简单地接受 Start With 值作为替换变量,而不是环境号。
有人对如何解决这个问题有更好的想法吗?
I'm trying to create a release script that can be deployed on multiple databases, but where the data can be merged back together at a later date. The obvious way to handle this is to set the sequence numbers for production data sufficiently high in subsequent deployments to prevent collisions.
The problem is in coming up with a release script that will accept the environment number and set the "Start With" value of the sequences appropriately. Ideally, I'd like to use something like this:
ACCEPT EnvironNum PROMPT 'Enter the Environment Number: '
--[more scripting]
CREATE SEQUENCE seq1 START WITH &EnvironNum*100000;
--[more scripting]
This doesn't work because you can't evaluate a numeric expression in DDL.
Another option is to create the sequences using dynamic SQL via PL/SQL.
ACCEPT EnvironNum PROMPT 'Enter the Environment Number: '
--[more scripting]
EXEC execute immediate 'CREATE SEQUENCE seq1 START WITH ' || &EnvironNum*100000;
--[more scripting]
However, I'd prefer to avoid this solution as I generally try to avoid issuing DDL in PL/SQL.
Finally, the third option I've come up with is simply to accept the Start With value as a substitution variable, instead of the environment number.
Does anyone have a better thought on how to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
COLUMN XX NEW_VALUE YY
语法在SQL*Plus中执行计算并将结果存储在变量中:you can use the
COLUMN XX NEW_VALUE YY
syntax to perform calculation in SQL*Plus and store the result in a variable:如果您的数据库数量相当有限,则可以使用不同的值启动序列,然后定义增量,以便序列值不会发生冲突。这将消除起始值中的表达式。
因此,如果您有 10 个数据库:
数据库 1 的 startval 为 1,数据库 2 的 startval 为 2,依此类推
(这也消除了增量值增长到下一个数据库范围时序列重叠的问题)
If you have a reasonably limited number of databases you could start the sequences with a different values and then define an increment so that the sequence values do not collide. This would eliminate the expression in the start value.
So if you have 10 databases:
and startval is 1 for database 1, 2 for database 2, etc.
(This also eliminates the problem of sequences overlapping if the increment values grow into the next database's range)
我使用的一个技巧是从主脚本创建一个 sqlplus 脚本,然后执行它:
也许类似
这样应该创建一个已评估
&EnvironNum
的脚本文件(假设用户输入“275” , 例如):One trick I've used is to create an sqlplus script from the main script and then execute it:
maybe something like
This should create a script file with
&EnvironNum
already evaluated (assuming user inputed '275', for example):