最近在研究postgresql,发现了其类似oracle declare … begin… end,即执行匿名块的功能,
语法如下
DO [ LANGUAGE lang_name ] code
附上例子2个
例子1:Grant all privileges on all views in schema public to role webuser:
DO $$DECLARE r record;
BEGIN
FOR r IN SELECT table_schema, table_name FROM information_schema.tables
WHERE table_type = 'VIEW' AND table_schema = 'public'
LOOP
EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
END LOOP;
END$$;
例子2:
do language plpgsql $$
declare
begin
raise notice 'hello postgresql world';
end $$;
自己动手去实验吧。
参考链接:https://www.postgresql.org/docs/current/sql-do.html