Solved

update data from one SQL database to another

Posted on 2012-03-29
4
247 Views
Last Modified: 2012-04-04
I have two identical databases.  All except, of course, the name of the data base itself.

These data bases are called

Company_2011
and
Grower_2011

Each of the tables in the databases are identical.

In the company_2011 there is a table called plant_city.  In this table there are several computed columns.

I need to be able to update the data from the table plant_city in company_2011 to plant_city in grower_2011.

I do this now with a query that looks like this:

BEGIN TRANSACTION
DELETE GROWER_2011.DBO.PLANT_CITY
INSERT INTO GROWER_2011.DBO.PLANT_CITY (THIS_COLUMN, THAT_COLUMN, THE_OTHER_COLUMN)
 
SELECT THIS_COLUMN, THAT_COLUMN, THE_OTHER_COLUMN
FROM COMPANY_2011.DBO.PLANT_CITY
WHERE (THAT_COLUMN = 'GROWER_NAME')

COMMIT

The reason I delete the table first is I have found if I do not then the query fails and tells me that the table already exists.

All I really need to do is update the data in this table every day and have the computed columns stay in tact.

What would be a better way to accomplish what I need to do??

Thanks!!
0
Comment
Question by:HDM
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 37782497
UPDATE GROWER_2011.DBO.PLANT_CITY
SET THIS_COLUMN = DB1.THIS_COLUMN...
FROM GROWER_2011.DBO.PLANT_CITY D2
INNER JOIN COMPANY_2011.DBO.PLANT_CITY D1
ON D1.THE_OTHER_COLUMN = D2.THE_OTHER_COLUMN
0
 
LVL 8

Expert Comment

by:gpizzuto
ID: 37782603
Probably your error is caused by a command like:

SELECT ... INTO GROWER_2011.DBO.PLANT_CITY
FROM ...
0
 

Author Comment

by:HDM
ID: 37782664
Thanks!!

What does the DB1 stand for??  What does the ... do??  Does it mean I need to add each of the other columns??

How does the INTER JOIN function work??

Thanks!!
0
 
LVL 15

Accepted Solution

by:
Anuj earned 500 total points
ID: 37782722
DB1 and DB2 is just an alias name for the tables, later you can refer this alias name in the query.
Add the required columns that you wish to update.
JOINS are set operation used in TSQL, to return rows from two or more tables based on matching condition in ON Clause. check here for details
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 32
Get the latest status 8 32
Flattening heirachies 3 30
New to SSRS, extremely slow running report. 8 20
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question