How to name the filenames of a database and set its location in Visual Studio 2015 Database project?

半世苍凉 提交于 2019-12-23 16:56:46

问题


By selecting "Publish" in the context menu of a VS 2015 Database project, I can create a script, which contains all the necessary commands to deploy the database to the SQL Server ("xyz.publish.sql").

The database name and its paths in this script are declared as variables:

:setvar DatabaseName "myDatabase"
:setvar DefaultFilePrefix "myDatabase"
:setvar DefaultDataPath "D:\Databases\"
:setvar DefaultLogPath "D:\Databases\"

also the filenames seems to be automatically generated:

PRIMARY(NAME = [$(DatabaseName)], FILENAME = N'$(DefaultDataPath)$(DefaultFilePrefix)_Primary.mdf')
LOG ON (NAME = [$(DatabaseName)_log], FILENAME = N'$(DefaultLogPath)$(DefaultFilePrefix)_Primary.ldf')...

Where can I set the paths and the filenames? I don't want "_Primary" to be attached at the filenames and the paths need an additional sub-folder.

If I change in the publish script, my changes will probably be overwritten the next time when this script will be generated by Visual Studio.


回答1:


You can add a pre-deployment script to the project which detaches the database, moves/renames the files, then reattaches the database using the new files.

-- detach db before moving physical files
USE [master]
GO
exec sp_detach_db @dbname = N'$(DatabaseName)'
GO

-- enable xp_cmdshell
exec sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
exec sp_configure 'xp_cmdshell', 1  -- 0 = Disable , 1 = Enable
GO
RECONFIGURE
GO

-- move physical files
EXEC xp_cmdshell 'MOVE "$(DefaultDataPath)$(DefaultFilePrefix)_Primary.mdf", "C:\$(DatabaseName)\$(DatabaseName).mdf"'
EXEC xp_cmdshell 'MOVE "$(DefaultLogPath)$(DefaultFilePrefix)_Primary.ldf", "C:\$(DatabaseName)\$(DatabaseName)_log.ldf"'
GO

-- reattach db with new filepath
CREATE DATABASE [$(DatabaseName)] ON 
(NAME = [$(DatabaseName)], FILENAME = 'C:\$(DatabaseName)\$(DatabaseName).mdf'),
(NAME = [$(DatabaseName)_log], FILENAME = 'C:\$(DatabaseName)\$(DatabaseName)_log.ldf')
FOR ATTACH
GO

-- disable xp_cmdshell
exec sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
exec sp_configure 'xp_cmdshell', 0  -- 0 = Disable , 1 = Enable
GO
RECONFIGURE
GO

USE [$(DatabaseName)];
GO

Some notes on this:

  • I hardcoded C:\ as the new location for the files for simplicity. You're better off creating a SQLCMD variable to store this path.
  • If xp_cmdshell 'MOVE ... fails, it will do so silently. To keep my answer simple I did not include any error checking, but you can roll your own by simply inserting the results of xp_cmdshell in a temp table. See How to capture the error output from xp_cmdshell in SQL Server.
  • You may run into permissions problems with the xp_cmdshell 'MOVE ... command. In that case, you may need to adjust the permissions of the source and target paths in the MOVE statement. You may also need to run the command as a different user -- see here (Permissions section) or here for starters.



回答2:


Yes, they will be changed but that's the way it works. You have to change the script. The other thing you can do is not specify the locations in which SQL Server will use the default ones for the instance - but that too involves changing the script.



来源:https://stackoverflow.com/questions/32091368/how-to-name-the-filenames-of-a-database-and-set-its-location-in-visual-studio-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!