Unix Technical Forum

Question about functions

This is a discussion on Question about functions within the pgsql Sql forums, part of the PostgreSQL category; --> I have been looking through FAQs and the docs and I cannot seem to find the answer to this ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 11:50 AM
Mike Plemmons
 
Posts: n/a
Default Question about functions

I have been looking through FAQs and the docs and I cannot seem to find the
answer to this question. If someone can point me to documentation I would
really appreciate it.

I am trying to run this function but the return is not correct. If I run the
select statement from the psql command line it works. My guess is that the
WHERE clause could be causing the problem. Then again, it may be how I am
using the FOR loop. The ides column is of type TEXT.

CREATE OR REPLACE FUNCTION sp_description_search(varchar) RETURNS varchar AS
$$
DECLARE
myrec record;
BEGIN
FOR myrec IN SELECT * FROM tblStuff WHERE ides LIKE '%$1%' LOOP
RETURN NEXT myrec;
END LOOP;
RETURN;
END;
$$ LANGUAGE 'plpgsql';

Thanks so much for any insight you can give me!!!
Mike

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 11:50 AM
Richard Huxton
 
Posts: n/a
Default Re: Question about functions

Mike Plemmons wrote:
> I am trying to run this function but the return is not correct.


PS - next time, details of what it *did* return and why that wasn't
correct would be useful. Not needed in this case, but good practice anyway.

PPS - the subject line was pretty good, but better might have been
"Problem with LIKE and variables in a plpgsql function"

PPPS - This is a fairly inefficient way of running simple queries.

PPPPS - Thanks for the question!

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 11:50 AM
Richard Huxton
 
Posts: n/a
Default Re: Question about functions

Mike Plemmons wrote:
> I am trying to run this function but the return is not correct. If I run the
> select statement from the psql command line it works. My guess is that the
> WHERE clause could be causing the problem. Then again, it may be how I am
> using the FOR loop. The ides column is of type TEXT.


> FOR myrec IN SELECT * FROM tblStuff WHERE ides LIKE '%$1%' LOOP


You're assuming plpgsql does variable interpolation in a similar way to
Perl/shell etc. It doesn't. You'll want something like:
...LIKE '%' || $1 || '%'

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 11:50 AM
Richard Huxton
 
Posts: n/a
Default Re: Question about functions

Mike Plemmons wrote:
>>
>>These are great tips! I will be more specific the next time I post.
>>Regarding the ineffiencient query. What do you suggest as a better way? I
>>would rather learn proper methods than continue to use improper ones.


Just send the query directly from the application - the way you're doing
it, the function will have to assemble the query, execute it, fetch all
the results and then return them to your application. Of course, if your
query altered each row as it fetched them (e.g. to calculate a running
total) then this would be a sensible approach.

PS - don't forget to CC: the list when replying - that's the convention
around here.
--
Richard Huxton
Archonet Ltd

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

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com