Updating 2 tables in sql

Posted by / 23-Feb-2017 18:46

I have two tables which i need to update using a single update statement.

F1 = 2 FROM Table1 a INNER JOIN Table2 b ON = Is that possible to make such a statement?

But a couple columns in the table, will be changing. But I will use MERGE once I get this other figured out Thank You Greatly Andrew SQLDBA UPDATE ODSET OD.

If the values have changed, I need to update the data in the other table.--===== If the test table already exists, drop it IF OBJECT_ID('Temp DB..#Original Data','U') IS NOT NULL DROP TABLE #Original Data--===== Create the test table with CREATE TABLE #Original Data ( Row ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED , Member ID int , Location ID varchar(5) , First Name varchar(25) , Last Name varchar(25) , Birthdate date , Area Code int , Phone Number varchar(8) ) --===== If the test table already exists, drop it IF OBJECT_ID('Temp DB..#Updated Data','U') IS NOT NULL DROP TABLE #Updated Data--===== Create the test table with CREATE TABLE #Updated Data ( Row ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED , Member ID int , Location ID varchar(5) , First Name varchar(25) , Last Name varchar(25) , Birthdate date , Area Code int , Phone Number varchar(8) )INSERT INTO #Original Data (Member ID, Location ID, First Name, Last Name, Birthdate, Area Code, Phone Number) SELECT 222, 'Viol','Sammy','Miller','1965-12-15', 888, '555-1212' UNION ALL SELECT 223, 'Green','Sarah','Smith','1968-03-02', 523,'458-2356' UNION ALL SELECT 224, 'Green','Michelle','Foster','1966-12-12', 657,'241-7815' UNION ALL SELECT 225, 'Gray', 'Robert','Gillum','1970-07-20', 879,'251-6300' UNION ALL SELECT 226, 'Red', 'Joe','Roberts','1960-05-05', 211,'205-8785' UNION ALL SELECT 227, 'Red', 'Danny','Jones','1961-10-30', 696,'575-5611' UNION ALL SELECT 228, 'Blue', 'Andy','Hill','1966-08-01', 315,'473-2501' UNION ALL SELECT 229, 'Blue', 'Bill','Height','1962-11-20', 315,'474-5670' INSERT INTO #Updated Data (Member ID, Location ID, First Name, Last Name, Birthdate, Area Code, Phone Number) SELECT 222, 'Viol', 'Sammy','Miller','1965-12-15', 888, '555-1212' UNION ALL SELECT 223, 'Green', 'Sarah','Smith','1968-03-02', 523,'458-2356' UNION ALL SELECT 224, 'Green', 'Michelle','Foster','1966-12-12', 657,'241-7800' UNION ALL -- Changed Phone Number SELECT 225, 'Gray', 'Robert','Gillum','1970-07-20', 879,'251-6300' UNION ALL SELECT 226, 'Red', 'Joe','Roberts','1960-05-07', 211,'205-8585' UNION ALL -- Changed Birthdate, Phone Number SELECT 227, 'Red', 'Danny','Jones','1961-10-30', 696,'575-5611' UNION ALL SELECT 228, 'Blue', 'Andrew','Hill','1966-08-01', 315,'473-2502' UNION ALL -- Changed First Name, Phone Number SELECT 229, 'Blue', 'Bill','Height','1962-11-20', 315,'474-5670' SELECT * FROM #Original Data SELECT * FROM #Updated Data DROP TABLE #Original Data DROP TABLE #Updated Data Member ID and Location ID are the only two columns that can never change.

Ignore all rows that have not changed, or are different between the two tables.

We’ll briefly explore both options so you can find what works best for you.

Phone Number FROM #Original Data AS ODINNER JOIN #Updated Data AS UDON OD.

-- Red Adair MERGE #Original Data original USING #Updated Data changed ON original.

I'm a bit stuck in the world of 2005 so haven't had to work with MERGE but I believe the same problem with NULLs may exist there, as well.

You could have quite the surprise if NULLs are involved.

updating 2 tables in sql-59updating 2 tables in sql-78updating 2 tables in sql-15

One thought on “updating 2 tables in sql”

  1. In addition, I’ve had two trips that took me back and forth from the two countries, giving me clear side-by-side comparisons. I was on the fence for the longest time about which country has more beautiful women.