用于通过递归循环层次结构的 T-SQL 存储过程
我有一个具体问题。
我的存储过程无法正常工作。我将向您展示过程、数据库和参数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
while 块中的 fetch 语句可能是原因。目前它显示为:
尝试将其替换为:
The fetch statement in the while block may be the cause. Currently it reads:
try replacing it with: