Solved

SQL to copy data from one table to another

Posted on 2009-05-12
9
690 Views
Last Modified: 2013-12-07
Hi,

I have two tables, one

HistorianData  which has a lot of fields      e.g. TimeStamp, TagA, TagB, TagX

the other

DataDump has two fields  Timestamp and TagX

I want to copy the TagX field from the DataDump to the HistorianData table where the Timestamp values match (so that the DataDump table can be deleted).....I got a data dump from client (HistorianData) and they forgot all the fields so need to join them

How can this be done with transact-sql?  

I thought something like this but get error
The multi-part identifier "DataDump.TIMESTAMP" could not be bound.

UPDATE [historiandata]
SET [historiandata].[TagX] = [DataDump].[TagX]
WHERE
[historiandata].[TIMESTAMP] = [DataDump].[TIMESTAMP]


Thanks in advance for help!
0
Comment
Question by:rwallacej
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 350 total points
ID: 24366820
UPDATE h
SET TagX = d.TagX
From HistorianData h
            Inner Join DataDump d On h.[Timestamp] = d.[Timestamp]
0
 
LVL 22

Assisted Solution

by:dportas
dportas earned 50 total points
ID: 24367417
Use MERGE:

MERGE INTO HistorianData
USING DataDump
ON HistorianData.Timestamp = DataDump.Timestamp
WHEN MATCHED THEN UPDATE SET TagX = DataDump.TagX ;

"Timestamp" is a reserved word. Not a good choice for a column name.
0
 
LVL 6

Assisted Solution

by:bokist
bokist earned 100 total points
ID: 24367681
or this way :

update HistorianData set TagX = D.TagX
  from DataDump D
 where HistorianData.Timestamp = D.Timestamp

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24370646
>>"Timestamp" is a reserved word.<<
Not yet in T-SQL (it may be the case on PL/SQL).  See here:
Reserved Keywords (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms189822.aspx

>>Not a good choice for a column name.<<
But I agree, I just did not want the thread to digress.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Closing Comment

by:rwallacej
ID: 31580633
thank-you all for help, I chose acperkins option and hope points are ok for all

0
 

Author Comment

by:rwallacej
ID: 24372617
thank-you all for help,

I agree Timestamp is not a good field name (I didn't choose it) - this was what was given in the data dump from client

I chose acperkins but may try the others for performance too

regards,

rwallacej
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24374172
>>I chose acperkins but may try the others for performance too<<
When choosing the option, aside from performance you may also want to check the database used.  For example, if it is MS SQL Server then you may find MERGE to be not recognized as a T-SQL clause.
0
 
LVL 22

Expert Comment

by:dportas
ID: 24374273
>> you may find MERGE to be not recognized as a T-SQL clause.

MERGE is supported in SQL Server 2008 :)
On the other hand the UPDATE syntax used here is non-standard and is ONLY supported by SQL Server :(
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24374404
>>MERGE is supported in SQL Server 2008 <<
I stand corrected.  Thank you.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

939 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now