Author |
Topic |
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2012-07-27 : 07:16:08
|
I have tried to create an example to illustrate the problem I'm dealing with at the moment because I can't seem to explain it in words.Please consider the following situation
declare @SomeTable table(ID int IDENTITY(1,1), someField varchar(50))declare @TableMaster table(ID int IDENTITY(1,1), FK_Some int null, FK_1 int null, RepeatCount int null, aField varchar(50))declare @TableDetail table(ID int IDENTITY(1,1), FK_Some int, FK_1 int, RepeatCount int null, FK_Master int, bField varchar(50))insert into @SomeTablevalues('something')insert into @TableMastervalues(null, null, null,'MasterValue1'),(1,1,null,'MasterValue2'),(1,2,1,'MasterValue3'),(1,2,2,'MasterValue4'),(1,2,3,'MasterValue5'),(1,1,null,'MasterValue6')insert into @TableDetailvalues(1,1,null,1,'DetailValue1'),(1,1,null,2,'DetailValue2'),(1,2,1,1,'DetailValue3'),(1,2,1,3,'DetailValue4'),(1,2,2,1,'DetailValue5'),(1,2,2,4,'DetailValue6'),(1,2,3,1,'DetailValue7'),(1,2,3,5,'DetailValue8'),(1,1,null,6,'DetailValue9') There is a fourth table, but since it is not relevant to the question I didn't include it. The foreign key FK_1 refers to this missing table.The data should look like this
1 NULL NULL NULL MasterValue12 1 1 NULL MasterValue23 1 2 1 MasterValue34 1 2 2 MasterValue45 1 2 3 MasterValue56 1 1 NULL MasterValue61 1 1 NULL 1 DetailValue12 1 1 NULL 2 DetailValue23 1 2 1 1 DetailValue34 1 2 1 3 DetailValue45 1 2 2 1 DetailValue56 1 2 2 4 DetailValue67 1 2 3 1 DetailValue78 1 2 3 5 DetailValue89 1 1 NULL 6 DetailValue9 At some point I'm inserting a new record to "SomeTable" (creating ID 2 in this example).What I want to do is make a copy of all records in the MasterTable and the DetailTable where FK_Some is equal to 1, for FK_Some 2.This should give the following output
1 NULL NULL NULL MasterValue12 1 1 NULL MasterValue23 1 2 1 MasterValue34 1 2 2 MasterValue45 1 2 3 MasterValue56 1 1 NULL MasterValue67 2 1 NULL MasterValue28 2 2 1 MasterValue39 2 2 2 MasterValue410 2 2 3 MasterValue511 2 1 NULL MasterValue61 1 1 NULL 1 DetailValue12 1 1 NULL 2 DetailValue23 1 2 1 1 DetailValue34 1 2 1 3 DetailValue45 1 2 2 1 DetailValue56 1 2 2 4 DetailValue67 1 2 3 1 DetailValue78 1 2 3 5 DetailValue89 1 1 NULL 6 DetailValue910 2 1 NULL 1 DetailValue111 2 1 NULL 7 DetailValue212 2 2 1 1 DetailValue313 2 2 1 8 DetailValue414 2 2 2 1 DetailValue515 2 2 2 9 DetailValue616 2 2 3 1 DetailValue717 2 2 3 10 DetailValue818 2 1 NULL 11 DetailValue9 I've tried with the following, but since the key what makes a record unique in the Detail Table, includes the foreign key to the Master table, I keep getting duplicates.Any suggestions on how to get my desired output?Thanks in advance!
declare @tmpInserted table(aNewID int, FK_Some int null, FK_1 int null, RepeatCount int null, aField varchar(50))insert into @TableMasteroutput inserted.* into @tmpInsertedselect 2, FK_1, RepeatCount, aFieldfrom @TableMasterwhere FK_Some = 1insert into @TableDetailselect 2, det1.FK_1, det1.RepeatCount, det1.FK_Master, det1.bFieldfrom @TableDetail as det1where det1.FK_Some = 1and det1.FK_Master not in (select mast1.ID from @TableMaster as mast1 where mast1.FK_Some = 1)union allselect 2, det2.FK_1, det2.RepeatCount, tmp.aNewID, det2.bFieldfrom @TableDetail as det2inner join @tmpInserted as tmp on tmp.FK_Some = 2 and tmp.FK_1 = det2.FK_1 and ( (tmp.RepeatCount = det2.RepeatCount) or (tmp.RepeatCount is null and det2.RepeatCount is null) )where det2.FK_Some = 1and det2.FK_Master in (select mast2.ID from @TableMaster as mast2 where mast2.FK_Some = 1)select * from @TableMasterselect * from @TableDetail
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-07-27 : 08:54:49
|
[code]declare @SomeTable table(ID int IDENTITY(1,1), someField varchar(50))declare @TableMaster table(ID int IDENTITY(1,1), FK_Some int null, FK_1 int null, RepeatCount int null, aField varchar(50))declare @TableDetail table(ID int IDENTITY(1,1), FK_Some int, FK_1 int, RepeatCount int null, FK_Master int, bField varchar(50))insert into @SomeTablevalues('something')insert into @TableMastervalues(null, null, null,'MasterValue1'),(1,1,null,'MasterValue2'),(1,2,1,'MasterValue3'),(1,2,2,'MasterValue4'),(1,2,3,'MasterValue5'),(1,1,null,'MasterValue6')insert into @TableDetailvalues(1,1,null,1,'DetailValue1'),(1,1,null,2,'DetailValue2'),(1,2,1,1,'DetailValue3'),(1,2,1,3,'DetailValue4'),(1,2,2,1,'DetailValue5'),(1,2,2,4,'DetailValue6'),(1,2,3,1,'DetailValue7'),(1,2,3,5,'DetailValue8'),(1,1,null,6,'DetailValue9')declare @newid int = 2declare @oldid int = 1insert @TableMasterselect @newid, FK_1, RepeatCount, aFieldfrom @TableMasterwhere FK_Some = @oldidinsert @TableDetailselect @newid, FK_1, RepeatCount, FK_Master, bFieldfrom @TableDetailwhere FK_Some = @oldidand FK_Master = 1insert @TableDetailselect @newid, d.FK_1, d.RepeatCount, m2.id, d.bFieldfrom @TableDetail djoin @TableMaster m1 on m1.FK_Some = @oldid and m1.ID = d.FK_Master and m1.FK_1 = d.FK_1join @TableMaster m2 on m2.FK_Some = @newid and m2.aField = m1.aFieldwhere d.FK_Some = @oldidand d.FK_Master <> 1select * from @TableMasterselect * from @TableDetailorder by FK_Some, bField [/code]==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
Zifter
Yak Posting Veteran
51 Posts |
Posted - 2012-07-27 : 09:11:35
|
Thank you nigel for the quick response!Your query does indeed return the desired output.I'm not sure if I will be able to join my Master and Detail tables on "aField" in my real live environment (this was just an example to demonstrate my problem) but let me see if I can adapt my query to your logic.edit: just noticed you're joining the Master table with itself. That works for me.Thanks again for your help! |
 |
|
|
|
|