BigQuery - query returns undefined variable but it's okay in console

I'm playing with some dynamic sql like this:

DECLARE columns STRING;

SET columns = (
SELECT
STRING_AGG(CONCAT('COUNT(DISTINCT `', column_name, '`) AS ', column_name, '_count'))
FROM dateset.INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = "<name>"
GROUP BY
table_name
);

But when I highlight the whole code and execute I'm getting Undeclared variable: columns. Is it because it's sent as separate queries?

1 comment
Avatar
Yuriy Vinogradov
Comment actions Permalink

Let's try this: go to File | Settings | Database | General -> set 'for selection execute' to 'exactly as one statement', then select entire query and run it. Will it work?

0

Please sign in to leave a comment.