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

* Postgresql Function
@ 2012-01-18 09:03  Rehan Saleem <pk_rehan@yahoo.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Rehan Saleem @ 2012-01-18 09:03 UTC (permalink / raw)
  To: pgsql-sql

hi 
i want to create a function in postgresql that take input for columns from user for example first_name , last_name, addres. and will put them into table , and i also want to use exception if user enters wrong data. will some one help me how can i create thats function , because i am new to postgresql . i know how to do it in MS-SQL , i also have that code .if you dont understand what am trying to say . i can post sql code .thanks
REHAN SALEEM  

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

* PostgreSQL Function
@ 2012-01-18 09:10  Rehan Saleem <pk_rehan@yahoo.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Rehan Saleem @ 2012-01-18 09:10 UTC (permalink / raw)
  To: pgsql-sql

hi 
i want to create a function in postgresql that take input for columns from user for example first_name , last_name, addres. and will put them into table , and i also want to use exception if user enters wrong data. will some one help me how can i create thats function , because i am new to postgresql . i know how to do it in MS-SQL , i also have that code .if you dont understand what am trying to say . i can post sql code .thanks 


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

* Re: PostgreSQL Function
@ 2012-01-18 09:25  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Rehan Saleem <pk_rehan@yahoo.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Pavel Stehule @ 2012-01-18 09:25 UTC (permalink / raw)
  To: Rehan Saleem <pk_rehan@yahoo.com>; +Cc: pgsql-sql

Hello

2012/1/18 Rehan Saleem <pk_rehan@yahoo.com>

>  hi
> i want to create a function in postgresql that take input for columns from
> user for example first_name , last_name, addres. and will put them into
> table , and i also want to use exception if user enters wrong data. will
> some one help me how can i create thats function , because i am new to
> postgresql . i know how to do it in MS-SQL , i also have that code .if you
> dont understand what am trying to say . i can post<http://www.dbforums.com/#; sql
> code .thanks
>
>
CREATE TABLE users(id serial PRIMARY KEY, first_name varchar(10), last_name
varchar(10));

CREATE OR REPLACE FUNCTION new_user(fname varchar, lname varchar)
RETURNS int AS $$
DECLARE r int;
BEGIN
  -- custom exception -- lname cannot be empty or NEMO
  IF trim(lname) = '' OR lower(lname) = 'nemo' THEN
    RAISE EXCEPTION 'bad last_name: "%"', lname;
  END IF;
  INSERT INTO users(first_name, last_name) VALUES(lname, fname) RETURNING
id INTO r;
  RETURN r;
END;
$$ LANGUAGE plpgsql;

postgres=# select new_user('pavel','stehule');
 new_user
----------
        1
(1 row)

postgres=# select new_user('pavel','very long text');
ERROR:  value too long for type character varying(10)
CONTEXT:  SQL statement "INSERT INTO users(first_name, last_name)
VALUES(lname, fname) RETURNING id"
PL/pgSQL function "new_user" line 8 at SQL statement
postgres=# select new_user('pavel','nemo');
ERROR:  bad last_name: "nemo"
postgres=#

Regards

Pavel Stehule

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


end of thread, other threads:[~2012-01-18 09:25 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-01-18 09:03 Postgresql Function Rehan Saleem <pk_rehan@yahoo.com>
2012-01-18 09:10 PostgreSQL Function Rehan Saleem <pk_rehan@yahoo.com>
2012-01-18 09:25 ` Re: PostgreSQL Function Pavel Stehule <pavel.stehule@gmail.com>

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