DO $demo_do$
DECLARE
demo_table VARCHAR ( 64 );
val TEXT;
sql TEXT;
err TEXT;
msg TEXT;
BEGIN
-- 创建临时表 demo_dept
demo_table = 'demo_dept';
sql = 'drop table if EXISTS ' || demo_table || ';';
sql = sql || 'CREATE temp table ' || demo_table || '(name VARCHAR(20) NULL);';
raise notice'sql___1(%)', sql;
EXECUTE ( sql );
-- 往临时表 demo_dept 写一行数据
val = 'Tom';
sql = 'INSERT into ' || demo_table || '(name) VALUES (''' || val || ''')';
raise notice'sql___2(%)', sql;
EXECUTE ( sql );
-- 异常捕获和处理(可选)
EXCEPTION
WHEN OTHERS THEN
GET stacked DIAGNOSTICS err = RETURNED_SQLSTATE,
msg = PG_EXCEPTION_DETAIL;
raise notice'err(%),msg(%)', err, msg;
-- do something
END;
$demo_do$;