问题描述
DO
$do$
BEGIN
IF EXISTS (SELECT FROM table1) THEN
DELETE FROM table2;
END IF;
END
$do$;
但是它要做的就是从table1中删除行(如果存在table2而不是表本身)。
可以使用与上面类似的功能吗?如果存在的话,我应该使用放置表吗?
解决方法
如果存在另一个表,则删除/删除一个表
那将是:
do $$
begin
if exists(select 1 from information_schema.tables where table_schema = current_schema() and table_name = 'table1') then
drop table table2;
end if;
end; $$ language plpgsql;
理论上:
-
从表中
-
select
进行检查不是检查其是否存在的正确方法-如果不存在,则会引发错误。相反,您可以查询信息模式。 -
要删除表,您需要
drop table
;另一方面,delete
删除表的内容,而不是表本身。