postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Escaping parameters ($1, $2,...) when creating SQL functions

Open powibol opened this issue 1 year ago • 1 comments

Hi all,

I am looking for the right way to escape $1, $2,..., when used inside a CREATE FUNCTION statement.

As a minimal example, let's say we have this query in a file:

CREATE OR REPLACE FUNCTION "sample_function"(integer) RETURNS boolean
AS
$$
SELECT CASE WHEN $1 > 15 THEN true ELSE false END AS x FROM (VALUES (1)) AS y
$$
LANGUAGE SQL
STABLE
RETURNS NULL ON NULL INPUT

How should I escape $$ and $1 in this example?

Thank you a lot in advance for your help!

powibol avatar Jan 16 '24 13:01 powibol

I don't understand your question. Where is the value being passed in for the parameter $1. Can you please write out the entire javascript instead of just the sql part?

I could be wrong but I feel you trying to ask a generic PostgreSQL question? If so you should be asking ask on the PostgreSQL mailing list or their slack channel. This is the repo for "postgres.js" , a javascript client library for PostgreSQL.

Louis-Tian avatar Jan 19 '24 02:01 Louis-Tian