Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark SQL passing a variable

Tags:

sql

select

I have following Spark sql and I want to pass variable to it. How to do that? I tried following way.

 sqlContext.sql("SELECT count from mytable WHERE id=$id")
like image 848
G G Avatar asked May 17 '16 18:05

G G


People also ask

Can we use variables in spark SQL?

The short answer is no, Spark SQL does not support variables currently. The SQL Server uses T-SQL, which is based on SQL standard extended with procedure programming, local variables and other features. Spark SQL is a pure SQL, partially compatible with SQL standard.

Can we pass variable in SQL query?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.


2 Answers

You can pass a string into sql statement like below

id = "1"
query = "SELECT count from mytable WHERE id='{}'".format(id)
sqlContext.sql(query)
like image 69
David Avatar answered Oct 12 '22 22:10

David


You are almost there just missed s :)

sqlContext.sql(s"SELECT count from mytable WHERE id=$id")
like image 42
WoodChopper Avatar answered Oct 12 '22 20:10

WoodChopper