Solved

Transfer data from one table to another

Posted on 2007-12-04
4
1,940 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:wizengamot
ID: 20408247
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
ID: 20408293
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
ID: 20408301
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
ID: 31412753
Great ! Seems to have worked. I will need to run some more tests but looks like that query did it !
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

690 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