Solved

Transfer data from one table to another

Posted on 2007-12-04
4
1,919 Views
Last Modified: 2010-04-21
Ill try to explain as best as I can: I am Using MS SQL 2005
Database has two tables: Cases and Caselinks
Right now I have 1000 cases, 1000 rows under the case table, I need to transfer the id's from that table to the 'Caselinks' table, here is how its needed:

"Cases" table has the following items:

id (int)
caseid (nvarchar)
MainCase(bit)
firmid (int)

---- Explanation:  

+I need to transfer the 'firmid' of the cases table to the 'firmid' of the 'caselinks' table.
+From the cases table I need to transfer the 'id' to the 'caseidlinked' ONLY if the 'maincase' = 0 and the 'caseidmain' will be that 'id' of a row where the 'caseid' is the same but the 'maincase' = 1

Caselinks table:
firmid (int)
caseidmain (int)
caseidlinked (int)

Please let me know if more information is needed. In short right now the 'cases' are linked by the 'caseid'  and now we are moving the info to a new table to link them by the 'id'.

This may be complex but I hope its possible.




0
Comment
Question by:amucinobluedot
  • 2
4 Comments
 
LVL 1

Expert Comment

by:wizengamot
Comment Utility
I modified your caseLinks table a bit to include caseid (nvarchar)  This column can be dropped later so I figured it would be no problem to add it for the purposes of this solution.
The following code will insert all the non-main cases into your caselinks table.  The caseid is being brought over so that
Insert Into CaseLinks(firmid,caseidlinked, caseid)
      Select firmid,id, caseid FROM cases where MainCase=0


In order to update each row so that it gets the ID of the record from cases where the MainCase bit is 1 (I assume that there is only one Main case per caseid value) a cursor code sequence will have to be written.  This cursor will loop all the records in the caselinks table and for each one finding the id of cases where the caseid fields match and then performing an update. I will try to get a bit of time to write you the cursor tonight.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
Comment Utility
insert into caselinks (firmid, caseidlinked, caseidmain)
select a.firmid, a.id, b.id
from cases a
 inner join cases b on b.maincase = 1and a.caseid = b.caseid
where a.maincase = 0
0
 
LVL 25

Expert Comment

by:imitchie
Comment Utility
In other words, for each combination of
caseid where there exists both Maincase=0 and maincase=1,
get the firmid from Maincase=0, field firmid
get the caseidlinked from Maincase=0, field id
get the caseidmain from Maincase=1, field id
0
 

Author Closing Comment

by:amucinobluedot
Comment Utility
Great ! Seems to have worked. I will need to run some more tests but looks like that query did it !
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query Missing Money orders... 6 53
MS SQL Backup 24 69
Powershell SMO script not working. 18 99
SQL Script to find duplicates 16 19
In this article I will describe the Backup & Restore 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

743 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

8 Experts available now in Live!

Get 1:1 Help Now