在 Oracle 中定义局部变量的最简单方法是什么?

2023-11-28数据库问题
13

本文介绍了在 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 中定义局部变量的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

如何在MySQL中为每个组选择第一行?
How to select the first row for each group in MySQL?(如何在MySQL中为每个组选择第一行?)...
2024-04-16 数据库问题
13

MySQL - 获取最低值
MySQL - Fetching lowest value(MySQL - 获取最低值)...
2024-04-16 数据库问题
8

在 cmakelist.txt 中添加和链接 mysql 库
Add and link mysql libraries in a cmakelist.txt(在 cmakelist.txt 中添加和链接 mysql 库)...
2024-04-16 数据库问题
41

考勤数据库的良好数据库设计(架构)是什么?
What is a good database design (schema) for a attendance database?(考勤数据库的良好数据库设计(架构)是什么?)...
2024-04-16 数据库问题
7