Table of Contents

SQL Anonymous Code Block

About

An SQL anonymous code block is a SQL procedure with no parameters that returns void.

It's a procedure that is not stored in the database but in a local script file.

Example

From the Postgres HowTo:

-- https://www.postgresql.org/docs/current/sql-do.html
DO
$$DECLARE r record;
BEGIN
    IF
NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'webuser') THEN
CREATE ROLE webuser;
END IF;
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$$;


Support

Database Database Support Tabulify Support (howto)
MySQL Not supported (Stored Procedure only)
Oracle PLSQL DECLARE/BEGIN/END block Oracle Anonymous Code Block statement
Postgres SQL DO statement Do statement
SQLite Not supported
SQL Server SQL BEGIN/END block SQL Server BEGIN/END statement

FAQ

What to do if a Anonymous call block is not supported by the database

Anonymous call block is not supported by all database.