alter procedure dbo.spCalcNodeName(@nodeId int) as
begin
	create table #t (id int, classId int, name nvarchar(255))
	create clustered index ixt on #t (id)

	insert into #t
	select 
		n.intNodeId,
		n.lintClassId,
		calc.CalcName
	from (
		select
			lintNodeId,
			isnull(stuff(
				isnull(', '+nullif([1],''),'') +
				isnull(', '+nullif([2],''),'') +
				isnull(', '+nullif([3],''),'') +
				isnull(', '+nullif([4],''),'') +
				isnull(', '+nullif([5],''),'') +
				isnull(', '+nullif([6],''),'') +
				isnull(', '+nullif([7],''),'') +
				isnull(', '+nullif([8],''),''), 1,2,''),'') CalcName
		from (
				select
					lintNodeId,
					txtValue,
					intNameOrder
				from tblProperty
				where isnull(intNameOrder,0)>0
			) piv
			pivot
			(
				max(txtValue)
				for intNameOrder in ([1],[2],[3],[4],[5],[6],[7],[8])
			) pvt
		) calc
		join tblNode n on n.intNodeId=calc.lintNodeId
	where isnull(n.txtName,'')!=isnull(calc.CalcName,'') collate Latin1_General_CS_AS
		and n.intNodeId=@nodeId

	insert into #t
	select
		s.SetNodeId,
		6,
		nodes.name
	from #t nodes
		join (
			select
				np.lintNodeId,
				s.intNodeId SetNodeId,
				count(pc.intNodeId) PcCnt
			from tblNodeParent np
				join tblNode s on s.intNodeId=np.lintParentNodeId and s.lintClassId=6
				join tblNodeParent np2 on np2.lintParentNodeId=s.intNodeId
				join tblNode pc on pc.intNodeId=np2.lintNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			group by np.lintNodeId, s.intNodeId
		) s on s.lintNodeId=nodes.id and s.PcCnt=1 and nodes.classId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)

	update n set n.txtName=c.name 
	from tblNode n
		join #t c on c.id=n.intNodeId

	declare @ob table (id int, p nvarchar(1024), done bit)

	insert into @ob
	select
		o.intNodeId,
		case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,''),
		0
	from #t n
		join tblNode o on o.lintParentId=n.id
		join tblNode par on par.intNodeId=n.id
		left join (
			select np.lintNodeId 
			from tblNodeParent np
				join #t n2 on n2.id=np.lintParentNodeId and np.lintNodeId!=np.lintParentNodeId
		) ex on ex.lintNodeId=n.id
	where ex.lintNodeId is null
		and isnull(o.txtPath,'')!=case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,'')

	while exists(select 1 from @ob where done=0)
	begin
		update n
		set n.txtPath=o.p
		from @ob o
			join tblNode n on n.intNodeId=o.id
		where o.done=0

		update @ob set done=1 where done=0

		insert into @ob
		select
			ch.intNodeId,
			case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,''),
			0
		from @ob o
			join tblNode n on n.intNodeId=o.id
			join tblNode ch on ch.lintParentId=o.id
		where isnull(ch.txtPath,'')!=case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,'')
	end

	drop table #t

	update s set s.txtName=pc.txtName
	from 
		(
			select
				s.intNodeId
			from tblNode s
				join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			where s.lintClassId=6
				and s.bolHidden=0
				and s.intNodeId=@nodeId
			group by s.intNodeId
			having count(*)=1
		) os
		join tblNode s on s.intNodeId=os.intNodeId
		join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
		left join (
			select 
				p.lintNodeId,
				count(*) cnt
			from
				tblProperty p 
			where isnull(p.intNameOrder,0)!=0
			group by p.lintNodeId
		) sp on sp.lintNodeId=os.intNodeId 
	where sp.lintNodeId is null
		and s.txtName!=pc.txtName
end


GO


alter procedure dbo.spCalcNodeNameAll as
begin
	create table #t (id int, classId int, name nvarchar(255))
	create clustered index ixt on #t (id)

	insert into #t
	select 
		n.intNodeId,
		n.lintClassId,
		calc.CalcName
	from (
		select
			lintNodeId,
			isnull(stuff(
				isnull(', '+nullif([1],''),'') +
				isnull(', '+nullif([2],''),'') +
				isnull(', '+nullif([3],''),'') +
				isnull(', '+nullif([4],''),'') +
				isnull(', '+nullif([5],''),'') +
				isnull(', '+nullif([6],''),'') +
				isnull(', '+nullif([7],''),'') +
				isnull(', '+nullif([8],''),''), 1,2,''),'') CalcName
		from (
				select
					lintNodeId,
					txtValue,
					intNameOrder
				from tblProperty
				where isnull(intNameOrder,0)>0
			) piv
			pivot
			(
				max(txtValue)
				for intNameOrder in ([1],[2],[3],[4],[5],[6],[7],[8])
			) pvt
		) calc
		join tblNode n on n.intNodeId=calc.lintNodeId
	where isnull(n.txtName,'')!=isnull(calc.CalcName,'') collate Latin1_General_CS_AS

	insert into #t
	select
		s.SetNodeId,
		6,
		nodes.name
	from #t nodes
		join (
			select
				np.lintNodeId,
				s.intNodeId SetNodeId,
				count(pc.intNodeId) PcCnt
			from tblNodeParent np
				join tblNode s on s.intNodeId=np.lintParentNodeId and s.lintClassId=6
				join tblNodeParent np2 on np2.lintParentNodeId=s.intNodeId
				join tblNode pc on pc.intNodeId=np2.lintNodeId and pc.lintClassId  IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			group by np.lintNodeId, s.intNodeId
		) s on s.lintNodeId=nodes.id and s.PcCnt=1 and nodes.classId  IN (SELECT intClassId FROM tblClass WHERE bComputer=1)

	update n set n.txtName=c.name 
	from tblNode n
		join #t c on c.id=n.intNodeId

	declare @ob table (id int, p nvarchar(1024), done bit)

	insert into @ob
	select
		o.intNodeId,
		case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,''),
		0
	from #t n
		join tblNode o on o.lintParentId=n.id
		join tblNode par on par.intNodeId=n.id
		left join (
			select np.lintNodeId 
			from tblNodeParent np
				join #t n2 on n2.id=np.lintParentNodeId and np.lintNodeId!=np.lintParentNodeId
		) ex on ex.lintNodeId=n.id
	where ex.lintNodeId is null
		and isnull(o.txtPath,'')!=case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,'')

	while exists(select 1 from @ob where done=0)
	begin
		update n
		set n.txtPath=o.p
		from @ob o
			join tblNode n on n.intNodeId=o.id
		where o.done=0

		update @ob set done=1 where done=0

		insert into @ob
		select
			ch.intNodeId,
			case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,''),
			0
		from @ob o
			join tblNode n on n.intNodeId=o.id
			join tblNode ch on ch.lintParentId=o.id
		where isnull(ch.txtPath,'')!=case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,'')
	end

	drop table #t

		update s set s.txtName=pc.txtName
	from 
		(
			select
				s.intNodeId
			from tblNode s
				join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			where s.lintClassId=6
				and s.bolHidden=0
			group by s.intNodeId
			having count(*)=1
		) os
		join tblNode s on s.intNodeId=os.intNodeId
		join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
		left join (
			select 
				p.lintNodeId,
				count(*) cnt
			from
				tblProperty p 
			where isnull(p.intNameOrder,0)!=0
			group by p.lintNodeId
		) sp on sp.lintNodeId=os.intNodeId 
	where sp.lintNodeId is null
		and s.txtName!=pc.txtName
end


GO


alter procedure dbo.spCalcNodeNameClass(@TemplateNodeId int) as
begin
	create table #t (id int, classId int, name nvarchar(255))
	create clustered index ixt on #t (id)

	insert into #t
	select 
		n.intNodeId,
		n.lintClassId,
		calc.CalcName
	from (
		select
			lintNodeId,
			isnull(stuff(
				isnull(', '+nullif([1],''),'') +
				isnull(', '+nullif([2],''),'') +
				isnull(', '+nullif([3],''),'') +
				isnull(', '+nullif([4],''),'') +
				isnull(', '+nullif([5],''),'') +
				isnull(', '+nullif([6],''),'') +
				isnull(', '+nullif([7],''),'') +
				isnull(', '+nullif([8],''),''), 1,2,''),'') CalcName
		from (
				select
					lintNodeId,
					txtValue,
					intNameOrder
				from tblProperty
				where isnull(intNameOrder,0)>0
			) piv
			pivot
			(
				max(txtValue)
				for intNameOrder in ([1],[2],[3],[4],[5],[6],[7],[8])
			) pvt
		) calc
		join tblNode n on n.intNodeId=calc.lintNodeId
		join tblNode tmp on tmp.lintClassId=n.lintClassId and tmp.intNodeId=@TemplateNodeId
	where isnull(n.txtName,'')!=isnull(calc.CalcName,'') collate Latin1_General_CS_AS

	insert into #t
	select
		s.SetNodeId,
		6,
		nodes.name
	from #t nodes
		join (
			select
				np.lintNodeId,
				s.intNodeId SetNodeId,
				count(pc.intNodeId) PcCnt
			from tblNodeParent np
				join tblNode s on s.intNodeId=np.lintParentNodeId and s.lintClassId=6
				join tblNodeParent np2 on np2.lintParentNodeId=s.intNodeId
				join tblNode pc on pc.intNodeId=np2.lintNodeId and pc.lintClassId  IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			group by np.lintNodeId, s.intNodeId
		) s on s.lintNodeId=nodes.id and s.PcCnt=1 and nodes.classId  IN (SELECT intClassId FROM tblClass WHERE bComputer=1)

	update n set n.txtName=c.name 
	from tblNode n
		join #t c on c.id=n.intNodeId

	declare @ob table (id int, p nvarchar(1024), done bit)

	insert into @ob
	select
		o.intNodeId,
		case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,''),
		0
	from #t n
		join tblNode o on o.lintParentId=n.id
		join tblNode par on par.intNodeId=n.id
		left join (
			select np.lintNodeId 
			from tblNodeParent np
				join #t n2 on n2.id=np.lintParentNodeId and np.lintNodeId!=np.lintParentNodeId
		) ex on ex.lintNodeId=n.id
	where ex.lintNodeId is null
		and isnull(o.txtPath,'')!=case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,'')

	while exists(select 1 from @ob where done=0)
	begin
		update n
		set n.txtPath=o.p
		from @ob o
			join tblNode n on n.intNodeId=o.id
		where o.done=0

		update @ob set done=1 where done=0

		insert into @ob
		select
			ch.intNodeId,
			case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,''),
			0
		from @ob o
			join tblNode n on n.intNodeId=o.id
			join tblNode ch on ch.lintParentId=o.id
		where isnull(ch.txtPath,'')!=case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,'')
	end

	drop table #t

	update s set s.txtName=pc.txtName
	from 
		(
			select
				s.intNodeId
			from tblNode s
				join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			where s.lintClassId=6
				and s.bolHidden=0
			group by s.intNodeId
			having count(*)=1
		) os
		join tblNode s on s.intNodeId=os.intNodeId
		join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
		join tblNode tmp on tmp.lintClassId=s.lintClassId and tmp.intNodeId=@TemplateNodeId
		left join (
			select 
				p.lintNodeId,
				count(*) cnt
			from
				tblProperty p 
			where isnull(p.intNameOrder,0)!=0
			group by p.lintNodeId
		) sp on sp.lintNodeId=os.intNodeId 
	where sp.lintNodeId is null
		and s.txtName!=pc.txtName
end

go


alter procedure dbo.spCalcNodeNameSubtree(@parentId int) as
begin

	create table #t (id int, classId int, name nvarchar(255))
	create clustered index ixt on #t (id)

	insert into #t
	select 
		n.intNodeId,
		n.lintClassId,
		calc.CalcName
	from (
		select
			lintNodeId,
			isnull(stuff(
				isnull(', '+nullif([1],''),'') +
				isnull(', '+nullif([2],''),'') +
				isnull(', '+nullif([3],''),'') +
				isnull(', '+nullif([4],''),'') +
				isnull(', '+nullif([5],''),'') +
				isnull(', '+nullif([6],''),'') +
				isnull(', '+nullif([7],''),'') +
				isnull(', '+nullif([8],''),''), 1,2,''),'') CalcName
		from (
				select
					lintNodeId,
					txtValue,
					intNameOrder
				from tblProperty
				where isnull(intNameOrder,0)>0
			) piv
			pivot
			(
				max(txtValue)
				for intNameOrder in ([1],[2],[3],[4],[5],[6],[7],[8])
			) pvt
		) calc
		join tblNode n on n.intNodeId=calc.lintNodeId
		join tblNodeParent np on np.lintNodeId=n.intNodeId and np.lintParentNodeId=@parentId
	where isnull(n.txtName,'')!=isnull(calc.CalcName,'') collate Latin1_General_CS_AS

	insert into #t
	select
		s.SetNodeId,
		6,
		nodes.name
	from #t nodes
		join (
			select
				np.lintNodeId,
				s.intNodeId SetNodeId,
				count(pc.intNodeId) PcCnt
			from tblNodeParent np
				join tblNode s on s.intNodeId=np.lintParentNodeId and s.lintClassId=6
				join tblNodeParent np2 on np2.lintParentNodeId=s.intNodeId
				join tblNode pc on pc.intNodeId=np2.lintNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			group by np.lintNodeId, s.intNodeId
		) s on s.lintNodeId=nodes.id and s.PcCnt=1 and nodes.classId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)

	update n set n.txtName=c.name 
	from tblNode n
		join #t c on c.id=n.intNodeId

	declare @ob table (id int, p nvarchar(1024), done bit)

	insert into @ob
	select
		o.intNodeId,
		case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,''),
		0
	from #t n
		join tblNode o on o.lintParentId=n.id
		join tblNode par on par.intNodeId=n.id
		left join (
			select np.lintNodeId 
			from tblNodeParent np
				join #t n2 on n2.id=np.lintParentNodeId and np.lintNodeId!=np.lintParentNodeId
		) ex on ex.lintNodeId=n.id
	where ex.lintNodeId is null
		and isnull(o.txtPath,'')!=case when par.txtPath is null then '' else par.txtPath+'/' end+isnull(par.txtName,'')

	while exists(select 1 from @ob where done=0)
	begin
		update n
		set n.txtPath=o.p
		from @ob o
			join tblNode n on n.intNodeId=o.id
		where o.done=0

		update @ob set done=1 where done=0

		insert into @ob
		select
			ch.intNodeId,
			case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,''),
			0
		from @ob o
			join tblNode n on n.intNodeId=o.id
			join tblNode ch on ch.lintParentId=o.id
		where isnull(ch.txtPath,'')!=case when n.txtPath is null then '' else n.txtPath+'/' end+isnull(n.txtName,'')
	end

	drop table #t

	update s set s.txtName=pc.txtName
	from 
		(
			select
				s.intNodeId
			from tblNode s
				join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
			where s.lintClassId=6
				and s.bolHidden=0
			group by s.intNodeId
			having count(*)=1
		) os
		join tblNode s on s.intNodeId=os.intNodeId
		join tblNode pc on pc.lintParentId=s.intNodeId and pc.lintClassId IN (SELECT intClassId FROM tblClass WHERE bComputer=1)
		join tblNodeParent np on np.lintNodeId=pc.intNodeId and np.lintParentNodeId=@parentId
		left join (
			select 
				p.lintNodeId,
				count(*) cnt
			from
				tblProperty p 
			where isnull(p.intNameOrder,0)!=0
			group by p.lintNodeId
		) sp on sp.lintNodeId=os.intNodeId 
	where sp.lintNodeId is null
		and s.txtName!=pc.txtName
end