45

i just want to know if it is possible to declare variables on the DBeaver´s sql editor and use them on a query

Elliott Urrutia
  • 589
  • 1
  • 5
  • 11

5 Answers5

89

You have to enable variable processing in the "SQL Processing" settings of DBeaver -> Window -> Preferences -> Database -> Editors -> SQL Editor -> SQL Processing. There is a block on Parameters with settings you can change. See the Dynamic Parameter binding section on the wiki.

enter image description here

You should then be able to do:

@set date = '2019-10-09'

SELECT ${date}::DATE, ${date}::TIMESTAMP WITHOUT TIME ZONE

which produces:

| date       | timestamp           |
|------------|---------------------|
| 2019-10-09 | 2019-10-09 00:00:00 |
nicoschl
  • 2,346
  • 1
  • 17
  • 17
24

Yes you can, using :.

An example:

SELECT * FROM "SYSIBM".SYSDUMMY1
WHERE IBMREQD = :YOUR_VARIABLE
Nifriz
  • 1,033
  • 1
  • 10
  • 21
5

Based on the incredibly helpful post from @nicoschl, here are a couple of minor improvements:

-- using declarations
@set datex_start = cast('2120-01-01' as date) as date_start;
-- datex_start is the var name
-- casting the value in the declaration saves us the work later
-- the var can be given a default fieldname (e.g. "date_start")

-- run as a standalone command since the subsequent SELECT statement doesn't return values when it's all run together

select 
    ${datex_start}
;

This will return a value "2120-01-01" with a fieldname of "date_start".

Tom Renish
  • 398
  • 4
  • 7
  • set with ';' at the end defines variable with that ';' at the end in dbeaver , so if you need clean value do set without ending ';' – vasq Oct 10 '22 at 03:13
  • On Oracle I ended up using `@set i_date = (date '2000-01-01')` (which makes it relatively simple to copypaste storedproc queries and just prepend the input parameters with `:` like `select :i_date as col1 from dual` – Pasi Savolainen Feb 13 '23 at 13:57
5

In the DBeaver SQL editor you can type the following:

-- define variable
@set my_var='hello SQL world'

-- call variable
select :my_var 

You can also use ${my_var} to reference the variable; $my_var however did not work for me. I am using DBeaver v. 21.1.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
n3rd
  • 173
  • 1
  • 6
-1

You have to enable at Dbeaver settings: Top Window > Preferences > and then see print below (updated 2022/08).

Updated screenshot

rd1218
  • 134
  • 12
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 04 '22 at 12:15