T-SQL statement to modify the logic name, database name, and physical name of the SQL Server database, t-sqlsql
This example describes how to modify the logical name, database name, and physical name of the SQL Server database using the T-SQL statement. We will share this with you for your reference. The details are as follows:
Change the SQL statement for the physical file name of the MSSQL database
Note: In the activity monitor, make sure that no process is connected to the database you want to rename !!!!!!!!!!!!!!!!!!!!
The SQL statement is as follows:
USE master -- MODIFY the logic name alter database YQBlog modify file (NAME = 'yqblogaa ', NEWNAME = 'yqblog') -- goalter database YQBlog modify file (NAME = 'yqblogaa _ log ', NEWNAME = 'yqblog _ log') -- GO -- change the database name EXEC sys. sp_renamedb @ dbname = 'yqbloga', -- sysname @ newname = 'yqblog' GO -- detach the database EXEC sp_detach_db YQBlogGO -- open the xp_cmdshell function EXEC sp_configure 'show advanced options ', 1 GORECONFIGUREGOEXEC sp_configure 'xp _ Your shell', 1GORECONFIGUREGO ---- change the physical name (Note: Do not show Chinese characters in the path) EXEC xp_your shell 'Ren C: \ Users \ Administrator \ Desktop \ YQBlogAA. mdf YQBlog. mdf '---- -- change the physical name EXEC xp_mongoshell 'Ren C: \ Users \ Administrator \ Desktop \ YQBlogAA_log.ldf yqblog_log.ldf' -- GO ---- re-append EXEC sp_attach_db @ dbname = n' YQBlog ', @ filename1 = n'c: \ Users \ Administrator \ Desktop \ YQBlog. mdf ', @ filename2 = n' C: \ Users \ Administrator \ Desktop \ YQBlog_log.ldf'