cancel
Showing results for 
Search instead for 
Did you mean: 
Community Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

pass a tuple as parameter to sql query

kll
New Contributor III
at_lst = ['131','132','133']

at_tup = (*at_lst,)
 
print(at_tup)
('131','132','133')<div> <div><span>In my sql query, i am trying to pass this on a parameter, however, it doesn't work. <div> <div><div><div><span>%sql<br /><div><span>select * from main.schema.tbl<div><span>where code IN <span>( '{{ at_tup }}' )<div> <div><span>^ This return no result. <div> <div><span>However, when I pass the actual tuple, i get the desired results: <div> <div><div><span>select * from main.schema.tbl<div><span>where code IN ('131','132','133')<div> <div>How do I pass a tuple as a paramter the sql query?</div></div></span></div></span></div></div></div></span></div></div></span></div></div></span></span></div></span></div></span></div></div></div></div></span></div></div>
2 REPLIES 2

Kaniz_Fatma
Community Manager
Community Manager

Hi @kllTo pass a tuple as a parameter to an SQL query, you can make use of the execute method of the cursor object in Python. The execute method allows you to prepare and run a database query or command, and it accepts a dictionary as a parameter where you can pass your tuple values.

kll
New Contributor III

@Kaniz_Fatma  I am writing sql using the magic command in the cell block, `%%sql`. Is there a way to pass a parameter in the query without using the `execute` method of the cursor object? Can you please share an example? 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!