2

I'm trying to convert a query from T-SQL to Spark's SQL. I've got 99% of the way, but we've made strong use of the DECLARE statement in T-SQL.

I can't seem to find an alternative in Spark SQL that behaves the same - that is, allows me to declare variables in the query itself, to be re-used in that query.

Example in T-SQL:

DECLARE @varA int
SET @varA = '4'
SELECT * FROM tblName where id = @varA;

How do i do the declaration of such a variable in Spark SQL? (I don't want to use string interpolation, unless necessary)

user3012708
  • 793
  • 1
  • 11
  • 33
  • 1
    Unfortunately this is not supported in Spark SQL https://stackoverflow.com/questions/26755230/dynamically-bind-variable-parameter-in-spark-sql – mck Nov 06 '20 at 08:14
  • Oh no :( Thank you! Guess I either hard code these values (yuck) or go string interpolation (which i guess is better than hard-coding). Odd feature to miss, but ok. – user3012708 Nov 06 '20 at 08:16

1 Answers1

2

You can try this:

sqlContext.sql("set id_value = 3")
sqlContext.sql("select * from country where id = ${id_value}").show()

enter image description here

CHEEKATLAPRADEEP
  • 12,191
  • 1
  • 19
  • 42