Thread: How to run a stored PL/pgSQL function?

How to run a stored PL/pgSQL function?

From
"Ken Winter"
Date:

How can I cause a PL/pgSQL function to be executed from a SQL script?

 

I know how to invoke a function using a trigger, but I don’t know any other way.

 

I’m looking at the SQL Command Reference at http://www.postgresql.org/docs/7.4/static/sql-commands.html, but the only command that looks like a candidate to do this, namely EXECUTE, seems to use only the result of a PREPARE statement, which in turn can only accept a SELECT, INSERT, UPDATE, or DELETE statement as input, according to http://www.postgresql.org/docs/7.4/static/sql-prepare.html.

 

I’m not sure if I’m clearly saying what I need to do.  I have a PL/pgSQL “foo (varchar)”.  I have a script containing a bunch of DDL statements.

 

Somewhere in that script, I want to call or invoke (or whatever the right verb is) foo and provide its argument, so that when the script is being executed, at that point foo is executed and has its desired effect on the database.  (I don’t need any return from foo.)  But I don’t know what to put into the script to make this happen.

 

~ TIA

~ Ken

Re: How to run a stored PL/pgSQL function?

From
Michael Glaesemann
Date:
On Dec 1, 2005, at 12:38 , Ken Winter wrote:

> How can I cause a PL/pgSQL function to be executed from a SQL script?
<snip />
> I have a PL/pgSQL “foo (varchar)”.
select foo(bar); -- where bar is of type varchar


Michael Glaesemann
grzm myrealbox com




Re: How to run a stored PL/pgSQL function?

From
"Joshua D. Drake"
Date:
> Somewhere in that script, I want to call or invoke (or whatever the
> right verb is) foo and provide its argument, so that when the script
> is being executed, at that point foo is executed and has its desired
> effect on the database. (I don’t need any return from foo.) But I
> don’t know what to put into the script to make this happen.
>

select foo('bar');

or

select * from foo('bar'); if an SRF..

Joshua D. Drake


> ~ TIA
>
> ~ Ken
>


--
The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: PLphp, PLperl - http://www.commandprompt.com/


Re: How to run a stored PL/pgSQL function?

From
"Ken Winter"
Date:
That works.  Thanks!  (Thanks also to Joshua Drake, who contributed the same
answer.)

~ Ken

> -----Original Message-----
> From: Michael Glaesemann [mailto:grzm@myrealbox.com]
> Sent: Wednesday, November 30, 2005 11:19 PM
> To: Ken Winter
> Cc: PostgreSQL pg-general List
> Subject: Re: How to run a stored PL/pgSQL function?
>
>
> On Dec 1, 2005, at 12:38 , Ken Winter wrote:
>
> > How can I cause a PL/pgSQL function to be executed from a SQL script?
> <snip />
> > I have a PL/pgSQL "foo (varchar)".
> select foo(bar); -- where bar is of type varchar
>
>
> Michael Glaesemann
> grzm myrealbox com
>
>