Insert new records with several ids from another table - sql

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;

Related

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

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;

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.

Oracle: INSERT on condition from other table

I am trying to create a SQL query where I need to insert a new sets of records based but 1 column is needed from required table.
E.g.,
TABLE_1
=======
ID,
A,
B
TABLE2
======
ID,
C,
D
Each ID are identical ID column.
I have a query of this format:
INSERT INTO TABLE_1 (ID, A, B) VALUES (???, "Yes", "What")
WHERE ID IN (SELECT ID FROM TABLE_2 WHERE ID > 10)
This, clearly doesn't work.
My question: how do I add the ID value from Table 2 into Table 1 and make it runnable? This is a query that must run on Oracle 11g.
Try this query:
INSERT INTO MyTable(ID, A, B)
SELECT ID,'YES','What' FROM TABLE_2 WHERE ID > 10

Select multi identity values for multiple inserts along with columns not inserted into the table

I am inserting only 1 column into TempId table i.e name from a select statement
How do i get corresponding orderId for the identity column.
INSERT INTO tempId
output inserted.ID
Select name FROM (
select 'erty' as name, 1 as orderid union
select 'rth' as name, 2 as orderid union
select 'yt' as name, 3 as orderid union
select '345' as name, 4 as orderid union
select 'rtyu' as name, 5 as orderid union
select 'uio' as name, 6 as orderid union
select 'yu' as name, 7 as orderid union
select 'xzf' as name, 8 as orderid
) as a
PS Note: SELECT with union is only done for sample query. Ideally I will get things from another table.
You put the id and any other fields you need to use later in a table varaiable or temp table by using the OUTPUT clause
DECLARE #MyTableVar table( ID int, orderid int);
INSERT mytable (field1, orderID)
OUTPUT INSERTED.ID, INSERTED.OrderID
INTO #MyTableVar
SELECT FIELD2, orderid FROM Myothertable
Now you have the data available in #MyTableVar to do inserts to child tables or the other work you wanted to do.
just arrange the columns:
create table #tempID( id int, name varchar(50))
INSERT INTO #tempID (name, id)
output inserted.ID
Select name, orderid FROM (
select 'erty' as name, 1 as orderid union
select 'rth' as name, 2 as orderid union
select 'yt' as name, 3 as orderid union
select '345' as name, 4 as orderid union
select 'rtyu' as name, 5 as orderid union
select 'uio' as name, 6 as orderid union
select 'yu' as name, 7 as orderid union
select 'xzf' as name, 8 as orderid
) as a
This is an example on inserting values directly. If there are no triggers you can get ##IDENTITY.
insert into [FTSwordDef] ([word]) values ('value')
select scope_identity();
Scope_identity (and the evil ##) will only return the last iden. If you are inserting multiple rows then I think you would need to loop in a SP and build up the list of iden. Iden is created by the insert and is not available IN the insert to my knowledge.
If you held a tablock on the the insert and retrieved the last identity and how many rows were inserted then in theory the insert got the last x inden values. If your insert was sorted you would know what iden went with which row.