Thread: Creating functions

Creating functions

From
Félix Sánchez Rodríguez
Date:
Are the functions in PostgreSQL like the Stored Procedures on SQL Server?? If so, could someone post a simple function which for instance inserts a new record. I just want to have a preview of the syntax of the code. I later will look up some documentation on this subject.
 
Thanks in advance.

Re: Creating functions

From
Scott Marlowe
Date:
2009/4/26 Félix Sánchez Rodríguez <fesanch@ciego.cult.cu>:
> Are the functions in PostgreSQL like the Stored Procedures on SQL Server??
> If so, could someone post a simple function which for instance inserts a new
> record. I just want to have a preview of the syntax of the code. I later
> will look up some documentation on this subject.

There are lots of examples in the docs, but here's a super simple one in sql:

# create table test (a int);
# create function addone(x int) returns int as $$
# insert into test (a) values ($1); select $1;
$$ language sql;
# select addone(5);
 addone
--------
      5
(1 row)
# select * from test;
 a
---
 5
(1 row)

Re: Creating functions

From
Félix Sánchez Rodríguez
Date:
Hi:

I tried the code you gave me and it didn't work. I got this error:
unterminated dollar-quoted string at or near "$$ insert into test(a)
values($1);"

To be more specfic, I need to create a function to insert a given string
into a text type field. I don't need to return anything. I'm also wondering
if you could attach some documentation on this subject. I have troubles with
the Internet account.

Thanks in advance

----- Original Message -----
From: "Scott Marlowe" <scott.marlowe@gmail.com>
To: "Félix Sánchez Rodríguez" <fesanch@ciego.cult.cu>
Cc: <pgsql-admin@postgresql.org>
Sent: Monday, April 27, 2009 7:28 AM
Subject: Re: [ADMIN] Creating functions


2009/4/26 Félix Sánchez Rodríguez <fesanch@ciego.cult.cu>:
> Are the functions in PostgreSQL like the Stored Procedures on SQL Server??
> If so, could someone post a simple function which for instance inserts a
> new
> record. I just want to have a preview of the syntax of the code. I later
> will look up some documentation on this subject.

There are lots of examples in the docs, but here's a super simple one in
sql:

# create table test (a int);
# create function addone(x int) returns int as $$
# insert into test (a) values ($1); select $1;
$$ language sql;
# select addone(5);
 addone
--------
      5
(1 row)
# select * from test;
 a
---
 5
(1 row)



Re: Creating functions

From
Scott Marlowe
Date:
2009/5/6 Félix Sánchez Rodríguez <fesanch@ciego.cult.cu>:
> Hi:
>
> I tried the code you gave me and it didn't work. I got this error:
> unterminated dollar-quoted string at or near "$$ insert into test(a)
> values($1);"

Yeah, I had an extra # in there.  should be:

# create table test (a int);
# create function addone(x int) returns int as $$
insert into test (a) values ($1); select $1;
$$ language sql;

> To be more specfic, I need to create a function to insert a given string
> into a text type field. I don't need to return anything. I'm also wondering
> if you could attach some documentation on this subject. I have troubles with
> the Internet account.

The docs are kind of big to include here.  I'll see what I can find
and send off list.