sql server 游标 示例代码 路径表示法

博主因SQL使用较少,常忘记写法,为避免多次查找,决定整理简单的SQL示例用于备忘,后续还会进一步完善。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mark jiaguoxinzhi linyee

因为sql用得少,,经常会忘记怎么写。。

找得多了,,不如自己整个简单的标例,备忘。

 

declare @chlid bigint  --定义变量用于保存游标对应记录的值
declare @pPath varchar --定义变量
declare cur2 cursor for select channelid,ChannelPath from dbo.Channel where ChannelDeep=2 --游标指向记录集
open cur2 --打开
    fetch next from cur2 into @chlid,@pPath --获取下一句,,
    while @@FETCH_STATUS=0 --状态未结束?
    begin
        --print @chlid
        --print @pPath
        update dbo.Channel set ChannelDeep=3,ChannelPath=@pPath+CAST(ChannelParent as varchar(20))+'/' where ChannelParent=@chlid --执行更新
        fetch next from cur2 into @chlid,@pPath --继续获取下一句
    end
close cur2 --关闭
deallocate cur2 --释放

路径中含本身

--UPDATE [dbo].[Channel]   SET [ChannelDeep] = 0 ,[ChannelPath] ='/'+CAST(ChannelId as varchar(20))+'/' WHERE ChannelParent=0

declare @chlid bigint  --定义变量用于保存游标对应记录的值
declare @pPath varchar --定义变量
declare cur1 cursor for select channelid,ChannelPath from dbo.Channel where ChannelDeep=2 --游标指向记录集
open cur1 --打开
    fetch next from cur1 into @chlid,@pPath --获取下一句,,
    while @@FETCH_STATUS=0 --状态未结束?
    begin
        --print @chlid
        --print @pPath
        update dbo.Channel set ChannelDeep=3,ChannelPath=@pPath+CAST(ChannelId as varchar(20))+'/' where ChannelParent=@chlid --执行更新
        fetch next from cur1 into @chlid,@pPath --继续获取下一句
    end
close cur1 --关闭
deallocate cur1 --释放
GO

再完善点点

declare @chlid bigint  --定义变量用于保存游标对应记录的值
declare @pPath varchar(255) --定义变量


SET nocount ON                   --忽略行数显示

declare cur1 cursor  --声明游标
for select channelid,ChannelPath from dbo.Channel where ChannelDeep=3 --游标指向记录集
open cur1 --打开

    fetch next from cur1 into @chlid,@pPath --获取下一句,,
    while @@FETCH_STATUS=0 --状态未结束?
    begin
        print @chlid
        print @pPath
        update dbo.Channel set ChannelDeep=4,ChannelPath=@pPath+CAST(ChannelId as varchar(20))+'/' where ChannelParent=@chlid --执行更新
        fetch next from cur1 into @chlid,@pPath --继续获取下一句
    end
close cur1 --关闭
deallocate cur1 --释放

SET nocount OFF      --打开计数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值