cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

I am new to Databricks SQL and want to create a variable which can hold calculations either from static values or from select queries similar to SQL Server. Is there a way to do so?

Sudeshna
New Contributor III

I was trying to create a variable and i got the following error -

command -

SET a = 5;

Error -

Error running query

Configuration a is not available.

1 ACCEPTED SOLUTION

Accepted Solutions

BilalAslamDbrx
Databricks Employee
Databricks Employee

@Sudeshna Bhakatโ€‹ what @Joseph Kambourakisโ€‹ described works on clusters but is restricted on Databricks SQL endpoints i.e. only a limited number of SET commands are allowed. I suggest you explore the curly-braces (e.g. {{ my_variable }}) in Databricks SQL.

View solution in original post

6 REPLIES 6

User16764241763
Honored Contributor

Hello @Sudeshna Bhakatโ€‹ 

Thank you for posting on Databricks Community Forum

Could you please use Dashboard filters

https://docs.databricks.com/sql/user/queries/query-filters.html

Query

SELECT l_linenumber AS linenumber, COUNT(*) AS cnt FROM lineitem WHERE l_linenumber = {{var}}

GROUP BY l_linenumber

Query editor looks like below

image 

When you add this query to the dashboard, the var looks like below

Dashboard

Dashboard 

Please let us know if this works for you.

I have filed a feature request internally to support this.

Anonymous
Not applicable

Here is an example of the syntax:

set a.variablename = value

Then to use it you need to reference it with ${a.variablename}. See the screenshot

BilalAslamDbrx
Databricks Employee
Databricks Employee

@Sudeshna Bhakatโ€‹ what @Joseph Kambourakisโ€‹ described works on clusters but is restricted on Databricks SQL endpoints i.e. only a limited number of SET commands are allowed. I suggest you explore the curly-braces (e.g. {{ my_variable }}) in Databricks SQL.

Using curly braces means it will create parameters. but i don't need a parameter. There is no way other than creating a parameter?

No, not yet. For now, you can only use SET commands to set a limited number of configuration options.

Sudeshna
New Contributor III

Got your point. Thank you very much for the help

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