批处理文件,用于将psql查询输出存储到变量中 [英] batch file to store the psql query output into a variable
问题描述
您好,我正在创建一个批处理文件以执行查询并为输出写一个日志文件.因此,根据某些条件,将执行两个查询.因此,我试图将查询的输出值存储到变量中,并使用该变量值检查条件.但是这里我面临一个问题,我应该如何假设使用psql命令声明输出值并将其存储到变量中.请帮忙!!这是我尝试过的:
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
这里的问题是变量 a
推荐答案
您可以使用for循环来解析输出
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)
这篇关于批处理文件,用于将psql查询输出存储到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!