windows - 用于将 psql 查询输出存储到变量中的批处理文件

标签 windows postgresql batch-file psql

您好,我正在创建一个批处理文件来执行查询并为输出编写一个日志文件。因此,根据某些条件,有两个查询要执行。 所以我试图将查询的输出值存储到一个变量中,并使用该变量值来检查条件。 但在这里我面临一个问题,我应该如何使用 psql 命令声明输出值并将其存储到变量中。 请帮忙!! 这是我尝试过的:

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循环来解析输出

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

(如果您在命令行控制台中尝试使用单个 %,在批处理文件中使用两个 %%)

关于windows - 用于将 psql 查询输出存储到变量中的批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54524508/

相关文章:

batch-file - 批处理不会显示命令结果 : closes window too quickly too read

ruby-on-rails - 如何在 Windows 上安装 RedCloth?

windows - FindBoost.cmake : Use different Compiler-version (vc90 instead of vc100)

sql - 数据库 |如何在调查中存储用户答案?

batch-file - 批处理文件 - 将命令和输出重定向到文件

java - 运行源代码中最近更改的 .bat 文件

c++ - 在 WinXP 及以上版本的 C++ 中获取无线适配器的 MAC 地址

c - VS2010 中包含目录优先级

ruby-on-rails - postgres中的绝对值

sql - 如何列出SQL中多个列中出现的值的次数?