This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--http://stackoverflow.com/questions/1435935/how-to-get-the-logical-name-of-the-transaction-log-in-sql-server-2005 | |
--get transaction log name | |
--select Name from sys.database_files | |
SELECT name as 'transaction log name' FROM sys.master_files WHERE database_id = db_id()AND type = 1; | |
--http://dba.stackexchange.com/questions/73850/shrink-transaction-log-while-using-alwayson-availability-group | |
--Execute on Primary Only | |
if (SELECT role | |
FROM sys.dm_hadr_availability_replica_states AS a | |
JOIN sys.availability_replicas AS b | |
ON b.replica_id = a.replica_id | |
WHERE b.replica_server_name = @@SERVERNAME) = 1 | |
BEGIN | |
Use [test_db] | |
-- 1) Bakup Trn | |
BACKUP LOG [test_db] TO DISK = N'D:\MSSQL\Backup\test_db.trn' WITH NOFORMAT, NOINIT, NAME = N' Trn Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10 | |
-- 2) Move used pages | |
DBCC SHRINKFILE (N'test_db_log' , 3000, NOTRUNCATE) | |
-- 3) SHRINKFILE Log | |
DBCC SHRINKFILE (N'test_db_log' , 3000) | |
END |