可能的解决方法:使用子块:
DO $do$
<< main >>
DECLARE
dummy CONSTANT text; -- without assingment it's just NULL
myvar text;
BEGIN
RAISE NOTICE 'dummy is >>%<<', dummy; -- <NULL>
-- dummy := 'foo'; -- would raise exception!
SELECT INTO myvar age FROM event.age limit 1; -- some computation
<< subblock >>
DECLARE
dummy CONSTANT text := myvar; -- assign result of earlier computation
BEGIN
RAISE NOTICE 'dummy is now >>%<<', dummy; -- 'boom'
RAISE NOTICE 'same with subblock.dummy: >>%<<', subblock.dummy; -- 'boom'
RAISE NOTICE 'dummy in outer block is >>%<<', main.dummy; -- <NULL>
END;
RAISE NOTICE 'dummy is >>%<< again', dummy; -- <NULL>
END
$do$ LANGUAGE plpgsql;
继续子块直到函数结束,以准确模拟您所要求的内容。
请注意,子块的成本开销很小,因此除非您需要它,否则您不会使用它。
另外:永远不要引用'plpgsql'。这是一个标识符。