Help with SQL Function

Started by Jeff Eckermannover 25 years ago2 messagesgeneral
Jump to latest
#1Jeff Eckermann
jeckermann@verio.net

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 do below? 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)

#2Alex Pilosov
alex@pilosoft.com
In reply to: Jeff Eckermann (#1)
Re: Help with SQL Function

Simpler than you think:
select count (*) from dedcolo where equip_type = $1

(note the space between = and $)

On Wed, 3 Jan 2001, Jeff Eckermann wrote:

Show quoted text

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 do below? 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)