注意定义成local类型的cursor !
PROCEDURE sp_get_all_lineal_parent @node_id int, @lineal_path varchar(200) output with encryption AS
SET NOCOUNT ON BEGIN IF ISNULL(@lineal_path, 'NULL') = 'NULL' SET @lineal_path = '' IF EXISTS( select DependentProductID from Dependence_TB where ProductID = @node_id ) BEGIN DECLARE cur_parent_node CURSOR FAST_FORWARD local FOR SELECT DependentProductID FROM Dependence_TB WHERE ProductID = @node_id
DECLARE @parent_node_id int
OPEN cur_parent_node FETCH NEXT FROM cur_parent_node INTO @parent_node_id WHILE @@FETCH_STATUS = 0 BEGIN set @lineal_path = @lineal_path + ',' + cast(@parent_node_id as varchar) exec sp_get_all_lineal_parent @parent_node_id, @lineal_path output
FETCH NEXT FROM cur_parent_node INTO @parent_node_id END CLOSE cur_parent_node DEALLOCATE cur_parent_node END insert into ##temp_a(a) select 100 select * from ##temp_a END
--调用示例begin declare @path varchar(200) create table ##temp_a(a int) EXEC sp_get_all_lineal_parent 12, @path output drop table ##temp_a select @path end
kitesky
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有