Reputation: 395
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.
Upvotes: 8
Views: 13111
Reputation: 97968
Given that your reasoning for attempting this is "performance", consider the following:
Upvotes: 2