I have a commercial application that installs a database instance and restores a database from a .BAK file. I must be able to support SQL 2005/2008 and 2012. The instance of the database is created during the install and it's data root is a subdirectory of the install directory which can be anywhere the user chooses.
To accomplish this I BACKUP the database from a SQL 2005 server and during the install attach to the SQL instance and execute the following SQL statements:
CREATE DATABASE databasename
RESTORE DATABASE databasename FROM DISK = 'X:\InstallDir\databasename.Bak' with replace
During the restore in 2005 the sql has no problems even if the database files are in a different directory.
During the restore in 2008 the sql database is converted from 2005 to 2008 and has no problems even if the database is in a different directory.
BUT in SQL 2012 I get the following error: (Even when the root is identical to the .BAK file root)
eb1>Directory lookup for the file "X:\InstallDir\Database\MSSQL.2\MSSQL\DATA\databasename.mdf" failed with the operating system error 3(The system cannot find the path specified.).
File 'databasename' cannot be restored to 'X:\InstallDir\Database\MSSQL.2\MSSQL\DATA\databasename.mdf'. Use WITH MOVE to identify a valid location for the file.
Directory lookup for the file "X:\InstallDir\Database\MSSQL.2\MSSQL\DATA\databasename_log.LDF" failed with the operating system error 3(The system cannot find the path specified.).
File 'databasename_log' cannot be restored to 'X:\InstallDir\Database\MSSQL.2\MSSQL\DATA\databasename_log.LDF'. Use WITH MOVE to identify a valid location for the file.
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
RESTORE DATABASE is terminating abnormally.
State:37000,Native:5133,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]
State:37000,Native:3156,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]
State:37000,Native:5133,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]
State:37000,Native:3156,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]
State:37000,Native:3119,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]
State:37000,Native:3013,Origin:[Microsoft][SQL Server Native Client 11.0][SQL Server]</eb1>
----------------------------------------------
The directory that was created on disk is:
X:\InstallDir\Databases\MSSQL11.InstanceName\MSSQL\DATA
This seems to be a bug???
On an aside, I also had to remove the command line switch /SQLSYSADMINACCOUNTS="Username" from the SQL 2012 instance install because of a known bug in SQL Server 2012.