SQL 脚本中的条件
我有一个由 SQL*Plus 执行的 SQL 脚本,需要与 Oracle 10g 和 Oracle 11g 一起运行。
该脚本为 11g 之前不存在的包提供授权:
GRANT EXECUTE ON sys.dbms_result_cache TO my_user;
我想避免 10g 上的异常,因为我想对脚本中的其他异常做出反应。
一种方法是使用条件编译 和 dbms_db_version
:
BEGIN
$IF dbms_db_version.ver_le_10 $THEN NULL; $ELSE
EXECUTE IMMEDIATE 'GRANT EXECUTE ON sys.dbms_result_cache TO my_user';
$END
END;
/
是否还有其他方法,最好不使用 PL/SQL?
I've got an SQL-script executed by SQL*Plus, that needs to run with Oracle 10g and Oracle 11g.
That script gives grants on a package that does not exist before 11g:
GRANT EXECUTE ON sys.dbms_result_cache TO my_user;
I would like to avoid the exception on 10g, since I want to react to other exceptions in the script.
One way is to use Conditional Compilation and dbms_db_version
:
BEGIN
$IF dbms_db_version.ver_le_10 $THEN NULL; $ELSE
EXECUTE IMMEDIATE 'GRANT EXECUTE ON sys.dbms_result_cache TO my_user';
$END
END;
/
Is there any other way, preferable without using PL/SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题和评论之一表明您希望避免 PL/SQL 块并立即执行。我还假设“对其他异常做出反应”是指在遇到异常时中止脚本的执行。
如果是这样,我认为在纯 SQL/SQL*Plus 中你能做的最好的事情就是忽略 grant 语句的异常退出:
Your question and one of the comments indicate that you want to avoid PL/SQL blocks and EXECUTE IMMEDIATE. I also assume that by "react to other exceptions" you mean abort execution of the script when an exception is encountered.
If so, I think the best you can do in pure SQL/SQL*Plus is to ignore the exception exit for the grant statement:
您可以事先检查该对象是否存在:
you could check if the object exists beforehand:
您可以通过编写生成 SQL 并将其假脱机到 SQL 脚本的 SQL 来模拟分支。然后运行sql脚本:
感谢@Vincent的数据字典查询。
You can simulate branching by writing SQL that generates SQL and spools it to a sql script. Then run the sql script:
Thanks to @Vincent for the data dictionary query.