Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « 24 Hours of PASS, WIT Edition: Register! Learn! Listen to Me!How To Unpivot (and pivot) Like a Boss »
    comments

    Using ALTER DATABASE and MODIFY FILE you can reset the metadata in the master database for renaming physical mdf and ldf files. This however does not actually change the physical file name itself on disk. To fix that, you are typically required to change the metadata in master and then manually change the physical files on disk after taking the database offline.  Then bring the database online and the task is complete.

    To get around this manual process you could use CLR with External access set in a DBA database.

    Note: DBAs should have a repository that stores all of their functions, procedures and other objects. This keeps your SQL Server clean and is easily created on new servers when they are built with scripts.

    The CLR procedure to handle the change can be used off the model code below:

    (Since this SQLCLR procedure uses System.IO, it requires External access)

    1. public partial class StoredProcedures
    2. {
    3.     [Microsoft.SqlServer.Server.SqlProcedure]
    4.     public static void ChangeFileName(string oldmdf, string oldldf, string newmdf, string newldf)
    5.     {
    6.         try
    7.         {
    8.             SqlContext.Pipe.Send(oldmdf);
    9.             SqlContext.Pipe.Send(oldldf);
    10.  
    11.             if (File.Exists(oldmdf))
    12.             {
    13.                 File.Move(oldmdf, newmdf);
    14.                 SqlContext.Pipe.Send("File moved");
    15.             }
    16.             if (File.Exists(oldldf))
    17.             {
    18.                 File.Move(oldldf, newldf);
    19.                 SqlContext.Pipe.Send("File moved");
    20.             }
    21.         }
    22.         catch (SqlException ex)
    23.         {
    24.             SqlContext.Pipe.Send(ex.ToString());
    25.         }
    26.     }
    27. };

    Then to follow this you can run the following T-SQL Statement to execute the change to master and also change the physical file names

    1. USE MASTER
    2. Go
    3. DECLARE @MDF NVARCHAR(1000)
    4. DECLARE @LDF NVARCHAR(1000)
    5. DECLARE @NEWMDF NVARCHAR(1000)
    6. DECLARE @NEWLDF NVARCHAR(1000)
    7. DECLARE @CMD NVARCHAR(2000)
    8. SET @NEWMDF = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks_Data.mdf'
    9. SET @NEWLDF = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\AdventureWorks_Log.ldf'
    10. SET @MDF = (SELECT physical_name FROM SYS.MASTER_FILES WHERE database_id = DB_ID('AdventureWorks') AND type_desc = N'ROWS')
    11. SET @LDF = (SELECT physical_name FROM SYS.MASTER_FILES WHERE database_id = DB_ID('AdventureWorks') AND type_desc = N'LOG')
    12.  
    13. ALTER DATABASE AdventureWorks SET OFFLINE
    14.  
    15. EXEC master.dbo.ChangeFileName @MDF,@LDF,@NEWMDF,@NEWLDF
    16.  
    17. SET @CMD = ('ALTER DATABASE AdventureWorks
    18.                        MODIFY FILE
    19.                              ( NAME = N''AdventureWorks_Data'',
    20.                                FILENAME = ''' + @NEWMDF + ''')')
    21. EXEC(@CMD)
    22.  
    23. SET @CMD = ('ALTER DATABASE AdventureWorks
    24.                        MODIFY FILE
    25.                              ( NAME = N''AdventureWorks_Log'' ,
    26.                                FILENAME = ''' + @NEWLDF + ''')')
    27. EXEC(@CMD)
    28.  
    29. ALTER DATABASE AdventureWorks SET ONLINE

    This is only valid for a database that has one mdf and one ldf.  To alter this to handle either multiple ldf or added ndf file, change the CLR procedure to simply change one at a time.  Alternately you could add in logic by passing the entire contents of the database sys.master_files and then based on the rows sent as the parameter, change accordingly.  Use this method as a guide to build it to your own flavor :-)

     

     

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt Personal Blog over at http://onpnt.wordpress.com/
    Social SitingsTwitterLinkedInLTD RSS Feed
    1873 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)