sql procedure 循环修改字段值?
create procedure proc_dept as declare @cursor cursor, @id varchar(50), @pid varchar(100), @name varchar(100) begin set @cursor = cursor for select deparet_id, parent_deparet_id, deparet_name from dbo.tb_department_tree; open @cursor fetch next from @cursor into @id, @pid, @name; while(@@FETCH_STATUS = 0) begin if(@pid = null) begin update tb_department_tree set scope = @name, tlevel = 1, tpath = @id where parent_deparet_id is null; end else begin update tb_department_tree set scope = @name, tlevel = (select tlevel from tb_department_tree where deparet_id = @pid)+1, tpath = (select tpath from tb_department_tree where deparet_id = @pid)+'-'+@id where parent_deparet_id is not null; end; fetch next from @cursor into @id, @pid, @name; end; end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
where条件弄错了