How to use sqlcmd to create a database

后端 未结 5 1627
刺人心
刺人心 2020-12-31 05:31

I have a .sql script and I want to build a database from it. How to do it in sqlcmd? I know it\'s like:

CREATE DATABASE dbName
GO

But how t

相关标签:
5条回答
  • 2020-12-31 06:11

    use this: sqlcmd -i "c:\my scripts\my script.sql"

    see sqlcmd description at MS for other options

    0 讨论(0)
  • 2020-12-31 06:20

    This is very simple. Just enter following command to run sqlcmd:
    sqlcmd -U sa -P password
    Then enter:

    CREATE DATABASE MYDB
    GO
    

    This will create the db. Then enter "quit" to exit.

    0 讨论(0)
  • 2020-12-31 06:26
    sqlcmd -i C:\path\to\file.sql
    

    More options can be found in SQL Server books online.

    0 讨论(0)
  • 2020-12-31 06:28

    Use @Jeremiah Peschka's answer to supply the sqlcmd utility with the script to execute.

    As for the location for the newly created database, it can be specified as part of the CREATE DATABASE command:

    CREATE DATABASE dbName
    ON (
      NAME = dbName_dat,
      FILENAME = 'D:\path\to\dbName.mdf'
    )
    LOG ON (
      NAME = dbName_log,
      FILENAME = 'D:\path\to\dbName.ldf'
    )
    

    As you can see from the linked article, you can specify other properties as well, like initial size, maximum size etc.

    0 讨论(0)
  • 2020-12-31 06:36

    without a file:

    sqlcmd -Q "CREATE DATABASE HelloWorld"
    
    0 讨论(0)
提交回复
热议问题