您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页按照一定规则批量修改表中新增字段的值

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

来源:五一七教育网


运行于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 $velocityCount-->
create procedure proc_dept
as
declare @cursor cursor,
@id varchar(50),
@pid varchar(100)
begin
	set @cursor = cursor
	for
	select deparet_id, parent_deparet_id from dbo.tb_department_tree order by sequence;
	open @cursor
	fetch next from @cursor into @id, @pid;
	while(@@FETCH_STATUS = 0)
	begin
	if(@pid is null) 
	begin
	update tb_department_tree set scope = @id, tlevel = 1, tpath = @id where deparet_id = @id;
	end
	else
	begin 
	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;
	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;
	end;
	fetch next from @cursor into @id, @pid;
	end;
	close @cursor;
end;

exec proc_dept;

drop procedure proc_dept;

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务