cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform 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? 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group