我写了一个存储过程。现在我明白了,这是非常糟糕的表现。我认为这是因为 while 循环。
ALTER PROCEDURE [dbo].[DeleteEmptyCatalogNodes]
@CatalogId UNIQUEIDENTIFIER,
@CatalogNodeType int = null
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CID UNIQUEIDENTIFIER
DECLARE @CNT int
SET @CID = @CatalogId
SET @CNT = @CatalogNodeType
DELETE cn FROM CatalogNodes cn
LEFT JOIN CatalogNodes as cnj on cn.CatalogNodeId = cnj.ParentId
LEFT JOIN CatalogArticles as ca on cn.CatalogNodeId = ca.CatalogNodeId
WHERE cn.CatalogId = @CID
AND cnj.CatalogNodeId IS NULL
AND ca.ArticleId IS NULL
AND (cn.CatalogNodeType = @CNT OR @CNT IS NULL)
WHILE (@@ROWCOUNT > 0)
BEGIN
DELETE cn FROM CatalogNodes cn
LEFT JOIN CatalogNodes as cnj on cn.CatalogNodeId = cnj.ParentId
LEFT JOIN CatalogArticles as ca on cn.CatalogNodeId = ca.CatalogNodeId
WHERE cn.CatalogId = @CID
AND cnj.CatalogNodeId IS NULL
AND ca.ArticleId IS NULL
AND (cn.CatalogNodeType = @CNT OR @CNT IS NULL)
END
END
你们中的任何人都可以给我一个关于如何更“设置”的提示吗?
非常感谢!
编辑评论和答案:
这些表是这样构建的:
目录节点:
CatalogNodeId|ParentId|Name
1|NULL|Root
2|1|Node1
3|1|Node2
4|2|Node1.1
目录文章:
CatalogNodeId|Name
3|Article1
3|Article2
3|Article3
调用我的 SP 后,必须删除 Node1 和 Node1.1。在第一个删除语句中,Node1.1 将被删除。在 While 循环中,Node1 将被删除。我希望我的问题现在更容易理解,它是一个树结构。