i want to execute a .sql file from cmd. Here's my command :
sqlcmd -e -S "(localdb)\myDB" -i "C:\PathToMyFile\File.sql" -U user -P password
Here's my errors :
Microsoft ODBC Driver 13.1 is installed. Someone knows what I've done wrong? Thx.
You are using the wrong server name:
sqlcmd -e -S "(localdb)\mssqllocaldb" -d MyDB -i "C:\PathToMyFile\File.sql" -U user -P password
or with Integrated Authentication:
sqlcmd -e -S "(localdb)\mssqllocaldb" -d MyDB -i "C:\PathToMyFile\File.sql" -E
Related
I am trying to run a file in Ubuntu using the sqlcmd command as shown below. We have a complex password set as per standards. Using that I am getting an error saying that "Login failed for the user "username" ".
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P ${sqlpassword}
-I -i ${inputfile} -s"|" -r1 1>${logfile} 2>${errorfile}
I tried to execute the same by providing values with password in single quotes. It worked fine
As we cannot hard code the password. I tried the things shown here, but those didn't work.
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P '${sqlpassword}'
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P "${sqlpassword}"
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P \'${sqlpassword}\'
Can someone please help me - am I doing something wrong?
Thank for #Larnu's comment.
The error caused by your password has has dollar sign in it.
In SQL Server, if I want to run a script from the command line, I can do this
/opt/mssql-tools/bin/sqlcmd -S $DB_HOST -U $DB_USER -P $DB_PASS -d $DB_NAME -i myscript.sql
Is it possible to run just a single command without a script and get the results? For instance, if I just wanted to run
SELECT 1
How would I do that from the command line?
I think you want the -q switch:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"
Documentation is here
Given that you want to:
run just a single command without a script and get the results
Another possible answer would be to run:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -Q "SELECT 1"
That is with a capital -Q.
This will run the command and then exit, allowing you to read the error code result.
The answer above uses lowercase -q, which will run the command but leave the sqlcmd prompt open and running.
Depending on what you want, the case of the -Q/-q argument matters.
I am trying to execute a large .sql file using sqlcmd. I can easily access the sa account through SSMS with the correct password. However, I am getting a
Login failed for user 'sa'
error when using sqlcmd.
Commands tried:
sqlcmd -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
osql -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
sqlcmd -S "servername\SQLEXPRESS" -U "sa" -P "REDACTED" -d "dbname" -i "C:\sample.sql"
What happens if you just login without the -d parameter and then type use dbname? I'm suspicious that the database you're trying to use can't be used by the "sa" for some reason. Is it offline?
How can I execute these series of commands in batch file? It only executes the first line which is the sqlcmd -S ********\SQLEXPRESS -U sa -P *****:
sqlcmd -S ********\SQLEXPRESS -U sa -P *****
USE Database
GO
SELECT * FROM TBLPERSON
GO
You can use the following command.
SQLCMD -S <server> -d <database> -U <user> -P <password> -Q "SELECT * FROM TBLPERSON"
Check out the SQLCMD documentation for more here.
I've been trying to run an MSSQL script on my docker and it always fails... is my syntax wrong or something... I've looked this post but it doesn't work
Executing SQL scripts on docker container
docker exec mssql '/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql'
this returned an error
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ************** -q </CRE.sql\": stat /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P AucMa1633485 -q </CRE.sql: no such file or directory"
I also tried something like this...
sudo docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'gitgood12345' -q </CRE.sql
The command returns me this error, which I'm not sure why....
Sqlcmd: '-q': Missing argument. Enter '-?' for help.
Please help me... the second method used to work, I think I forget a parameter or something...
The sqlcmd -q option requires an argument of the command you want to run. As your example is piping commands in on standard input, try without the -q.
docker exec mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 < /CRE.sql
Quoting everything results in the container trying to execute a binary named "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql" which doesn't exist.