SQL Update column with random data from another table - sql

I'd like to insert random names of table 2 into the records of table 1.
Table 1: ID ... Name ... FurtherData
Table 2: Name
Is it possible to achieve this in a simple SQL statement?
Kind regards

I think, you want to insert all names from table2 into table1 those are not already there
INSERT INTO Table1 (name)
SELECT table2.Name
From table2
Where table2.Name NOT IN (SELECT table1.Name From table1)

Something like this to get 100 random names:
insert into table1(name)
select name
from (select name
from table2
order by dbms_random.random()
) t
where rownum <= 100;
Of course, you may want to supply values for the other columns, but your question seems to be specifically about getting random values from the second table.
In Oracle 12, you don't need the subquery. You can use fetch first <n> rows only.

Related

Oracle - Conditional Insert with Sequence

I know the sequence can not be used in these places.
For a SELECT statement:
In a WHERE clause
In a GROUP BY or ORDER BY clause
In a DISTINCT clause
Along with a UNION or INTERSECT or MINUS
In a sub-query
Please help to achieve the below requirement with conditional insert.
Objective is - Need to insert if the name is not exist in the table as its protected by Sequence as primary key.
INSERT
WHEN EXISTS (SELECT 0 FROM TABLE1 WHERE NAME = 'DUPLICATE_NAME_TEST')
THEN
INTO TABLE1 (KEY, NAME, GROUP)
SELECT TESTSEQ.NEXTVAL, 'DUPLICATE_NAME_TEST', 30 FROM DUAL;
SQL has no INSERT WHEN construct. It does have WHERE. So you intend something like this:
INSERT INTO TABLE1 (NAME, GROUP)
SELECT 'DUPLICATE_NAME_TEST', 30
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM TABLE1 T1 WHERE T1.NAME = 'DUPLICATE_NAME_TEST');

Big Query - Only insert if column value does not exist

Does Big Query support operations like "REPLACE INSERT" or something related to that?
If I run a query like this twice:
INSERT INTO table(column1) VALUES(1)
It'll create a duplicated row, is it possible to insert a row only if a column with the same value does not exist?
Thanks!
Below should make it
#standardSQL
INSERT INTO yourTable(column1)
SELECT value FROM (SELECT 1 AS value)
LEFT JOIN yourTable
ON column1 = value
WHERE column1 IS NULL
Does this work for you?
INSERT INTO table(column1)
WITH s AS (SELECT 1 src)
SELECT src FROM s WHERE NOT EXISTS (
SELECT * FROM table t WHERE t.column1 = s.src
)

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.

Insert distinct values from one table into another table

So for each distinct value in a column of one table I want to insert that unique value into a row of another table.
list = select distinct(id) from table0
for distinct_id in list
insert into table1 (id) values (distinct_id)
end
Any ideas as to how to go about this?
Whenever you think about doing something in a loop, step back, and think again. SQL is optimized to work with sets. You can do this using a set-based query without the need to loop:
INSERT dbo.table1(id) SELECT DISTINCT id FROM dbo.table0;
There are some edge cases where looping can make more sense, but as SQL Server matures and more functionality is added, those edge cases get narrower and narrower...
insert into table1 (id)
select distinct id from table0
The following statement works with me.
insert into table1(col1, col2) select distinct on (col1) col1 col2 from table0
The below query will also check the existing data in the Table2.
INSERT INTO Table2(Id) SELECT DISTINCT Id FROM Table1 WHERE Id NOT IN(SELECT Id FROM Table2);
Other Simple way to copy distinct data with multiple columns from one table to other
Insert into TBL2
Select * from (Select COL1, ROW_NUMBER() over(PARTITION BY COL1 Order By COL1) AS COL2 From TBL1)T
where T.COL2 = 1

Copy rows from one table to another in MySQL

I have two tables with the same structure. I need to select data from one table and have to store them into the other.
How can I do that?
insert into tablea(id,name) select id,name from tableb;
Since they are the same structure then you can just do
insert into table1 select colum1, column2,... from table2
insert into blacklist
select *
from newblacklist
for uploading any single number
insert into blacklist
select *
from newblacklist
where numbers ='123456'
leave out the values keyword
insert into tbl1
select * from tbl2