test=# DO $$DECLARE i record;
test$# BEGIN
test$# FOR i IN 1..10
test$# LOOP
test$# execute 'select loop_insert(1)';
test$# END LOOP;
test$# END$$;
test=#
DO $$
<<first_block>>
DECLARE
counter integer := 0;
BEGIN
counter := counter + 1;
RAISE NOTICE 'The current value of counter is %', counter;
END first_block $$;
'<<first_block>>
DECLARE
counter integer := 0;
BEGIN
counter := counter + 1;
RAISE NOTICE ''The current value of counter is %'', counter;
END first_block';
DO $$
<<outer_block>>
DECLARE
counter integer := 0;
BEGIN
counter := counter + 1;
RAISE NOTICE 'The current value of counter is %', counter;
DECLARE
counter integer := 0;
BEGIN
counter := counter + 10;
RAISE NOTICE 'The current value of counter in the subblock is %', counter;
RAISE NOTICE 'The current value of counter in the outer block is %', outer_block.counter;
RAISE NOTICE 'The current value of counter in the outer block is %', counter;
执行结果如下:
NOTICE: The current value of counter is 1
NOTICE: The current value of counter in the subblock is 10
NOTICE: The current value of counter in the outer block is 1
NOTICE: The current value of counter in the outer block is 1