Solved

Copying Data from one table to another Sql Server 2005

Posted on 2009-07-08
5
186 Views
Last Modified: 2012-05-07
Good afternoon,

I've got a table "Labor" that has existing employees hours in it.  I have created another table called "LaborReport" that needs to have all the data existing in Labor transfered to it.  It has all the same columns, minus it's own unique identifying column.

The columns in Labor are:
LaborId
JobId
EmployeeId
LaborDate
LaborHoursWorked
EmployeeRoleId
TimeTypeId
LaborNotes
LaborPeriodFrom
LaborPeriodTo
CreatedBy
CreatedDt
UpdatedBy
UpdatedDt

The columns in LaborReport except it has an added column called "ID"

What's the easiest way to get all the data from one to the other?  Is there a query I could write that would do it?

Also this only needs to be a one time transfer, I have the triggers that will keep the two tables the same after I can get all the existing data in there already.

While I'm very famliar with coding and accessing data in a database, I'm really novice when it comes to doing things inside a database.. so I'm assuming there has to be an easy way to do this rather than having to code a function to loop through and grab all the records, store the data in variables, then do an insert into the new table....
0
Comment
Question by:arhame
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
pssandhu earned 500 total points
ID: 24804619
INSERT INTO LaborReport (LaborId,JobId,EmployeeId,LaborDate,LaborHoursWorked,EmployeeRoleId,TimeTypeId,LaborNotes,LaborPeriodFrom,LaborPeriodTo,CreatedBy,CreatedDt,UpdatedBy,UpdatedDt)
Select LaborId,JobId,EmployeeId,LaborDate,LaborHoursWorked,EmployeeRoleId,TimeTypeId,LaborNotes,LaborPeriodFrom,LaborPeriodTo,CreatedBy,CreatedDt,UpdatedBy,UpdatedDt
From Labor

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24804626
Oh, by the way I am assuming your column names in the LaborReport table are the same as in Labor table.
Hope that helps.
P.
0
 
LVL 8

Author Comment

by:arhame
ID: 24804661
They are the same, except that LaborReport has it's own UniqueID column, and just stores LaborID (the unique ID on the main table) as an int.  Having the Unique ID's match isn't so important as just having the value there to access if I need it.

Let me log in and try that statement I'll be right back.
0
 
LVL 8

Author Closing Comment

by:arhame
ID: 31601151
Perfect and fast, thanks for your time :)
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24804711
No problem!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

24 Experts available now in Live!

Get 1:1 Help Now