0
WITH migration (first_name , middle_name , last_name,fourth_name,
secondary_school ,faculty_id,department_id,university_id,name ) AS (
    SELECT first_name , middle_name , last_name,fourth_name,
    secondary_school ,faculty,department ,university_id , 
    concat(first_name,' ', middle_name,' ', last_name,' ', fourth_name) AS name 
    From student_migration ),
insert1 AS 
(
   insert into res_users(name ,login ,password)
   SELECT name, university_id,university_id FROM migration   -- DISTINCT? see below
   RETURNING id AS user_id
),
insert2 AS 
(
    insert into uni_student (first_name , middle_name , last_name,fourth_name,
    secondary_school ,faculty_id,department_id,university_id,name)
    SELECT first_name , middle_name , last_name,fourth_name,
    secondary_school ,faculty_id,department_id,university_id,name
)

I am trying to make insert1 and insert2 using data on migration and insert2 it depends on insert1 on returning value user_id. I am trying to use the script shown above but it throws a syntax error

ERROR: syntax error at or near ";" LINE 19: FROM migration RETURNING id AS partner_id); ^ ********** Error **********

ERROR: syntax error at or near ";" SQL state: 42601 Character: 851

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
ayman adam
  • 31
  • 3
  • 2
    What is your question? –  Mar 13 '18 at 21:01
  • using above script am trying to insert data on res_users then on uni_student by passing user_id that created on first inset .. but it did't working for me ERROR: syntax error at or near ";" LINE 19: FROM migration RETURNING id AS partner_id); ^ ********** Error ********** ERROR: syntax error at or near ";" SQL state: 42601 Character: 851 – ayman adam Mar 13 '18 at 21:09
  • With a FUNCTION, perhaps? – Alfabravo Mar 13 '18 at 21:15
  • A CTE must end with a "regular" statement. Yours doesn't have one. You are also missing a `FROM` clause in the last SELECT statement –  Mar 13 '18 at 21:18
  • Please **[EDIT]** your question and add some [sample data](http://plaintexttools.github.io/plain-text-table/) and the expected output based on that data. [**Formatted text**](http://stackoverflow.com/help/formatting) please, [**no screen shots**](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). **[edit]** your question - do **not** post code or additional information in comments. –  Mar 13 '18 at 21:18
  • 1
    Possible duplicate of [Insert data in 3 tables at a time using Postgres](https://stackoverflow.com/questions/20561254/insert-data-in-3-tables-at-a-time-using-postgres) – teppic Mar 13 '18 at 23:55

0 Answers0