cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Using variables in Spark SQL

HariharaSam
Contributor

Is there a way to declare variables in Spark SQL like we do it in T-SQL?

4 REPLIES 4

Hubert-Dudek
Esteemed Contributor III

Nice question. There was a discussion about the topic here https://community.databricks.com/s/question/0D53f00001c9RUYCA2/whats-the-equivalent-of-declare-in-da...

So there are functions, widgets, or just combining Python with SQL. Probably I would choose SQL functions as they are permanent and stay in metastore (but can not be used for everything).

    CREATE OR REPLACE FUNCTION my_name(name STRING COMMENT 'my name')
      RETURNS STRING
      COMMENT 'just first name'
      CONTAINS SQL DETERMINISTIC
      RETURN name || 'Hubert'

Hi Hubert ,

As you have mentioned that it can not be used for everything , in my case also it doesn't suit as I have a lot variables declaration and having a function created for each variable doesn't look good.

Debayan
Esteemed Contributor III
Esteemed Contributor III

Could you please follow the below link and let us know if this helps?

https://community.databricks.com/s/question/0D53f00001HKHa3CAH/how-do-i-pass-parameters-to-my-sql-st...

Kaniz
Community Manager
Community Manager

Hi @Hariharan Sambath​, We haven't heard from you on the last response from @Debayan Mukherjee​ and @Hubert Dudek​, and I was checking back to see if his suggestions helped you.

Or else, If you have any solution, please share it with the community as it can be helpful to others.

Also, Please don't forget to click on the "Select As Best" button whenever the information provided helps resolve your question.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.