gpt4 book ai didi

sql - 如何在 Where 子句中使用别名?

转载 作者:行者123 更新时间:2023-12-04 22:27:55 25 4
gpt4 key购买 nike

我有这个程序:

 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 生成此错误:

错误(20,10):PL/SQL:ORA-00904:“EXPIRED_DATE”:标识符无效

我相信 PLSQL 允许我在 Where 子句中使用别名,但是有什么我忘记了吗?

提前致谢。

最佳答案

您不能在 WHERE 子句中引用列别名 - 您的选项是:

  • 复制 WHERE 子句中的 CASE 语句
  • 使用子查询:
    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;
  • 关于sql - 如何在 Where 子句中使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3852831/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com