Value of SQL variable in IF statement using Spark SQL

RicardoS
New Contributor II

Hi there,

I am new to Spark SQL and would like to know if it possible to reproduce the below T-SQL query in Databricks. This is a sample query, but I want to determine if a query needs to be executed or not.

 

DECLARE
      @VariableA AS INT
,     @VariableB AS INT;

SELECT @VariableA = COUNT(*)
FROM [xxx].[TableExample]

SELECT @VariableB = COUNT(*) + 1
FROM [xxx].[TableExample]

IF (@VariableA = @VariableB)
BEGIN
      PRINT 'Equal'
END
ELSE
BEGIN
      PRINT 'Not equal'
END

 

I want to use an IF statement to determine it. I already know how to set a variable using a SELECT statement:

 

SET VariableA = SELECT COUNT(*) FROM [xxx].[TableExample];
SET VariableB = SELECT COUNT(*) + 1 FROM [xxx].[TableExample];

 

Next I want to use VariableA and VariableB in an IF statement:

 

IF (${VariableA} = ${VariableB}, 'Equal', 'Not equal');

 

This is not working, because underwater this expression writes down the full SELECT statements of both variables instead of the results (numbers) to compare. When I write down only ${VariableA}; it gives me the correct result (number).

How can I make this work in the IF statement?

So to be clear, no PySpark solution, but SQL (Spark). Thank you!