简体   繁体   English

从Oracle迁移到SQL Server

[英]Migrate from Oracle to SQL Server

I have the following query written with Oracle 我用Oracle编写了以下查询

delete from EQUI_HIERARCHY; 

select max(level) into v_max_depth 
from EQUIP_D

connect by prior EQUIP_NO=PARENT_EQUIP_NO 
start with PARENT_EQUIP_NO is null; 

How to transform it into SQL Server ? 如何将其转换为SQL Server?

The SQL standard way to implement recursive queries, as implemented eg by IBM DB2 and SQL Server , is the WITH clause. 例如,由IBM DB2和SQL Server实施的实现递归查询的SQL标准方法是WITH子句。 See this article for one example of translating a CONNECT BY into a WITH (technically a recursive CTE) -- the example is for DB2 but I believe it will work on SQL Server as well. 请参阅本文,了解一个将CONNECT BY转换为WITH(技术上是递归CTE)的示例-该示例适用于DB2,但我相信它也可以在SQL Server上使用。

In SQL Server , IBM DB2, or PostgreSQL 8.4 (as well as in the SQL standard, for what that's worth;-), the perfectly equivalent solution is instead a recursive query (more complex syntax, but, actually, even more power and flexibility): SQL Server ,IBM DB2或PostgreSQL 8.4中(以及在SQL标准中,这是值得的;-),完美等效的解决方案是递归查询(更复杂的语法,但实际上,甚至更大的功能和灵活性) ):

    WITH n(v_max_depth ) AS 
         (SELECT max(level)
          FROM EQUIP_D
          WHERE PARENT_EQUIP_NO IS NULL
               UNION ALL
          SELECT max(level)
          FROM EQUIP_D as nplus1, n
          WHERE n.EQUIP_NO= nplus1.PARENT_EQUIP_NO)

   SELECT max(v_max_depth) FROM n

Oracle's START WITH clause becomes the first nested SELECT , the base case of the recursion, to be UNIONed with the recursive part which is just another SELECT . Oracle的START WITH子句变成第一嵌套SELECT ,递归的基座的情况下,要UNIONed与作为只是另一种递归部分SELECT

SQL Server's specific flavor of WITH is of course documented on MSDN , which also gives guidelines and limitations for using this keyword, as well as several examples. 当然, MSDN上记录了SQL Server特定的WITH风格,它还提供了使用此关键字的准则和限制以及几个示例。

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

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