I have a multi line SQL code where I want to store a single column with multiple rows into a variable. Can someone please help me with this? I am new to batch and learning but this is something I am finding difficulty with.
Heres what my SQl query in batch looks like:
SQl Query: Select email,username from table1,table2, table3 where table1.id = table2.id and table1.id=table3.id and table1.somefilter and table2.somefilter; I am storing the output of the same into a folder which is denoted below as "C:\filelocation"
psql -d Database -p 8000 -U username -t -A -f C:\filelocation -o "C:\text.txt"
Output: [email protected] abc [email protected] xyz
So this is a list of email addresses and their associated usernames. So, I would like to put them in a variable.
I am using -f as it is a multi line sql query with 2 different columns and multiple rows.
I have managed to get the output of the query into a folder, but what I really want is that I want to loop them into a variable.
Thanks
-
please post a example on how the output is, and what is the information you want to retrieve so we may help you. we have not a crystal ball to guess what exactly you are trying to achieve!elzooilogico– elzooilogico2017年05月16日 21:40:21 +00:00Commented May 16, 2017 at 21:40
-
Hey elzooilogico, I hope this helps. I apologize for the not providing all the details required. Thanksuser7933836– user79338362017年05月17日 02:38:32 +00:00Commented May 17, 2017 at 2:38
1 Answer 1
I suppose the output should be
[email protected] abc
[email protected] xyz
...
You only need to parse the output file as in
for /f "tokens=1,2 delims= " %%a in (C:\text.txt) do (
echo(Email address is: %%a
echo(Username is: %%b
)
or, if you want to want to grab them into variables
setlocal enabledelayedexpansion
rem grab fields into variables
set /a counter=0
for /f "tokens=1,2 delims= " %%a in (C:\text.txt) do (
set /a counter+=1
set "eMail_!counter!=%%a"
set "uName_!counter!=%%b"
)
rem process them
for /L %%i in (1,1,!counter!) do (
rem whatever you wanna do
echo(Email address is: !eMail_%%i!
echo(Username is: !uName_%%i!
)
Endlocal
This works if each pair is in a single line and there is a space between then, if the separator is a comma just change "tokens=1,2 delims= "
to "tokens=1,2 delims=,"
1 Comment
Explore related questions
See similar questions with these tags.