Обсуждение: Help with SQL Function

Поиск
Список
Период
Сортировка

Help with SQL Function

От
Jeff Eckermann
Дата:
I sent this message yesterday, but it doesn't appear to have made it to the list.
Apologies to anyone who receives it twice.

I'm falling at the first hurdle.  Can someone tell me how to pass an attribute value into a function, as I'm trying to
dobelow?  I've studied the docs every which way, but can't seem to find the cause of my problem. 
Thanks in advance for ignorance relief.

extracts=# create function testfunc(text) returns int4 as '
extracts'# select count (*) from dedcolo where equip_type = ''$1''    --- That's doubled single quotes
extracts'# ' language 'sql';
CREATE
extracts=# select testfunc('Dialup');
 testfunc
----------
        0
(1 row)
extracts=# create function testfunc() returns int4 as '
extracts'# select count (*) from dedcolo where equip_type = ''Dialup''   --- Doubled single quotes, again
extracts'# ' language 'sql';
CREATE
extracts=# select testfunc();
 testfunc
----------
     3453
(1 row)

Re: Help with SQL Function

От
Tom Lane
Дата:
Jeff Eckermann <jeff@akira.eckermann.com> writes:
> extracts=# create function testfunc(text) returns int4 as '
> extracts'# select count (*) from dedcolo where equip_type = ''$1''    --- That's doubled single quotes
> extracts'# ' language 'sql';
> CREATE

That's looking for rows where equip_type = '$1'  ... ie, the literal
string $1.  What you probably wanted is

create function testfunc(text) returns int4 as '
select count (*) from dedcolo where equip_type = $1
' language 'sql';

            regards, tom lane