是否有类似“如果不存在,请创建序列..."之类的内容?在Oracle SQL中? [英] Is there something like "if not exist create sequence ..." in Oracle SQL?

查看:71
本文介绍了是否有类似“如果不存在,请创建序列..."之类的内容?在Oracle SQL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用Oracle 8 DB的应用程序,我提供了一个SQL脚本来设置触发器,序列等内容,可以将其复制并粘贴到SQL * Plus中.如果我要创建的序列已经存在,我希望脚本不会因错误而停止.对于触发器,可以使用创建或替换触发器..."轻松完成此操作,但是对于序列,此操作不起作用.我还尝试过如果不存在mysequence,则创建序列...",但它也没有.有其他选择吗?

For my application that uses an Oracle 8 DB, I am providing an SQL script to setup stuff like triggers, sequences etc., which can be copied and pasted into SQL*Plus. I would like the script to not stop with an error if a sequence that I am trying to create already exists. For a Trigger this can easily be done using "create or replace trigger ...", but for a sequence this does not work. I also tried ""if not exists mysequence then create sequence ..." but it did not too. Is there some alternative?

或者,如果不可能的话,有没有办法在不存在mysequence的情况下执行丢弃序列mysequence"而不用SQL * Plus中止脚本?

Alternatively, if this is not possible, is there a way to do a "drop sequence mysequence" without SQL*Plus aborting the script if mysequence does not exist?

推荐答案

如果您确定脚本将始终在SQL * Plus下运行,则可以在CREATE SEQUENCE语句后加上指令以继续执行错误操作:

If you're sure the script will always run under SQL*Plus, you can bracket the CREATE SEQUENCE statements with a directive to continue on error:

WHENEVER SQLERROR CONTINUE
-- create sequences here, ignoring errors
WHENEVER SQLERROR EXIT SQL.SQLCODE

请注意,在创建序列语句中是否还有其他错误(权限问题,语法错误等),将被忽略

Be aware if there are other errors (permission problems, syntax failures, etc.) in the create sequence statements they will be ignored

这篇关于是否有类似“如果不存在,请创建序列..."之类的内容?在Oracle SQL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆