Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

60 Days of Data Days! Live and on-demand sessions, challenges, study groups and more! And it's all FREE!. Join now. Learn more

Reply
NotebookEnjoyer
Advocate II
Advocate II

Declaring SQL variables in Notebooks

Hello, I have an, I guess, trivial question. In a notebook, this code throws a syntax error:

 

%%sql
declare @myvar int;
select @myvar = 254812;
 
I suppose I can't use variables with cell magic, but I can't find any official sources about the limits. Generally, the difference in SQL syntax and capabilities between the lakehouse SQL endpoint and notebook cells annoys me. Does anyone happen to know where there is a tidy overview about the differences?
 
Thanks in advance!
1 ACCEPTED SOLUTION
v-prasare
Community Support
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

View solution in original post

3 REPLIES 3
v-prasare
Community Support
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

NandanHegde
Super User
Super User

You can use normal variables in notebook.

And then you can use spark.sql() to execute your query.




----------------------------------------------------------------------------------------------
Nandan Hegde (MSFT Data MVP)
LinkedIn Profile : www.linkedin.com/in/nandan-hegde-4a195a66
GitHUB Profile : https://github.com/NandanHegde15
Twitter Profile : @nandan_hegde15
MSFT MVP Profile : https://mvp.microsoft.com/en-US/MVP/profile/8977819f-95fb-ed11-8f6d-000d3a560942
Topmate : https://topmate.io/nandan_hegde
Blog :https://datasharkx.wordpress.com

Thanks, but I know that. I'm not looking for a workaround, but rather for an explanation and an official source of information.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

June Fabric Update Carousel

Fabric Monthly Update - June 2026

Check out the June 2026 Fabric update to learn about new features.