1

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

asked May 16, 2017 at 16:54
2
  • 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! Commented May 16, 2017 at 21:40
  • Hey elzooilogico, I hope this helps. I apologize for the not providing all the details required. Thanks Commented May 17, 2017 at 2:38

1 Answer 1

2

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=,"

answered May 18, 2017 at 9:04

1 Comment

Thanks for the solutions!! Cheers!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.