How can I write a dynamic SELECT INTO
query inside a PL/pgSQL function in Postgres?
Say I have a variable called tb_name
which is filled in a FOR
loop from information_schema.tables
. Now I have a variable called tc
which will be taking the row count for each table. I want something like the following:
FOR tb_name in select table_name from information_schema.tables where table_schema='some_schema' and table_name like '%1%'
LOOP
EXECUTE FORMAT('select count(*) into' || tc 'from' || tb_name);
END LOOP
What should be the data type of tb_name
and tc
in this case?