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’
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
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 have a .sql file in my linux machine. I would like to connect to MS SQL database in the remote windows machine and run the .sql file in that database.
osql -S servername -U xx -P yy runs okay and returns a SQL window like:
SQL>
I can run individual queries using this. I have a .sql file with lot of sql commands which I need to run on a database called abc. How can I do that using shell command. The following doesn't work for me,
osql -S mssql -U xx -P yy -i /home/admin/Script.sql -D abc
I get the following error,
Illegal option -i
Syntax: osql -S server -U user -P password
The path of Script.sql file is correct as this opens the file - vi /home/admin/Script.sql. Not sure what the problem is. Any help would be appreciated
osql isn't working for me. I tried sqlcmd and it worked like a charm. Referred these links for ms sql tools installation - http://www.thesqlreport.com/?p=1494, https://sqlserveronlinuxbackup.com/sqlcmd-command-not-found-ubuntu/.
sqlcmd syntax:
sqlcmd -S 10.0.0.0 -U xx -P yy -d mydb -i /home/admin/abc.sql
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.