?
Solved

Transfer data from one table to another

Posted on 2007-12-04
4
Medium Priority
?
1,943 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 2000 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

764 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