我有一个创建函数语句,我想将其包装在 DO 语句中以检查该函数是否存在。但是,我对语法有点困惑。任何帮助我都感激不尽。谢谢!
功能、工作原理:
CREATE FUNCTION write_pos_history()
RETURNS TRIGGER AS $$
BEGIN
EXECUTE ...
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
尝试换行,错误:输入结束时函数定义意外结束
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_proc
WHERE proname = 'write_pos_history'
AND pg_function_is_visible(oid)) THEN
CREATE FUNCTION write_pos_history()
RETURNS TRIGGER AS $$
BEGIN
EXECUTE ...
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
END IF;
END $$;