How do I only update rows in table A with the data which has changed in table B - sql

I'm inserting data into table A. After a few days have past the data in the Table A might be out of date from the live system.
What I want to do is load the rows that would have changed from the live system into say a temp table and update only the rows which have to corresponding GUID to update with the correct data.
I need help with how the update statement should be formatted.

This would update only the column name Table A with column name of TableB with UID matches on both table: Give a try
update set a.name=b.name from TestA a INNER JOIN TestB b on a.UID=b.UID;

Related

how to update the table from the new rows that come to another table every day in sql

I have Table A and its connected to some server that give him the new transactions every day . I created a table B and inserted the data from table A, How I can write a query that make Table B get updated every day from table A with the new transactions.
I have not tried a query
have createdon and updatedon columns on both tables and update the columns whenever records are inserted and updated. This way createdon,updateon columns can be used to retrieve the records

BEFORE or INSTEAD OF Insert SQL Server triggers

I have a small problem. What I want to achieve is before inserting data to table A, I want to check if this data is in table B and table C.
When tables B and C don't have this data, I'd like to add data from 'inserted' to table B and C (and insert data to table A at the end), but if they have already this row, I don't want to insert anything to table A and B (and don't know to insert anything to table A as well).
Is there any way I can control if the data from inserted will be inserted in table A or not?

SQL query to update one for one

I have table A with one to many relationship to table B. Whenever I do an update I create new record in table A with a new batch of records related created in table B. I had a bug in my code so when new items where being created the order of them was being reversed. I need to write a SQL query to update items in table B related to second item in table A to be the same as the ones related to first item in table A. I hope it makes any sense, will try to illustrate:
A1 -> B - 1234
A2 -> B - 4321
I want to update second set of values from table B to be the same as the ones related to A1 (1234)
You should not think of records order in database table. The only way to specify order of rows is ORDER BY clause in SELECT statement.
Make row_order or idx column in table B and put there required value for each item.
In some cases you will also get different order of B items for the first/source A record when selecting them without specifying order in ORDER BY clause.
Relational database table has no notion about neither row order nor column order.

How do I copy data from each record of one table into multiple records of another table related to the first table?

I'm working in SQL Server 2012. I need to migrate data from one table to another, but I need to duplicate the data as well. Here is the setup:
Table A has a 1 to many relationship to Table B. Some of the data in each record in Table A needs to be moved to multiple records of Table B related to the Table A record. Each record in Table A needs to copy its data into multiple records of table B for any records in Table B that links to a given Table A record.
I need a way to do this with one single SQL UPDATE if at all possible. And I need to understand how the SQL works. I've not been able to find a way to do this through Google or searching SO.
Table A:
ID : int
Name : varchar2
Age : int
Type_ID : int
Rating : int
Table B:
ID : int
Table_A_ID : int
Name : varchar2
Age : int
Subject_ID : int
Note : varchar2
So in this example Age and Name need to be copied from Table A to all records of Table B that share Table_A_ID with Table A's ID field. Once the migration is successful, I will remove Name and Age from Table A.
Updating B is straightforward, simply JOIN the two tables together, indicate which table you intend updating, and map the columns across:
UPDATE b
SET b.Age = a.Age,
b.Name = a.Name
FROM TableB b
INNER JOIN TableA a
ON b.Table_A_ID = a.ID;
If you need to to also add missing rows, or delete removed rows, you might instead look at MERGE instead to avoid needing to run separate INSERT, UPDATE and DELETE statements

delete old values of a table and update the table with results of same query

My question is to simple, but I can't find out a way to delete old values of a table and update same table with results of same query.
UPDATE
The query is an SELECT on Table A, and the results be Table B. And nothing on Table B different of the result of last query on Table A.
I have a very big table, and I need to process the records and create a new table regularly. The old values of this table are not important, only the new ones.
I will appreciate any help.
What about a view? If you only need table B to query on. You said you have a select on table A. Lets say your select is SELECT * FROM TableA WHERE X = Y. Then your statement would be
CREATE VIEW vwTableB AS
SELECT * FROM TableA WHERE X = Y
And then instead of querying tableB you would query vwTableB. Any changes to the data in table A would be reflected in the view so you don't have to keep running a script yourself.
This was the data in vwTableB would be kept updated and you wouldn't have to keep deleting and inserting into the second table.
you can use a temporary table to store results you are working with, if you only need it for one session. it will automatically be dropped when you sign out.
you didn't say what db you are using, but try this
create temp tableB AS select * from tableA