Re: Recursive CTE in function problem - Mailing list pgsql-general

From David G. Johnston
Subject Re: Recursive CTE in function problem
Date
Msg-id CAKFQuwZ4eGOKPUZt3HToA=SA=e=2wLK8uAfP_2f62WcE-hHowQ@mail.gmail.com
Whole thread Raw
In response to Recursive CTE in function problem  (Doug Kyle <dkyle@grpl.org>)
Responses Re: Recursive CTE in function problem  (Doug Kyle <dkyle@grpl.org>)
List pgsql-general
On Thu, Feb 4, 2016 at 12:37 PM, Doug Kyle <dkyle@grpl.org> wrote:
But when I use it as a function it always returns false:

CREATE OR REPLACE FUNCTION grpl_collection.copy_in_collection_name(cpid bigint, colname text)
 RETURNS boolean
 LANGUAGE plpgsql
AS $function$
BEGIN
    RETURN cpid in (select copy from grpl_collection.collections_copy_map where copy=cpid and collection in
(WITH    RECURSIVE
        q AS
        (
        SELECT  c.id
        FROM    grpl_collection.collections c
        WHERE   name=$$colname$$
​      <<<------------​

        UNION
        SELECT  cn.id
        FROM    q
        JOIN    grpl_collection.collections cn
        ON      cn.parent = q.id
        )
SELECT id FROM q ));
END;
$function$

​$$colname$$ is a string whose contents is the literal 'colname'​, not the function argument named colname as you seem to want.

David J.


pgsql-general by date:

Previous
From: Alex Magnum
Date:
Subject: Cannot Create Objects
Next
From: Doug Kyle
Date:
Subject: Re: Recursive CTE in function problem