alter procedure dbo.spRemoveNodes(@nodes nvarchar(max)) as
begin
	set nocount on
	
	create table #nids (id int primary key clustered)
	insert into #nids (id)
	select id from dbo.ftCommaListToTableIds(@nodes)

	if(not exists(select 1 from #nids))
		return

	update tblDocument set lintSrcNodeId=null where lintSrcNodeId in (select id from #nids)
	update tblDocument set lintDestNodeId=null where lintDestNodeId in (select id from #nids)
	update tblHistory set lintOpNodeId=null where lintOpNodeId in (select id from #nids)
	update tblHistory set lintOp2NodeId=null where lintOp2NodeId in (select id from #nids)
	update tblInventoryAsset set lintNodeId=null where lintNodeId in (select id from #nids)
	update tblInventoryAsset set lintUserNodeId=null where lintUserNodeId in (select id from #nids)
	update tblUserNodeRight set lintNodeId=null where lintNodeId in (select id from #nids)
	update tHdTicket set liHdTicketNodeId=null where liHdTicketNodeId in (select id from #nids)
	delete from tblProperty where lintNodeId in (select id from #nids)
	delete from tblHistory where lintNodeId in (select id from #nids)
	delete from NodeRelation where BeginNodeId in (select id from #nids) or EndNodeId in (select id from #nids)
		
	delete from tDocument where AMNoticeId in (select intNoticeId from tblNotice where lintNodeId in (select id from #nids))
	delete from tblNotice where lintNodeId in (select id from #nids)
	delete from tblNodeAtom where lintNodeId in (select id from #nids)
	delete from tblLog where lintNodeId in (select id from #nids)

	delete from tNodeProduct where liNodeId in (select id from #nids)

	delete from tblAtomLend where lintPersonNodeId in (select id from #nids)

	delete from SamAlert where ComputerId in (select id from #nids)
	
	delete from tblLicTrans where NodeId in (select id from #nids)
	delete from tblLicTrans where LicNodeId in (select id from #nids)
	delete from tblInstTrial where lintComputerNodeId in (select id from #nids)
	delete from tblInstHist where lintComputerNodeId in (select id from #nids)

	update tblLicHistItem set NodeId=null where NodeId in (select id from #nids)
	
	update LicHistItemAssign set SrcNodeId=null where SrcNodeId in (select id from #nids)
	update LicHistItemAssign set DestNodeId=null where DestNodeId in (select id from #nids)

	delete from tblDetectOpts where lintComputerNodeId in (select id from #nids)

	declare @detids nvarchar(max)
	select @detids=stuff((select ','+cast(intDetectId as nvarchar(max))
		from tblDetect
		where lintComputerNodeId in (select id from #nids) for xml path('')),1,1,'')
	
	declare @err nvarchar(max)
	begin try
		exec spRemoveDetects @detids
	end try
	begin catch
		select @err=ERROR_MESSAGE()
		raiserror(@err,16,1)
		return
	end catch
	
	begin tran t1
	update tblNode set lintParentId=null where intNodeId in (select id from #nids)
	begin try
		delete from tblNode where intNodeId in (select id from #nids)
	end try
	begin catch
		select @err=ERROR_MESSAGE()
		rollback tran t1
		drop table #nids
		raiserror(@err,16,1)
		return
	end catch
	delete from tblNodeParent where lintNodeId in (select id from #nids)
	commit tran t1

	drop table #nids
end