| Author | Topic | 
                            
                                    | rosetulipStarting Member
 
 
                                        15 Posts | 
                                            
                                            |  Posted - 2014-09-17 : 18:43:16 
 |  
                                            | I have a master and a child tableStudentMaster tableID is PK identity fieldStuDetail tableID is PK identitify fieldStudentMasterID is FKthe relationship between table StudentMaster and StuDetail table is one to many by StudentMaster.ID ->StuDetail.StudentMasterIDI want to do the following1. Insert a row in Master table using the row to be cloned2. Save the NEW IDENTITY value3. Insert a row in Child table using the row to be cloned and assign the NEW IDENTITY value saved in step 2.the data should be fromStudentMasterID           name              address1             Alex                ABC2             Eric                 DDD....StuDetailID        StudentMasterID    SourceCode    23                    1                      AZ34524                    1                      SF345....to after running this procedure IF I choose input ID =1 from StudentMaster tableStudentMasterID           name              address1             Alex                ABC2             Eric                 DDD3             Alex               ABC....StuDetailID        StudentMasterID    SourceCode    23                    1                      AZ34524                    1                      SF34525                    3                      AZ34526                    3                      SF345...anyone has ideas how to do this in scripts? |  | 
       
                            
                       
                          
                            
                                    | bitsmedAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2014-09-17 : 20:38:03 
 |  
                                          | Maybe: declare @fromid int;declare @toid int;set @fromid=1;insert into StudentMaster ([name],[address]) select [name]       ,[address]   from StudentMaster  where id=@fromid;select @toid=scope_identity();insert into StuDetail (StudentMasterID,SourceCode) select @toid       ,SourceCode   from StuDetail  where StudentMasterID=@fromid; |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rosetulipStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 00:40:55 
 |  
                                          | If I add one more detail table for Detail tableStudentMaster -> StuDetail -> MoreDetail   one        -> many                  one     ->   ManyHere is the data look like if I choose ID=1 from StudentMaster------------Before Clone-----------------------StudentMasterID           name              address1             Alex                ABC2             Eric                DDD....HeaderID        StudentMasterID    SourceCode    23                    1                      AZ34524                    1                      SF345....HeaderDetailID                   HeaderID         Source345                 23                     ppp346                 23                     UUU347                 23                     POI348                 23                     LKI678		    24			   OIU679		    24			   PYT680		    24                     URE...-------------After Clone---------------------------StudentMasterID           name              address1             Alex                ABC2             Eric                DDD3             Alex                ABC....HeaderID        StudentMasterID    SourceCode    23                    1                      AZ34524                    1                      SF34525                    3                      AZ34526                    3                      SF345...HeaderDetailID                   HeaderID         Source345                 23                     ppp346                 23                     UUU347                 23                     POI348                 23                     LKI349                 25                     ppp350                 25                     UUU351                 25                     POI352                 25                     LKI     678		    24			   OIU679		    24			   PYT680		    24                     URE681		    26			   OIU682		    26			   PYT683		    26                     UREHow to improve your scripts? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Upendra GuptaStarting Member
 
 
                                    12 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 06:20:42 
 |  
                                          | unspammed |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rosetulipStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 11:15:23 
 |  
                                          | Any one has ideas how to do this? It seems it needs a while loop to accomplish from 2th form to 3 form linking |  
                                          |  |  | 
                            
                       
                          
                            
                                    | bitsmedAged Yak Warrior
 
 
                                    545 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 13:32:13 
 |  
                                          | Try this: declare @fromid int;declare @toid int;set @fromid=1;insert into StudentMaster ([name],[address]) select [name]       ,[address]   from StudentMaster  where id=@fromid;select @toid=scope_identity();insert into Header (StudentMasterID,SourceCode) select @toid       ,SourceCode   from Header  where StudentMasterID=@fromid;insert into HeaderDetail (HeaderId,Source) select c.ID       ,b.Source   from Header as a        inner join HeaderDetail as b                on b.HeaderID=a.ID        inner join Header as c                on c.StudentMasterID=@toid               and c.SourceCode=a.SourceCode  where a.StudentMasterID=@fromid; |  
                                          |  |  | 
                            
                       
                          
                            
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 13:37:45 
 |  
                                          | You don't need to write a while loop. Just write a third INSERT statement similar to the second one but targeting the new HeaderDetail table.  Give it a go and post your results, whether they work or not.Note: Since this looks like a homework question, we expect that you'll want to work at writing the query, so that you can build your confidence. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rosetulipStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2014-09-19 : 23:28:33 
 |  
                                          | This is not a homework question. All the table name and data I make up, not the real table and data I am working on for my project. table name like "Student" is makeup. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rosetulipStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2014-09-20 : 01:14:37 
 |  
                                          | Here is what I wroteDECLARE @NewHeaderID integerSET @NewHeaderID = SCOPE_IDENTITY()insert into HeaderDetail (HeaderId,Source) select @NewHeaderID,       ,b.Source   from Header as a        inner join HeaderDetail as b                on b.HeaderID=a.ID  where a.StudentMasterID=@fromid  --- I input 1 here;It is working. But, not satisfactory Why?I clone the entire record set when I input 1I expect the result look like blewHeaderID StudentMasterID SourceCode23 1 AZ34524 1 SF34525 3 AZ34526 3 SF345...HeaderDetailID HeaderID Source345 23 ppp346 23 UUU347 23 POI348 23 LKI349 25 ppp350 25 UUU351 25 POI352 25 LKI678 24 OIU679 24 PYT680 24 URE681 26 OIU682 26 PYT683 26 URENow it comes like thisHeaderID StudentMasterID SourceCode23 1 AZ34524 1 SF34525 3 AZ34526 3 SF345...HeaderDetailID HeaderID Source345 23 ppp346 23 UUU347 23 POI348 23 LKI678 24 OIU679 24 PYT680 24 URE349 26 ppp350 26 UUU351 26 POI352 26 LKI681 26 OIU682 26 PYT683 26 UREAll become 26, I would like to see those 23 are 25 and those 24 are 26How do you recommend improve the script? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | rosetulipStarting Member
 
 
                                    15 Posts | 
                                        
                                          |  Posted - 2014-09-22 : 00:34:12 
 |  
                                          | How do you recommend improve the scripts? |  
                                          |  |  | 
                            
                            
                                |  |