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.
Related
I'm trying to restore a 500MB .sql file to SQL Server; I've tried running the script directly from a query page, but the memory isn't enough, so I tried with the sqlcmd command as follows:
sqlcmd -S <my server name> -d <my dbname>-U <user> -P <password> -i <pathfile> -a 32767
I've also tried with the -o command, too see if the log would report something useful, but from that command I didn't get any error.
Still, my database in SQL Server is empty, without any single table.
I'm running this on a Windows 10 system.
Any suggestions?
If you are using a SQL instance then the syntax will be like this:
sqlcmd -S <my server name>\instance -d <my dbname> -U <user> -P <password> -i <pathfile> -a 32767
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.
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.
osql -U sa -d master -i ConnectInternal.sql -U sa -P ""
I need to login with sa account with blank password. But it returns an error:
The syntax of the command is incorrect.
According to the SQLCMD documentation on MSDN, if you have a "blank" password, just specify -P at the end of your command and nothing after that - so try:
sqlcmd -d master -i ConnectInternal.sql -U sa -P
Also: you're not specifying any server here...... you probably need to add -S yourserver to the command line, too!