Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

update data from one SQL database to another

Posted on 2012-03-29
4
Medium Priority
?
265 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:Curtis Long
  • 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:Curtis Long
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

783 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