Can MSSQL server be configured to create a database in a specified folder? - sql-server

I use SQL Server 2008. Each time when I create a new database with it, it attempts to the database in the destination folder like this:
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA
I understand that I can manually change it. But I am curious whether the default destination folder can be configured to another folder.

This can be configured in the Database Settings of the Server Properties:

Related

What is the SQL Loader equivalent in SQL Server for extracting a database from SQL Server to the same server?

I am trying to replicate or copy a database in SQL Server 2014 in order to create a sandbox. I know SQL Loader does that for Oracle. Is there an equivalent tool in SQL Server?
We use bcp.exe as Loader program. It is located in C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn
You might do this in following ways:
1.Stop the sql server, copy the *.mdf file locating it right clicking on existing database -> properties from the server to the destination sever and attach it to the destination server using SSMS.
2.Create SSIS package and execute that using DTExec.exe.You can create a .bat file ,configure and call the DTexec.exe from that .bat file.This will be alternatve to oracle SQL loader for ETL tasks.
One method would be to create a database backup. Once the backup is completed, restore that backup under a different name.
Steps:
right click database from SSMS
Tasks -> Backup
Follow wizard
Once backup is complete, right click databases folder from the server and select restore database
Follow wizard, ensuring you're restoring the database to a new name.
Here are more detailed tutorials:
https://msdn.microsoft.com/en-us/library/ms187510.aspx
https://msdn.microsoft.com/en-us/library/ms177429.aspx
Note that if your intention is to test an application with the copied DB, ensure you're updating connection strings and whatnot to point to the copy.

Attach Database using Management studio points to the wrong file path for the log file

I am trying to attach a SQL server 2000 MDF and LDF which were created on a different machine and attach them to a SQL Server 2008 R2 on different machine. The SQL server files at the original machine were located at:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
The 2008 Sql Server machine also contains the same file structure and I place the MDF and LDF files in their appropriate folders:
C:\DB\SqlServerDataBase.mdf
D:\Logs\SqlServerDatabase.ldf
When using the SQL Server Management studio Attach Database option, I point to the MDF file however the log file is automatically is pointed to C:\DB\SqlServerDatabase.ldf instead of D:\Logs\SqlServerDatabase.ldf and subsequently the attach fails since it can not find the log file.
Interesting thing is if I use :
EXEC sp_attach_db #dbname =N'SqlServerDataBase',
#filename1=N'C:\DB\SqlServerDataBase.mdf',
#filename2=N'D:\Logs\SqlServerDatabase.ldf';
The database is restored and pointing to the correct paths for both MDF and LDF.
Thanks
When you're using the GUI (SQL Server Management Studio) and you select Attach Database and go pick a .mdf file, by default SQL Server Mgmt Studio will use the path that the database log file was originally stored on - since that's the only information it has.
But you can and you must change that to match your current setup - just type in the proper path (or select it from the path selector popup when you click on [...] if you prefer that)
Once you've typed in the correct paths, click [OK] and your database will be attached as requested.

SQL Server database export to another computer

I use SQL Server Managment Studio, SQL Server Express. I gererate a script with schema and data from Task -> Generate Script. And I try to import this script in other computer with the same SQL Server but this giving me error :
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf" failed with the operating system error 3 (The system cannot find the path specified.).
What should I have done to import /export entire database from one PC to another .
If you read through the first few lines of the script, you will see code that specifies the name and location of the files of the database. In your case look for 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\WHFM.mdf' in your script. Then look at the directory structure of the target machine. You will need to either:
1. create a directory 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\' and make sure the sql instance on the machine has RW access to that directory.
or much better,
2. Modify the script to put the file in a location that the target server wants to put files.
In regards to #2, you can have the target server give you a hint as to where to put files by right-clicking on "Databases" in Microsoft SQL Server Management Studio and create a script to create a database. The directory of the files in the create database script from the target-server should show you what to do.

Copying .mdf file from app_data folder to default localhost folder

My friend gave me a database file: record.mdf. I copied that .mdf file to my app_data folder and I can access it.
However, the connection string contains absolute path:
AttachDbFilename="C:\Users\Dell\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\App_Data\record.mdf"
But I want it to connect using:
Data Source=localhost\SQLEXPRESS;
How do I copy .mdf file to SQL Server's local folder, so that the connection string does not use an absolute path to the database?
I am using Visual Studio 2010. I do not have SQL Server Management Studio.
Step 1: you need to find out your SQL Server's data directory. This will be something like
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data
by default (for SQL Server 2008 R2 Express) - it might be slightly different in your case, depending on how you installed your SQL Server Express (and which version you have).
Step 2: copy that record.mdf file to that directory
Step 3: attach it to your SQL Server Express instance - using sqlcmd if you don't have Mgmt Studio at hand:
c:\> sqlcmd -S .\SQLExpress
Then at the sqlcmd prompt, type in:
USE [master]
GO
CREATE DATABASE record ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\record.mdf' )
FOR ATTACH_REBUILD_LOG;
GO
This will attach the .mdf file as your new "logical" database record to your SQL Server Express instance, and it will rebuild the missing transaction log file (.ldf) in the process.
From now on, you can use
server=.\SQLEXPRESS;Database=record;Integrated Security=SSPI;
as your connection string to connect to your database
Rather than copying it to the SQL server local folder, you can access it from the App_Data directory using |DataDirectory|\record.mdf
Documentation: http://msdn.microsoft.com/en-us/library/ms247257(v=vs.80).aspx

Microsoft Search Server Express - how to move all the databases?

I have just installed the excellent Search Server Express 2008 onto one of our servers.
As a default all the databases created are put into locations on the c: drive.
Does anyone know where all the databases are located and how they can be safely moved.
The data files will be located within a subdirectory of the detault SQL Server Folder so
C:\Program Files\Microsoft SQL Server\100\
See How to: Install SQL Server 2008 (Setup)
In order to move your databases data files the most straightforward method would be the Detach/Attach Method
As always, please ensure that you either perform or already posses a backup of your database data prior to carrying out such administrative tasks..

Resources