Solved

Transfer data from one table to another

Posted on 2007-12-04
4
1,923 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

867 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

20 Experts available now in Live!

Get 1:1 Help Now