pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Insert Function
3+ messages / 2 participants
[nested] [flat]

* Insert Function
@ 2002-07-10 19:59  David Durst <ddurst@la-rubber.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David Durst @ 2002-07-10 19:59 UTC (permalink / raw)
  To: pgsql-sql

Is there anyway to create a insert function?
I am trying:
CREATE FUNCTION add_user(varchar(20),varchar(20),varchar(20),int4,int4)
RETURNS int4 AS 'INSERT INTO usr
(user_name,first_name,last_name,permission_set_id,customer_id) values
($1,$2,$3,$4,$5)' language 'sql';

and get:

ERROR:  function declared to return integer, but final statement is not a
SELECT
I thought that a insert would return a internal row #, but I am not sure
about this.

Thanks,
David Durst
MIS Manager
www.la-rubber.com





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Insert Function
@ 2002-07-10 20:45  Ian Barwick <barwick@gmx.net>
  parent: David Durst <ddurst@la-rubber.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Ian Barwick @ 2002-07-10 20:45 UTC (permalink / raw)
  To: David Durst <ddurst@la-rubber.com>; pgsql-sql

On Wednesday 10 July 2002 21:59, David Durst wrote:
> Is there anyway to create a insert function?
> I am trying:
> CREATE FUNCTION add_user(varchar(20),varchar(20),varchar(20),int4,int4)
> RETURNS int4 AS 'INSERT INTO usr
> (user_name,first_name,last_name,permission_set_id,customer_id) values
> ($1,$2,$3,$4,$5)' language 'sql';
>
> and get:
>
> ERROR:  function declared to return integer, but final statement is not a
> SELECT
> I thought that a insert would return a internal row #, but I am not sure
> about this.

You can do something like this:

CREATE FUNCTION add_user(varchar(20),varchar(20),varchar(20),int4,int4)
RETURNS VARCHAR AS 
'INSERT INTO usr
 (user_name,first_name,last_name,permission_set_id,customer_id) 
 VALUES
 ($1,$2,$3,$4,$5);
 SELECT ''created user ''|| $1::VARCHAR'
 language 'sql';

HTH

Ian Barwick




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Insert Function
@ 2002-07-11 06:38  Ian Barwick <barwick@gmx.net>
  parent: Ian Barwick <barwick@gmx.net>
  0 siblings, 0 replies; 3+ messages in thread

From: Ian Barwick @ 2002-07-11 06:38 UTC (permalink / raw)
  To: David Durst <ddurst@la-rubber.com>; +Cc: pgsql-sql

On Wednesday 10 July 2002 23:04, David Durst wrote:
> I am not sure what the end select does, so if you can give me a explination
> it would be apreciated

It is there to satisfy the requirement that the function should return
a value from a select statement:

"12.2. Query Language (SQL) Functions

 SQL functions execute an arbitrary list of SQL statements, returning the
  result of the last query in the list, which must be a SELECT. ..."

(see http://www.postgresql.org/idocs/index.php?xfunc-sql.html )

After an INSERT statement you need to provide an arbitrary
select statement, which could return anything. All the example
does is return a string telling you what you've just done.

Ian Barwick

> > On Wednesday 10 July 2002 21:59, David Durst wrote:
> >> Is there anyway to create a insert function?
> >> I am trying:
> >> CREATE FUNCTION
> >> add_user(varchar(20),varchar(20),varchar(20),int4,int4) RETURNS int4
> >> AS 'INSERT INTO usr
> >> (user_name,first_name,last_name,permission_set_id,customer_id) values
> >> ($1,$2,$3,$4,$5)' language 'sql';
> >>
> >> and get:
> >>
> >> ERROR:  function declared to return integer, but final statement is
> >> not a SELECT
> >> I thought that a insert would return a internal row #, but I am not
> >> sure about this.
> >
> > You can do something like this:
> >
> > CREATE FUNCTION add_user(varchar(20),varchar(20),varchar(20),int4,int4)
> > RETURNS VARCHAR AS
> > 'INSERT INTO usr
> > (user_name,first_name,last_name,permission_set_id,customer_id)
> > VALUES
> > ($1,$2,$3,$4,$5);
> > SELECT ''created user ''|| $1::VARCHAR'
> > language 'sql';
> >
> > HTH
> >
> > Ian Barwick
> >
> >
> > ---------------------------(end of
> > broadcast)--------------------------- TIP 5: Have you checked our
> > extensive FAQ?
> >
> > http://www.postgresql.org/users-lounge/docs/faq.html




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-07-11 06:38 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-10 19:59 Insert Function David Durst <ddurst@la-rubber.com>
2002-07-10 20:45 ` Ian Barwick <barwick@gmx.net>
2002-07-11 06:38   ` Ian Barwick <barwick@gmx.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox