Demo To Execute SQL Query From Batchfile



  1.     Connect to SSMS and deploy below query
/**************************************/
Create Database Demo
GO
Use Demo
Go
Create Table emp (id int , name varchar (100))
GO
Insert into emp values (1,'Saurabh')
Insert into emp values (2,'Sinha')
GO
Select * from emp
/**************************************/

22.   Create 3 file in C:\ BatchJob\

SQL Script: Script.sql|  Select * from emp

Batch file: execute.bat | osql -S MachineName\SQLEXPRESS2016 -U sa -P saurabh  -i " C:\BatchJob\script.sql" -o " C:\BatchJob\Log.txt"

Log file Location: C:\BatchJob\Log.txt

 3.        This will look like :

Now double click on execute.bat and you can see results in C:\BatchJob\Log.txt
If you run insert / update it will show you no. of rows processed


1 comment:

  1. hey Saurabh this is the good post. Please change the back color of the page.

    ReplyDelete