Saturday, August 3, 2013

Rewrite a Cursor

with out cursors



set nocount on;
set ansi_padding on;
set ansi_nulls on;
set quoted_identifier on;

-- declare variables.
declare @spid as varchar(20)
declare @loginname as varchar(255)
declare @kill as int
declare @cmd as varchar(max)=''

set @kill = 01


IF @kill=01
begin
select
@cmd =@cmd+ 'kill '+cast(spid as varchar(10))+';'
from
sys.sysprocesses p
where-- spid>52
(db_name(dbid) = N'MY Db Name Here')
and (program_name = N'Microsoft Dynamics NAV Classic client')
and (datediff(ss, last_batch, getdate()) > 3600)
print @cmd
Exec(@cmd)
end
else
begin
select
@cmd =@cmd+ 'kill '+loginame +';'
from
sys.sysprocesses p
where
(db_name(dbid) = N'MY Db Name Here')
and (program_name = N'Microsoft Dynamics NAV Classic client')
and (datediff(ss, last_batch, getdate()) > 3600)
print @cmd
end
go





Satheesh


No comments:

Post a Comment