понедельник, 15 апреля 2024 г.

Shrink Log files

 declare @dbn varchar(max), @dbf varchar(max), @cmd varchar(max)

declare  CURS cursor for

SELECT 

    db.name AS [Database Name], 

    mf.name AS [Logical Name] 

FROM 

     sys.master_files AS mf

     INNER JOIN sys.databases AS db ON

            db.database_id = mf.database_id

where mf.type_desc = 'LOG' and db.database_id > 4;


open CURS;

FETCH NEXT FROM curs INTO @dbn, @dbf


WHILE @@FETCH_STATUS = 0

BEGIN

--print @dbn; print @dbf

select @cmd=''

select @cmd = 'use ['+@dbn+']; ALTER DATABASE ['+@dbn+'] MODIFY FILE (NAME = '''+@dbf+''', SIZE = 64, FILEGROWTH = 16MB); DBCC SHRINKFILE('''+@dbf+''',64);';

--print @cmd;

exec (@cmd);

FETCH NEXT FROM curs INTO @dbn, @dbf

end 

close CURS;

deallocate CURS;

SQL replication monitoring

 --SELECT * FROM [distribution].dbo.MSReplication_monitordata WHERE status not in(1,2,3,4)


SELECT * FROM [distribution].dbo.MSReplication_monitordata 

where not( agent_type=1 and status=2)

and not(agent_type = 2 and status=3)


3--2 - error



/*

agent_type

1 = Snapshot Agent

2 = Log Reader Agent

3 = Distribution Agent

4 = Merge Agent

9 = Queue Reader Agent


status

1 = Started

2 = Succeeded

3 = In progress

4 = Idle

5 = Retrying

6 = Failed

*/

-- 1 = Snapshot Agent / 2 = Succeeded

SELECT * FROM [distribution].dbo.MSReplication_monitordata where agent_type=1 and status<>2 

-- 2 = Log Reader Agent / 3 = In progress

SELECT * FROM [distribution].dbo.MSReplication_monitordata where agent_type=2 and status <>3

-- 3 = Distribution Agent / 3 = In progress, 4 = Idle 

SELECT * FROM [distribution].dbo.MSReplication_monitordata where agent_type=3 and (status <>3 or status <>4)



SELECT count(1) FROM [distribution].dbo.MSReplication_monitordata 

where 

(agent_type=1 and status<>2)

or (agent_type=2 and status <>3)

or (agent_type=3 and (status <>3 and status <>4) )


SELECT * FROM [distribution].dbo.MSReplication_monitordata