SQL Oracle:使用 where 语句从 select 保存变量
SQL Oracle: Save variable from select with where statement
我正在使用 SQL Developer 19.1。我有一个包含多个 subselect 语句的巨大脚本。看起来像这样
def var1 = '1234'
def var2 = '5678'
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = &var1 and number2 = &var2)
我想通过另一个 select 语句获取 var1 和 var2。我怎么做?我试过了
declare
var1 number;
begin
select somenumber into var1 from ... where ...
end;
但似乎我不能在那里使用 where 语句。我也尝试过使用 bind 或 @var1..
你们知道我接下来可以尝试什么吗?
我的目标是
var1 = (select somenumber from ... where ... )
如果你想继续使用替代变量,你可以使用 the column ... new_value
functionality(该文档适用于 SQL*Plus,但主要适用于 SQL Developer;并着重于将其用于报告标题,但它比建议的更有用):
-- define not needed now
--def var1 = '1234'
--def var2 = '5678'
column var1 new_value var1
select somenumber as var1 from ... where ...
column var2 new_value var2
select somenumber as var2 from ... where ...
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = &var1 and number2 = &var2)
用简单的例子来说明这一点;如果工作表有:
column var1 new_value var1
select 1234 as var1 from dual;
column var2 new_value var2
select 5678 as var2 from dual;
select * from dual where 1234 = &var1 and 5678 = &var2;
然后脚本输出面板显示:
VAR1
----------
1234
VAR2
----------
5678
old:select * from dual where 1234 = &var1 and 5678 = &var2
new:select * from dual where 1234 = 1234 and 5678 = 5678
D
-
X
或者您可以使用绑定变量:
var var1 number;
var var2 number;
begin
select somenumber into :var1 from ... where ...
select somenumber into :var2 from ... where ...
end;
/
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = :var1 and number2 = :var2)
请注意,对 var1
和 var2
的引用现在前面有一个冒号来指示绑定变量,而不是一个符号来指示替换变量;那个冒号也出现在 into :var1
部分 - 它仍然指的是绑定变量,而不是本地 PL/SQL 变量。
又是一个简单的例子;工作表脚本:
var var1 number;
var var2 number;
begin
select 1234 into :var1 from dual;
select 5678 into :var2 from dual;
end;
/
select * from dual where 1234 = :var1 and 5678 = :var2;
获取输出:
PL/SQL procedure successfully completed.
D
-
X
如果您想查看由匿名 PL/SQL 块分配的值,您可以使用 print var1
。
我正在使用 SQL Developer 19.1。我有一个包含多个 subselect 语句的巨大脚本。看起来像这样
def var1 = '1234'
def var2 = '5678'
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = &var1 and number2 = &var2)
我想通过另一个 select 语句获取 var1 和 var2。我怎么做?我试过了
declare
var1 number;
begin
select somenumber into var1 from ... where ...
end;
但似乎我不能在那里使用 where 语句。我也尝试过使用 bind 或 @var1..
你们知道我接下来可以尝试什么吗?
我的目标是
var1 = (select somenumber from ... where ... )
如果你想继续使用替代变量,你可以使用 the column ... new_value
functionality(该文档适用于 SQL*Plus,但主要适用于 SQL Developer;并着重于将其用于报告标题,但它比建议的更有用):
-- define not needed now
--def var1 = '1234'
--def var2 = '5678'
column var1 new_value var1
select somenumber as var1 from ... where ...
column var2 new_value var2
select somenumber as var2 from ... where ...
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = &var1 and number2 = &var2)
用简单的例子来说明这一点;如果工作表有:
column var1 new_value var1
select 1234 as var1 from dual;
column var2 new_value var2
select 5678 as var2 from dual;
select * from dual where 1234 = &var1 and 5678 = &var2;
然后脚本输出面板显示:
VAR1
----------
1234
VAR2
----------
5678
old:select * from dual where 1234 = &var1 and 5678 = &var2
new:select * from dual where 1234 = 1234 and 5678 = 5678
D
-
X
或者您可以使用绑定变量:
var var1 number;
var var2 number;
begin
select somenumber into :var1 from ... where ...
select somenumber into :var2 from ... where ...
end;
/
select a.id
from tablea a
where something.id = (select other.id from .... where number1 = :var1 and number2 = :var2)
请注意,对 var1
和 var2
的引用现在前面有一个冒号来指示绑定变量,而不是一个符号来指示替换变量;那个冒号也出现在 into :var1
部分 - 它仍然指的是绑定变量,而不是本地 PL/SQL 变量。
又是一个简单的例子;工作表脚本:
var var1 number;
var var2 number;
begin
select 1234 into :var1 from dual;
select 5678 into :var2 from dual;
end;
/
select * from dual where 1234 = :var1 and 5678 = :var2;
获取输出:
PL/SQL procedure successfully completed.
D
-
X
如果您想查看由匿名 PL/SQL 块分配的值,您可以使用 print var1
。