oracle - 如何在 Oracle PL-SQL(或 sql-plus)的某些选择语句中使用变量
问题描述
我有一些选择语句,它们都需要使用相同的变量。那我能不能先定义一个变量,然后在下面的语句中直接使用,像这样:
declare variable; -- a to_date function
select statement 1;
select statement 2;
select statement 3;
...
所以当条件改变时,我可以修改变量。如何在 oracle 中使用 pl-sql(或 sql-plus)来完成它?
解决方案
您需要简单的 sql*plus 变量。
variable my_date varchar2(30)
exec :my_date := '01-oct-2019';
select * from t where date_col = to_date(:my_date,'dd-mon-yyyy');
-- other select statements
您也可以使用替换变量来实现这一点。
select * from t1 where date_col = to_date(&&my_date,'dd-mon-yyyy');
select * from t2 where date_col = to_date(&&my_date,'dd-mon-yyyy');
-- other select statements using &&my_date
在这里,oracle 将提示my_date
一次(因为我们使用了两个&
),并且它的值将用于该会话的所有 select 语句。
干杯!!
推荐阅读
- python - DescriptorMatcher.match() 中的奇怪行为
- python - 如何解决conda环境不兼容的问题
- javascript - 如何通过 api 请求访问 JavaScript 中的 try catch 错误?
- python - Python Markdown2 防护代码块缩进问题
- php - 获取在联系表 7 提交时解析的电子邮件正文
- android - 应用程序开发语言
- django - 表单中 Django Group 模型的子集
- mongodb - MongoDB Compass - 文档不显示来自另一个集合的对象 ID 引用(Ref)的字段
- javascript - 来自firebase的图像的html2canvas jsPdf访问错误
- javascript - Javascript:如何删除嵌入式删除按钮的父列表项