What is the simplest way to define a local variable in Oracle?(在 Oracle 中定义局部变量的最简单方法是什么?)
问题描述
在 SQL Server 中,我可以像这样定义局部变量.
In the SQL Server, I can define local variables like this.
declare @id number := 1000
select * from tbl_A where id = @id;
select * from tbl_B where id = @id;
非常方便.我试图在 PL/SQL 中做同样的事情,但它不起作用.
It is very convenient.
I tried to do same thing in PL/SQL but it doesn't work.
DECLARE id number;
select 1000 into id from dual;
你知道如何做类似的事情吗?最简单的方法是我的目标.
Do you know how to do something similar? The simplest method is my objective.
推荐答案
PL/SQL中如果要定义局部变量,需要一个完整的PL/SQL块
If you want to define a local variable in PL/SQL, you need a complete PL/SQL block
DECLARE
id NUMBER;
BEGIN
SELECT 1000
INTO id
FROM dual;
END;
或者只是
DECLARE
id NUMBER := 1000;
BEGIN
<<do something that uses the local variable>>
END;
如果你想在 SQL*Plus 中声明一个变量
If you want to declare a variable in SQL*Plus
SQL> variable id number
SQL> begin
select 1000 into :id from dual;
end;
/
SQL> print id
ID
----------
1000
SQL> SELECT * FROM tbl_a WHERE id = :id
这篇关于在 Oracle 中定义局部变量的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Oracle 中定义局部变量的最简单方法是什么?
基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
