Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:13:46+00:00 2026-05-25T02:13:46+00:00

I am writing a maintenance plan where databases will be detached then the content

  • 0

I am writing a maintenance plan where databases will be detached then the content of the folder it resided in will be moved to another drive for archiving.

declare @db varchar(500), @path varchar(max), @SQL varchar(max)

declare u_cur cursor fast_forward for
    select name, [filename] from #DbsToBeDetached
open u_cur

fetch next from u_cur into @db, @path
while (@@FETCH_STATUS = 0)
begin
    SET @SQL = 'ALTER DATABASE ' + @db + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE;'

    EXEC(@SQL)      

    EXEC sp_detach_db @db, 'true';

    set @path = MagicFunction(@path)

    exec sp_xp_cmdshell 'move /Y "' + @path + '" "e:\archive\"' + @db + '"'

    fetch next from u_cur into @db, @path
end
close u_cur
deallocate u_cur

The only thing I am stuck on is what do I do where it says MagicFunction to turn a path like

D:\data\conversions\wi_sql2005\30950 example database\30950_data.mdf

to

D:\data\conversions\wi_sql2005\30950 example database
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:13:47+00:00Added an answer on May 25, 2026 at 2:13 am

    You could create a function using the following logic (or just use the SQL inline):

    DECLARE @fileName VARCHAR(100)
    SET     @fileName = 'D:\data\conversions\wi_sql2005\30950 example database\30950_data.mdf'
    SELECT LEFT(@fileName, LEN(@fileName) - CHARINDEX('\', REVERSE(@fileName)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Maintenance Script for MongoDB that will compact collections from a
I am writing a scheduled job to mimic a SQL Server maintenance plan for
I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005)
Okay so I'm writing a SQL Server 2008 Stored Procedure (maintenance script). In doing
What is the best free way to get your maintenance plan-generated backups compressed? I
I'm writing a dummy MyAgenda application in Java which has to allow maintenance of
I'm writing a webapp that will only be used by authenticated users. Some temporary
I'm writing some PHP that will interact with MySQL to 1. upload and 2.
I've been working writing fairly small applications and doing some maintenance work for some
So I am writing a simple website crawler for maintenance of in house sites.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.