SQL Server: use parameter in CREATE DATABASE

前端 未结 2 1482
一整个雨季
一整个雨季 2020-12-01 18:26

I want to specify the path where data file and log file is created in a SQL script using parameters. Here is what I wrote:

DECLARE @DataFilePath AS NVARCHAR(         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 19:30

    Try using SQLCMD mode. In SSMS, under the Query menu, choose SQLCMD Mode, then run this script.

    :SETVAR DataFilePath N'C:\ProgramData\Gemcom\TestDB.mdf'    
    :SETVAR LogFilePath N'C:\ProgramData\Gemcom\TestDB.ldf'
    
    USE master
    Go
    CREATE DATABASE TestDB
    ON 
    PRIMARY 
    ( NAME = N'TestDB_Data', FILENAME = $(DataFilePath) )
    LOG ON 
    ( NAME = N'TestDB_Log', FILENAME = $(LogFilePath)  )
    
    GO
    

提交回复
热议问题