Solved

SQL to copy data from one table to another

Posted on 2009-05-12
9
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to recover a database from a user managed backup
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 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