>  기사  >  데이터 베이스  >  按照一定规则批量修改表中新增字段的值

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

WBOY
WBOY원래의
2016-06-07 14:55:061618검색

运行于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
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;
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.