How to use Alias in Where clause?(如何在 Where 子句中使用别名?)
问题描述
我有这个程序:
PROCEDURE P_LOAD_EXPIRED_ACCOUNT
(
pDayDiff IN NUMBER,
ExpiredCur OUT MEGAGREEN_CUR
)
IS
BEGIN
OPEN ExpiredCur FOR
SELECT
ACCOUNT_NAME, SERVICE_TYPE,
CASE
WHEN SERVICE_TYPE = 1 THEN ADD_MONTHS(ACTIVATED_DATE,3)
WHEN SERVICE_TYPE = 2 THEN ADD_MONTHS(ACTIVATED_DATE,6)
WHEN SERVICE_TYPE = 3 THEN ADD_MONTHS(ACTIVATED_DATE,12)
END
AS EXPIRED_DATE
FROM SUBSCRIBERS
WHERE (EXPIRED_DATE - CURRENT_DATE) < pDayDiff;
END;
但 SQL Developer 生成此错误:
but SQL Developer generate this error:
错误(20,10):PL/SQL:ORA-00904:EXPIRED_DATE":标识符无效
Error(20,10): PL/SQL: ORA-00904: "EXPIRED_DATE": invalid identifier
我相信 PLSQL 允许我在 Where 子句中使用别名,但是我忘记了什么吗?
I believe that PLSQL allows me to use Alias in Where clause, but is there something I forgot?
提前致谢.
推荐答案
您不能在 WHERE 子句中引用列别名 - 您的选择是:
You can't reference the column alias in the WHERE clause - your options are:
- 复制 WHERE 子句中的 CASE 语句
使用子查询:
- replicate the CASE statement in the WHERE clause
use a subquery:
PROCEDURE P_LOAD_EXPIRED_ACCOUNT(pDayDiff NUMBER,
ExpiredCur OUT MEGAGREEN_CUR)
IS
BEGIN
OPEN ExpiredCur FOR
SELECT x.account_name,
x.service_type,
x.expired_date
FROM (SELECT s.account_name,
s.service_type,
CASE
WHEN s.service_type = 1 THEN ADD_MONTHS(ACTIVATED_DATE,3)
WHEN s.service_type = 2 THEN ADD_MONTHS(ACTIVATED_DATE,6)
WHEN s.service_type = 3 THEN ADD_MONTHS(ACTIVATED_DATE,12)
END AS EXPIRED_DATE
FROM SUBSCRIBERS s) x
WHERE x.expired_date - CURRENT_DATE < pDayDiff;
END;
甲骨文 9i+
WITH summary AS (
SELECT s.account_name,
s.service_type,
CASE
WHEN s.service_type = 1 THEN ADD_MONTHS(ACTIVATED_DATE,3)
WHEN s.service_type = 2 THEN ADD_MONTHS(ACTIVATED_DATE,6)
WHEN s.service_type = 3 THEN ADD_MONTHS(ACTIVATED_DATE,12)
END AS EXPIRED_DATE
FROM SUBSCRIBERS s)
SELECT x.account_name,
x.service_type,
x.expired_date
FROM summary x
WHERE x.expired_date - CURRENT_DATE < pDayDiff;
这篇关于如何在 Where 子句中使用别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Where 子句中使用别名?
基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01