无限级联删除region下的区域以及下属二级区域(sqlserver 存储过程)

/***************************************************************
//    存储过程名称: [u_region_nodedelete]
//    功能描述: 无限级联删除region下的区域以及下属二级区域
//    作    者: czy
//
//    修改记录
// ---------------------------------------------------------------
//    修改日期    版本号        修改人        修改摘要
// ---------------------------------------------------------------
//    2013-10-25    V1.0     czy        无限级联删除
// ***************************************************************/
    SET ANSI_NULLS ON  
    GO  
    SET QUOTED_IDENTIFIER ON  
    GO  
    CREATE PROCEDURE u_region_nodedelete  
        @id BIGINT  
    AS  
    BEGIN  
        DECLARE @tbIds TABLE(id BIGINT)  
        DECLARE @tempTbs TABLE(id BIGINT)  
        DECLARE @tb TABLE(id BIGINT,orderIndex BIGINT IDENTITY(1,1))  
        INSERT INTO @tbIds(id) VALUES(@id)  
        INSERT INTO @tempTbs(id) VALUES(@id)  
        INSERT INTO @tb(id) VALUES(@id)  
        WHILE EXISTS(SELECT id FROM @tbIds)  
            BEGIN  
                DELETE FROM @tbIds  
                INSERT INTO @tb SELECT regid FROM region WHERE up_regid IN (SELECT ID FROM @tempTbs)  
                INSERT INTO @tbIds SELECT regid FROM region WHERE up_regid IN (SELECT ID FROM @tempTbs)  
                DELETE FROM @tempTbs  
                INSERT INTO @tempTbs SELECT id FROM @tbIds  
            END  
        DECLARE @tid INT   
        DECLARE myCursor CURSOR FOR SELECT id FROM @tb ORDER BY orderIndex DESC   
        OPEN myCursor   
        FETCH NEXT FROM myCursor INTO @tid   
        WHILE @@fetch_status=0   
        BEGIN  
            DELETE FROM region WHERE regid = @tid
            DELETE FROM regionpre WHERE regid = @tid
            FETCH NEXT FROM myCursor INTO @tid    
        END  
        CLOSE myCursor     
        DEALLOCATE myCursor  
    END  
    GO 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值