How to save a Redshift SELECT atribute into a script variable

This question already has an answer here:

  • Insert into … values ( SELECT … FROM … ) 21 answers

  • Check psql options, but sample script can be the following:

    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
    

    Result for cat sample.txt will be:

     id_process 
    ------------
              2
    (1 row)
    

    If you want to get just pure value from SELECT statement, consider following params in the example above:

    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).

    链接地址: http://www.djcxy.com/p/94312.html

    上一篇: 我怎样才能SQL插入另一个表中的每个ID的记录?

    下一篇: 如何将Redshift SELECT atribute保存到脚本变量中