Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query each table in a column in big query

I need to "touch" all of our materialized views in a big query dataset periodically for a business use case. I was hoping to be able to gather all of the table names in a SELECT statement, and then do a "FOR EACH" type of loop through each view name, but it seems like BQ will not allow this. Is there a better way to do what I am attempting below?

FOR record IN
  (SELECT CONCAT('project.dataset.', table_name) as full_name from `project.dataset.INFORMATION_SCHEMA.TABLES`
  WHERE table_type = 'MATERIALIZED VIEW')
DO
  SELECT 1 FROM record.full_name;
END FOR;

This query is supposed to run against the information schema to find all of the materialized views, and then loop through those and "select 1" from each table returned.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

but it seems like BQ will not allow this.

You can try it with a dynamic sql.

FOR record IN
  (SELECT CONCAT('project.dataset.', table_name) as full_name from `project.dataset.INFORMATION_SCHEMA.TABLES`
  WHERE table_type = 'MATERIALIZED VIEW')
DO
  EXECUTE IMMEDIATE FORMAT("SELECT 1 FROM `%s`;", record.full_name);
END FOR;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading