简体   繁体   English

替代“连接方式” Oracle SQL

[英]Alternative for "connect by" Oracle SQL

Anyone be able to give me some script that does the same as the below but not using "connect by"?任何人都可以给我一些与以下内容相同但不使用“连接方式”的脚本吗? The code works well, but I cannot use connect by in the BI publisher.该代码运行良好,但我无法在 BI 发布者中使用 connect by。

select to_char(to_date('2022-05-01') + (level -1),'YYYY-MM-DD') as read_date 
from dual 
connect by to_date('2022-05-01') + (level -1) <= to_date('2022-05-05')

CONNECT BY is Oracle's propriatary and concise way to write a recursive query. CONNECT BY是 Oracle 编写递归查询的专有且简洁的方法。 You can replace it with a standard SQL compliant recursive query that has been supported by Oracle since 2002.您可以将其替换为自 2002 年以来 Oracle 支持的标准 SQL 兼容递归查询。

with read_dates(read_date) as
(
  select date '2022-05-01' from dual
  union all
  select read_date + interval '1' day from read_dates
  where read_date < date '2022-05-05'
)
select to_char(read_date, 'YYYY-MM-DD')
from read_dates;

Two remarks:两个备注:

  • Your own code is vulnarable, because it uses an implicit string to date conversion ( to_date('2022-05-01') ) that relies on session date settings and can thus fail miserably.您自己的代码很容易受到攻击,因为它使用依赖于 session 日期设置的隐式字符串到日期转换( to_date('2022-05-01') ),因此可能会失败。
  • It is rare that we select dates as strings ( to_char(..., 'YYYY-MM-DD') ), because we usually want our app to know that we are selecting dates.我们很少将 select 日期作为字符串 ( to_char(..., 'YYYY-MM-DD') ),因为我们通常希望我们的应用程序知道我们正在选择日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM