Execute SQL script from command line

后端 未结 4 778
一向
一向 2020-12-23 09:02

I need to alter a database using a batch file, for a simple example, drop a table. I´m using local SQL Express (SQL Server 2008 R2) with user sa and its passwor

相关标签:
4条回答
  • 2020-12-23 09:22

    You can do like this

    sqlcmd -S <server Name> -U sa -P sapassword -i inputquery_file_name -o outputfile_name
    

    From your command prompt run sqlcmd /? to get all the options you can use with sqlcmd utility

    0 讨论(0)
  • 2020-12-23 09:30

    Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.

    http://msdn.microsoft.com/en-us/library/ms162773.aspx

    It's all in there in the documentation, but the syntax should look something like this:

    sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName 
        -Q "DROP TABLE MyTable"
    
    0 讨论(0)
  • 2020-12-23 09:47

    If you use Integrated Security, you might want to know that you simply need to use -E like this:

    sqlcmd -S Serverinstance -E -i import_file.sql
    
    0 讨论(0)
  • 2020-12-23 09:49

    Feedback Guys, first create database example live; before execute sql file below.

    sqlcmd -U SA -P yourPassword -S YourHost -d live -i live.sql
    
    0 讨论(0)
提交回复
热议问题