How to pass in parameters to a SQL Server script called with sqlcmd?

后端 未结 1 1483
温柔的废话
温柔的废话 2020-12-09 15:53

Is it possible to pass parameters to a SQL Server script? I have a script that creates a database. It is called from a batch file using sqlcmd. Part of that SQL script is as

相关标签:
1条回答
  • 2020-12-09 16:03

    Use the -v switch to pass in variables.

    sqlcmd -v varMDF="C:\dev\SAMPLE.mdf" varLDF="C:\dev\SAMPLE_log.ldf"
    

    Then in your script file

    CREATE DATABASE [SAMPLE] ON  PRIMARY 
    ( NAME = N'SAMPLE', FILENAME = N'$(varMDF)' , SIZE = 23552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
     LOG ON 
    ( NAME = N'SAMPLE_log', FILENAME = N'$(varLDF)' , SIZE = 29504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
    
    0 讨论(0)
提交回复
热议问题