1

I have this postgresql function

CREATE OR REPLACE FUNCTION sp_select_test(_id bigint)
  RETURNS void AS
$BODY$                

BEGIN
        copy (select sub.id
        from (
          SELECT id FROM sim s where id = _id) sub) TO '/tmp/results.tsv';
END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_select_test(bigint)
OWNER TO postgres;

When I run

select * from sp_select_test(264);

I get error - column "_id" does not exist

If I replace the variable _id in the function with a value say 264, the function works. That is if instead of id = _id I use id = 264

Any reason why the function is failing?

3
  • try $1 instead of _id Commented Jun 23, 2015 at 9:51
  • Get "there is no parameter $1" message Commented Jun 23, 2015 at 9:58
  • 1
    ah, I see now :) use dynamic query instead Commented Jun 23, 2015 at 10:30

1 Answer 1

2
CREATE OR REPLACE FUNCTION sp_select_test(_id bigint)
  RETURNS void AS
$BODY$                

BEGIN
        execute $$copy (select sub.id
        from (
          SELECT id FROM sim s where id = $$||_id||$$) sub) TO '/tmp/results.tsv'$$;
END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION sp_select_test(bigint)
OWNER TO postgres;
Sign up to request clarification or add additional context in comments.

1 Comment

What's the logic behind that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.