Here I wanted to insert data in the child table using primary key id of parent table but getting an execution error ERROR: syntax error at or near "int"
I want to use IndiaCountry variable in Insert query for this task. How can I implement this?
CREATE TABLE Countries
(
id SERIAL,
description VARCHAR(100),
CONSTRAINT coutry_pkey PRIMARY KEY (id)
);
CREATE TABLE Cities
(
country_code_id int,
city_id int,
description VARCHAR(100),
CONSTRAINT cities_pkey PRIMARY KEY (city_id),
CONSTRAINT fk_cities_countries FOREIGN KEY (country_code_id) REFERENCES Countries (id)
);
INSERT INTO COUNTRIES (description) VALUES('asdf');
DECLARE indiaCountry int;
@indiaCountry = 'SELECT id FROM COUNTRIES WHERE description = 'asdf';'
INSERT INTO cities VALUES (@indiaCountry, 1 , 'abc');
3
Answers
In postgresql you can do it like this:
Or if you must use variables, you could use PL/pgSQL within anonymous block or a function. An example with anonymous block:
Documentation for anonymous block: https://www.postgresql.org/docs/current/sql-do.html
Or you could skip the select statement and do this:
Your query uses SQL Server-like syntax that is not supported in PostgreSQL.
You may use a Data-Modifying CTE like this:
It looks to me as though id is a serial column in your countries table, getting automatically generated, and you need to find out what this value is, before inserting into cities.
In PostgreSQL a serial column is just convenient syntax for a
sequence
. To find out the last value added to thesequence
we use thecurrval
function. Unfortunately thesequence
name is a generated name (there are rules), so it is easiest in addition to use another helper functionpg_get_serial_sequence
.Putting all this together, what you can do in your case is the following:
Note that
currval
is session safe. Even if there are concurrent insertions, it will return the value from the insert within your session.