Run .sql file from batch file - sql

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.

You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

Related

Need help to make sqlcmd run as per requirements

I wanted to run SQLCMD.EXE as mentioned here - http://blog.daringa.com/archives/tag/error-hresult-e_fail-has-been-returned-from-a-call-to-a-com-component
I used the line in link with proper path to sqlcmd, user and password I use to login to my SQL server - sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword -i C:\Database\hugescript.sql
Problem - I see a window and some message, but it vanishes so quickly that I cannot even see what it is. How do I see this window and how do I then execute an SQL file via SQLCMD.EXE
Why am I executing a script via SQLCMD and not SQL SERVER (ie SS) MGMT STUDIO ?
SS throws an error when sql files are big, ie about 100mb or more.
You can redirect the output from sqlcmd
sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword
-i C:\Database\hugescript.sql > log.txt 2> error.txt
It will write the output to log.txt file and errors to error.txt file (you can specify the full path if you want). You can then see what's happening.

Cannot execute script: Insufficient memory to continue the execution of the program

I have a 123MB sql file which I need to execute in my local PC. But I am getting
Cannot execute script: Insufficient memory to continue the execution of the program
How to solve this issue?
use the command-line tool SQLCMD which is much leaner on memory. It is as simple as:
SQLCMD -d <database-name> -i filename.sql
You need valid credentials to access your SQL Server instance or even to access a database
Taken from here.
It might help you! Please see below steps.
sqlcmd -S server-name -d database-name -i script.sql
Open cmd.exe as Administrator.
Create Documents directory.
Put your SQL Script file(script.sql) in the documents folder.
Type query with sqlcmd, server-name, database-name and script-file-name as like above highlighted query or below command line screen.
For Windows Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
Note: If there is any space in the sql file path then use " (Quotation marks) "
For SQL Server Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -U sa -P sasa -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
-S TestSQLServer\SQLEXPRESS: Here specify SQL Server Name
-U sa: Username (in case of SQL Server Authentication)
-P sasa: Password (in case of SQL Server Authentication)
-d AdventureWorks2018: Database Name come here
-i "d:\document\sql document\script.sql": File Path of SQLFile
You can also simply increase the Minimum memory per query value in server properties. To edit this setting, right click on server name and select Properties > Memory tab.
I encountered this error trying to execute a 30MB SQL script in SSMS 2012. After increasing the value from 1024MB to 2048MB I was able to run the script.
(This is the same answer I provided here)
My database was larger than 500mb, I then used the following
C:\Windows>sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -d DATABASE -i C:\FILE.sql
It loaded everything including SP's
*NB: Run the cmd as Administrator
If I understand your problem correctly, you are trying to restore (transact sql) xyz.sql - database + schema. You can try this command which worked for me:
SQLCMD -U sa -i xyz.sql
Try this step,
1)Open PowerShell
2)Write this command:
sqlcmd -S PCNAME\SQLEXPRESS -U user -P password -d databanse_name -i C:\script.sql
3)Press Return
:-)
Below script works perfectly:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Symptoms:
When executing a recovery script with sqlcmd utility, the ‘Sqlcmd: Error: Syntax error at line XYZ near command ‘X’ in file ‘file_name.sql’.’ error is encountered.
Cause:
This is a sqlcmd utility limitation. If the SQL script contains dollar sign ($) in any form, the utility is unable to properly execute the script, since it is substituting all variables automatically by default.
Resolution:
In order to execute script that has a dollar ($) sign in any form, it is necessary to add “-x” parameter to the command line.
e.g.
Original:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql
Fixed:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Sometimes, due to the heavy size of the script and data, we encounter this type of error. Server needs sufficient memory to execute and give the result. We can simply increase the memory size, per query.
You just need to go to the sql server properties > Memory tab (left side)> Now set the maximum memory limit you want to add.
Also, there is an option at the top, "Results to text", which consume less memory as compare to option "Results to grid", we can also go for Result to Text for less memory execution.
sqlcmd -S mamxxxxxmu\sqlserverr -U sa -P x1123 -d QLDB -i D:\qldbscript.sql
Open command prompt in run as administrator
enter above command
"mamxxxxxmu" is computer name
"sqlserverr" is server name
"sa" is username of server
"x1123" is password of server
"QLDB" is database name
"D:\qldbscript.sql" is sql script file to execute in database
If you need to connect to LocalDB during development, you can use:
sqlcmd -S "(localdb)\MSSQLLocalDB" -d dbname -i file.sql
As in most answers given here use the command-line tool. In my case the script already has database creation code. If your script contains CREATE DATABASE command, for example
USE [master]
GO
CREATE DATABASE [your-database-name]
Then do not use the -d your-database-name, instead use the following command.
For Windows Authentication use the command
sqlcmd -S ServerName\InstanceName -i "script.sql" -x
For SQL Server Authentication use the command
sqlcmd -S ServerName\InstanceName -U usename -P password -i "script.sql" -x

Executing series of SQL commands on the command line

I want to run a series of SQL statements against a SQL Server 2005 database from the command line.
When I launch 1st statement
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
it is going to prompt window 1> from there after I am unable to proceed further through script.
How to give input to 1> prompt I mean giving next SQL statement
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
and finally exit to that prompt
I tried with && but I guess that is for only commandline commands.
You ae looking for the -Q switch on the sqlcmd tool (don't use osql on sqlserver 2005 or higher) (type sqlcmd /? to see all options) or lookit up on msdn
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -Q "BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP"
Alternatively you can create a sqlscript file where you put all the sql statements in you want to execute. Assuming you name your file myscript.sql the osql command would go like this:
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -i myscript.sql
Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).
You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Linux). For example:
script.TXT:
#echo off
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
exit
Execute previous "script" this way:
cmd < script.TXT
If you perform this test, please report the result...
Antonio

Execute SQL script from command line

I need to alter a database using a batch file, for a simple example, drop a table. I´m using local SQL Express (SQL Server 2008 R2) with user sa and its password.
How would the bat file be?
How can I specify in the script the password and that I use in SQL Express?
Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.
http://msdn.microsoft.com/en-us/library/ms162773.aspx
It's all in there in the documentation, but the syntax should look something like this:
sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName
-Q "DROP TABLE MyTable"
You can do like this
sqlcmd -S <server Name> -U sa -P sapassword -i inputquery_file_name -o outputfile_name
From your command prompt run sqlcmd /? to get all the options you can use with sqlcmd utility
If you use Integrated Security, you might want to know that you simply need to use -E like this:
sqlcmd -S Serverinstance -E -i import_file.sql
Feedback Guys, first create database example live; before execute sql file below.
sqlcmd -U SA -P yourPassword -S YourHost -d live -i live.sql
Firstly create an empty database in SQL server, then run this command.
sqlcmd -s ServerName -d CreatedDatabaseName -i ScriptFileName.sql
ScriptFileName should be with a complete path like "D:\Folder Name\ScriptFileName.sql".
You can also use -u and -p if you have userName and password in your sql server.
First set the path of MYSQL in Environment variable-> System variables-> Click on Path-> Add -> "C:\Program Files\MySQL\MySQL Server 8.0\bin"
Open cmd-> navigate to the folder which has the sql script-> type as below ->
mysql --user=root -p < employees.sql -> Enter the password which was set during MYSQL setup-> hit enter
Done.
CMD Screenshot after it worked form me

Running a T-SQL script under different credentials using SQLCMD.exe utility

I created a batch file and placed the following line into it:
runas /user:internal\c39293 "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"
I was trying to run the stored procedure usp_Test on the server WLDZ9454 against the ChadDb database.
When I execute it, I just get info regarding param options, no error mesage, so it isn't clear to me what I am doing wrong.
Note that when I run the code minus the code that attempts to run under different credentials, it works:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"
I suspect it's because you have not enclosed the program you are running and its parameters in quotes.
SqlCmd is therefore executed without any parameters.
runas /user:internal\c39293 """C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"" -E -S WLDZ9454 -d ChadDb -Q ""usp_Test"""