9

I have a large SQL script which I am trying to run using the command line.

I have found these instructions here: https://learn.microsoft.com/en-us/sql/relational-databases/scripting/sqlcmd-run-transact-sql-script-files

Which seem to say that I can use this command:

sqlcmd -S myServer\instanceName -i C:\myScript.sql

Which makes sense to me. I assume I would use the same server details as I would in a Web.Config connection string.

The issue is, this does not seem to specify the database name to run the script on.

I think this is because it is assumed that the SQL file will have the DB name, but my file is too big to open to confirm this.

Also, I would like to specify the DB name to be double safe.

Can anyone advise how to do this?

Also, if anyone can advise how I can read the first line of a file using the command line that would be great aswell..

Thanks in advance

5
  • 2
    -d Specifies the database name - learn.microsoft.com/en-us/sql/tools/sqlcmd-utility Commented Jan 22, 2018 at 15:53
  • You find out all the parameters for sqlcmd by running sqlcmd -?. That tells you use can use -d to specify the database name. Thus: sqlcmd -S myServer\instanceName -i C:\myScript.sql -d MyDatabase. This command also assumes you are using integrated security. Commented Jan 22, 2018 at 15:54
  • @Ctznkane525 that is the correct answer, if you write that as answer I will accept it Commented Jan 22, 2018 at 15:55
  • I'm curious how large this file is that you can't open it in a text editor. Notepad++ handles large text files more gracefully than Notepad if you can't get the standard notepad to open it. I've never tried to open a .sql file in server management studio that was anywhere near that size though. Commented Jan 22, 2018 at 16:04
  • @EMUEVIL SSMS does start to struggle quite early on, from my experience. I wouldn't trust it opening a file 100MB or more. I certainly can't open the >=500MB files I have in SSMS. notepad++ will open them, but you can't execute them from there. Commented Jan 22, 2018 at 16:11

2 Answers 2

11

-d - Specifies the database name - learn.microsoft.com/en-us/sql/tools/sqlcmd-utility – Ctznkane525

sqlcmd -S myServer\instanceName -i C:\myScript.sql -d DatabaseName
Sign up to request clarification or add additional context in comments.

Comments

7

in my case I needed SQL Server Authentication

sqlcmd -d database_name -U sa -P Password  -S 192.168.1.1 -i "D:\tmp\script.sql"

so I launched sqlcmd with these switches. I Hope that this can help somebody

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.