Solved

SQL to copy data from one table to another

Posted on 2009-05-12
9
682 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
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.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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

744 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

11 Experts available now in Live!

Get 1:1 Help Now