<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4778156#M11292</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1320744"&gt;@austworks19&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's a cool use case!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Couldn't you do something like this? (note, this is pseudo code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; )&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;for workspace in workspaces:
    for schema in schemas: 
        for table in tables:
             table_name = f"{workspace.name}.{schema.name}.{table.name}"
             spark.sql(f"SELECT * FROM {table_name}")
             &lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, just use the full identifier of the table? If this does not work, you might wanna try out to add some spark.sql(f"USE [catalog}.{schema}").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know if this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Tom&lt;BR /&gt;&lt;A href="https://www.tackytech.blog/" target="_blank"&gt;https://www.tackytech.blog/&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://www.instagram.com/tackytechtom/" target="_blank"&gt;https://www.instagram.com/tackytechtom/&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Jul 2025 07:57:37 GMT</pubDate>
    <dc:creator>tackytechtom</dc:creator>
    <dc:date>2025-07-28T07:57:37Z</dc:date>
    <item>
      <title>T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4776675#M11256</link>
      <description>&lt;P&gt;Hello, I have a Python notebook in fabric that assesses data quality errors on source tables across various workspaces, lakehouses, and warehouses. I have an input delta table with rows with workspace names, lakehouse names, and T-SQL queries. My goal is to use the Fabric rest api to map the workspace and lakehouse names to their ids (done), then run the T-SQL in the SQL endpoint associated with the given workspace and lakehouse. I am unable to change workspaces using spark.sql(), and am hoping to use the t-sql magic function listed in this article:&amp;nbsp;&lt;A href="https://fabric.guru/querying-sql-endpoint-of-fabric-lakehousewarehouse-in-a-notebook-with-t-sql" target="_blank" rel="noopener"&gt;https://fabric.guru/querying-sql-endpoint-of-fabric-lakehousewarehouse-in-a-notebook-with-t-sql&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The issue is to configure the workspace and lakehouse, I need individual cells (one per query), but I would like to do this in a for loop iterating through my input delta table row by row. Any ideas on how I can achieve this?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Jul 2025 16:28:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4776675#M11256</guid>
      <dc:creator>austworks19</dc:creator>
      <dc:date>2025-07-25T16:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4778156#M11292</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1320744"&gt;@austworks19&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's a cool use case!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Couldn't you do something like this? (note, this is pseudo code &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; )&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;for workspace in workspaces:
    for schema in schemas: 
        for table in tables:
             table_name = f"{workspace.name}.{schema.name}.{table.name}"
             spark.sql(f"SELECT * FROM {table_name}")
             &lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, just use the full identifier of the table? If this does not work, you might wanna try out to add some spark.sql(f"USE [catalog}.{schema}").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know if this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Tom&lt;BR /&gt;&lt;A href="https://www.tackytech.blog/" target="_blank"&gt;https://www.tackytech.blog/&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://www.instagram.com/tackytechtom/" target="_blank"&gt;https://www.instagram.com/tackytechtom/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 07:57:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4778156#M11292</guid>
      <dc:creator>tackytechtom</dc:creator>
      <dc:date>2025-07-28T07:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4778503#M11302</link>
      <description>&lt;P&gt;Hi Tackytechtom, for some reason Spark cannot access tables inside lakehouses that are not the Spark session default lakehouse. However, I did find a workaround that works for my use case: dynamically creating the abfss paths from the workspace and lakehouse ids and replacing table references with their full abfss paths in backticks in the t-sql modified for spark.sql.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Jul 2025 11:26:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4778503#M11302</guid>
      <dc:creator>austworks19</dc:creator>
      <dc:date>2025-07-28T11:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4782711#M11386</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1320744"&gt;@austworks19&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;Thanks for reaching out to the Microsoft fabric community forum. You're absolutely right Fabric notebooks using&amp;nbsp;spark.sql()&amp;nbsp;are scoped to the default lakehouse of the Spark session, and unfortunately, you can't dynamically switch workspaces or lakehouses mid-session using Spark commands.&lt;/P&gt;
&lt;P&gt;Your workaround of constructing the full&amp;nbsp;abfss&amp;nbsp;path and replacing table references with backtick-enclosed paths is spot on and probably the most flexible approach for your use case.&lt;/P&gt;
&lt;P&gt;As for using the&amp;nbsp;%%sql&amp;nbsp;magic function from the blog you linked, you're also correct that it requires separate cells for each query, which doesn’t play well with loops or dynamic execution.&lt;/P&gt;
&lt;P&gt;If you're looking to keep everything inside a loop, here’s a suggestion that continue using&amp;nbsp;spark.sql()&amp;nbsp;but dynamically rewrite each query to use the full&amp;nbsp;abfss&amp;nbsp;path. You can store the rewritten queries in a list and iterate through them, executing each with&amp;nbsp;spark.sql().&lt;/P&gt;
&lt;P&gt;This way, you avoid the cell-per-query limitation and still get access to the correct lakehouse data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would also take a moment to thank &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/353745"&gt;@tackytechtom&lt;/a&gt;&amp;nbsp;, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I misunderstand your needs or you still have problems on it, please feel free to let us know.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Hammad.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jul 2025 09:14:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4782711#M11386</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-07-31T09:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4782906#M11389</link>
      <description>&lt;P&gt;@Anonymous&lt;/a&gt;&amp;nbsp;this is exactly what I ended up implementing, thank you for your reply!&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jul 2025 11:37:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4782906#M11389</guid>
      <dc:creator>austworks19</dc:creator>
      <dc:date>2025-07-31T11:37:39Z</dc:date>
    </item>
    <item>
      <title>Re: T-SQL magic cell with parameters in for loop Fabric Python Notebook</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4783617#M11402</link>
      <description>&lt;P&gt;I'm doing something like the below code to loop through TSQL commands in a python notebook. I rewrote it slightly so it has ficticious data, but I think you'll get the point after seeing the example. Creating views have to be done one at a time, so what I did was for a different purpose, but similar situation in that they need to interate TSQL for loop.....&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;LI-CODE lang="python"&gt;from IPython import get_ipython
import sempy.fabric as fabric

ipython = get_ipython()

# Example test input
view_scripts = {
    "vw_SalesOrders": (
        "CREATE OR ALTER VIEW [gold_sales].[vw_SalesOrders] AS\n"
        "SELECT OrderID, CustomerID, OrderDate, TotalAmount\n"
        "FROM bronze_orders.Orders",
        "Sample Workspace",
        "Sample Lakehouse"
    ),
    "vw_Products": (
        "CREATE OR ALTER VIEW [gold_sales].[vw_Products] AS\n"
        "SELECT ProductID, ProductName, Category, Price\n"
        "FROM bronze_catalog.Products",
        "Sample Workspace",
        "Sample Lakehouse"
    ),
    "vw_Customers": (
        "CREATE OR ALTER VIEW [gold_sales].[vw_Customers] AS\n"
        "SELECT CustomerID, FirstName, LastName, Email\n"
        "FROM bronze_customers.Customers",
        "Sample Workspace",
        "Sample Lakehouse"
    )
}

# Create views
for view_name, (view_script, workspace_name, item_name) in view_scripts.items():
    print(f"\nCreating view: {view_name} in {item_name} of {workspace_name}")
    try:
        workspace_id = fabric.resolve_workspace_id(workspace_name)
        run_info = f"-artifact {item_name} -type Lakehouse -workspace {workspace_id}"
        
        # Run the SQL via Fabric T-SQL magic
        result = ipython.run_cell_magic("tsql", run_info, view_script)
        
        print(f"Successfully created view: {view_name} on {item_name}")
    except Exception as e:
        print(f"Failed to create view {view_name} on {item_name}: {e}")
​&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jul 2025 23:55:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/T-SQL-magic-cell-with-parameters-in-for-loop-Fabric-Python/m-p/4783617#M11402</guid>
      <dc:creator>Nathan_Mosher</dc:creator>
      <dc:date>2025-07-31T23:55:52Z</dc:date>
    </item>
  </channel>
</rss>

