如何将 Redshift SELECT 属性保存到脚本变量中
How to save a Redshift SELECT atribute into a script variable
我想创建一个脚本来使用 Redshift 自动执行某些流程。具体来说,我想找到一个带有 SELECT 的表的属性,然后在 INSERT 中使用它。我的脚本如下所示:
psql -h ... -c "SELECT id_process FROM process WHERE de_process = 'EMR'"
psql -h ... -c "INSERT INTO execution (id_process) values (X);"
在第一句话中,我得到了一个唯一值,即我要查找的 ID,格式如下:
id_proceso
------------
2
(1 row)
那我想把它作为值插入到第二句中,代入“X,但我不知道如何保存到一个变量中,然后重新使用第一句的输出。
有什么建议吗?
P.D。在其他 question 它显示了如何在一个独特的句子中做到这一点,但我需要保存该值以备将来使用。
检查psql
选项,但示例脚本可以是以下内容:
psql -h localhost -d testdb <<EOF
\out sample.txt
\pset border 1
WITH test_data AS ( SELECT 2 AS id_process)
SELECT id_process FROM test_data;
\out
EOF
cat sample.txt
的结果将是:
id_process
------------
2
(1 row)
如果您只想从 SELECT
语句中获取纯值,请考虑在上面的示例中使用以下参数:
\t
:
Toggles the display of output column name headings and row count
footer. This command is equivalent to \pset tuples_only
and is
provided for convenience.
\pset format unaligned
:
unaligned format writes all columns of a row on one line, separated by
the currently active field separator. This is useful for creating
output that might be intended to be read in by other programs (for
example, tab-separated or comma-separated format).
我想创建一个脚本来使用 Redshift 自动执行某些流程。具体来说,我想找到一个带有 SELECT 的表的属性,然后在 INSERT 中使用它。我的脚本如下所示:
psql -h ... -c "SELECT id_process FROM process WHERE de_process = 'EMR'"
psql -h ... -c "INSERT INTO execution (id_process) values (X);"
在第一句话中,我得到了一个唯一值,即我要查找的 ID,格式如下:
id_proceso
------------
2
(1 row)
那我想把它作为值插入到第二句中,代入“X,但我不知道如何保存到一个变量中,然后重新使用第一句的输出。
有什么建议吗?
P.D。在其他 question 它显示了如何在一个独特的句子中做到这一点,但我需要保存该值以备将来使用。
检查psql
选项,但示例脚本可以是以下内容:
psql -h localhost -d testdb <<EOF
\out sample.txt
\pset border 1
WITH test_data AS ( SELECT 2 AS id_process)
SELECT id_process FROM test_data;
\out
EOF
cat sample.txt
的结果将是:
id_process
------------
2
(1 row)
如果您只想从 SELECT
语句中获取纯值,请考虑在上面的示例中使用以下参数:
\t
:
Toggles the display of output column name headings and row count footer. This command is equivalent to
\pset tuples_only
and is provided for convenience.
\pset format unaligned
:
unaligned format writes all columns of a row on one line, separated by the currently active field separator. This is useful for creating output that might be intended to be read in by other programs (for example, tab-separated or comma-separated format).