简化PostgreSQL功能

时间:2014-09-25 14:05:32

标签: sql postgresql stored-procedures plpgsql sql-delete

我有一个PostgreSQL存储过程,其中包含以下代码:

IF something = TRUE THEN
    SELECT id INTO some_id FROM some_table WHERE some conditions LIMIT 1;
    RETURN QUERY SELECT * FROM some_table WHERE some conditions LIMIT 1;
ELSE
    SELECT id INTO some_id FROM some_table WHERE some OTHER conditions LIMIT 1;
    RETURN QUERY SELECT * FROM some_table WHERE some OTHER conditions LIMIT 1;
END IF;

DELETE FROM some_table where id = some_id;

有没有办法简化上面的代码?我想我们无能为力 关于IFELSE中重复的代码,但有没有办法避免 每次有2 SELECT个?是否可以在some_id中插入内容 RETURN QUERY

1 个答案:

答案 0 :(得分:3)

如果该功能仅发布您发布的内容,则无需执行此操作:

delete from some_table
where 
    something and (some conditions)
    or
    something is not true and (some other conditions)
returning *