什么是好的Perl模式匹配/正则表达式模块?(What are good Perl Pattern-matching / Regex Modules?)
最近我一直需要做很多正则表达式/模式匹配的东西,并且从我的网页搜索中查看不同的示例/论坛帖子,似乎人们有时会提到perl有很好的模块来帮助简化模式匹配/正则表达式的任务,但他们忽略提及哪些是最好的..我已经看过CPAN这个,但他们的网站是不是很容易导航,因为我似乎无法有效地搜索类别..任何意见是赞赏。
I've been having a need to do a lot of regex / pattern-matching stuff lately and, in looking at different examples / forum posts from my web searches it seems people sometimes mention that perl has good modules to help in simplifying pattern matching / regex tasks, however they neglect to mention which ones are the best for this.. I have looked at CPAN for this but their site isn't very easy to navigate as I can't seem to search effectively by category.. any advice is appreciated.
原文:https://stackoverflow.com/questions/3258847
最满意答案
由于您试图将树放入表的一部分,看起来您无论如何都需要使用
SET IDENTITY_INSERT ON
。 您需要确保新树有空间。 在这种情况下,我假设49是表中当前的最大id
,因此我们不需要担心超出表中后面的树。您需要能够将ID从旧树映射到新树。 除非有一些围绕id的规则,只要它准确,确切的映射应该是无关紧要的,所以在这种情况下,我只是做这样的事情:
SET IDENTITY_INSERT dbo.Relations ON ;WITH CTE_MappedIDs AS ( SELECT old_id, ROW_NUMBER() OVER(ORDER BY old_id) + 49 AS new_id FROM ( SELECT DISTINCT parent AS old_id FROM #relationtree UNION SELECT DISTINCT child AS old_id FROM #relationtree ) SQ ) INSERT INTO dbo.Relations (Id, ParentId) SELECT CID.new_id, PID.new_id FROM #relationtree RT INNER JOIN CTE_MappedIDs PID ON PID.old_id = RT.parent INNER JOIN CTE_MappedIDs CID ON CID.old_id = RT.parent -- We need to also add the root node UNION ALL SELECT NID.new_id, NULL FROM #relationtree RT2 INNER JOIN CTE_MappedIDs NID ON NID.old_id = RT2.parent WHERE RT2.parent NOT IN (SELECT DISTINCT child FROM #relationtree) SET IDENTITY_INSERT dbo.Relations OFF
我没有测试过,但如果它没有按预期工作,那么希望它能指出你正确的方向。
Since you're trying to put the tree into a segment of the table it looks like you're going to need to use
SET IDENTITY_INSERT ON
for the table anyway. You're going to need to make sure that there is room for the new tree. In this case, I'll assume that 49 is the current maximumid
in your table so that we don't need to be concerned with overrunning a tree that's later in the table.You'll need to be able to map the IDs from the old tree to the new tree. Unless there's some rule around the ids, the exact mapping should be irrelevant as long as it's accurate, so in that case, I'd just do something like this:
SET IDENTITY_INSERT dbo.Relations ON ;WITH CTE_MappedIDs AS ( SELECT old_id, ROW_NUMBER() OVER(ORDER BY old_id) + 49 AS new_id FROM ( SELECT DISTINCT parent AS old_id FROM #relationtree UNION SELECT DISTINCT child AS old_id FROM #relationtree ) SQ ) INSERT INTO dbo.Relations (Id, ParentId) SELECT CID.new_id, PID.new_id FROM #relationtree RT INNER JOIN CTE_MappedIDs PID ON PID.old_id = RT.parent INNER JOIN CTE_MappedIDs CID ON CID.old_id = RT.parent -- We need to also add the root node UNION ALL SELECT NID.new_id, NULL FROM #relationtree RT2 INNER JOIN CTE_MappedIDs NID ON NID.old_id = RT2.parent WHERE RT2.parent NOT IN (SELECT DISTINCT child FROM #relationtree) SET IDENTITY_INSERT dbo.Relations OFF
I haven't tested that, but if it doesn't work as expected then hopefully it will point you in the right direction.