pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpgsql howto question
2+ messages / 2 participants
[nested] [flat]

* plpgsql howto question
@ 2004-12-23 14:19  Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>
  0 siblings, 1 reply; 2+ messages in thread

From: Joost Kraaijeveld @ 2004-12-23 14:19 UTC (permalink / raw)
  To: pgsql-interfaces

Hi all,

I want to use a plpgsql function that returns the value of a column, depending on the arguments ( the code below does not work, hence the question):

CREATE OR REPLACE FUNCTION getcustomername(text, int4)
  RETURNS text AS
'
BEGIN
IF $2 = 1 THEN
SELECT lastname FROM person WHERE objectid = $1;
RETURN lastname;
ELSIF $2 = 2 THEN
SELECT name into name1 from company WHERE objectid = $1;
RETURN name1;
END IF;
END
'  LANGUAGE 'plpgsql' VOLATILE;

What is the correct way of doing this?

TIA

Groeten,

Joost Kraaijeveld
Askesis B.V.
Molukkenstraat 14
6524NB Nijmegen
tel: 024-3888063 / 06-51855277
fax: 024-3608416
e-mail: J.Kraaijeveld@Askesis.nl
web: www.askesis.nl 



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: plpgsql howto question
@ 2004-12-23 15:27  Frank van Vugt <ftm.van.vugt@foxi.nl>
  parent: Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>
  0 siblings, 0 replies; 2+ messages in thread

From: Frank van Vugt @ 2004-12-23 15:27 UTC (permalink / raw)
  To: Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>; +Cc: pgsql-interfaces

Hi Joost,

> What is the correct way of doing this?

As 'always', there's more than one way of doing this, for example
 - selecting into a var and returning that var
 - selecting into a record and returning the proper field of that record
 - return the proper values immediately
 - use 'plain' sql in combination with a case statement

Some examples:

CREATE OR REPLACE FUNCTION getcustomername(text, int4)
RETURNS text AS
'
DECLARE
	my_person TEXT;
	my_company RECORD;
BEGIN
IF $2 = 1 THEN
	SELECT INTO my_person lastname FROM person WHERE objectid = $1;
	RETURN my_person;
ELSIF $2 = 2 THEN
	SELECT INTO my_company name1 from company WHERE objectid = $1;
	RETURN my_company.name1;
END IF;
END
'  LANGUAGE 'plpgsql' VOLATILE;

*****

CREATE OR REPLACE FUNCTION getcustomername(text, int4)
RETURNS text AS
'
DECLARE
	my_id ALIAS FOR $1;
	my_type ALIAS FOR $2;
BEGIN
IF my_type = 1 THEN
	RETURN my_person lastname FROM person WHERE objectid = my_id;
ELSIF my_type = 2 THEN
	RETURN name1 from company WHERE objectid = my_id;
END IF;
END
'  LANGUAGE 'plpgsql' VOLATILE;

*****

SELECT CASE
	WHEN type = 1 THEN (SELECT person)
	WHEN type = 2 THEN (SELECT company)
	ELSE null
END;

(albeit this is not plpgsql anymore)



Obviously you want to choose one of the approaches ;)



NB. Prettige kerstdagen alvast !


-- 
Best,




Frank.




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-12-23 15:27 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-23 14:19 plpgsql howto question Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>
2004-12-23 15:27 ` Frank van Vugt <ftm.van.vugt@foxi.nl>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox