How to execute these series of commands in batch file? - batch-file

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.

Related

How to I run a single sql command from the sqlcmd prompt?

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.

SQL cmd login failed

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?

Executing .SQL file from cmd

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

How to run a .sql file through query in SQL Server 2008

I have a stored procedure through which I am creating a dynamic .sql file on the server and the file have successfully generated. Now I want o execute the same file through query in the same stored procedure .
I have go through the links and found the below lines useful
osql -D db_name -S server_name -U username -P password -i sqlfile
or
sqlcmd -D db_name -S server_name -U username -P password -i sqlfile
I have tried to implement the above but it is giving me the error of
osql is not a recognized option
Please tell me the correct way. Thanks
sqlcmd is a command line tool and cannot be called directly from inside a stored procedure.
Use xp_cmdshell to execute it:
EXEC master.dbo.xp_cmdshell ‘sqlcmd -D db_name -S server_name -U username -P password -i sqlfile’

Can anyone tell me what the mistake is in the following osql syntax?

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!

Resources