Is it possible to declare and use $1, $2, etc values in a Postgres INSERT?

Viewed 923

I have a ton of statements like:

INSERT INTO app.organization (name, org_type)
        VALUES ($1, $2)

in my code. I'd like to grab that, paste it into a SQL query window, declare $1 and $2 on the first two lines and then run the query. I haven't found an answer to this. It looks like it's not possible but I would like to confirm it directly instead of indirectly. It would be a lot faster than selecting the $1 and pasting a new value in there.

1 Answers

You can use the PREPARED statement, you can prepare a set of values and define it on execute.

From postgreSQL docs:

PREPARE fooplan (int, text, bool, numeric) AS
    INSERT INTO foo VALUES($1, $2, $3, $4);
EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);

Follow the ref:

Related