Help Center > > Developer Guide> SQL Reference> SQL Syntax> DO

DO

Updated at: Mar 13, 2020 GMT+08:00

Function

DO executes an anonymous code block.

A code block is a function body without parameters that returns void. It is analyzed and executed at the same time.

Precautions

  • Before using a programming language, install it in the current database using CREATE LANGUAGE. If no language is specified, plpgsql is installed by default.
  • To use an untrusted language, you must be a system administrator or have the USAGE permission for programming languages.

Syntax

1
DO [ LANGUAGE lang_name ] code;

Parameter Description

  • lang_name

    Parses the programming language used by the code. If not specified, the default value plpgsql is used.

  • code

    Specifies executable programming language code. The language is specified as a string.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-- Create user webuser:
CREATE USER webuser PASSWORD 'Bigdata123@';

-- Grant to user webuser all the operation permissions on views in the tpcds schema:
DO $$DECLARE r record;
BEGIN
    FOR r IN SELECT c.relname,n.nspname FROM pg_class c,pg_namespace n 
             WHERE c.relnamespace = n.oid AND n.nspname = 'tpcds' AND relkind IN ('r','v')
    LOOP
        EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
    END LOOP;
END$$;


-- Delete user webuser:
DROP USER webuser CASCADE;

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel