HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to Substitute Variables in PostgreSQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlsubstitutevariableshow

Problem

The given Oracle query inserts data into table by substituting variables. How I achieve the same in PostgreSQL?

INSERT INTO control_threshold (
    threshold_id, group_name, description, sql, low_value, high_value)
VALUES(
    threshold_seq.nextval, '&2', '&1',  TRIM('&5' || '&6' || '&7' || '&8' || '&9'),
    trim('&3'), trim('&4'));

Solution

These variables are a feature of SQL*Plus.

psql, the equivalent program in the PostgreSQL world, also has variables.

Use

\set variable 'value'


and

insert into mytable(mycolumn)
values (:variable);

Code Snippets

\set variable 'value'
insert into mytable(mycolumn)
values (:variable);

Context

StackExchange Database Administrators Q#64673, answer score: 7

Revisions (0)

No revisions yet.