• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Attach many sql database´s in a folder

I have a lot of database´s  in a folder and i need attach them to sql server. How can make a bulk attach.
1 Solution
Barry CunneyCommented:
Set up a SQL Server Agent job of type powershell

$files = ls C:\YourDatabaseFileFolder\*.mdf
foreach($f in $files)

#dynamically build up the sp_attach sql from the $f variable - file full name etc.
$sql = "EXEC sp_attach_db @dbname = N.................$f........

Invoke-Sqlcmd -Query $sql -ServerInstance "YourComputer\YourInstance"

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now