本文介绍了将表数据复制到同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个''页''表,其父母和子女的关系如...I have a ''Page'' table with parent and child relations like..PageId Name ParentPageId1 A 02 B 13 C 14 D 25 E 26 F 4 我的要求是,我想基于pageId复制上述数据。 例如,如果我传递PageId = 1,则所有相关行应该复制.. 我想要的输出是这样的..My requirement is, I want to copy of the above data based on the pageId.For example if I pass PageId=1, all the relate rows should copy..My desired output is like this..PageId Name ParentPageId7 A 08 B 79 C 710 D 711 E 712 F 10 -------------------------------------- ------------------ 为此,我使用了递归方法.. 我的代码在下面给出..--------------------------------------------------------For this I used recursion method..my code is given below..private void CreatePageCopy(int pId){ try { objpagebo.Action = "COPY"; objpagebo.PageID = pId; if (dsChildPages != null) { if (dsChildPages.Tables[0].Rows.Count != 0) { objpagebo.PageName = dsChildPages.Tables[0].Rows[0]["PageName"].ToString(); } } else { objpagebo.PageName = HfPageName.Value; } objpagebo.ActiveStatus = "Active"; objpagebo.CreationDate = Convert.ToDateTime(System.DateTime.Now).ToString("dd/mm/yyyy"); objpagebo.CreatedBy = Convert.ToString(oEmployeeBO.EmployeeId); objpagebo.ModifiedBy = Convert.ToString(oEmployeeBO.EmployeeId); objpagebo.ModificationDate = Convert.ToDateTime(System.DateTime.Now).ToString("dd/MM/yyyy"); DataTable dtGetParentPageId = PageMasterBLL.GetParentPageid(objpagebo); if (dsChildPages != null) { if (dsChildPages.Tables[0].Rows.Count != 0) { objpagebo.ParentPageId = Convert.ToInt32(dsChildPages.Tables[1].Rows[0]["ParentId"]); } } else { objpagebo.ParentPageId = Convert.ToInt32(dtGetParentPageId.Rows[0]["ParentPageId"]); } objpagebo.PageDescription = ""; dsChildPages = PageMasterBLL.InsertPageCopy(objpagebo); int PageIdForCopy = Convert.ToInt32(dsChildPages.Tables[1].Rows[0]["ParentId"]); if (dsChildPages.Tables[0].Rows.Count > 0 && dsChildPages != null) { foreach (DataRow drChildPages in dsChildPages.Tables[0].Rows) { CreatePageCopy(Convert.ToInt32(drChildPages["PageId"])); } } } catch (Exception Ex) { throw Ex; }} 但是这个方法只有在页面有一个级别的子页面时才有效...下一级别ParentPageId没有正确地进行..因为在这里我把父PageId作为最后一个插入行。 请给我一个解决方案..But this method will work only if a page have child pages with one level...next level ParentPageId is not comming correctely..because here I am taking Parent PageId as last Inserted row,.Pls give me a solution..推荐答案结合子代码和父代码: 您的记录代码是:Level1-level2-level3 -....- levelN 和每个破折号( - )显示一代人的生成。这意味着从父代码构建子代码+ - + childNumber: level 1: 1 1-1 1-1-1 1-1-2 1-2 $ 2 2-1 2-2 3 4 5 和使用喜欢你可以选择节点的子节点和父节点Combine Child Code and Parent Code : your Record Code is : Level1-level2-level3-....-levelNand each dash(-) show one level of generation. this means child code build from parent code + "-" + childNumber : level 1 :1 1-1 1-1-1 1-1-2 1-2 1-32 2-1 2-2345and using Like you can select child and parent of nodes不知道您的分页层次结构,您可以编写脚本来复制这些行。如果您注意到,您可以看到,对于特定页面,在原始记录和复制记录中,ParentPageId和PageId之间保持相同的差异。你可以利用它。Without knowing your hierarchy of paging, you could write a script to copy these rows. If you notice, you could see, for a particular Page, same difference is maintained between ParentPageId and PageId in original record and copied record. You could take advantage of that.DECLARE @maxpageid INTSELECT @maxpageid = MAX(PageId) FROM PageListINSERT INTO PageList (PageId, Name, ParentPageId) SELECT (PageId + @maxpageid), Name,CASE ParentPageId WHEN 0 THEN 0 ELSE (ParentPageId + @maxpageid)ENDFROM PageList 这不是正确的做法。如果这个复制是一次而pageIds是+ ve整数,那么它将正常工作。我建议使用递归存储过程/ sql函数来执行此操作。 干杯,This is not is correct approach. If this copying is one time and pageIds are +ve integer, then it would work fine. I would recommend to use recursive stored procedure/ sql function to do this.Cheers, 这篇关于将表数据复制到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!
05-22 13:37