I've been scouring this site to find out how to dump the results of a select statement to an array, so I can iterate through an array. However I have not been lucky enough to find a simple example. The code below is an example of what I did. However I cannot figure out how to do this with an array. Only a row construct. Whenever I attempt to assign the query results to an array I get this subquery error (i.e. something like "array := (select...from sometable)", which I understand, but there has to be a way to do it. Thanks in advance. (Addition: PSeudo-Code for what I prefer beneath this actual code).
DO
$$
DECLARE
nRowCount bigint;
i record;
BEGIN
DROP TABLE IF EXISTS companies_sample_db_temp;
CREATE TABLE companies_sample_db_temp (
col1 varchar
, col2 varchar
, col3 varchar
);
INSERT INTO companies_sample_db_temp VALUES ('McDonalds','Los Angeles','CA');
INSERT INTO companies_sample_db_temp VALUES ('Starbucks','Seattle','WA');
INSERT INTO companies_sample_db_temp VALUES ('Oracle','San Francisco','CA');
-- SELECT * FROM companies_sample_db_temp;
FOR i IN
with a as
(
SELECT
ARRAY[col1::text
, col2::text
, col3::text
] as coltext
FROM companies_sample_db_temp AS my_arr
)
select row_number() over(), coltext from a
LOOP
-- RAISE INFO 'nRowCount: %', nRowCount;
RAISE INFO 'Array Info: %', i.coltext[1];
END LOOP;
END
$$;
/*********** Pseudo Code of what I'd rather do *******************/
DO
$$
DECLARE
-- Assign results of this query to an array
my_arr := SELECT col1, col2,col3 FROM companies_sample_db_temp;
i record;
BEGIN
-- Loop through an "array" not a table using a select statement.
FOR i IN
-- Iterate through each row of the array
my_arr[i] -- Row from the select query
LOOP
-- Display an elements within a single array row
RAISE INFO 'Array Info: %', my_arr[i][1]; -- col1
RAISE INFO 'Array Info: %', my_arr[i][2]; -- col2
RAISE INFO 'Array Info: %', my_arr[i][3]; -- col3
END LOOP;
END $$;
Hope this clears up the question.
PostgreSQL allows us to define a table column as an array type. The array must be of a valid data type such as integer, character, or user-defined types. To insert values into an array column, we use the ARRAY constructor.
You can easily create arrays in PostgreSQL by adding square brackets [] immediately after the data type for the column. create table employees ( first_name varchar, last_name varchar, phone_numbers integer[] ); In the above example, we have created column phone_numbers as an array of integers.
To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { <array field>: { <operator1>: <value1>, ... } }
PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.
Given that your reasoning for attempting this is "performance", consider the following:
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