Shrikant Sahu
Shrikant Sahu

Reputation: 13

batch file to store the psql query output into a variable

Hi I am creating a batch file to execute a query and write a log file for the output. So based on some condition the there two queries to be executed. So I was trying to store a output value of a query in to a variable and use that variable value to check the condition. But here I am facing a problem that how should I suppose to declare and store output value into a variable using psql command. Please help!! Here what I tried:

SET /A a = psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"
SET /A b = 1

if %a% == 3 goto is_stat 
else goto is_start

REM to copy the log
:is_start  
psql -U postgres -d rpd -c "SELECT
a.table_name , 'MATCH' status  FROM rpd.rpm_rpd_count A  WHERE
a.rpd_count = a.rpm_count UNION ALL SELECT a.table_name, 'NOT MATCH'
AS status FROM rpd.rpm_rpd_count A WHERE a.rpd_count <>  a.rpm_count;" >> C:\Users\admin\Desktop\err.log
goto END
:is_stat 
psql -U postgres -d rpd -c "SELECT a.table_name , 'MATCH'
status  FROM rpd.rpm_rpd_count A  WHERE a.rpd_count = a.rpm_count;" >>
C:\Users\admin\Desktop\err.log 
goto END
:END 
echo %b% >> C:\Users\admin\Desktop\err.log

Problem here is that nothing is getting saved in the variable a

Upvotes: 1

Views: 2185

Answers (1)

JGH
JGH

Reputation: 17866

You can use a for loop to parse the output

for /f %%i in ('psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"') do set A=%%i

(use a single % if you try it in the command line console, and double %% in a batch file)

Upvotes: 1

Related Questions