Wednesday, June 21, 2017

Database Shrink file Error - 'Could not locate file' error

When you try to shrink a database log file, you might encounter the below error message:

Could not locate file 'dbname_log' for database 'DBNAME' in sys.database_files. The file either does not exist, or was dropped. 

This happens if the logical file name does not match the file name in sys.master_files. To verify that there is a file name mismatch, you can run the below commands:

Command 1:

use dbname
go
sp_helpfile
go


Command 2:

select name from sys.master_files where database_id = db_id('dbname')

You will see that the log file name is different in each one. Get the name of the log file from the second command (that appears different) and we will use that as 'oldfilename' in the command below

To fix this, we just have to run the 'alter db' command by specifying the correct file name (referred to as 'newfilename', should be something like ‘dbname_log’) as shown below:


USE [DBANAME]
GO
ALTER DATABASE [DBNAME] MODIFY FILE (NAME=N'oldfilename',NEWNAME=N'newfilename')
GO

If that didn’t do the trick try running the below command using ‘new filename’ in both places:

USE [DBANAME]
GO
ALTER DATABASE [DBNAME] MODIFY FILE (NAME=N'newfilename',NEWNAME=N'newfilename')

GO

No comments:

Post a Comment

Display the list of indexes and columns for a given table

You might find yourself in a situation where you have to analyze the indexes for a given table and want to know which columns are part of wh...