Inserting data from one database to another conditionally - sql

In my MSQL server I have a first database with a table that contains ID and Data columns, a second database, which is a copy of the first, except the Data column is empty.
I would like to transfer data from the first database into the second, inserting into the row with the corresponding ID. I would like the query to look something like this, but I'm not sure about the syntax
INSERT INTO db1.dbo.Table (Data)
SELECT (Data)
FROM db2.dbo.Table
WHERE db1.dbo.Table(ID) = db2.dbo.Table(ID)

You can do an update on your second table like that:
UPDATE
t2
SET
t2.Data = t1.Data,
FROM
dbone.t1 AS t1
INNER JOIN dbtwo.t2 AS t2 ON t1.ID = t2.ID
If your second table is empty you can "copy" your data like that:
INSERT INTO dbone.t1 (ID, Data)
SELECT ID, Data
FROM dbtwo.t2

Related

How to update a nested bigquery column with data from another bigquery table

I have 2 bigquery tables with nested columns, I need to update all the columns in table1 whenever table1.value1=table2.value, also those tables having a huge amount of data.
I could update a single nested column with static column like below,
#standardSQL
UPDATE `ck.table1`
SET promotion_id = ARRAY(
SELECT AS STRUCT * REPLACE (100 AS PromotionId ) FROM UNNEST(promotion_id)
)
But when I try to reuse the same to update multiple columns based on table2 data I am getting exceptions.
I am trying to update table1 with table2 data whenever the table1.value1=table2.value with all the nested columns.
As of now, both tables are having a similar schema.
I need to update all the columns in table1 whenever table1.value1=table2.value
... both tables are having a similar schema
I assume by similar you meant same
Below is for BigQuery Standard SQL
You can use below query to get combining result and save it back to table1 either using destination table or CREATE OR REPLACE TABLE syntax
#standardSQL
SELECT AS VALUE IF(value IS NULL, t1, t2)
FROM `project.dataset.table1` t1
LEFT JOIN `project.dataset.table2` t2
ON value1 = value
I have not tried this approach with UPDATE syntax - but you can try and let us know :o)

How do I copy rows from one table to another in SQL?

I have been trying to copy the data listed in one table to another that are both located in the same database. However, every time I have everything entered, it runs the query and says 0 rows updated.
I have tried several variations in an attempt to get this to work. One such attempt is as listed below. I found this while researching in an attempt to get this done.
UPDATE
t1
SET
t1.column = t2.column
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.id = t2.id;
Any help on this would be greatly appreciated.
UPDATE will only modify records that already exist, if you want to insert rows that exist on another table use INSERT.
You can combine INSERT and SELECT to copy a whole table1 into a table2.
INSERT INTO table2 SELECT * FROM table1;
You can use INSERT IGNORE to copy only records that don't exist yet (based on unique keys).
You can also specify fields to copy in case the tables are different:
INSERT INTO table2 (id, first_name, last_name) SELECT id, name, surname FROM table1;

SQL: Add multiple rows from one table to another when no data for that date in new table

I'm trying to update medical data from one table to another after switching from one system to another. We have two tables, for simplicity I'll make this a simple example. There are many columns in these tables in reality (not just 5).
Table1:
name, date, var1, var2, var3
Table2:
name, date, var1a, var2a, var3a
I want to transfer data from Table 1 to Table 2 for any rows where there isn't previous data for that date, where var1 = var1a, etc (same columns with different names).
I was trying to do something with a loop, but realized that may not be necessary.
I had gotten this far but keep wasn't sure if this was ok:
UPDATE Table2 VALUES (date, var1a, var2a, var3a)
SELECT date, var1, var2, var3 FROM Table1
Is that correct syntax so far? Or do I need to map the variables to translate var1 into var1a, etc?
How do I add a check to make sure I don't overwrite any data already in Table1? I don't want to add data if there is already data for that date/name combination.
Thanks!
You can INSERT into TABLE2 all values from TABLE1 that do not already exist in Table2:
INSERT INTO Table2 (date, var1a, var2a, var3a)
SELECT date, var1, var2, var3
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2 WHERE t2.date = t1.date)
Already existing values are specified by comparing the date column. You can add any other predicates in the SELECT subquery of the NOT EXISTS expression to suit your needs.
You could use an update with a join. And you dont need to update the date column since that's what you are using to find the matches in the 2 tables.
Either you generate a dynamic query based on the empty/null valued columns, or you could do something like the below, which puts the same value in the column if it exists in table2 or else puts the corresponding value from table1.
The below approach requires less logic and easier to implement but will produce IO equivalent to updating the entire table.
update tbl2
set val1a=isnull(val1a,val1)
, val2a=isnull(val2a,val2)
, val3a=isnull(val3a.val3)
from table1 tbl1
inner join table2 tbl2
on tbl1.name=tbl2.name
and tbl1.date=tbl2.date
Considerations:
The approach requires less logic and easier to implement but will produce IOs equivalent to updating the entire table2. If you have a smallish table i would go with this approach.
If its a big table then you should look into building specific query sets to reduce IO
This code is tested in Access but something very similar should work in SQL Server 2012:
UPDATE Table2 RIGHT JOIN Table1 ON Table2.date = Table1.date
SET Table2.name = Table1.name, Table2.date = Table1.date, Table2.var1 = Table1.var1a, Table2.var2 = Table1.var2a, Table2.var3 = Table1.var3a
WHERE (Table2.date Is Null);
Explanation: this uses a right join so that within the query all the data from Table1 is present and where there is a matched date for Table2 that data is present too. We then ignore all cases where there is any data for Table2 and update the query in all other cases - the update in fact inserts new data into Table2.

SQL-Change values based on foreign keys (Input/Output)

I am trying to make an MS SQL database, and I am quite new to this.
Lets say I have tables with columns
Table1=[Client_ID, Client_Name]
Table2=[Product, Client_ID]. (Client_ID is a FK to Table1)
Now I want to update data in Table 2 and I already have some info in Table 1. Is there a way to insert into T2 if I only know Product and Client_Name? (Somehow lookup client_ID in first table based on his name) What if I dont have that specific client in Table1 yet, can he be inserted during the insertion in T2?
Also I need to grab the data from T2, but I would want to see a table that is [Product, Client_Name]. Is that kind of lookup/replace possible in SQL?
I know I could try to solve these problems beforehand - I'm using Excel as a frontend for accessing my DB, but I hope there is an easier way to do this with SQL itself.
You can insert into Table 2 as follows
'You are passing #Product, #Client_Name
'First look for whether client name is present in table1
declare #clientId as integer;
select #clientId =Client_ID from Table1 where Client_Name=#Client_Name
if #clientId >0 'When there is client name in Table1
begin
insert into table2
values(#Product, #clientId)
end
else 'There is no client name, so we have to insert client name first then gets its id
begin
insert into table1
values(#Client_Name)
'Get id of newly added client, you can done this many way, but here is the simplest way like previous
select #clientId=Client_ID from Table1 where Client_Name=#Client_Name
'Now insert into table2
insert into table2
values(#Product, #clientId)
end
You can get the data the way you wanted by using Join
Select Table2.Product, Table1.Client_Name from table1 inner join table2 on table1.Client_ID = table2.Client_ID

Swap columns from two sql server tables

I would like to know if there is anyway I can compare two columns in SQL Server.
The two columns are located in two different tables.
When the column 1's value is smaller than the column 2's value:
I want to replace the value of the column 1 with the value of the column 2.
update table1 t1
set t1.col1 = (select t2.col2
from table2 t2
where t2.id = t1.id
and t1.col1 < t1.col2)
Something like that should do it easily.
The only tricky point I see is matching the row from table2 to the row from table1. In my example, I supposed both tables share an unique "id" column which enables easy matching. Modify the query with something more appropriate.
You should be able to do something like this:
update tablename set column1=column2
from table1 inner join table2 on joincondition
where column1 < column2;
Hard to be more spesific without the actual table structure.