我使用tsql来分离这样的数据库:
EXEC sp_detach_db @dbname = 'my_db'
然后我使用PHP重命名物理文件。我能够重命名mdf文件,但不能重命名ldf文件!我甚至尝试过dos命令REN
,但这对ldf文件也不起作用!
我想问一下,物理日志文件有什么特别的东西可以不重命名吗?
有更好的方法吗?
全部谢谢
答案 0 :(得分:10)
分离数据库,重命名文件,再次附加。
答案 1 :(得分:9)
您可以使用ALTER DATABASE
语句执行此操作 - 如下所示:
ALTER DATABASE database_name
MODIFY FILE ( NAME = logical_file_name,
FILENAME = ' new_path/os_file_name_with_extension ' )
您需要单独修改每个文件,例如如果您有多个数据文件,则需要修改其中的每一个。
有关详细信息,请参阅Technet documentation on this topic。
答案 2 :(得分:8)
“ALTER 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
答案 3 :(得分:3)
答案 4 :(得分:2)
重命名SQL server physical database files
的最简单方法是:
SQL server
。OldDatabaseName
”替换为要将其名称更改为的数据库的新名称(“NewDatabaseName
”)。将所有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);
然后右键点击OldDatabaseName
,选择Tasks
,然后选择Take Offline
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\...
),并将其重命名为您在数字2中指定的NewDatabaseName
。请记住检查这些文件的绝对路径计算机。Microsoft SQL Server Management Studio
。右键点击OldDatabaseName
,选择Tasks
,然后选择Bring Online
。OldDatabaseName
重命名为NewDatabaseName
。你做完了: - )答案 5 :(得分:1)