Oracle PL/SQL 用户输入错误 "Bind variable not declared"

Oracle PL/SQL User input error "Bind variable not declared"

accept x char prompt 'Please enter something: '
declare 
  a varchar2(50);
begin
  a := '&x';
  Select MIN(lowest_elevation) as lowest , Max(highest_elevation)as highest FROM countries where country_name = :a;
end;           

我正在接受用户的输入,然后将其传递到 where 子句中。而且我似乎无法在网上找到任何解决方案。我不知道我做错了什么。提前谢谢你。

a 是一个 PL/SQL 局部变量,而不是 SQL 绑定变量,所以当你引用它时它不应该有冒号:

where country_name = a;

要成为绑定变量,需要由 client/application/IDE 定义;在 SQL 开发人员中,例如:

accept x char prompt 'Please enter something: '
variable a varchar2(50);

begin
  :a := '&x';
  Select MIN(lowest_elevation) as lowest , Max(highest_elevation)as highest
  FROM countries
  where country_name = :a;
end; 

请注意,现在没有 declare 部分,因为您不再将 a 声明为 PL/SQL 变量 - 它是一个客户端变量。要将替换变量 &x 分配给它,它需要冒号 :a := '&x';,因为它也是一个绑定变量。

我并不是说那是你应该做的;只是显示差异。

你也可以完全跳过a变量,当然:

accept x char prompt 'Please enter something: '

begin
  Select MIN(lowest_elevation) as lowest , Max(highest_elevation)as highest
  FROM countries
  where country_name = '&x';
end;

顺便说一下,你必须 select 进入 一些东西,所以你的代码仍然无法像当前编写的那样工作。