I want to create a script to automate some processes with Redshift. Specifically, I want find an attribute of one of my tables with a SELECT and then use it in a INSERT. My script looks like this:
psql -h ... -c "SELECT id_process FROM process WHERE de_process = 'EMR'"
psql -h ... -c "INSERT INTO execution (id_process) values (X);"
In the first sentence I get a unique value, the ID I'm looking for, in a format like this:
id_proceso
------------
2
(1 row)
Then I would like to use it as the value to insert in the second sentence, substituting the "X, but I don't know how to save into a variable and then reuse the output of the first sentence.
Any suggestion?
P.D. In other question it shows how to do it in a unique sentence, but I need to save the value for a future use.
You can duplicate or "clone" a Redshift table's contents by executing a CREATE TABLE ... AS SELECT statement: CREATE TABLE new_table AS SELECT * FROM original_table; Please be careful when using this to clone big tables.
A deep copy recreates and repopulates a table by using a bulk insert, which automatically sorts the table. If a table has a large unsorted Region, a deep copy is much faster than a vacuum.
An IDENTITY column contains unique autogenerated values. The data type for an IDENTITY column must be either INT or BIGINT.
You can then use the ALTER TABLE ALTER DISTKEY command to add or modify the distribution key of a table, without impacting concurrent read or write queries. When you specify the appropriate distribution key for a table, Amazon Redshift places a similar number of rows on each node when loading data into that table.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With