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.
I want to run a query with CMD file
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
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
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?
Hi I have run some test below is the format. sqlcmd -S 192.168.1.1 -d picodb -q SELECT * FROM messagehistory mh LEFT JOIN sourceref sr ON mh.RefID = sr.RefID WHERE Timestamp_dt > NOW(); ORDER BY timestamp_dt DESC, timestamp_ms DESC LIMIT 100000
C:\test\testfile.csv the file generated was empty. Either my query is wrong or something is missing.
I used below line in .sql file to save the data on file and it works. INTO OUTFILE 'C:/Data/Export/Export_Data.txt' However there is a catch. The query doesn't replace the file if the filename already exist.
If its possible to add date and time to the filename, then it will work perfect. Do you have any solution?
Please login to leave a reply, or register at first.