| Author |
Topic |
|
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-03-18 : 11:10:30
|
| Hi Team,I have a #MyCenter table as follows. A table with the similar structure is available in another database (offline database). A SQL Server job has to update the offline table every night. It has to insert new records and update the existing records. What is the best procedure for this?I tried the following, but when the number of columns is high it become highly lenghty. Is there a better way?CREATE TABLE #MyCenter( [Center_ID] [int] IDENTITY(1,1) NOT NULL, [Center_No] [int] NOT NULL, [OwnerName] [varchar](100) NULL, CONSTRAINT [PK_Center_Center_ID] PRIMARY KEY NONCLUSTERED ([Center_ID] ASC)) ON [PRIMARY]CREATE TABLE #MyCenterOffline( [Center_ID] [int], [Center_No] [int] NOT NULL, [OwnerName] [varchar](100) NULL,) ON [PRIMARY]UPDATE #MyCenterOffline SET Center_No = A.Center_No FROM #MyCenterOffline O INNER JOIN #MyCenter A ON A.Center_ID = O.Center_IDUPDATE #MyCenterOffline SET OwnerName = A.OwnerName FROM #MyCenterOffline O INNER JOIN #MyCenter A ON A.Center_ID = O.Center_IDThanksLijo Cheeran Joseph |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-03-18 : 11:35:17
|
| Thanks for the quick resposne.By 'Offline' I meant just another database.Please help.ThanksLijo |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-18 : 12:14:39
|
| you need to compare both the tables based on PK value ([Center_ID]) and do one of below1. if record in yourtable not in offline, insert2. if record in both tables and any of other fields are different, then do updatesince the tables are in different db and in different server (i assume so) you need to set up linked server connection before you do above update/insert------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-03-18 : 12:26:51
|
| Hi Visakh,Both the databases are on same server.The main part of the question is - whether I need to write separate query for each column (please see sample below). That seems to be non-acceptable in my scenario. Any better approach available?UPDATE #MyCenterOffline SET Center_No = A.Center_NoFROM #MyCenterOffline OINNER JOIN #MyCenter AON A.Center_ID = O.Center_IDUPDATE #MyCenterOffline SET OwnerName = A.OwnerNameFROM #MyCenterOffline OINNER JOIN #MyCenter AON A.Center_ID = O.Center_IDThanksLijo |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-18 : 12:34:14
|
and for insert doINSERT #MyCenterOffline (Center_No,OwnerName,.. )SELECT A.Center_No,A.OwnerName,..FROM #MyCenter ALEFT JOIN #MyCenterOffline OON A.Center_ID = O.Center_IDWHERE O.Center_ID IS NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-03-18 : 12:56:23
|
Thank you Visakh and Tara.End of the day, it was a simple affair. I was thinking in a limited cirlce. Hope I will improve one day ThanksLijo |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|