Sam
Sam

Reputation: 323

Spark SQL - declaring and using variables in SQl Notebook

In Azure data bricks i created SQL note book. I am trying to use the variables and use that across multiple SQL statements. e.g. declare fiscal year and use that across where criteria. Intent is to avoid hardcoding.

It looks i have to use Python / Scala. Is there any way to achieve this using pure SQL statements?

e.g.:

var @fiscalYear = 2018;

select * from table where fiscalyear = @fiscalyear

Upvotes: 3

Views: 6867

Answers (1)

Ronieri Marques
Ronieri Marques

Reputation: 459

Check this link out:

https://forums.databricks.com/questions/176/how-do-i-pass-argumentsvariables-to-notebooks.html

Another way is to do this (to set variable value):

%python
dbutils.widgets.text("var","text")
dbutils.widgets.remove("var")

Then you can go:

%sql
select * from table where value = '$var'

Upvotes: 2

Related Questions