【问题标题】:Is there a way to execute an SQL statement with multiple variables and an operator in DBeaver?有没有办法在 DBeaver 中执行具有多个变量和运算符的 SQL 语句?
【发布时间】:2021-11-22 16:49:11
【问题描述】:

我目前正在运行带有此类变量的 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 错误 [42601]:错误:“and”处或附近的语法错误

当我这样运行 SQL 语句时它会起作用

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

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

【问题讨论】:

  • 您的变量包括单引号和等号后输入的分号。所以你的where 评估为where col1 = 'hello'; and col2 = 'world';。只需删除前两行中的分号即可,不需要它们。
  • 哦,哇,这有效。非常感谢!
  • 我把我的评论变成了一个正确的答案,这样如果其他人寻找这个,它会更容易找到。

标签: sql postgresql dbeaver


【解决方案1】:

您的变量包括单引号和等号后输入的分号。所以你的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';

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 2012-01-22
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多