Change the name of the CSV File Automatically - sql

sqlcmd -S PC03 -d db_test -E -o "test\MyData.csv" ^
-Q "[test2]" ^
-W -w 999 -s","
I would like to change the name of the file into "20150512". The name of the file should be today's date.
I do not know how to do it.
Thanks!

try this to generate YYYYMMDD format output file. (append your filename)
Edited:
sqlcmd -S PC03 -d db_test -E -o c:\test-%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%.csv

sqlcmd -S servername -d dbtest -E -o c:\asdf\%date:~0,10%.csv ^ -Q
"[dbname].[sp]" ^ -W -w 999 -s","

Related

Two of the same commands give different result (output as utf8)

I have a .bat file that contains two commands:
SQLCMD -S . -d "databaseName" -E -i "path_to_query1.sql" -y0 -s "|" -f o:65001 > outputPath1.json
SQLCMD -S . -d "databaseName" -E -i "path_to_query2.sql" -y0 -s "|" -f o:65001 > outputPath2.json
The argument -f o:65001 is to output it to utf8 format, but only the second line outputs the query in an utf8 format.
Why is this? Why does it seem that the argument "-f o:65001" only works for the second command?
I checked it by switching the order and then again only the second command outputs the query in utf8 format.
Thanks for any tips on this.
EDIT
The solution for my specific problem was to put "chcp 65001" before the SQLCMD's. You then also don't need the argument -f 0:65001

BCP import all files from a folder to database

BCP Import
How to do BCP import with all files in a folder.
folder
file1.csv
file2.csv
Need to import both the files.
bcp <tableName> in <filename> -t "^" -r "\n" -c -C 28591 -S <databaseinstance> -U <username> -P <password>
Using the above BCP cmd, we can import only one file at a time.
simple BCP command import only single file.
To achieve the above we need to use looping with the command.
I have used the following command simple command.
for /r %i in (*) do bcp <tablename> in %i -t "^" -r "\n" -c -C 28591 -S <databaseinstance> -U <username> -P <password>
It works.

Saving psql output to csv file

I have a query written in a file located at /path/to/query. How can I save the output result to a csv file, without using COPY in the query? I tried the following command, but the output file's fields are separated by " | ".
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ','
It is not explained in the documentation, but the -F option requires the -A option (unaligned table output) to work:
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A
If you don't wish the headers in your csv, this means, without extra rows at the top and at the bottom, use the -t option too.
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A -t
From the help:
-A, --no-align unaligned table output mode
-F, --field-separator=STRING
set field separator (default: "|")
-t, --tuples-only print rows only

How to pass a bash variable into an sql file

I have a bash script to call a select in postgres. I would like to be able to pass a variable from the command line into the sql file.
sh myscript.sh 1234
#!/bin/bash
dbase_connect="psql -h server -U username dbase"
file="/tmp/$fname.csv"
sql="/home/user/sql_files/query.sql"
sudo bash -c "$dbase_connect -c -q -A -F , -f $sql -o $file"
The query can be as simple as:
select name from table where id = $1;
But I don't know how to call the $1 into the sql file. The actual query is much larger and I prefer to keep it out of the bash query itself because it is easier to maintain when called as a seperate .sql file.
you can use sed to insert parameter :
#!/bin/bash
dbase_connect="psql -h server -U username dbase"
file="/tmp/$fname.csv"
sql="/home/user/sql_files/query.sql"
tmp="home/user/sql_files/query.sql.tmp"
s="s/\$1/$1/g"
cat $sql | sed $s > $tmp
sudo bash -c "$dbase_connect -c -q -A -F , -f $tmp -o $file"
rm -f $tmp

DOS batch file - loop and increment by 1

I have this batch file that logs into sql on a remote machine runs a stored procedure and then sends the output to a text file. I would like it to increment both the 3rd octet in the IP address and the name of the output text file by 1 and loop so I don't have to repeat the command over and over. Also, I would like it to stop when it reaches a certain number. Is there a way to do this?
sqlcmd -U user -P password -S 192.168.1.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput01.txt
sqlcmd -U user -P password -S 192.168.2.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput02.txt
sqlcmd -U user -P password -S 192.168.3.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput03.txt
sqlcmd -U user -P password -S 192.168.4.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput04.txt
sqlcmd -U user -P password -S 192.168.5.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput05.txt
this will do what you want. the /L specifier tells it to act like a regular programming for loop. the first parameter is the starting integer, the next is the number to step, and the last is the count. so this will loop starting at 1, incrementing by 1 for 6 integers:
#echo off
FOR /L %%G IN (1, 1, 6) DO (
echo sqlcmd -U user -P password -S 192.168.%%G.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput0%%G.txt
)
and if you want to do a list of IPs instead of a range, you can leave off the /L and just list the numbers. e.g.
FOR %%G IN (1,2,3,99,121) DO ...
obviously the "echo" before sqlcmd is just for testing;)
On the assumption that you're actually using cmd.exe rather than MS-DOS, one way to increment and test a variable is as follows:
#setlocal enableextensions enabledelayedexpansion
#echo off
set /a "i = 1"
:loop
if !i! leq 15 (
if !i! lss 10 (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput0!i!.txt
) else (
echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput!i!.txt
)
set /a "i = i + 1"
goto :loop
)
endlocal
This is a slightly modified version of what you need which echoes the relevant bits rather than executing them, and it outputs:
sqlcmd -S 192.168.1.2 -o c:\sql\ouput01.txt
sqlcmd -S 192.168.2.2 -o c:\sql\ouput02.txt
sqlcmd -S 192.168.3.2 -o c:\sql\ouput03.txt
sqlcmd -S 192.168.4.2 -o c:\sql\ouput04.txt
sqlcmd -S 192.168.5.2 -o c:\sql\ouput05.txt
sqlcmd -S 192.168.6.2 -o c:\sql\ouput06.txt
sqlcmd -S 192.168.7.2 -o c:\sql\ouput07.txt
sqlcmd -S 192.168.8.2 -o c:\sql\ouput08.txt
sqlcmd -S 192.168.9.2 -o c:\sql\ouput09.txt
sqlcmd -S 192.168.10.2 -o c:\sql\ouput10.txt
sqlcmd -S 192.168.11.2 -o c:\sql\ouput11.txt
sqlcmd -S 192.168.12.2 -o c:\sql\ouput12.txt
sqlcmd -S 192.168.13.2 -o c:\sql\ouput13.txt
sqlcmd -S 192.168.14.2 -o c:\sql\ouput14.txt
sqlcmd -S 192.168.15.2 -o c:\sql\ouput15.txt
Simply take the echo off the line and adjust the command to put back the other bits.
If you have access to cmd.exe then you also have access to cscript which allows you to write a DOS batch file in Javascript.