2009-09-23 01:43:43 +02:00
|
|
|
<!--
|
2010-04-03 09:23:02 +02:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/ref/do.sgml,v 1.6 2010/04/03 07:23:00 petere Exp $
|
2009-09-23 01:43:43 +02:00
|
|
|
PostgreSQL documentation
|
|
|
|
-->
|
|
|
|
|
|
|
|
<refentry id="SQL-DO">
|
|
|
|
<refmeta>
|
2010-04-03 09:23:02 +02:00
|
|
|
<refentrytitle>DO</refentrytitle>
|
2009-09-23 01:43:43 +02:00
|
|
|
<manvolnum>7</manvolnum>
|
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>DO</refname>
|
|
|
|
<refpurpose>execute an anonymous code block</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<indexterm zone="sql-do">
|
|
|
|
<primary>DO</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<indexterm zone="sql-do">
|
|
|
|
<primary>anonymous code blocks</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<refsynopsisdiv>
|
|
|
|
<synopsis>
|
2010-02-16 01:49:42 +01:00
|
|
|
DO [ LANGUAGE <replaceable class="PARAMETER">lang_name</replaceable> ] <replaceable class="PARAMETER">code</replaceable>
|
2009-09-23 01:43:43 +02:00
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>DO</command> executes an anonymous code block, or in other
|
|
|
|
words a transient anonymous function in a procedural language.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The code block is treated as though it were the body of a function
|
|
|
|
with no parameters, returning <type>void</>. It is parsed and
|
|
|
|
executed a single time.
|
|
|
|
</para>
|
2009-09-23 17:41:51 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
The optional <literal>LANGUAGE</> clause can be written either
|
|
|
|
before or after the code block.
|
|
|
|
</para>
|
2009-09-23 01:43:43 +02:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">code</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The procedural language code to be executed. This must be specified
|
|
|
|
as a string literal, just as in <command>CREATE FUNCTION</>.
|
|
|
|
Use of a dollar-quoted literal is recommended.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">lang_name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the procedural language the code is written in.
|
2010-01-26 17:33:40 +01:00
|
|
|
If omitted, the default is <literal>plpgsql</>.
|
2009-09-23 01:43:43 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The procedural language to be used must already have been installed
|
|
|
|
into the current database by means of <command>CREATE LANGUAGE</>.
|
2010-01-26 17:33:40 +01:00
|
|
|
<literal>plpgsql</> is installed by default, but other languages are not.
|
2009-09-23 01:43:43 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The user must have <literal>USAGE</> privilege for the procedural
|
|
|
|
language, or must be a superuser if the language is untrusted.
|
|
|
|
This is the same privilege requirement as for creating a function
|
|
|
|
in the language.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 id="sql-do-examples">
|
|
|
|
<title id="sql-do-examples-title">Examples</title>
|
|
|
|
<para>
|
2009-09-23 17:41:51 +02:00
|
|
|
Grant all privileges on all views in schema <literal>public</> to
|
|
|
|
role <literal>webuser</>:
|
2009-09-23 01:43:43 +02:00
|
|
|
<programlisting>
|
2009-09-23 17:41:51 +02:00
|
|
|
DO $$DECLARE r record;
|
2009-09-23 01:43:43 +02:00
|
|
|
BEGIN
|
2009-09-23 17:41:51 +02:00
|
|
|
FOR r IN SELECT table_schema, table_name FROM information_schema.tables
|
|
|
|
WHERE table_type = 'VIEW' AND table_schema = 'public'
|
2009-09-23 01:43:43 +02:00
|
|
|
LOOP
|
2009-09-23 17:41:51 +02:00
|
|
|
EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser';
|
2009-09-23 01:43:43 +02:00
|
|
|
END LOOP;
|
|
|
|
END$$;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
There is no <command>DO</command> statement in the SQL standard.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2010-04-03 09:23:02 +02:00
|
|
|
<member><xref linkend="sql-createlanguage"></member>
|
2009-09-23 01:43:43 +02:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|