仅在表存在时删除行

Leo*_*ado 0 sql oracle

如果以前创建了一个表,我需要在我的数据库上运行一个delete语句.

问题是 - 我不能只运行delete语句,因为产品不在每个客户端的生产环境中 - 因此,他们没有我想要运行delete语句的表,并且最终会出现错误00942. 00000 - "table or view does not exist".

一个例子:

我想运行这样的东西:

IF EXISTS (TABLE TB_FIELD)
    DELETE FROM TB_FIELD WHERE ID = '213';
Run Code Online (Sandbox Code Playgroud)

如果没有通用语句,我想要一个可以运行Oracle数据库的语句

小智 5

这是Oracle的一个.这假设当前用户拥有该表.如果你要更新别人的表,你需要用dba_tables交换user_tables.

declare
    table_name_l    user_tables.table_name%type;
begin
    select table_name into table_name_l from user_tables where table_name = 'TB_FIELD';
    -- if we didn't raise an exception the table must exist 
    execute immediate 'delete from tb_field where id = :1' using '213';

    exception 
        when no_data_found then 
            -- do nothing - table doesn't exist
            null;
end;
Run Code Online (Sandbox Code Playgroud)