database 如果表存在于 sql 语句中,则从 oracle 数据库中删除表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22875300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drop table from oracle database if table exist in sql statement
提问by user3500109
I have to use an SQL statement to drop a table, it will crash if the table doesn't exist. Is it possible to use IFstatement to drop the table
s.executeUpdate("DROP TABLE employee");
我必须使用 SQL 语句来删除一个表,如果该表不存在,它会崩溃。是否可以使用IF语句删除表
s.executeUpdate("DROP TABLE employee");
回答by Mark J. Bobak
Oracle does not support a construct like drop table if exists my_table, which is apparently legal syntax in MySQL (and possibly other RDBMSs).
Oracle 不支持像 那样的结构drop table if exists my_table,这在 MySQL(可能还有其他 RDBMS)中显然是合法的语法。
In a .SQLscript, where you're running DDLto DROPand/or CREATEvarious objects, the Oracle standard is to drop the object, and ignore the error in cases where the object does not exist. If you wish, you can write code to check if the object exists (see DBA_OBJECTSview) to only drop if it exists.
在一个.SQL脚本,在那里你正在运行DDL到DROP和/或CREATE各种物体中,甲骨文的标准是下降的对象,而忽视了错误-在对象不存在的情况下。如果您愿意,您可以编写代码来检查对象是否存在(请参阅DBA_OBJECTS视图)以仅在它存在时才删除。
from the s.executeUpdate, I gather that you're doing this in Java? If it was me, I'd just do the drop and ignore any not exists error.
从s.executeUpdate,我猜你是在用 Java 做这件事?如果是我,我会直接删除并忽略任何不存在的错误。
Hope that helps.
希望有帮助。
回答by user158017
assuming you have the right permissions, you can do something like the below
假设您拥有正确的权限,您可以执行以下操作
declare var_count int;
select count(*) INTO var_count
from all_tables where OWNER = [schema] and table_name = "EMPLOYEE";
if var_count > 0 then
begin
drop table employee;
end
adapt accordingly if you are doing this in front-end code instead of a pl/sql procedure.
如果您在前端代码而不是 pl/sql 过程中执行此操作,请相应地进行调整。
回答by user2302449
This will resolve your problem, when the table doesn't exist no error will be thrown.
这将解决您的问题,当表不存在时,不会抛出错误。
BEGIN DROP TABLE employee; EXCEPTION WHEN OTHERS THEN NULL; END;
BEGIN DROP TABLE 员工;其他人为空时的异常;结尾;
回答by EvilTeach
Traditionally in pl/sql you can specify an exception section in the block, and catch the exception if the table is not there to drop.
传统上,在 pl/sql 中,您可以在块中指定一个异常部分,并在表不在那里删除时捕获异常。
请参阅Oracle 错误处理
回答by Steve Byrne
I would use the following code:
我会使用以下代码:
s.executeUpdate("DROP TABLE IF EXISTS employee")
But depending on your verions, you could also use this:
但是根据你的版本,你也可以使用这个:
IF OBJECT_ID('dbo.employee', 'U') IS NOT NULL
DROP TABLE dbo.employee
Answer taken from: here, also this seems like it might be a duplicate of the same post? I would highly, highly recommend reading the SQL Documentation, and trying to do a bit more research before posting, but sense I see you are new, be sure to read the rules of posting here on StackOverFlow.
答案取自:here,这似乎也可能是同一帖子的副本?我强烈建议您阅读 SQL 文档,并在发布之前尝试进行更多研究,但感觉我看到您是新手,请务必阅读在 StackOverFlow 上发布的规则。

