<?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: How to execute SQL stored procedure in Azure Database for SQL Server using Azure Databricks Note in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-execute-sql-stored-procedure-in-azure-database-for-sql/m-p/103475#M41448</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/131859"&gt;@singhanuj2803&lt;/a&gt;,&lt;/P&gt;
&lt;P class="p1"&gt;To execute a SQL stored procedure in Azure Databricks, you can follow these steps:&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Required Libraries:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;You need to install the pyodbc library to connect to Azure SQL Database using ODBC. You can install it using the following command in a Databricks notebook cell or via cluster libraries:&lt;/LI&gt;
&lt;/UL&gt;
&lt;/OL&gt;
&lt;P class="p1"&gt;%pip install pyodbc&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Connecting to Azure Database for SQL Server using ODBC:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;You can use the pyodbc library to establish a connection to your Azure SQL Database. Here is a Python code snippet to connect using a Service Principal:&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="p1"&gt;import pyodbc&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;# Define the connection string&lt;/P&gt;
&lt;P class="p1"&gt;server = "your_server_name.database.windows.net"&lt;/P&gt;
&lt;P class="p1"&gt;database = "your_database_name"&lt;/P&gt;
&lt;P class="p1"&gt;clientId = "your_service_principal_client_id"&lt;/P&gt;
&lt;P class="p1"&gt;clientSecret = "your_service_principal_client_secret"&lt;/P&gt;
&lt;P class="p1"&gt;authority = "&lt;A href="https://login.microsoftonline.com/your_tenant_id" target="_blank"&gt;https://login.microsoftonline.com/your_tenant_id&lt;/A&gt;"&lt;/P&gt;
&lt;P class="p1"&gt;connection_string = (&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Driver={{ODBC Driver 17 for SQL Server}};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Server={server};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Database={database};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Authentication=ActiveDirectoryServicePrincipal;"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"UID={clientId};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"PWD={clientSecret};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Encrypt=yes;"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"TrustServerCertificate=no;"&lt;/P&gt;
&lt;P class="p1"&gt;)&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;try:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Establish the database connection&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;conn = pyodbc.connect(connection_string)&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;cursor = conn.cursor()&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Execute SQL queries here&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;cursor.execute("EXEC your_stored_procedure_name")&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;rows = cursor.fetchall()&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;for row in rows:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;print(row)&lt;/P&gt;
&lt;P class="p1"&gt;except Exception as e:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;print(f"Error: {str(e)}")&lt;/P&gt;
&lt;P class="p1"&gt;finally:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Close the connection&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;conn.close()&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Executing SQL Stored Procedure in Databricks:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;Once you have established the connection using the above code, you can execute your stored procedure using the cursor.execute method. Replace "EXEC your_stored_procedure_name" with the actual name of your stored procedure.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/OL&gt;
&lt;P class="p1"&gt;Please refer to: &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/connect/external-systems/sql-server" target="_blank"&gt;https://learn.microsoft.com/en-us/azure/databricks/connect/external-systems/sql-server&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 29 Dec 2024 22:37:22 GMT</pubDate>
    <dc:creator>Alberto_Umana</dc:creator>
    <dc:date>2024-12-29T22:37:22Z</dc:date>
    <item>
      <title>How to execute SQL stored procedure in Azure Database for SQL Server using Azure Databricks Notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-execute-sql-stored-procedure-in-azure-database-for-sql/m-p/103465#M41446</link>
      <description>&lt;P&gt;We have&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;Stored Procedure&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;available in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;Azure Database for SQL&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;Server&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and we want to call or run or execute the postgreSQL stored procedures in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;Azure Databricks&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;through Notebook&lt;/P&gt;&lt;P&gt;We are attempting to run SQL stored procedures, through Azure Databricks notebook and would need help how to execute a stored procedure. I would need clarity in below points&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;What are the required libraries that needs to be installed in Databricks cluster (if any)?&lt;/LI&gt;&lt;LI&gt;How to connect with Azure Database for SQL Server using ODBC?&lt;/LI&gt;&lt;LI&gt;How to Execute SQL stored procedure in Databricks?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I am referring to below link,&lt;/P&gt;&lt;P&gt;&lt;A href="https://nachiketrajput44.medium.com/how-to-run-stored-procedure-in-azure-data-warehouse-using-databricks-caa912d123d5" rel="ugc nofollow" target="_blank"&gt;https://nachiketrajput44.medium.com/how-to-run-stored-procedure-in-azure-data-warehouse-using-databricks-caa912d123d5&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Let me know if anyone has step-by-step approach how to solve this issue.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 16:51:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-execute-sql-stored-procedure-in-azure-database-for-sql/m-p/103465#M41446</guid>
      <dc:creator>singhanuj2803</dc:creator>
      <dc:date>2024-12-29T16:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to execute SQL stored procedure in Azure Database for SQL Server using Azure Databricks Note</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-execute-sql-stored-procedure-in-azure-database-for-sql/m-p/103475#M41448</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/131859"&gt;@singhanuj2803&lt;/a&gt;,&lt;/P&gt;
&lt;P class="p1"&gt;To execute a SQL stored procedure in Azure Databricks, you can follow these steps:&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Required Libraries:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;You need to install the pyodbc library to connect to Azure SQL Database using ODBC. You can install it using the following command in a Databricks notebook cell or via cluster libraries:&lt;/LI&gt;
&lt;/UL&gt;
&lt;/OL&gt;
&lt;P class="p1"&gt;%pip install pyodbc&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Connecting to Azure Database for SQL Server using ODBC:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;You can use the pyodbc library to establish a connection to your Azure SQL Database. Here is a Python code snippet to connect using a Service Principal:&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="p1"&gt;import pyodbc&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;# Define the connection string&lt;/P&gt;
&lt;P class="p1"&gt;server = "your_server_name.database.windows.net"&lt;/P&gt;
&lt;P class="p1"&gt;database = "your_database_name"&lt;/P&gt;
&lt;P class="p1"&gt;clientId = "your_service_principal_client_id"&lt;/P&gt;
&lt;P class="p1"&gt;clientSecret = "your_service_principal_client_secret"&lt;/P&gt;
&lt;P class="p1"&gt;authority = "&lt;A href="https://login.microsoftonline.com/your_tenant_id" target="_blank"&gt;https://login.microsoftonline.com/your_tenant_id&lt;/A&gt;"&lt;/P&gt;
&lt;P class="p1"&gt;connection_string = (&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Driver={{ODBC Driver 17 for SQL Server}};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Server={server};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Database={database};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Authentication=ActiveDirectoryServicePrincipal;"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"UID={clientId};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"PWD={clientSecret};"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"Encrypt=yes;"&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;f"TrustServerCertificate=no;"&lt;/P&gt;
&lt;P class="p1"&gt;)&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;try:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Establish the database connection&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;conn = pyodbc.connect(connection_string)&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;cursor = conn.cursor()&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Execute SQL queries here&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;cursor.execute("EXEC your_stored_procedure_name")&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;rows = cursor.fetchall()&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;for row in rows:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;print(row)&lt;/P&gt;
&lt;P class="p1"&gt;except Exception as e:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;print(f"Error: {str(e)}")&lt;/P&gt;
&lt;P class="p1"&gt;finally:&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;# Close the connection&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;conn.close()&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Executing SQL Stored Procedure in Databricks:&lt;/STRONG&gt;&lt;/LI&gt;
&lt;UL class="ul1"&gt;
&lt;LI class="li1"&gt;Once you have established the connection using the above code, you can execute your stored procedure using the cursor.execute method. Replace "EXEC your_stored_procedure_name" with the actual name of your stored procedure.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/OL&gt;
&lt;P class="p1"&gt;Please refer to: &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/connect/external-systems/sql-server" target="_blank"&gt;https://learn.microsoft.com/en-us/azure/databricks/connect/external-systems/sql-server&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 22:37:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-execute-sql-stored-procedure-in-azure-database-for-sql/m-p/103475#M41448</guid>
      <dc:creator>Alberto_Umana</dc:creator>
      <dc:date>2024-12-29T22:37:22Z</dc:date>
    </item>
  </channel>
</rss>

