BigQuery · Scripting statement
BigQuery EXECUTE IMMEDIATE
EXECUTE IMMEDIATE runs a SQL statement built as a string at run time, with values bound through USING and results stored with INTO. Use it for dynamic table names, generated PIVOT column lists and maintenance across many tables; bind values as parameters, never by concatenation.
EXECUTE IMMEDIATE sql_string parses and runs one statement: a query, a DDL statement, a DML statement or a DCL statement, including a BEGIN ... END block. The string can be any STRING expression, usually built with FORMAT() or CONCAT(). It cannot be a control statement such as IF or WHILE, and EXECUTE IMMEDIATE cannot be nested inside another EXECUTE IMMEDIATE, but you can call it from inside a loop.
USING passes values into the statement as query parameters: positional ? placeholders are bound in order (USING 10, 'paid'), named @name placeholders by alias (USING 10 AS min_qty). INTO var1, var2 stores the columns of the result row in script variables; zero rows set them to NULL, and more than one row is an error. The dynamic statement cannot see the script's local variables, only system variables and what you pass in USING, and if it returns rows, they become the result of the statement.
Parameters can only replace values. Identifiers such as table and column names, PIVOT IN lists and keywords have to be part of the string, so format them in, preferably from a trusted list such as INFORMATION_SCHEMA, and quote table names with backticks. Never paste user input into the SQL string. Each EXECUTE IMMEDIATE runs as a child job of the script, billed on demand for its own bytes; a dry run of the script does not look inside the dynamic SQL.
Syntax
EXECUTE IMMEDIATE sql_expression
[INTO variable[, ...]]
[USING { variable | value } [AS alias][, ...]];Arguments and return value
| Argument | Description |
|---|---|
sql_expression | A STRING expression holding one statement (query, DDL, DML or DCL). Not IF, LOOP, WHILE or another EXECUTE IMMEDIATE. |
INTO | Variables that receive the columns of a single result row, by position. No rows sets them to NULL; several rows fail. |
USING | Values bound to ? placeholders in order, or to @alias placeholders by name. Values only, not identifiers. |
Returns: The result of the dynamic statement, if it returns rows, or the values stored in the INTO variables.
Examples
Bind values with positional and named placeholders
DECLARE net NUMERIC;
EXECUTE IMMEDIATE 'SELECT ROUND(? / (1 + ?), 2)' INTO net USING NUMERIC '119', NUMERIC '0.19';
EXECUTE IMMEDIATE 'SELECT @net * @qty AS line_total' USING net AS net, 3 AS qty;| line_total |
|---|
| 300 |
Build a table name dynamically
Table names can't be parameters, so FORMAT puts the name into the SQL. The date suffix comes from the script, not from user input.
DECLARE day DATE DEFAULT DATE '2026-11-22';
DECLARE row_count INT64;
EXECUTE IMMEDIATE FORMAT(
'SELECT COUNT(*) FROM `project.analytics.events_%s`',
FORMAT_DATE('%Y%m%d', day)
) INTO row_count;
SELECT row_count;Run the same maintenance on every table of a dataset
Lists tables from INFORMATION_SCHEMA and sets a label on each. Each ALTER TABLE is a free metadata operation; the INFORMATION_SCHEMA query bills at least 10 MB on demand.
FOR t IN (
SELECT table_name
FROM `project.staging`.INFORMATION_SCHEMA.TABLES
WHERE table_type = 'BASE TABLE'
) DO
EXECUTE IMMEDIATE FORMAT(
'ALTER TABLE `project.staging.%s` SET OPTIONS (labels = [("env", "staging")])',
t.table_name
);
END FOR;Common mistakes
- Concatenating values into the SQL string. Quotes in the data break the statement and open the door to SQL injection; pass values with USING.
- Trying to parameterize a table or column name with
?or@. Parameters only stand for values; format identifiers into the string. - Referencing a script variable directly inside the dynamic SQL. It is not visible there; pass it with
USING var AS nameand write@name. - Expecting INTO to collect several rows. More than one result row is an error; aggregate with ARRAY_AGG into an array variable instead.
- Putting a loop or IF in the string. Control statements can't be executed dynamically; keep the control flow in the script and run single statements inside it.
Cost and performance
- Every EXECUTE IMMEDIATE is a child job billed on demand for its own bytes, with the 10 MB minimum per referenced table. A script that runs it over 500 tables pays 500 minimums even if each table is tiny.
- Dry runs stop at EXECUTE IMMEDIATE and do not evaluate the generated SQL, so the script's estimate leaves out everything that runs dynamically. Check child jobs in INFORMATION_SCHEMA.JOBS after a run.
FAQ
How do I use a variable as a table name in BigQuery?
Build the statement with FORMAT and run it with EXECUTE IMMEDIATE, as in the second example. Variables and parameters can't stand for identifiers in static SQL.
What is the difference between ? and @name in EXECUTE IMMEDIATE?
? placeholders are bound by position to the USING list; @name placeholders are bound to USING values with a matching AS name alias. One statement can't mix them: that fails with "EXECUTE IMMEDIATE USING parameters must be all positional or all named".
How do I pivot with dynamic columns in BigQuery?
Collect the column values with STRING_AGG into a variable, then EXECUTE IMMEDIATE a PIVOT query that formats that list into its IN clause. See PIVOT.
Related
Errors you may hit
More scripting and data movement
All BigQuery scripting and data movement · All statements and clauses · BigQuery error messages
