alter procedure dbo.spUpdatePropCache_ObjMoved(@objId int) as
begin	
	update p 
	set p.ParentNodeId=pp.lintNodeId,
		p.txtValue=pp.txtValue,
		p.NodeIdValue=pp.NodeIdValue,
		p.intState=pp.intState
	from (
		select 
			np.lintNodeId,
			p.lintKindId,
			max(len(n.txtPath)) MaxLen
		from tblProperty p
			join tblNode n on n.intNodeId=p.lintNodeId
			join tblNodeParent np on np.lintParentNodeId=p.lintNodeId and np.lintParentNodeId!=np.lintNodeId
		where p.bolInheritable=1 and p.ParentNodeId is null and np.lintNodeId=@objId
		group by np.lintNodeId,p.lintKindId
		) mp
		join tblNode nmp on len(nmp.txtPath)=mp.MaxLen
		join tblNodeParent npp on npp.lintParentNodeId=nmp.intNodeId and npp.lintNodeId=@objId
		join tblProperty pp on pp.lintNodeId=nmp.intNodeId and pp.lintKindId=mp.lintKindId and pp.ParentNodeId is null and pp.bolInheritable=1
		join tblProperty p on p.lintKindId=mp.lintKindId and p.lintNodeId=mp.lintNodeId and p.ParentNodeId is not null
	where p.ParentNodeId!=pp.lintNodeId
	

	insert into tblProperty (lintNodeId,lintKindId,bolInheritable,intState,txtValue,NodeIdValue,ParentNodeId)
	select
		t.lintNodeId,
		t.lintKindId,
		1,
		pp.intState,
		pp.txtValue,
		pp.NodeIdValue,
		pp.lintNodeId
	from (
			select 
				np.lintNodeId,
				p.lintKindId,
				max(len(n.txtPath)) MaxLen
			from tblProperty p
				join tblNode n on n.intNodeId=p.lintNodeId
				join tblNodeParent np on np.lintParentNodeId=p.lintNodeId and np.lintParentNodeId!=np.lintNodeId
			where p.bolInheritable=1 and p.ParentNodeId is null
			group by np.lintNodeId,p.lintKindId
		) t
		join tblNode n on len(n.txtPath)=t.MaxLen
		join tblProperty pp on pp.lintNodeId=n.intNodeId and pp.lintKindId=t.lintKindId and pp.bolInheritable=1 and pp.ParentNodeId is null
		join tblNodeParent np on np.lintParentNodeId=n.intNodeId and np.lintNodeId=t.lintNodeId
		join tblNodeParent np2 on np2.lintNodeId=t.lintNodeId
		left join (
			select
				p.lintNodeId,
				p.lintKindId
			from tblProperty p
				join tblNodeParent np on np.lintNodeId=p.lintNodeId and np.lintParentNodeId=@objId
		) pch on pch.lintNodeId=t.lintNodeId and pch.lintKindId=t.lintKindId
	where np2.lintParentNodeId=@objId 
		and pch.lintNodeId is null

	
	delete from pch
	from tblNodeParent np
		join tblProperty pch on pch.lintNodeId=np.lintNodeId and pch.ParentNodeId is not null and bolInheritable=1
		left join tblNodeParent np2 on np2.lintNodeId=pch.lintNodeId and np2.lintParentNodeId=pch.ParentNodeId
	where np.lintParentNodeId=@objId
		and np2.lintNodeId is null
end