简体   繁体   English

Oracle等同于SQL Server查询

[英]Oracle equivalent of a SQL Server query

I need to know the Oracle equivalent of my SQL Sever query. 我需要知道等同于SQL Sever查询的Oracle。 Could someone please help me? 有人可以帮我吗?

select recno = (select top 1 ld.recno from load ld where ld.crecno = i.recno) 
from inputtable i

Check Below Query 检查以下查询

select (select  ld.recno from load ld where ld.crecno = i.recno AND RowNum =1)
AS recno  from inputtable i

Converting TOP(N) T-SQL Syntax to Oracle Compatible SQL 将TOP(N)T-SQL语法转换为Oracle兼容的SQL

The SUBQUERY you wrote is not necessary. SUBQUERY你写的是没有必要的。 A INNER JOIN operation between the LOAD and INPUTTABLE tables is a quick way to get the set you are looking for. LOADINPUTTABLE表之间进行INNER JOIN操作是获取所需集合的快速方法。

SQL Syntax Interpretation: Of all the records in LOAD , how many of the ones with values CRECNO have matching values in the column RECNO of table INPUTTABLE ? SQL语法解释:LOAD所有记录中,有多少个值为CRECNO的记录在表INPUTTABLE RECNO列中具有匹配的值? Taking those results, what is the FIRST value (TOP(1)) in the output if the results are sorted by...(?)... 取得这些结果,如果结果按...(?)...排序,则输出中的FIRST值(TOP(1))是多少?

There isn't any SORT priority designated in the OP. OP中未指定任何SORT优先级。

In a SELECT statement, always use an ORDER BY clause with the TOP clause. 在SELECT语句中,始终将ORDER BY子句与TOP子句一起使用。 This is the only way to predictably indicate which rows are affected by TOP. 这是可预测地指示哪些行受TOP影响的唯一方法。 Reference From: Microsoft Technet . 参考来源:Microsoft Technet

To illustrate a correctly formatted SQL statement with repeatable results, I went ahead and rewrote the OP's SQL query assuming that the TOP(n) solution wanted the first value in an ASCENDING SORT of the RECNO column values. 为了说明具有正确结果的正确格式的SQL语句,我继续写了OP的SQL查询,并假设TOP(n)解决方案想要RECNO列值的升序排序中的第一个值。

Database servers do have a default sorting and selection behavior if important expressions such as an ORDER BY statement are missing, it is risky however to assume that all defaults are set the same in any given environment. 如果缺少重要的表达式(例如ORDER BY语句),则数据库服务器确实具有默认的排序和选择行为,但是冒充风险是假定在任何给定环境中所有默认值都设置为相同。

The Rewritten SQL: 重写的SQL:

WITH sub_query AS (
     SELECT i.recno
       FROM inputtable i, load ld
      WHERE i.recno = ld.crecno
      ORDER BY i.recno ASC
     )
     SELECT s.recno
       FROM sub_query s
      WHERE rownum = 1

The ROWNUM evaluation and the ORDER BY criteria can be changed to obtain the TOP(n) behavior intended. 可以更改ROWNUM评估和ORDER BY标准以获得预期的TOP(n)行为。

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

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