无法在SQL Server 2012中重命名逻辑文件名

时间:2014-03-20 14:59:02

标签: sql sql-server

我正在使用SQL Server 2012,我需要重命名数据库的逻辑文件名。

如果我运行下面的代码,我会得到以下结果。

enter image description here

 SELECT * FROM (
       --  OR select indented code to see all databases and perfrom the check manually
       SELECT d.Name as DBName, m.name as LogicalName, --m.physical_name,
       Left(Right(m.physical_name,Len(m.physical_name)-41),Len(m.physical_name)-45) as PhysicalFileName,
       recovery_model_desc as BackupType
       FROM sys.master_files m 
       inner join sys.databases d 
       ON (m.database_id = d.database_id) 
       WHERE d.database_id > 6

) as DatabaseData 
WHERE backuptype != 'Full'
or DBName != LogicalName and LogicalName != PhysicalFileName

逻辑文件名目前是“ExternalWebsiteDemo_log”,这是错误的,所以我跑了

ALTER DATABASE ExternalWebsite MODIFY FILE (NAME=N'ExternalWebsiteDemo_log', NEWNAME=N'ExternalWebsite_Log')

返回

  

MODIFY FILE失败。文件'ExternalWebsiteDemo_log'不存在。

任何想法?

1 个答案:

答案 0 :(得分:1)

我已经运行测试但无法重现问题,请注意我包含USE MASTER

use master

select database_id
        ,DB_NAME(database_id) as DatabaseName
        ,name as FileLogicalName
from sys.master_files
where DB_NAME(database_id) = 'TestDb';

Alter database testdb modify file ( name ='TestDemoDb_log', NewNAME = 'TestDb_log')

select database_id
        ,DB_NAME(database_id) as DatabaseName
        ,name as FileLogicalName
from sys.master_files
where DB_NAME(database_id) = 'TestDb';

Results