Request a demo

BigQuery · Scripting statement

BigQuery DECLARE and SET

DECLARE creates a variable in a BigQuery multi-statement query and SET assigns it: DECLARE run_date DATE DEFAULT CURRENT_DATE(); SET run_date = '2026-11-22'. Variables make scripts reusable, and a date variable in a WHERE clause still prunes partitions.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

DECLARE name[, ...] [type] [DEFAULT expression] creates one or more variables. Without DEFAULT the value is NULL; without a type, the type is inferred from DEFAULT, so one of the two is required. All DECLARE statements must come before any other statement of the script, or at the start of a BEGIN ... END block, where they are scoped to the block. Variable names are case-insensitive, and redeclaring a name in the same or an enclosing block is an error. A request with DECLARE is a multi-statement query (a script), which also lets you use loops, IF, temp tables and EXECUTE IMMEDIATE.

SET name = expression assigns one variable; SET (a, b) = (expr_a, expr_b) assigns several at once, and SET (a, b) = (SELECT AS STRUCT x, y FROM ...) assigns them from one query row. A scalar subquery that returns more than one row fails with Scalar subquery produced more than one element; one that returns no rows sets NULL. SET also changes system variables such as @@dataset_id, @@time_zone and @@query_label.

In queries, a column with the same name as a variable wins, so prefix variable names (v_start_date) or avoid collisions. A variable can hold up to 1 MB and all variables of a script up to 10 MB together. In a session, variables declared outside a BEGIN block persist across queries of the session. Variables cannot be used where BigQuery needs a constant at parse time: table names, PIVOT IN lists, TABLESAMPLE percentages or OPTIONS values in DDL. Build such statements as strings and run them with EXECUTE IMMEDIATE.

Syntax

DECLARE variable_name[, ...] [variable_type] [DEFAULT expression];

SET variable_name = expression;

SET (variable_name[, ...]) = (expression[, ...]);

Arguments and return value

ArgumentDescription
variable_typeAny GoogleSQL type, including ARRAY and STRUCT. Optional when DEFAULT is given.
DEFAULT expressionInitial value; may reference earlier variables and run a query. Without it the variable is NULL.
SET (a, b) = (...)Assigns several variables at once, from a list of expressions or one SELECT AS STRUCT row.

Returns: No result set by itself; the variables are available to the rest of the script (or block, or session).

Examples

Declare, set and use variables

DECLARE v_start DATE DEFAULT DATE '2026-11-01';
DECLARE v_days INT64;
SET v_days = DATE_DIFF(DATE '2026-11-23', v_start, DAY);
SELECT v_start AS start_date, v_days AS days, DATE_ADD(v_start, INTERVAL v_days DAY) AS end_date;
Result
start_datedaysend_date
2026-11-01222026-11-23

Assign two variables from one query row

DECLARE v_orders, v_revenue INT64;
SET (v_orders, v_revenue) = (
  SELECT AS STRUCT COUNT(*), SUM(amount)
  FROM UNNEST([120, 80, 45]) AS amount
);
SELECT v_orders AS orders, v_revenue AS revenue;
Result
ordersrevenue
3245

A column beats a variable with the same name

Inside the query, amount refers to the column, not the variable.

DECLARE amount INT64 DEFAULT 1000;
SELECT amount
FROM UNNEST([5, 7]) AS amount
ORDER BY amount;
Result
amount
5
7

Look up a boundary once, then prune partitions

A subquery in the WHERE clause would not prune events. Computing the watermark into a variable first lets the main query read only the new partitions; the lookup is billed separately.

DECLARE v_watermark TIMESTAMP DEFAULT (
  SELECT MAX(loaded_until) FROM `project.ops.load_log` WHERE pipeline = 'events'
);

SELECT event_name, COUNT(*) AS events
FROM `project.dataset.events`
WHERE event_ts > v_watermark
GROUP BY event_name;

Common mistakes

  • Placing DECLARE after another statement. Declarations must come first in the script or at the start of a BEGIN block.
  • Naming a variable like a column. The column wins inside queries, and the variable is silently ignored.
  • Using a variable as a table name or in DDL OPTIONS. FROM v_table is read as a table called v_table; build the statement as a string and use EXECUTE IMMEDIATE.
  • Assigning from a subquery that can return several rows. It fails at runtime; aggregate or add LIMIT 1 with an ORDER BY.
  • Expecting SET @var = ... as in MySQL or SQL Server. Only system variables use @@; user variables are DECLAREd and used without a prefix.

Cost and performance

  • DECLARE and SET with no table references cost nothing. A DEFAULT or SET that queries a table is billed like any query, on its own.
  • A variable used in a partition filter prunes partitions when the query runs, but a dry run of the script cannot know its value, so the estimate can overstate the bytes.

FAQ

How do I declare a variable in BigQuery?

DECLARE my_date DATE DEFAULT CURRENT_DATE(); at the top of the script, then use my_date in later statements. Change it with SET my_date = ....

Can I use a variable as a table name in BigQuery?

No. Build the SQL as a string and run it with EXECUTE IMMEDIATE FORMAT('SELECT COUNT(*) FROM %s', table_name), after checking that table_name comes from a trusted list.

Why do I get "Variable declarations are allowed only at the start of a block or script"?

A DECLARE comes after another statement. Move all declarations above the first other statement, or to the start of a BEGIN ... END block.

Errors you may hit

More scripting and data movement

All BigQuery scripting and data movement · All statements and clauses · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.