Search code examples
sqlpostgresqldbeaver

Is there a way to execute an SQL statement with multiple variables and an operator in DBeaver?


I'm currently running SQL statements with variables like this

@set var1 = 'hello';
@set var2 = 'world';

select * 
from "Table" 
where col1 = ${var1};

So far this has been working perfectly fine but I recently tried to run a statement that contained the AND operator.

select * 
from "Table" 
where col1 = ${var1} and col2 = ${var2};

DBeaver throws the following error:

SQL Error [42601]: ERROR: syntax error at or near "and"

The SQL statement works when I run it like so

select * 
from "Table" 
where col1 = 'hello' and col2 = 'world';

Any ideas on how I can use two variables in one statement?


Solution

  • Your variables include the single quotes and the semicolons you entered after the equal sign. So your where evaluates to

    where col1 = 'hello'; and col2 = 'world';;
    

    Causing a syntax error after the first semicolon. Just remove the semicolons in the first two lines, you don't need them:

    @set var1 = 'hello'
    @set var2 = 'world'
    select * 
    from "Table" 
    where col1 = ${var1} and col2 = ${var2};--where col1 = 'hello' and col2 = 'world';