T-SQL statement modifies SQL Server database logical name, database name, physical name _mssql

Source: Internet
Author: User
Tags sql server query

The example in this article describes how T-SQL statements modify SQL Server database logical names, database names, and physical names. Share to everyone for your reference, specific as follows:

To change the physical file name of the MSSQL database SQL statement

Note: In the activity Monitor, make sure there are no processes connected to the database you want to rename!!!!!!!!!!!!!!!!!!!!

The SQL statement is as follows

 use master--Change the logical name ALTER DATABASE yqblog MODIFY FILE (name= ' Yqblogaa ', newname= ' Yqblog ')--  Go ALTER db yqblog MODIFY FILE (name= ' Yqblogaa_log ', newname= ' Yqblog_log ')--go--Change database name EXEC sys.sp_renamedb @dbname = ' Yqblogaa ',--sysname @newname = ' yqblog ' Go--detach database exec sp_detach_db yqblog go--open xp_cmdshell feature exec Sp_configu Re ' show advanced Options ', 1 go reconfigure to EXEC sp_configure ' xp_cmdshell ', 1 go reconfigure go------Change physical name (note: Do not show in the path exec xp_cmdshell ' ren C:\Users\Administrator\Desktop\YQBlogAA.mdf yqblog.mdf '------Change physical name exec xp_cmdshell ' ren c:\u Sers\administrator\desktop\yqblogaa_log.ldf yqblog_log.ldf '--go------Reattach EXEC sp_attach_db @dbname = N ' Yqblog ', @fil ename1 = N ' C:\Users\Administrator\Desktop\YQBlog.mdf ', @filename2 = N ' C:\Users\Administrator\Desktop\YQBlog_log.ldf '

More about SQL Server-related content readers can view the site topics: SQL Server stored procedure tips, SQL Server query operations tips, SQL Server indexing operations tips, SQL Server Paging technology summary and SQL Server Common Functions Summary

I hope this article will help you with your SQL Server database program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.