0

I try send INSERT one request in postgresql DB. I want insert into table users login and password, then take away from users id, and insert in the ud_user into tables personal_data it id, and other params(name, gender etc).

INSERT INTO personal_data (id_user, name, gender, city)
VALUES ((INSERT INTO users (login, password) 
VALUES('123', '123')
RETURNING id), 'Max', 'Male', 'Moscow');

syntax error at or newar "INTO" LINE 2: VALUES ((INSERT INTO users...

  • Hello Pavel. Welcome to SO, you are using incorrect syntax, here is the answer for your question https://stackoverflow.com/a/6561437/2460416 – mpospelov Feb 06 '19 at 18:40

1 Answers1

0

In Postgres, you can update both tables in the same statement using CTEs:

WITH u as (
      INSERT INTO users (login, password) 
          VALUES('123', '123')
          RETURNING id
     )
INSERT INTO personal_data (id_user, name, gender, city)
    SELECT id, 'Max', 'Male', 'Moscow'
    FROM u;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786