]> begriffs open source - ai-pg/blob - full-docs/txt/sql-do.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-do.txt
1
2 DO
3
4    DO — execute an anonymous code block
5
6 Synopsis
7
8 DO [ LANGUAGE lang_name ] code
9
10 Description
11
12    DO executes an anonymous code block, or in other words a transient
13    anonymous function in a procedural language.
14
15    The code block is treated as though it were the body of a function with
16    no parameters, returning void. It is parsed and executed a single time.
17
18    The optional LANGUAGE clause can be written either before or after the
19    code block.
20
21 Parameters
22
23    code
24           The procedural language code to be executed. This must be
25           specified as a string literal, just as in CREATE FUNCTION. Use
26           of a dollar-quoted literal is recommended.
27
28    lang_name
29           The name of the procedural language the code is written in. If
30           omitted, the default is plpgsql.
31
32 Notes
33
34    The procedural language to be used must already have been installed
35    into the current database by means of CREATE EXTENSION. plpgsql is
36    installed by default, but other languages are not.
37
38    The user must have USAGE privilege for the procedural language, or must
39    be a superuser if the language is untrusted. This is the same privilege
40    requirement as for creating a function in the language.
41
42    If DO is executed in a transaction block, then the procedure code
43    cannot execute transaction control statements. Transaction control
44    statements are only allowed if DO is executed in its own transaction.
45
46 Examples
47
48    Grant all privileges on all views in schema public to role webuser:
49 DO $$DECLARE r record;
50 BEGIN
51     FOR r IN SELECT table_schema, table_name FROM information_schema.tables
52              WHERE table_type = 'VIEW' AND table_schema = 'public'
53     LOOP
54         EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_i
55 dent(r.table_name) || ' TO webuser';
56     END LOOP;
57 END$$;
58
59 Compatibility
60
61    There is no DO statement in the SQL standard.
62
63 See Also
64
65    CREATE LANGUAGE