Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run SQL script via command line to remote server

Tags:

sql-server

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

like image 271
Alex Avatar asked Jan 03 '23 22:01

Alex


2 Answers

-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
like image 161
Ctznkane525 Avatar answered Jan 08 '23 11:01

Ctznkane525


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

like image 23
Roberto Petrilli Avatar answered Jan 08 '23 10:01

Roberto Petrilli