Обсуждение: anonymous block in Postgres - Hello World

Поиск
Список
Период
Сортировка

anonymous block in Postgres - Hello World

От
"Abraham, Danny"
Дата:

#!/bin/sh

psql --quiet  -t << EOF

declare

   x varchar(80);

begin

   x:='Hello World';

   raise notice x;

end;

\q

EOF

 

======

 

Hi,

 

This code is my first “like Oracle anonymous blocl”. It does not go through.

 

Can anyone help?

 

Thanks

 

Danny

 

Re: anonymous block in Postgres - Hello World

От
Tom Lane
Дата:
"Abraham, Danny" <danny_abraham@bmc.com> writes:
> This code is my first "like Oracle anonymous blocl". It does not go =
> through.

There are no anonymous blocks in Postgres --- you must create a
function.

            regards, tom lane

Re: anonymous block in Postgres - Hello World

От
"Igor Neyman"
Дата:
Coming from Oracle world, I also was missing the ability to execute anonymous blocks.
So I wrote this function:

CREATE OR REPLACE FUNCTION exec_plpgsql_block(exec_string text)
RETURNS BOOLEAN
AS $THIS$
DECLARE lRet BOOLEAN;
BEGIN
EXECUTE 'CREATE OR REPLACE FUNCTION any_block()
RETURNS VOID
AS $$ ' || exec_string || ' $$LANGUAGE PLPGSQL;' ;
PERFORM any_block();
RETURN TRUE;
END;
$THIS$LANGUAGE PLPGSQL;

to which I pass my "anonymous" block as a parameter.
As you can see, this function creates/replaces "on the fly" function "any_block()" and executes it.
Pretty simple solution.

Igor Neyman


-----Original Message-----
From: pgsql-admin-owner@postgresql.org [mailto:pgsql-admin-owner@postgresql.org] On Behalf Of Tom Lane
Sent: Thursday, May 24, 2007 11:12 AM
To: Abraham, Danny
Cc: pgsql-admin@postgresql.org; Devrim GÜNDÜZ
Subject: Re: [ADMIN] anonymous block in Postgres - Hello World

"Abraham, Danny" <danny_abraham@bmc.com> writes:
> This code is my first "like Oracle anonymous blocl". It does not go =
> through.

There are no anonymous blocks in Postgres --- you must create a function.

            regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org


Re: anonymous block in Postgres - Hello World

От
Tom Lane
Дата:
"Igor Neyman" <ineyman@perceptron.com> writes:
> CREATE OR REPLACE FUNCTION exec_plpgsql_block(exec_string text)
> RETURNS BOOLEAN
> AS $THIS$
> DECLARE lRet BOOLEAN;
> BEGIN
> EXECUTE 'CREATE OR REPLACE FUNCTION any_block()
> RETURNS VOID
> AS $$ ' || exec_string || ' $$LANGUAGE PLPGSQL;' ;
> PERFORM any_block();
> RETURN TRUE;
> END;
> $THIS$LANGUAGE PLPGSQL;

That hasn't failed for you yet?  It will the first time you use $$
in the argument.

Use quote_literal() please ...

            regards, tom lane