Search Postgresql Archives

Re: How to use LIKE and $1 in a function ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I am learning PostgreSQL with an O'Reilly book and I have a problem
> with functions : I created a very basic function to look for a name in
> a table :

If your reading that book then you're not going to be using INOUT -
which is what I'm learning. But here is how you could do it with inout
in postgres 8.1:

---- ----

CREATE OR REPLACE FUNCTION func1(INOUT var_1 varchar) AS
'
  DECLARE
     X VARCHAR(50);
  BEGIN
     var_1 = var_1 || ''%'';
     SELECT INTO X "Books" FROM  testtable WHERE "Books" LIKE var_1;

   RAISE NOTICE ''found X = %'', X;

     var_1 := X;
  END
'
  LANGUAGE 'plpgsql' VOLATILE;
---- - --- -

SELECT INTO returns first returned row. So if you called "SELECT
func1('P');" you'd get any books starting with the letter P. I'm sure
there are many ways but I hope this helps some.

Troy -


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux