How to connect a dedicated sql pool with azure function

C-5961 756 Reputation points
2022-08-24T05:51:22.45+00:00

I would like to know how to use python in azure function to connect to synapse's dedicated SQL pool.

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Azure Synapse Analytics
Azure Synapse Analytics

An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.


Answer accepted by question author
MartinJaffer-MSFT 26,161 Reputation points
2022-08-24T22:42:12.59+00:00

I think you would benefit from looking at this how-to guide, @Anonymous
Get connection info for dedicated sql pool
Quickstart: Use Python to query a database (generic)
connection-strings for Synapse SQL

Specifically, this excerpt, changed for making relevant to Synapse taken from example connection strings. Do note that other authentication options are available, such as Active Directory, integrated or password. You may need to add the Function to Synapse Access Control if you choose the Active Directory options.

import pyodbc  
server = '<server>.sql.azuresynapse.net'  
database = '<database>' # substitute pool name  
username = '<username>'  
password = '{<password>}'     
driver= '{ODBC Driver 13 for SQL Server}' # or possibly 17 or 18 or {SQL Server Native Client 11.0}  
  
with pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:  
    with conn.cursor() as cursor:  
        cursor.execute("SELECT TOP 3 name, location FROM mytable")  
        row = cursor.fetchone()  
        while row:  
            print (str(row[0]) + " " + str(row[1]))  
            row = cursor.fetchone()  

While this is example code for illustrating usage, do note that credentials-in-code is terrible, bad, dangerous practice. It is recommended you have your Function get secrets from Key Vault.

Hope this will help. Please let us know if any further queries.

------------------------------

  • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
  • Want a reminder to come back and check responses? Here is how to subscribe to a notification
  • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.