我有一个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;
有没有办法简化上面的代码?我想我们无能为力
关于IF
和ELSE
中重复的代码,但有没有办法避免
每次有2 SELECT
个?是否可以在some_id
中插入内容
RETURN QUERY
?
答案 0 :(得分:3)
如果该功能仅发布您发布的内容,则无需执行此操作:
delete from some_table
where
something and (some conditions)
or
something is not true and (some other conditions)
returning *