【问题标题】:How to make pl/sql code to Ansi-Sql?如何将 pl/sql 代码制作成 Ansi-Sql?
【发布时间】:2015-11-09 08:01:43
【问题描述】:

我的问题对于 sql 专家来说可能没有挑战性。我想将我的 sql 重写为 ansi-sql。如何在 Oracle 中将下面的 sql 更改为 ansi-sql?

select * 
from TEST r
start with r.childid=@CHILDID 
connect by prior r.PARENTID=r.childid and r.recordstatus=1

【问题讨论】:

  • @CHILDID 不是有效的 PL/SQL 变量引用。您在哪里以及如何运行此语句?
  • 您确定您使用的是 Oracle RDBMS 而不是 SQL Server。
  • 我正在使用 Oracle RDBMS
  • 您想要 ISO/ANSI 定义的 ANSI SQL,还是迁移到特定的 dbms,例如MS SQL 服务器?

标签: sql oracle recursive-query ansi-sql connect-by


【解决方案1】:

ANSI SQL 等效项是递归公用表表达式:

with recursive tree as (
   select * 
   from test
   where childid = .... --<< this is the START WITH part
   union all
   select child.* 
   from test child
     join tree parent ON child.parentid = parent.childid and child.recordstatus = 1  --<< this is the CONNECT BY part
) 
select *
from tree

如果您还想将recordstatus = 1 条件应用于递归开始,我不是 100%。


这里Oracle不符合标准,不允许使用recursive关键字。

所以您需要从上面的查询中删除 recursive(SQL Server 也是如此)

有关递归公用表表达式(在 Oracle 中称为“子查询分解”)的更多详细信息,请参阅手册:

https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#SQLRF55268

【讨论】:

  • 注意:如果 OP 正在迁移到 SQL Server(可能由 @ 参数暗示),那么 SQL Server 不支持这种语法,但支持这种想法,它只需要recursive 关键字被忽略。
  • @hvd:Penguen专门要求ANSI SQL,按照标准需要recursive关键字。
  • 我知道这一点,我并不是想说您的回答对于 OP 实际提出的问题是错误的。但是,我怀疑 OP 提出了错误的问题。
  • 它会导致“缺少关键字”错误。在下面画“树”。我猜“使用递归树”有问题
  • "C:\Program Files (x86)\PLSQL Developer\plsqldev.exe
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 2015-07-02
相关资源
最近更新 更多