Solved

SQL to copy data from one table to another

Posted on 2009-05-12
9
699 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 32
Field name with special character (Ñ) in Oracle 11 51
SSRS Enable Remote Errors 4 26
syntax sql error 2 14
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to take different types of Oracle backups using RMAN.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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