Postgresql – Insert data in multiple tables

postgresql

I have three tables and I want to insert data into that three tables as per their respective data.

Query:

with acupressure_point_data as (
    insert into acupressure_point(id, url)
        values('LL909', 'ww/33/11')
    returning id as acupressure_point_id
),
acupressure_sub_type as (
    insert into acupressure_point_region_sub_type
        (acupressure_point_id, region_sub_type_id)
        values(acupressure_point_id , '9898')
    returning id as acupressure_sub_type_id
);

But it gives the error as:

"ERROR: syntax error at end of input Position"

I can not understood this. Please help me. Thanks

Best Answer

For PostgerSQL:

with acupressure_point_data as ( -- insert into first table with output
                                 insert into acupressure_point(id, url)
                                 values('LL909', 'ww/33/11')
                                 returning id as acupressure_point_id )
-- insert into second table, use CTE output
insert into acupressure_point_region_sub_type(acupressure_point_id, region_sub_type_id)
SELECT acupressure_point_id , '9898'
FROM acupressure_point_data
-- return id for both tables 
returning acupressure_point_id, id as acupressure_sub_type_id;

MySQL does not allow INSERT in CTE. Use separate queries and LAST_INSERT_ID().