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?
Please sign in to leave a comment.
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?
This is way later, but if anyone has this same issue, the above answer works. Only thing to note is that in DataGrip version 2024.1.4 the setting is in: File | Settings | Database | Query Execution.