B prince
B prince

Reputation: 11

Databricks: calling widget value in query

I've created a widget called yrmo with the values such as 202001.

I need to call this value from a query but it doesn't recognize it, I think because the field I am applying it to is Int but I can only reference the widget with quotes around it.

If I don't use quotes then it thinks I'm using a field in the table. If I use single quote then it interprets it as a literal. I've tried getArugument but it says it doesn't recognize it (do I load something?)

The query is is scala.

val x = sqlContext.sql("select domain from TABLENAME where partsn_mo=yrmo)")

Thanks

Upvotes: 0

Views: 2583

Answers (2)

Eric
Eric

Reputation: 340

You can use Scala's string interpolation with an expression inside of ${} that may include double quotes.

So you could do:

val x = spark.sql(s"select domain from TABLENAME where partsn_mo = ${dbutils.widgets.get("yrmo")}")

Upvotes: 1

Ritesh
Ritesh

Reputation: 1034

Try doing this

 val query = "select domain from TABLENAME where partsn_mo=" + dbutils.widgets.get("yrmo")
 val x = sqlContext.sql(query)

Upvotes: 0

Related Questions