Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an ad-hoc script in PostgreSQL?

I'm trying to run this in PostgreSQL 9.2:

RAISE NOTICE 'hello, world!'; 

And the server says:

Error : ERROR:  syntax error at or near "RAISE" LINE 1: RAISE NOTICE 'hello, world!'         ^ 

Why?

like image 331
yegor256 Avatar asked Sep 16 '13 12:09

yegor256


People also ask

What is do $$ in PostgreSQL?

“lang_name” is simply the name of the procedural language. If the language is not mentioned, PostgreSQL will use the default procedural language, PL/pgSQL. $$ (double quoting) is a PostgreSQL substitute for single quotes to avoid quoting issues inside the BEGIN block.

How do I run an anonymous block in PostgreSQL?

To execute a block from pgAdmin, you click the Execute button as shown in the following picture: Notice that the DO statement does not belong to the block. It is used to execute an anonymous block. PostgreSQL introduced the DO statement since version 9.0.

How do you use begin and end in PostgreSQL?

PL/pgSQL's BEGIN / END are only for grouping; they do not start or end a transaction. See Section 43.8 for information on managing transactions in PL/pgSQL. Also, a block containing an EXCEPTION clause effectively forms a subtransaction that can be rolled back without affecting the outer transaction.


2 Answers

Use an anonymous code block:

DO language plpgsql $$ BEGIN   RAISE NOTICE 'hello, world!'; END $$; 

Variables are referenced using %:

RAISE NOTICE '%', variable_name; 
like image 164
Tomas Greif Avatar answered Sep 24 '22 05:09

Tomas Greif


raise is PL/pgSQL only.

http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

create or replace function r(error_message text) returns void as $$ begin     raise notice '%', error_message; end; $$ language plpgsql;  select r('an error message'); NOTICE:  an error message 
like image 25
Clodoaldo Neto Avatar answered Sep 21 '22 05:09

Clodoaldo Neto