Home > Software engineering >  Assigning Table ID as a value to a variable in BigQuery
Assigning Table ID as a value to a variable in BigQuery

Time:08-02

I am trying to set a variable's value as a table's name. This is the query I am using.

DECLARE var STRING;


SELECT var = (SELECT

              ARRAY_AGG(table_id)[OFFSET(0)]

              FROM `dx-api-project.dv360_integration.__TABLES__`);

SELECT var

The query runs all fine, without any error. But when I see var's value, it shows null. However, when I just execute the query without variable declaration and assignment, ARRAY_AGG(table_id)[OFFSET(0)]'s value comes out to be what it should be.

Can anyone help me understand the issue here? Thank you

CodePudding user response:

Use SET instead of SELECT. Valid syntax for setting a variable is :

DECLARE var STRING;
SET var = (SELECT ARRAY_AGG(table_id)[OFFSET(0)]
             FROM `dx-api-project.dv360_integration.__TABLES__`);

or initialize a variable with a default value,

DECLARE var STRING DEFAULT (
  SELECT ARRAY_AGG(table_id)[OFFSET(0)]
    FROM `dx-api-project.dv360_integration.__TABLES__`
);
  • Related