oracle - SQL避免在select语句中调用同一个函数两次

标签 oracle function select

我有一个 select 语句调用同一个函数两次以返回两个不同的列,因为同一个函数被调用两次,它会产生性能问题。

我只想调用函数一次并将其值复制到另一列。在 Oracle 中有可能吗?

SELECT ID
    ,PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) "BLOCK" 
    ,PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) "MASK"
    from table 
    where ID=condition;

最佳答案

您可以使用 with clause一次获取数据

with block as (
select PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) as block 
from table
 where ID=condition;
) select ID, block.block as "BLOCK" , block.block as "MASK" from block 

The WITH clause, or subquery factoring clause, is part of the SQL-99 standard and was added into the Oracle SQL syntax in Oracle 9.2. The WITH clause may be processed as an inline view or resolved as a temporary table. The advantage of the latter is that repeated references to the subquery may be more efficient as the data is easily retrieved from the temporary table, rather than being requeried by each reference. You should assess the performance implications of the WITH clause on a case-by-case basis.

关于oracle - SQL避免在select语句中调用同一个函数两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53387661/

相关文章:

c - 尝试理解 C 程序

sql - 此处不允许使用组功能错误

mysql - Liquibase DB 独立插入语句

C++函数链返回函数

sql - Spark SQL 传递变量

php - 在 Php $db->select 上选择数据库

mysql - 我有一个具有挑战性的 MySQL SELECT 查询,涉及库存和阈值边界

oracle - oracle表中自动填充日期

sql - 如何调用Oracle MD5哈希函数?

C - 无法设置 pcap 过滤器。功能无法正常工作