This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreDid you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now
Hello, I have an, I guess, trivial question. In a notebook, this code throws a syntax error:
Solved! Go to Solution.
Hi @NotebookEnjoyer,
%sql in notebooks cannot handle T‑SQL pragmas like DECLARE or SET are part of T-SQL (Transact-SQL), a richer SQL dialect used by platforms like SQL Server or Lakehouse SQL endpoints. These platforms are designed to handle advanced operations such as control-of-flow, variable declarations, and stored procedures.
Notebook %sql magics are intended for lightweight, interactive querying during data exploration, not for executing full T-SQL scripts. Their goal is simplicity and speed, often at the expense of functionality.
Use a Lakehouse / Synapse SQL endpoint: These endpoints support full T‑SQL syntax, including variable declarations, control flow (BEGIN...END), and stored procedures. You can run these scripts in dedicated SQL script editors or SQL cells—not inside %sql magic cell.
If procedural features are needed in a notebook, you can use Python or PySpark to define variables and control logic, and then dynamically build or pass SQL queries to spark.sql() This approach is flexible and integrates well with notebook workflows.
Thanks,
Prashanth Are
MS Fabric community support
Hi @NotebookEnjoyer,
%sql in notebooks cannot handle T‑SQL pragmas like DECLARE or SET are part of T-SQL (Transact-SQL), a richer SQL dialect used by platforms like SQL Server or Lakehouse SQL endpoints. These platforms are designed to handle advanced operations such as control-of-flow, variable declarations, and stored procedures.
Notebook %sql magics are intended for lightweight, interactive querying during data exploration, not for executing full T-SQL scripts. Their goal is simplicity and speed, often at the expense of functionality.
Use a Lakehouse / Synapse SQL endpoint: These endpoints support full T‑SQL syntax, including variable declarations, control flow (BEGIN...END), and stored procedures. You can run these scripts in dedicated SQL script editors or SQL cells—not inside %sql magic cell.
If procedural features are needed in a notebook, you can use Python or PySpark to define variables and control logic, and then dynamically build or pass SQL queries to spark.sql() This approach is flexible and integrates well with notebook workflows.
Thanks,
Prashanth Are
MS Fabric community support
You can use normal variables in notebook.
And then you can use spark.sql() to execute your query.
Thanks, but I know that. I'm not looking for a workaround, but rather for an explanation and an official source of information.
Check out the April 2026 Fabric update to learn about new features.
Sign up to receive a private message when registration opens and key events begin.
| User | Count |
|---|---|
| 12 | |
| 10 | |
| 7 | |
| 5 | |
| 4 |
| User | Count |
|---|---|
| 24 | |
| 15 | |
| 9 | |
| 7 | |
| 6 |