用于通过递归循环层次结构的 T-SQL 存储过程

发布于 2024-10-15 15:37:59 字数 1929 浏览 1 评论 0原文

我有一个具体问题。

我的存储过程无法正常工作。我将向您展示过程、数据库和参数:

ALTER PROCEDURE [dbo].[ManufacturerParentToChild] 

@ServiceProviderId int,
@CarmakerId int

AS 开始

SET NOCOUNT ON;

DECLARE @childSPPId int, @isDeleted bit

DECLARE ServiceProviderChildren_Cursor CURSOR FOR

SELECT ServiceProviderId, isDeleted
FROM ServiceProvider 
WHERE ParentServiceProviderId = @ServiceProviderId; 

OPEN ServiceProviderChildren_Cursor;

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId, @isDeleted;

WHILE @@FETCH_STATUS = 0
   BEGIN
        IF @ServiceProviderId > 0
        BEGIN
            EXEC ManufacturerParentToChild @childSPPId, @CarmakerId;

            IF (SELECT COUNT(*) FROM dbo.CarmakerPartnership WHERE ServiceProviderId = @childSPPId AND CarmakerId = @CarmakerId) = 0
            BEGIN
                IF (@isDeleted = 0)
                BEGIN
                    INSERT INTO dbo.CarmakerPartnership (CarmakerId, ServiceProviderId, CreatedBy, ChangedBy, ValidityPeriodFrom, ValidityPeriodTo) VALUES (@CarmakerId, @childSPPId, SYSTEM_USER, SYSTEM_USER, '01.01.1900 00:00:00', '31.12.9999 23:59:00.000')
                END
            END 
        END
      FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId;
   END;
CLOSE ServiceProviderChildren_Cursor;
DEALLOCATE ServiceProviderChildren_Cursor; END

你可以看到我上面的存储过程。

数据库表服务提供者有 7 行:

ServiceProviderId Name ParentServiceProviderId isDeleted
1 '父级' 空 0
2 '子 1' 1 0
3 '子 2' 1 0
4 'Child4' 2 0
5 'Child5' 3 0
6 'child6' 4 0
7 'Child7' 6 0

参数获取值:

@ServiceProviderId = 1 @CarmakerId = 5

该过程将 ServiceProviderId 7、6、4 和 2 插入 CarmakerPartnerShip,但不要插入 3 和 5!

有人知道为什么递归循环会跳转到 ServiceProviderId = 2 的子级,但不会跳转到 ServiceProviderId = 3 的子级吗?

感谢您的帮助,抱歉我的英语不好!

如果您有疑问可以问我。

最好的问候

亚历克斯

i have a specific Question.

My Stored Procedure don't work correctly.. I will show you the Procedure, the database and the parameter:

ALTER PROCEDURE [dbo].[ManufacturerParentToChild] 

@ServiceProviderId int,
@CarmakerId int

AS
BEGIN

SET NOCOUNT ON;

DECLARE @childSPPId int, @isDeleted bit

DECLARE ServiceProviderChildren_Cursor CURSOR FOR

SELECT ServiceProviderId, isDeleted
FROM ServiceProvider 
WHERE ParentServiceProviderId = @ServiceProviderId; 

OPEN ServiceProviderChildren_Cursor;

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId, @isDeleted;

WHILE @@FETCH_STATUS = 0
   BEGIN
        IF @ServiceProviderId > 0
        BEGIN
            EXEC ManufacturerParentToChild @childSPPId, @CarmakerId;

            IF (SELECT COUNT(*) FROM dbo.CarmakerPartnership WHERE ServiceProviderId = @childSPPId AND CarmakerId = @CarmakerId) = 0
            BEGIN
                IF (@isDeleted = 0)
                BEGIN
                    INSERT INTO dbo.CarmakerPartnership (CarmakerId, ServiceProviderId, CreatedBy, ChangedBy, ValidityPeriodFrom, ValidityPeriodTo) VALUES (@CarmakerId, @childSPPId, SYSTEM_USER, SYSTEM_USER, '01.01.1900 00:00:00', '31.12.9999 23:59:00.000')
                END
            END 
        END
      FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId;
   END;
CLOSE ServiceProviderChildren_Cursor;
DEALLOCATE ServiceProviderChildren_Cursor; END

You See my stored Procedure above.

The Database Table Service Provider have 7 Rows:

ServiceProviderId Name ParentServiceProviderId isDeleted
1 'Parent' Null 0
2 'Child1' 1 0
3 'Child2' 1 0
4 'Child4' 2 0
5 'Child5' 3 0
6 'child6' 4 0
7 'Child7' 6 0

The Paramater get the Values:

@ServiceProviderId = 1
@CarmakerId = 5

The Procedure insert the ServiceProviderId 7, 6, 4 and 2 into CarmakerPartnerShip but don't insert 3 and 5!

Have anybody a idea why the recursive loop jump trough the childs of ServiceProviderId = 2 but don't jump trough the childs of ServiceProviderId = 3 ??

Thank you for your Help and Sorry for my bad english!!

If you have Questions you can ask me.

Best Regards

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-10-22 15:37:59

while 块中的 fetch 语句可能是原因。目前它显示为:

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId;

尝试将其替换为:

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId, @isDeleted;

The fetch statement in the while block may be the cause. Currently it reads:

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId;

try replacing it with:

FETCH NEXT FROM ServiceProviderChildren_Cursor INTO @childSPPId, @isDeleted;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文