Support Questions

Find answers, ask questions, and share your expertise

SQL SELECT into Variable

avatar
New Contributor

There are two different examples of SQL in SQL Server

1. DECLARE @Max_date_id varchar(10)
SELECT @Max_date_id = MAX(date_id) FROM t_my_table
SELECT @Max_date_id

2. DECLARE @Max_date_id varchar(10) = (SELECT MAX(date_id) FROM t_my_table)
SELECT @Max_date_id

How can I do the same thing in "Impala Query Manager"?

 

 

 

1 ACCEPTED SOLUTION

avatar
Rising Star

Hi @AKO , 

Impala has variable substitution like this:

[hostname.local.net:21000] default> SET VAR:query=SELECT 1+2;
Variable QUERY set to SELECT 1+2
[hostname.local.net:21000] default> ${VAR:query};
Query: SELECT 1+2
Query submitted at: 2024-10-15 15:54:29 (Coordinator: https://hostname.local.net:25000)
Query progress can be monitored at: https://hostname.local.net:25000/query_plan?query_id=nnnn
+-------+
| 1 + 2 |
+-------+
| 3     |
+-------+
Fetched 1 row(s) in 1.15s

See official Impala docs at:
https://impala.apache.org/docs/build/html/topics/impala_shell_running_commands.html

This is a feature of impala-shell, and not impala itself, so depending on what you call "Impala Query Manager", your experience might be different.

If you want a solution that is more database independent, then I recommend to use a view or a SELECT CTE (WITH statement) instead:

WITH sub_query AS (
SELECT 1+2
)
SELECT * FROM sub_query;

View solution in original post

2 REPLIES 2

avatar
Rising Star

Hi @AKO , 

Impala has variable substitution like this:

[hostname.local.net:21000] default> SET VAR:query=SELECT 1+2;
Variable QUERY set to SELECT 1+2
[hostname.local.net:21000] default> ${VAR:query};
Query: SELECT 1+2
Query submitted at: 2024-10-15 15:54:29 (Coordinator: https://hostname.local.net:25000)
Query progress can be monitored at: https://hostname.local.net:25000/query_plan?query_id=nnnn
+-------+
| 1 + 2 |
+-------+
| 3     |
+-------+
Fetched 1 row(s) in 1.15s

See official Impala docs at:
https://impala.apache.org/docs/build/html/topics/impala_shell_running_commands.html

This is a feature of impala-shell, and not impala itself, so depending on what you call "Impala Query Manager", your experience might be different.

If you want a solution that is more database independent, then I recommend to use a view or a SELECT CTE (WITH statement) instead:

WITH sub_query AS (
SELECT 1+2
)
SELECT * FROM sub_query;

avatar
New Contributor

I know about CTE solution I was trying to find if the variable like SQL Server solution is there or not. I will use CTE. thanks