有没有办法在 DBeaver 中执行带有多个变量和运算符的 SQL 语句?

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

我目前正在 运行宁 SQL 带有这样变量的语句

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

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

到目前为止,这一直运行良好,但我最近尝试 运行 包含 AND 运算符的语句。

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

DBeaver 抛出以下错误:

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

SQL 语句在我 运行 时有效

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

关于如何在一个语句中使用两个变量有什么想法吗?

您的变量包括您在等号后输入的单引号和分号。所以你的 where 评估为

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

导致第一个分号后出现语法错误。只需删除前两行中的分号,您不需要它们:

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