Re: Why I cannot call a function from within an SQL function?

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why I cannot call a function from within an SQL function?
Date: 2009-02-20 13:17:22
Message-ID: 20090220131722.GH5379@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Eus :
> Hi Ho!
>
> The following query works well:
>
> select count (*)
> from item_audit
> where audit_ts >= '2008-05-30 00:00:00'
> and audit_ts <= '2008-10-30 00:00:00'
> and 'wst' != (select split_part(category, '-', 2)
> from description
> where split_part(category, '-', 1) = 'item'
> and shorthand = status
> )
>
> But, when I transform it into the following SQL function, the function cannot be created barking:
>
> ERROR: syntax error at or near "-"
> LINE 6: and $1 != (select split_part(category, '-', 2)"
>
> create or replace function get_I(text, timestamp, timestamp) returns bigint as
> 'select count (*)
> from item_audit as ia
> where audit_ts >= $2
> and audit_ts <= $3
> and $1 != (select split_part(category, '-', 2)
> from description
> where split_part(category, '-', 1) = 'item'
> and shorthand = ia.status
> )
> ' language sql;
>
> What's wrong?

The quoting. Use $$-quoting around the function, for instance:

create or replace function get_I(text, timestamp, timestamp) returns bigint as $$
select count (*) ...

$$ language plpgsql;

Now you can use simple ' inside the function. Other, but inferior solution,
use ''' instead ' inside the function.

HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Miguel Ángel MF 2009-02-20 13:21:58 Re: Why I cannot call a function from within an SQL function?
Previous Message Eus 2009-02-20 13:14:45 Re: Why I cannot call a function from within an SQL function?