2 doc/src/sgml/ref/do.sgml
3 PostgreSQL documentation
8 <refentrytitle>DO</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
15 <refpurpose>execute an anonymous code block</refpurpose>
18 <indexterm zone="sql-do">
22 <indexterm zone="sql-do">
23 <primary>anonymous code blocks</primary>
28 DO [ LANGUAGE <replaceable class="PARAMETER">lang_name</replaceable> ] <replaceable class="PARAMETER">code</replaceable>
33 <title>Description</title>
36 <command>DO</command> executes an anonymous code block, or in other
37 words a transient anonymous function in a procedural language.
41 The code block is treated as though it were the body of a function
42 with no parameters, returning <type>void</>. It is parsed and
43 executed a single time.
47 The optional <literal>LANGUAGE</> clause can be written either
48 before or after the code block.
53 <title>Parameters</title>
57 <term><replaceable class="PARAMETER">code</replaceable></term>
60 The procedural language code to be executed. This must be specified
61 as a string literal, just as in <command>CREATE FUNCTION</>.
62 Use of a dollar-quoted literal is recommended.
68 <term><replaceable class="PARAMETER">lang_name</replaceable></term>
71 The name of the procedural language the code is written in.
72 If omitted, the default is <literal>plpgsql</>.
83 The procedural language to be used must already have been installed
84 into the current database by means of <command>CREATE LANGUAGE</>.
85 <literal>plpgsql</> is installed by default, but other languages are not.
89 The user must have <literal>USAGE</> privilege for the procedural
90 language, or must be a superuser if the language is untrusted.
91 This is the same privilege requirement as for creating a function
96 <refsect1 id="sql-do-examples">
97 <title id="sql-do-examples-title">Examples</title>
99 Grant all privileges on all views in schema <literal>public</> to
100 role <literal>webuser</>:
102 DO $$DECLARE r record;
104 FOR r IN SELECT table_schema, table_name FROM information_schema.tables
105 WHERE table_type = 'VIEW' AND table_schema = 'public'
107 EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
110 </programlisting></para>
114 <title>Compatibility</title>
117 There is no <command>DO</command> statement in the SQL standard.
122 <title>See Also</title>
124 <simplelist type="inline">
125 <member><xref linkend="sql-createlanguage"></member>