Re: [SQL] Function returns error (view) (RESOLVED)

2008-02-28 Thread Professor Flávio Brito
Hi

I discovered that when a person did not change the password, there is no
information into change_user_password  table, then a exception raise but
wasn't  treated. Now it is OK.

Thanks for all


CREATE OR REPLACE FUNCTION seach_password(USER_FOO varchar(100))
RETURNS SETOF vw_change_password AS
$BODY$
DECLARE
r vw_change_password%ROWTYPE;
USER_FOO alias for $1;
sql TEXT;
BEGIN
sql= 'SELECT u.cod_user, u.user_name, u.openpsw, t.user_password, t.end,
t.validate, t.date_add, t.user_time, u.ok
   FROM table_user u, change_user_password t
  WHERE u.cod_user = t.cod_user  AND t.cod_user
IN
(SELECT cod_user
FROM table_user
WHERE login='|| quote_literal(USER_FOO) ||')';

FOR r IN EXECUTE sql
 LOOP
 RETURN NEXT r;
END LOOP;
 IF NOT FOUND THEN
RAISE EXCEPTION 'User not found in change_user_password';
 END IF;
 RETURN;
 END
 $BODY$
  LANGUAGE 'plpgsql' VOLATILE;



2008/2/28, Bart Degryse <[EMAIL PROTECTED]>:
>
>  Please send the complete DDL for your function and the tables it uses.
> Also inform us of the database version you're using.
>
> >>> "Professor Flávio Brito" <[EMAIL PROTECTED]> 2008-02-27 21:42
> >>>
>
> Hi
>
> After I did it I received it
>
> SELECT * FROM search_password('Paul');
>
>
>
>
> ERROR: set-valued function called in context that cannot accept a set
> SQL state: 0A000
> Context: PL/pgSQL function "search_password(" line 14 at return next
>
> Error at WHERE login= Paul ??
>
> Thanks for your help
>
> Flávio
>
> 2008/2/27, Bart Degryse <[EMAIL PROTECTED]>:
> >
> >  How do you call your function? You should call it like this:
> > SELECT * FROM seach_password('Flavio');
> >
> > Replace Flavio with the login of someone in table_user.
> > Also watch out for the function name: if you copied my suggestion it is
> > seach_... and not search_...
> >
> > I would also suggest you replace the
> > ...t.cod_user IN (subselect)
> > by a join construction. I think it's more performant.
> >
> >
> > >>> "Professor Flávio Brito" <[EMAIL PROTECTED]> 2008-02-26
> > 19:20 >>>
> > Hi
> >
> > After I did it I received it
> >
> > ERROR: set-valued function called in context that cannot accept a set
> > SQL state: 0A000
> > Context: PL/pgSQL function "seach_password(" line 14 at return next
> >
> > Error at WHERE login= USER_FOO ??
> >
> > Thanks for your help
> >
> > Flávio
> >
> >  2008/2/26, Bart Degryse <[EMAIL PROTECTED]>:
> > >
> > >  I think you have a quoting problem
> > > You want something like
> > >   WHERE login= 'Flavo'
> > > But you're making something like
> > >   WHERE login = Flavo
> > >
> > > Something like this should work...
> > > CREATE OR REPLACE FUNCTION seach_password(USER_FOO
> > > IN table_user.login%TYPE)
> > > RETURNS SETOF vw_change_password AS
> > > $BODY$
> > > DECLARE
> > > r vw_change_password%ROWTYPE;
> > > USER_FOO alias for $1;
> > > BEGIN
> > >   FOR r IN (
> > > SELECT u.cod_user, u.user_name, u.openpsw, t.user_password, t.end,
> > > t.validate, t.date_add, t.user_time, u.ok
> > > FROM usuario u, change_user_password t
> > > WHERE u.cod_user = t.cod_user  AND t.cod_user IN (SELECT
> > > cod_user FROM table_user WHERE login= USER_FOO))
> > >   LOOP
> > > RETURN NEXT r;
> > >   END LOOP;
> > >   IF NOT FOUND THEN
> > > RAISE EXCEPTION 'USER not found (%)', USER_FOO;
> > >   END IF;
> > >   RETURN;
> > > END
> > > $BODY$
> > > LANGUAGE 'plpgsql' VOLATILE;
> > >
> > >
> > > >>> "Professor Flávio Brito" <[EMAIL PROTECTED]> 2008-02-26
> > > 17:32 >>>
> > > Hi
> > >
> > > Don't know why I can't receive a return like my view fields (I'm
> > > newbie in plpgsql). Postgresql returns me a erro . How can I received a
> > > answer like my view structure?
> > >
> > > When I Test my view I receive
> > >
> > > SELECT  seach_password('user_login_foo')
> > >
> > > My view returns me
> > >
> > > 25746;"MARCELO
> > > ";"bio1";"bio1";"2008-02-19";"FALSE";"2008-02-12";"2008-02-12 12:51:
> > > 40.229282";"TRUE"
> > > 30356;"JOSE DE JESUS
> > > ";"977";"377";"2008-02-19";"FALSE";"2008-02-12";"2008-02-12 12:52:
> > > 19.688381";"TRUE"
> > >
> > > It's OK
> > >
> > > but when I use function it returns me
> > >
> > > ERROR: column "user_login_foo" does not exist
> > > SQL state: 42703
> > > Context: PL/pgSQL function "search_password" line 14 at for over
> > > execute statement
> > >
> > > Where is my fault?
> > >
> > > Thanks
> > >
> > > Flávio
> > >
> > >
> > > *
> > > vw_change_password  attributes
> > >
> > > cod_user integer,
> > > user_name varchar(150),
> > > openpsw varchar (32),
> > > user_password varchar (50),
> > > end timestamp,
> > > validate boolean,
> > > date_add timestamp,
> > > user_time timestamp,
> > > ok boolean
> > >
> > >
> > > CREATE OR REPLACE FUNCTION seach_password(USER_FOO varchar(100))
> > > RETURNS SETOF vw_change_password AS
> > > $BODY$
> > > DECLARE
> > > r vw_change_password%ROWTYPE;
> > > USER_FOO alias for $1

[SQL] pl/pgsql and error handling

2008-02-28 Thread Alex Hochberger
Is it possible to grab access to the actually user-friendly error  
message?  I have a bunch of files that come into my database, each  
slightly different.  The submitter fills out a form that records the  
info, and sticks it into a table.


If the file is malformed (wrong number of columns in a row) from a  
missing comma or something, I want to record the result to store in  
the database.  I assume that I should exception handle the COPY FROM  
line, and store the results, but I can't for the life of me figure out  
how to get the text like "invalid line on line 470352" or whatever  
psql shows.  Is that possible to obtain in plpgsql?


I've done extensive plpgsql programming before, but this is the first  
time I need it to validate the external data in question.


Alex

---(end of broadcast)---
TIP 1: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly


Re: [SQL] pl/pgsql and error handling

2008-02-28 Thread Tom Lane
Alex Hochberger <[EMAIL PROTECTED]> writes:
> Is it possible to grab access to the actually user-friendly error  
> message?

Doesn't the SQLERRM variable do what you want?

regards, tom lane

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

   http://archives.postgresql.org


Re: [SQL] pl/pgsql and error handling

2008-02-28 Thread Alex Hochberger
I do not know.  It's sounds like the Oracle PL/SQL behavior is what I  
want.  Does PostgreSQL's pl/pgsql have such a feature?  It's not in  
the documentation anywhere.  I'll check it from the office tomorrow.


Alex

On Feb 28, 2008, at 11:43 PM, Tom Lane wrote:


Alex Hochberger <[EMAIL PROTECTED]> writes:

Is it possible to grab access to the actually user-friendly error
message?


Doesn't the SQLERRM variable do what you want?

regards, tom lane

---(end of  
broadcast)---

TIP 4: Have you searched our list archives?

  http://archives.postgresql.org



---(end of broadcast)---
TIP 7: You can help support the PostgreSQL project by donating at

   http://www.postgresql.org/about/donate


Re: [SQL] pl/pgsql and error handling

2008-02-28 Thread Bart Degryse
www.postgresql.org/docs/8.2/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING
 would be the place to look.

>>> Alex Hochberger <[EMAIL PROTECTED]> 2008-02-29 6:29 >>>
I do not know.  It's sounds like the Oracle PL/SQL behavior is what I  
want.  Does PostgreSQL's pl/pgsql have such a feature?  It's not in  
the documentation anywhere.  I'll check it from the office tomorrow.

Alex

On Feb 28, 2008, at 11:43 PM, Tom Lane wrote:

> Alex Hochberger <[EMAIL PROTECTED]> writes:
>> Is it possible to grab access to the actually user-friendly error
>> message?
>
> Doesn't the SQLERRM variable do what you want?
>
> regards, tom lane
>
> ---(end of  
> broadcast)---
> TIP 4: Have you searched our list archives?
>
>   http://archives.postgresql.org ( 
> http://archives.postgresql.org/ )


---(end of broadcast)---
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate