Tuesday, 28 May 2013

Executing stored procedure or query through batch file

(1) Create a batch file with .bat extension.
Open a notepad and save it as .bat instead of .txt
Now this file becomes executable. I gave EXEC.bat as filename.
Write the following text inside the bat file.
----------------------------------------
@ECHO OFF
SQLCMD -S yourservername -d database_name -U username -P password -i "C:\Documents and Settings\sudiptasanyal\Desktop\TEST\some.sql" -o "C:\Documents and Settings\sudiptasanyal\Desktop\TEST\output.txt"
----------------------------------------
 SQLCMD - It is another means to execute queries and do lot more that we perform in SQL Server 
but in COMMAND PROMPT.
    -S is used to provide the server name that i'm pointing to[Here i'm pointing to my local system only].
    -d is the database.
    -U is user to provide the server username.
    -P is used to provide the server password.
    -i is used to provide input file location/path to SQLCMD from which it executes. Here i'm taking on .sql file which contains multiple queries.
    -o is used to provide output file location/path to SQLCMD where the messages from SQL Server 
----------------------------------------
(2) Now open notepad and save it with .sql extension. Write whatever queries that you want to execute here in this file which needs to be executed.
in my case i have written like this(remove these lines)
----------------------------------------
USE yourdatabasename
GO
SELECT member_id,last_name,first_name FROM tbl_member 
----------------------------------------


No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...