Sql Server ShrinkFile Error Solution

Message
Executed as user: CN\HKSQLPWV625sqlagent. Cannot shrink log file 2 (DIX_Log) because the logical log file located at the end of the file is in use. [SQLSTATE 01000] (Message 9008)  DBCC execution completed. If DBCC printed error messages, contact your system administrator. [SQLSTATE 01000] (Message 2528)  DBCC execution completed. If DBCC printed error messages, contact your system administrator. [SQLSTATE 01000] (Message 2528)  Cannot shrink log file 2 (LTD_Log) because the logical log file located at the end of the file is in use. [SQLSTATE 01000] (Message 9008)  DBCC execution completed. If DBCC printed error messages, contact your system administrator. [SQLSTATE 01000] (Message 2528)  Backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized. Reissue the statement after the current backup or file manipulation operation is completed. [SQLSTATE 42000] (Error 3023).  The step failed.

 

use DIX

dbcc loginfo

- Create a table to store information loginfo

create table DoxLoginfo
(
ID int identity,
RecoveryUnitId int null,
fileld int null,
filesize int null,
StartOffset int null,
Status int null,
Parity int null,
CreateLSN int null,
CreateDate datetime default getdate()
)

insert into DixLoginfo(RecoveryUnitId,fileld,filesize,StartOffset,FSeqNo,Status,Parity,CreateLSN)

EXEC ('DBCC loginfo')

- query the virtual log and determine the final status of a shrink to zero

declare @status int select @status = Status from DixLoginfo where id = (select MAX(id) from DixLoginfo)   

if (@status =0)      

begin        

DBCC SHRINKFILE(DIX_log,TRUNCATEONLY)     

- Delete loginfo than 7 days        

DELETE FROM DixLoginfo where datediff(day,createdate,getdate())>7      

end 

GO

 

- Transfer from the foreboding of the Internet

Each database has at least one log file, regardless of the physical file to define a number of small transaction log, SQL Server are regarded as one continuous file. The transaction log file is actually managed by a series of virtual log files VLF. The size of the virtual log file is determined by the total size of the log file of SQL Server. The physical structure of the virtual log file as follows:

When the log file is contracted, VLF unused end of the log file can be deleted.

In SQL server2000, the log file can only be contracted from the tail of the log files, but Microsoft has corrected previously in the question of the 7.0 SQL server, when you back up or truncate the log, SQL Server will automatically transfer the active portion of the log to a file the beginning of the end, and then you run the DBCC SHRINKFILE or DBCC SHRINKDATABASE command to free up unused space.

To determine if the log file how many virtual log files, and which is active virtual log file, you can use the undocumented command DBCC command: DBCC LOGINFO, its syntax is as follows:

DBCC LOGINFO [ ( dbname ) ]

Let us introduce DBCC LOGINFO usage through an example, while viewing the working principle of contraction and log truncation and implementation mechanisms.

First, create a test database, the script is as follows:

USE MASTER;

Go

CREATE DATABASE logtest

GO

ALTER DATABASE logtest SET recovery FULL

GO

USE logtest;

GO

DBCC loginfo; GO

Can know from the figure, state of the virtual log file activity (status) is 2, logtest database has two virtual log files, currently only a virtual log file is active, now create a table, then fill some lines to generate some log and then view the changes in the log.

SELECT TOP 10000 * INTO bigOrderHeader

FROM AdventureWorks.Sales.SalesOrderHeader

GO

DBCC loginfo GO

At this point you will see in the log file 12 virtual log files, and they are active (both state 2), now, to shrink the log and then see what happens?

DBCC SHRINKFILE (logtest_log) DBCC LOGINFO GO

Because the database is not backed up, still not active transactions, SQL Server will think you do not need to retain an inactive portion of the log, it will be deleted. Now back up the database.

BACKUP DATABASE logtest

TO DISK = 'f: \ logtest.bak' GO database has 'logtest', file 'logtest' (located Document 1) processed 440.

It has database 'logtest', file 'logtest_log' (located Document 1) for the two.

BACKUP DATABASE successfully processed 442 pages, it takes 0.851 seconds (4.246 MB / sec).

Now run changes in some logs, re-check log:

SET ROWCOUNT 1000

GO

BEGIN TRAN

DELETE bigOrderHeader

ROLLBACK TRAN

GO

SET ROWCOUNT 0

GO

DBCC loginfo

GO

From FIG noted, there are three active transaction labeled 2, then shrinkage of the log:

DBCC shrinkfile ( logtest_log)

GO

Can not shrink log file 2 (logtest_log), because all logical log files are in use.

(1 row affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Know from the output information, a virtual log file on the file is still active, so the occurrence of the failure, SQL Server can not be contracted from the end of the file, then we execute another transaction, so log continues to grow:

SET ROWCOUNT 5000 GO BEGIN TRAN DELETE bigOrderHeader ROLLBACK TRAN GO SET ROWCOUNT 0 GO DBCC loginfo GO

At this time, the log can not be shrunk because of the virtual log mark for the restore operation, only the logs made a backup or truncation, the space can be released.

BACKUP LOG logtest WITH TRUNCATE_only

DBCC loginfo

GO

Now the marked virtual log will no longer need (logging either truncated or is already backed up to disk), the log files can be shrunk.

DBCC shrinkfile (logtest_log)

DBCC loginfo

GO

(1 row affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(2 rows affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Reproduced in: https: //www.cnblogs.com/tutuyforever/p/6322894.html

Guess you like

Origin blog.csdn.net/weixin_34185320/article/details/94682066