I want to run a query with CMD file

nadeemcoorg's profile image nadeemcoorg posted 20 hours ago in Import/Export Permalink

I want to create a CMD file which can run a saved query(Data export) and save the result in a particular file. Please help.

wlodekh's profile image wlodekh posted 18 hours ago Permalink

In Windows download and install Microsoft Command Line Utilities 15 for SQL Server 32 or 64 bit.

In batch file: sqlcmd -S server\sql_server -d database -q "yourQuery" > file.txt or sqlcmd -S server\sql_server -d database -q "yourQuery" > \path_to_file\file.txt or sqlcmd -S server\sql_server -d database -i "\path_to_file\file.sql" > file.txt or sqlcmd -S server\sql_server -d database -i "\path_to_file\file.sql" > \path_to_file\file.txt

wlodekh's profile image wlodekh posted 18 hours ago Permalink

In Windows download and install Microsoft Command Line Utilities 15 for SQL Server 32 or 64 bit.

In batch file: sqlcmd -S server\sql_server -d database -q "yourQuery" > file.txt or sqlcmd -S server\sql_server -d database -q "yourQuery" > \\path_to_file\file.txt or sqlcmd -S server\sql_server -d database -i "\\path_to_file\file.sql" > file.txt or sqlcmd -S server\sql_server -d database -i "\\path_to_file\file.sql" > \\path_to_file\file.txt

nadeemcoorg's profile image nadeemcoorg posted 7 hours ago Permalink

sqlcmd -S server\sql_server -d database -q "yourQuery" > \path_to_file\file.txt

If I want to run above query, I should replace

server with Server name

database with Database name

yourQuery with query I want to run

path_to_file\file.txt with destination path and filename.

is my understanding correct?

wlodekh's profile image wlodekh posted 27 minutes ago Permalink

Yes. A query may also be an executive procedure: sqlcmd -S server\sql_server -d database -q "EXEC yourProcedure" > \path_to_file\file.txt Very mportant: the bat file *.bat must be placed in the SQL server directory.

Please login to leave a reply, or register at first.