Solved

Transfer data from one table to another

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Dump exec output to table 3 21
What is the best way to use power bi and ssrs 3 20
SQL R 21 23
Need help in debugging a UDF results 7 16
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

808 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