将表数据复制到同一个表 [英] Copy the table data to the same table
问题描述
我有一个''页''表,其父母和子女的关系如...
I have a ''Page'' table with parent and child relations like..
PageId Name ParentPageId
1 A 0
2 B 1
3 C 1
4 D 2
5 E 2
6 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 ParentPageId
7 A 0
8 B 7
9 C 7
10 D 7
11 E 7
12 F 10
-------------------------------------- ------------------
为此,我使用了递归方法..
我的代码在下面给出.. < br $>
--------------------------------------------------------
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-....-levelN
and 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-3
2
2-1
2-2
3
4
5
and 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 INT
SELECT @maxpageid = MAX(PageId) FROM PageList
INSERT INTO PageList (PageId, Name, ParentPageId) SELECT (PageId + @maxpageid), Name,
CASE ParentPageId
WHEN 0 THEN 0
ELSE (ParentPageId + @maxpageid)
END
FROM 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,
这篇关于将表数据复制到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!