当前位置:Gxlcms > mysql > 按照一定规则批量修改表中新增字段的值

按照一定规则批量修改表中新增字段的值

时间:2021-07-01 10:21:17 帮助过:24人阅读

运行于SQLServer 2008 SQL Server create procedure proc_deptasdeclare @cursor cursor,@id varchar(50),@pid varchar(100)beginset @cursor = cursorforselect deparet_id, parent_deparet_id from dbo.tb_department_tree order by sequence;open @cursorf

运行于SQLServer 2008 SQL Server
  1. create procedure proc_dept
  2. as
  3. declare @cursor cursor,
  4. @id varchar(50),
  5. @pid varchar(100)
  6. begin
  7. set @cursor = cursor
  8. for
  9. select deparet_id, parent_deparet_id from dbo.tb_department_tree order by sequence;
  10. open @cursor
  11. fetch next from @cursor into @id, @pid;
  12. while(@@FETCH_STATUS = 0)
  13. begin
  14. if(@pid is null)
  15. begin
  16. update tb_department_tree set scope = @id, tlevel = 1, tpath = @id where deparet_id = @id;
  17. end
  18. else
  19. begin
  20. update tb_department_tree set 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 deparet_id = @id;
  21. update tb_department_tree set scope =(select SUBSTRING(tpath, 0, CHARINDEX('-',tpath, 0)) from tb_department_tree where deparet_id = @id) from tb_department_tree where deparet_id = @id;
  22. end;
  23. fetch next from @cursor into @id, @pid;
  24. end;
  25. close @cursor;
  26. end;
  27. exec proc_dept;
  28. drop procedure proc_dept;

人气教程排行