14

I have used tsql to detach a database like this:

EXEC sp_detach_db @dbname = 'my_db'

I then made use of PHP to rename the physical files. I was able to rename the mdf file but not the ldf file! I even tried a dos command REN but that didn't work for the ldf file either!

I wanted to ask, is there something special about the physical log files that allow it not to be renamed?

Is there a better way of doing this?

Thanks all

4

6 回答 6

15

分离数据库,重命名文件,再次附加。

于 2015-08-29T14:07:51.197 回答
13
  1. 备份原始数据库
  2. 删除原始数据库
  3. 从备份中恢复原始数据库,但名称不同;考虑到新数据库名称,恢复数据库的文件也将自动命名。
于 2018-11-13T19:41:39.000 回答
10

“ALTER DATABASE (your database) MODIFY FILE”命令只会重命名逻辑名称。这篇文章展示了如何使用 xp_cmdshell 来重命名物理文件:http ://www.mssqltips.com/sqlservertip/1891/best-practice-for-renaming-a-sql-server-database/

请注意以下几点:
1. xp_cmdshell 将在运行 SQL Server 进程的用户下执行,可能没有重命名数据库文件所需的文件系统权限
2. 出于安全原因,请记住禁用 xp_xmdshell

以下是如何根据上述博客文章进行重命名的示例。它将用数据库 NewMyDB 替换数据库 MyDB。原始 MyDB(重命名为 MyDB_OLD)将保持分离状态。

-- Enable xp_cmdshell:
sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO

-- Get physical file names:
declare @MyDBOriginalFileName nvarchar(300) = (select physical_name FROM sys.master_files where name = 'MyDB')
declare @MyDBLogOriginalFileName nvarchar(300) = (select physical_name FROM sys.master_files where name = 'MyDB_log')
declare @NewMyDBOriginalFileName nvarchar(300) = (select physical_name FROM sys.master_files where name = 'NewMyDB')
declare @NewMyDBLogOriginalFileName nvarchar(300) = (select physical_name FROM sys.master_files where name = 'NewMyDB_log')
declare @Command nvarchar(500)
declare @Sql nvarchar(2000)

IF (EXISTS (select * from sys.databases where name = 'NewMyDB') 
AND EXISTS (select * from sys.databases where name = 'MyDB'))
BEGIN
    USE master

    ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    ALTER DATABASE NewMyDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

        -- Set new database name
        ALTER DATABASE MyDB MODIFY NAME = MyDB_OLD
        ALTER DATABASE NewMyDB MODIFY NAME = MyDB

        -- Update logical names
        ALTER DATABASE MyDB_OLD MODIFY FILE (NAME=N'MyDB', NEWNAME=N'MyDB_OLD')
        ALTER DATABASE [MyDB] MODIFY FILE (NAME=N'NewMyDB', NEWNAME=N'MyDB')

        EXEC master.dbo.sp_detach_db @dbname = N'MyDB_Old'
        EXEC master.dbo.sp_detach_db @dbname = N'MyDB'

        -- Rename physical files
        SET @Command = 'RENAME "' + @MyDBOriginalFileName + '" "MyDB_OLD.mdf"'; PRINT @Command
        EXEC xp_cmdshell @Command
        SET @Command = 'RENAME "' + @MyDBLogOriginalFileName + '" "MyDB_OLD_log.mdf"'; PRINT @Command
        EXEC xp_cmdshell @Command
        SET @Command = 'RENAME "' + @NewMyDBOriginalFileName + '" "MyDB.mdf"'; PRINT @Command
        EXEC xp_cmdshell @Command
        SET @Command = 'RENAME "' + @NewMyDBLogOriginalFileName + '" "MyDB_log.mdf"'; PRINT @Command
        EXEC xp_cmdshell @Command

        -- Attach with new file names
        declare @NewMyDBFileNameAfterRename nvarchar(300) = replace(@NewMyDBOriginalFileName, 'NewMyDB',  'MyDB')
        declare @NewMyDBLogFileNameAfterRename nvarchar(300) = replace(@NewMyDBOriginalFileName, 'NewMyDB_log',  'MyDB_log')
        SET @Sql = 'CREATE DATABASE MyDB ON ( FILENAME = ''' + @NewMyDBFileNameAfterRename + '''), ( FILENAME = ''' + @NewMyDBLogFileNameAfterRename + ''') FOR ATTACH'
        PRINT @Sql
        EXEC (@Sql)

    ALTER DATABASE MyDB SET MULTI_USER 

END

-- Disable xp_cmdshell for security reasons:
GO
sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
GO
于 2013-06-27T07:51:42.237 回答
9

您可以使用ALTER DATABASE语句来执行此操作 - 如下所示:

ALTER DATABASE database_name
   MODIFY FILE ( NAME = logical_file_name, 
                 FILENAME = ' new_path/os_file_name_with_extension ' )

您需要分别修改每个文件,例如,如果您有多个数据文件,则需要修改每个文件。

有关详细信息,请参阅有关此主题的 Technet 文档

于 2011-01-21T12:25:14.580 回答
7

最简单的重命名方法SQL server physical database files是:

  1. 打开并连接到SQL server要重命名的数据库所在的位置。
  2. 在查询窗口中执行以下脚本以更改物理名称和逻辑名称。请记住将所有“”替换为您要更改其名称OldDatabaseName的数据库的新名称(“ ”)。NewDatabaseName将 all 替换NewDatabaseName为您要为数据库设置的新名称

use OldDatabaseName

ALTER DATABASE OldDabaseName MODIFY FILE (NAME='OldDatabaseName', FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewDatabaseName.mdf');

ALTER DATABASE OldDatabaseName MODIFY FILE (NAME='OldDatabaseName_log', FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\NewDatabaseName_log.ldf');

ALTER DATABASE OldDatabaseName MODIFY FILE (NAME = OldDatabaseName, NEWNAME = NewDatabaseName);
ALTER DATABASE OldDatabaseName MODIFY FILE (NAME = OldDatabaseName_log, NEWNAME = NewDatabaseName_log);
  1. 然后右键单击OldDatabaseName,选择Tasks然后选择Take Offline

  2. 转到C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\...物理文件所在的位置 ( ) 并将它们重命名为NewDatabaseName您在数字 2 中指定的名称。请记住检查要在您的计算机上使用的这些文件的绝对路径。
  3. 回到Microsoft SQL Server Management Studio. 右键单击OldDatabaseName,选择Tasks,然后选择Bring Online
  4. 最后,继续将您OldDatabaseName的重命名为NewDatabaseName. 你完成了:-)
于 2016-11-25T01:57:44.307 回答
3
  1. 分离(右键单击数据库)

  2. 重命名两个文件(ldf 和 mdf):C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA

  3. 附加(右键单击“数据库”顶部文件夹)

在此处输入图像描述

于 2021-07-27T22:05:56.467 回答