0

I have a database which gets generated from my C# code which has a connection string to my local SQL Server instance on my C drive.

The database is 150GBs in size and wont fit on my HDD. How do I create the db directly on my external HDD with my local SQL server instance?

4
  • 1
    What do you mean by install the database? This is both unclear and extremely broad. You need to add a lot of clarity for this question to be answerable. Commented May 14, 2018 at 14:45
  • 2
    You can specify where you want the .ldf and .mdf files on the CREATE DATABASE command. stackoverflow.com/questions/120917/… Commented May 14, 2018 at 14:49
  • Does seem like a bad idea, however, putting the database on an external device. If the device is missing, for whatever reason, you could have unexpected behaviour. Commented May 14, 2018 at 15:41
  • I see that, but really don't have a choice in my scenario. :\ Commented May 14, 2018 at 15:45

1 Answer 1

2

I ended up taking another approach to my answer. Rather than initializing it on the external hdd itself since that wasn't an option, generate the database files with little to no data with your local instance on your local storage.

Write a script for the db you want to move

USE master; 

ALTER DATABASE db
MODIFY FILE (name='db_name'
             ,filename='E:\dbfile.mdf'); 

If you have a log file attached with the db, modify that as well

ALTER DATABASE db
MODIFY FILE (name='db_name_log'
            ,filename='E:dbfile_log.ldf'); 

Set the database offline

ALTER DATABASE db SET OFFLINE WITH ROLLBACK IMMEDIATE;

Copy the .mdf and .ldf file to the external drive and then

ALTER DATABASE db SET ONLINE;

Hope this helps somebody!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.