pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sql query with join and parameter in postgresql function
3+ messages / 2 participants
[nested] [flat]

* sql query with join and parameter in postgresql function
@ 2004-01-16 11:30  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 1 reply; 3+ messages in thread

From: Philippe Lang @ 2004-01-16 11:30 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I'd like to write a function similar to that:

------------------------------------------------
CREATE FUNCTION public.report_join_parameter(char(6))
  RETURNS ??? AS
'
SELECT

tableA.field1,
tableA.field2,
tableB.field1,
tableB.field2,
tableC.field1,
tableC.field2,

FROM tableA

INNER JOIN tableB
ON tableA.pk = tableB.fk

INNER JOIN tableC
ON tableB.pk = tableC.fk

WHERE tableC.field3= 0
AND tableB.field1= $1
'
  LANGUAGE 'sql' VOLATILE;
------------------------------------------------

Do I ***HAVE TO*** define a composite type for the columns my function
is supposed to return? Isn't there a simpler method, that could allow me
to avoid this extra work?

I thought a view would be more appropriate, but if I'm not wrong, a view
does not accept any parameter as input...

Thanks!

Philippe



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

* Re: sql query with join and parameter in postgresql function
@ 2004-01-16 11:44  Michael Glaesemann <grzm@myrealbox.com>
  parent: Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 0 replies; 3+ messages in thread

From: Michael Glaesemann @ 2004-01-16 11:44 UTC (permalink / raw)
  To: Philippe Lang <philippe.lang@attiksystem.ch>; +Cc: pgsql-sql


On Jan 16, 2004, at 8:30 PM, Philippe Lang wrote:

> Hello,
>
> I'd like to write a function similar to that:
>
> ------------------------------------------------
> CREATE FUNCTION public.report_join_parameter(char(6))
>   RETURNS ??? AS

<snip />

> Do I ***HAVE TO*** define a composite type for the columns my function
> is supposed to return?

As far as I know. It's not that hard.

CREATE TYPE report_join_type (
	tableAfield1 <type>,
	tableAfield2 <type>,
	tableBfield1 <type>,
	tableBfield2 <type>,
	tableCfield1 <type>,
	tableCfield2 <type>
);

> I thought a view would be more appropriate, but if I'm not wrong, a 
> view
> does not accept any parameter as input...

But you can just define the view and select it with a WHERE clause with 
your parameter, just like a normal table. Would that help?

Michael Glaesemann
grzm myrealbox com




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

* Re: sql query with join and parameter in postgresql function
@ 2004-01-16 13:03  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 0 replies; 3+ messages in thread

From: Philippe Lang @ 2004-01-16 13:03 UTC (permalink / raw)
  To: pgsql-sql; +Cc: Michael Glaesemann <grzm@myrealbox.com>

> > I thought a view would be more appropriate, but
> > if I'm not wrong, a view does not accept any parameter
> > as input...
> 
> But you can just define the view and select it with a WHERE clause
with
> your parameter, just like a normal table. Would that help?

Thanks! That's perfect. I join in a view, and limit in a function, with
a WHERE clause. And no need for any redundant composite type, although I
agree this is not that complicated to use...

Philippe Lang



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


end of thread, other threads:[~2004-01-16 13:03 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-01-16 11:30 sql query with join and parameter in postgresql function Philippe Lang <philippe.lang@attiksystem.ch>
2004-01-16 11:44 ` Michael Glaesemann <grzm@myrealbox.com>
2004-01-16 13:03 Re: sql query with join and parameter in postgresql function Philippe Lang <philippe.lang@attiksystem.ch>

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