Solved

SQL to copy data from one table to another

Posted on 2009-05-12
9
705 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

821 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