In Oracle Database, we can use PL/SQL and create stored procedure and functions. As for PostgreSQL, we can use and create function.
Here, we write the way to execute PL/pgSQL on the spot, without creating function.
Environment
- Client: version 9.6
- Server: version 9.3
How to Do
Use DO
.
1 2 3 4 5 6 |
DO $$ DECLARE -- BEGIN -- END$$; |
If you write like aboce, PL/pgSQL is used for code processor and execute the code immediately. When we use other language such as PL/Tcl, PL/TclU, PL/Perl, PL/PerlU, PL/PythonU, write the language just after DO
, like DO plperl
.
Example
1 2 3 4 5 6 7 |
DO $$ DECLARE T VARCHAR := 'Test'; BEGIN RAISE NOTICE '%', T; RAISE NOTICE '%', (SELECT NOW()); END$$; |
DO
statement is introduced from PostgreSQL 9.0.