Solved

Copying Data from one table to another Sql Server 2005

Posted on 2009-07-08
5
190 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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