How to use a WITH query to insert into a table and insert the returning id into another table? - sql

I'm trying to insert a row into a table named table1 if only 'value' exists in table2. The newly created row returns an id and I want to use this id through a WITH query and insert it into a third table named table3.
Here is the sql statement I used.
WITH new_key AS (
INSERT INTO table1(name, value) 
SELECT 'test', value
FROM table2 
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
VALUES (new_key);
The INSERT query in the WITH query works and returns an id. Unfortunately the whole statement returns "new_key column does not exist".

You need to select the value from the CTE:
WITH new_key AS (
INSERT INTO table1(name, value)
SELECT 'test', value
FROM table2
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
SELECT id
FROM new_key;

Related

INSERT with SELECT returning multiple values throws error "subquery returned more than 1 value"

Trying to insert multiple values in a table (as many as ID's from the SELECT result), but the select Id query returns more than 1 value (expected), hence the error.
INSERT INTO table1 (UserId, Date, ...)
VALUES
((SELECT Id from table2 WHERE ClientId = 26), GETDATE(), ...)
what would be the best approach for this?
I'm not sure how your columns line up. It looks like GETDATE() is inserting into Name. But if that's what you need, what about this? You could just do an INSERT SELECT without the VALUES.
INSERT INTO table1 (UserId, Name, ...)
SELECT Id, GETDATE(), ...
from table2
WHERE ClientId = 26

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).
I have one varchar value = 'premium'.
I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:
1, 'premium';
2, 'premium';
etc.
How to write the function for SQL?
Are you looking for insert . . . select or create table as?
insert into table2 (id, value)
select id, 'premium'
from table1;
or:
create table table2 as
select id, 'premium' as value
from table1;
Do you want this?
demo:db<>fiddle
INSERT INTO second_table (id, text_value)
SELECT id, 'premium'
FROM first_table;

A sql query to create multiple rows in different tables using inserted id

I need to insert a row into one table and use this row's id to insert two more rows into a different table within one transaction. I've tried this
begin;
insert into table default values returning table.id as C;
insert into table1(table1_id, column1) values (C, 1);
insert into table1(table1_id, column1) values (C, 2);
commit;
But it doesn't work. How can I fix it?
updated
You need a CTE, and you don't need a begin/commit to do it in one transaction:
WITH inserted AS (
INSERT INTO ... RETURNING id
)
INSERT INTO other_table (id)
SELECT id
FROM inserted;
Edit:
To insert two rows into a single table using that id, you could do that two ways:
two separate INSERT statements, one in the CTE and one in the "main" part
a single INSERT which joins on a list of values; a row will be inserted for each of those values.
With these tables as the setup:
CREATE TEMP TABLE t1 (id INTEGER);
CREATE TEMP TABLE t2 (id INTEGER, t TEXT);
Method 1:
WITH inserted1 AS (
INSERT INTO t1
SELECT 9
RETURNING id
), inserted2 AS (
INSERT INTO t2
SELECT id, 'some val'
FROM inserted1
RETURNING id
)
INSERT INTO t2
SELECT id, 'other val'
FROM inserted1
Method 2:
WITH inserted AS (
INSERT INTO t1
SELECT 4
RETURNING id
)
INSERT INTO t2
SELECT id, v
FROM inserted
CROSS JOIN (
VALUES
('val1'),
('val2')
) vals(v)
If you run either, then check t2, you'll see it will contain the expected values.
Please find the below query:
insert into table1(columnName)values('stack2');
insert into table_2 values(SCOPE_IDENTITY(),'val1','val2');

insert into value in select statement mutliple values

I try to insert into table with 1 column is (select from table).
I should copy all the id to table1 with 1 column is (select from table )
This not working:
insert into table1 (id,resoucrce,rate) values ((select id from table2),0,0)
I want to do something like that insert all the id from table to another table with default values.
Use insert . . . select:
insert into table1 (id, resource, rate)
select id, 0, 0
from table2;
For copy the content of one table to another table within the same database use this :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
or your query like that :-
Insert into table1 (id, resource, rate) select id, 0, 0 from table2;
You should focus "values" keyword;
values ((select id from table2),0,0)
When you use values(, , ,) you have to specify value columns. But you're trying to pass both resultset and single values together. That's why you get an error. You should only pass single values...
If its clear for you; you can easily find the correct sql syntax.

Postgres insert with select and values

So I'm trying to insert a value into a secondary table, get the returned id from the insert, and use it together with some values in a prepared statement I'm writing.The return of the first statement(table2) is an id for a foreign key column in the first table.
I want to get the id of table 1 in the end.Something like :
WITH table2ID AS
(
INSERT INTO table2 (value) VALUES ('somevalue') RETURNING id;
)
INSERT INTO table1(table2returnvalue,othervalue) VALUES
(table2ID 'val2') RETURNING id
I can see that I will probably need a transaction as well, because I don't want an isolated in table2 if table1's statement fails for some reason.
Could you please help?
To do this with a CTE its usually easiest to return the entire row (output from table2) and reference that in your second insert query.
WITH table2 AS
(
INSERT INTO table2 ( value )
VALUES ( 'somevalue' )
RETURNING table2.*;
)
INSERT INTO table1 ( table2returnvalue, othervalue )
SELECT table2.id, 'val2'
FROM table2
RETURNING table1.id
I think you're looking for something like this:
WITH table2ID AS
(
INSERT INTO table2 (value) VALUES ('somevalue') RETURNING id;
)
INSERT INTO table1(table2returnvalue,othervalue)
SELECT table2ID.id, 'val2' FROM table2ID;