alter procedure dbo.spCreateDetects(@nodes nvarchar(max),@kindId int,@isManual bit,@methodId int,@sharePath nvarchar(255), @tcpIpPort int,@isSwFull bit,@swInclude nvarchar(255),@swExclude nvarchar(255),@update bit,@period int,@personId int=null) as begin set nocount on if( isnull(@nodes,'')='' or isnull(@methodId,0)=0 or isnull(@kindId,0)=0) return if( isnull(@personId,0)=0 ) select @personId=iPersonId from tPerson where bPersonSystem=1 create table #ids (id int primary key) if( isnull(@nodes,'')!='') insert #ids select id from dbo.ftCommaListToTableIds(@nodes) if( @isManual=1 ) begin delete do from tblDetectOpts do join tblDetect d on d.intDetectId=do.lintDetectId and do.lintDetectKindId=@kindId join #ids ids on ids.id=d.lintComputerNodeId where d.lintDetectStatusId=1 delete l from tblDetect d join #ids ids on ids.id=d.lintComputerNodeId join tblLog l on d.intDetectId = l.lintDetectId where d.lintDetectStatusId=1 and d.lintDetectKindId=@kindId set nocount off delete d from tblDetect d join #ids ids on ids.id=d.lintComputerNodeId where d.lintDetectStatusId=1 and d.lintDetectKindId=@kindId end --neplanovat, pokud detekce uz existuji create table #plan (id int primary key) insert #plan (id) select ids.id from #ids ids left join tblDetect d on d.lintComputerNodeId=ids.id and d.lintDetectKindId=@kindId and lintDetectStatusId=1 where d.lintComputerNodeId is null create table #ids2 (detId int primary key, compId int) set nocount off merge into tblDetect d using #plan pl on 1=0 when not matched by target then insert (lintComputerNodeId,lintDetectKindId,lintDetectStatusId,dteRqCreated,liRqCreatedPersonId,ManualRq) values (pl.id,@kindId,1/*detsNoAnswer*/,getutcdate(),@personId,@isManual) output inserted.intDetectId,pl.id into #ids2 (detId,compId); set nocount on insert tblDetectOpts (lintDetectId,lintComputerNodeId,lintDetectMethodId,txtSharePath,intTcpipPort,lintDetectKindId,bolSwFull,txtSwInclude,txtSwExclude,bolUpdate,intDetectPeriod) select ids.detId, pl.id, @methodId, @sharePath, @tcpIpPort, @kindId, @isSwFull, @swInclude, @swExclude, @update, @period from #plan pl join #ids2 ids on ids.compId=pl.id drop table #ids drop table #plan drop table #ids2 end