如果我多次运行带有行限制的SQL语句,每次都会获得相同的结果吗?

时间:2016-05-26 20:51:46

标签: sql oracle rownum

鉴于我的陈述保持不变。鉴于有15000个可用行,但我使用rownum进行行限制我是否会每次都获得相同的输出?

我正在使用的示例SQL:

SELECT distinct 
     TO_CHAR(S.DID), 
     TO_CHAR(S.DPERMCONTAINER), 
     S.DBARCODE, 
     S.DMEDIATYPE, 
     S.DDOCTITLE, 
     S.XCOMMENTS, 
     rm.xcategoryid, 
     s.xdivision, 
     S.XPROJECTNAME, 
     S.XLEGACYPROJECTNAME,
     S.XLABELNOTE, 
     S.XPROJECTENDDATE, 
     S.XEVENTDATE 
FROM 
     PRD64_URMSERVER.EXTITEMS S 
     left join prd64_urmserver.extitemsrm rm on s.did = rm.did 
WHERE 
     S.DMEDIATYPE = 'Folder' 
     AND S.DPERMCONTAINER IN
          (SELECT distinct TO_CHAR(S.DID)
          FROM PRD64_URMSERVER.EXTITEMS S 
          left join DIDV V on s.did = v.did 
          left join prd64_urmserver.extitemsrm rm on s.did = rm.did
          WHERE v.did IS NULL
               and RM.xDerivedIsFrozen = '0' 
               AND s.dmediatype = 'Box' 
               and S.XLABELNOTE = 'DISP: GPO-CRM-US-ATT-20160415-09' 
               and s.xprojectenddate < to_date('05/25/2006', 'mm-dd-yyyy') 
               and rownum <= 5000)
UNION
(SELECT distinct 
     TO_CHAR(S.DID), 
     TO_CHAR(S.DPERMCONTAINER), 
     S.DBARCODE, 
     S.DMEDIATYPE, 
     S.DDOCTITLE, 
     S.XCOMMENTS, 
     rm.xcategoryid, 
     s.xdivision, 
     S.XPROJECTNAME, 
     S.XLEGACYPROJECTNAME,
     S.XLABELNOTE, 
     S.XPROJECTENDDATE, 
     S.XEVENTDATE
FROM 
     PRD64_URMSERVER.EXTITEMS S 
     left join DIDV V on s.did = v.did 
     left join prd64_urmserver.extitemsrm rm on s.did = rm.did
WHERE 
     v.did IS NULL 
          and RM.xDerivedIsFrozen = '0' 
          AND s.dmediatype = 'Box' 
          and S.XLABELNOTE = 'DISP: GPO-CRM-US-ATT-20160415-09' 
          and s.xprojectenddate < to_date('05/25/2006', 'mm-dd-yyyy') 
          and rownum <= 5000);

如果我能更详细一点,请告诉我。

2 个答案:

答案 0 :(得分:3)

仅当您添加ORDER BY子句时,结果始终采用相同的顺序。否则,结果将以非确定性顺序返回,通常是索引使用或统计信息的结果。

答案 1 :(得分:2)

根据Ask Tom,每次都不会获得相同的行。即使有你的订单也不会。在订购行之前评估rownum。如果每次需要在子查询中排序时都需要相同的行:

select * from 
(
SELECT distinct 
     TO_CHAR(S.DID), 
     TO_CHAR(S.DPERMCONTAINER), 
     S.DBARCODE, 
     S.DMEDIATYPE, 
     S.DDOCTITLE, 
     S.XCOMMENTS, 
     rm.xcategoryid, 
     s.xdivision, 
     S.XPROJECTNAME, 
     S.XLEGACYPROJECTNAME,
     S.XLABELNOTE, 
     S.XPROJECTENDDATE, 
     S.XEVENTDATE 
FROM 
     PRD64_URMSERVER.EXTITEMS S 
     left join prd64_urmserver.extitemsrm rm on s.did = rm.did 
WHERE 
     S.DMEDIATYPE = 'Folder' 
     AND S.DPERMCONTAINER IN
          (SELECT distinct TO_CHAR(S.DID)
          FROM PRD64_URMSERVER.EXTITEMS S 
          left join DIDV V on s.did = v.did 
          left join prd64_urmserver.extitemsrm rm on s.did = rm.did
          WHERE v.did IS NULL
               and RM.xDerivedIsFrozen = '0' 
               AND s.dmediatype = 'Box' 
               and S.XLABELNOTE = 'DISP: GPO-CRM-US-ATT-20160415-09' 
               and s.xprojectenddate < to_date('05/25/2006', 'mm-dd-yyyy') 
 order by ...)
where rownum <= 5000
UNION
select * from 
(SELECT distinct 
     TO_CHAR(S.DID), 
     TO_CHAR(S.DPERMCONTAINER), 
     S.DBARCODE, 
     S.DMEDIATYPE, 
     S.DDOCTITLE, 
     S.XCOMMENTS, 
     rm.xcategoryid, 
     s.xdivision, 
     S.XPROJECTNAME, 
     S.XLEGACYPROJECTNAME,
     S.XLABELNOTE, 
     S.XPROJECTENDDATE, 
     S.XEVENTDATE
FROM 
     PRD64_URMSERVER.EXTITEMS S 
     left join DIDV V on s.did = v.did 
     left join prd64_urmserver.extitemsrm rm on s.did = rm.did
WHERE 
     v.did IS NULL 
          and RM.xDerivedIsFrozen = '0' 
          AND s.dmediatype = 'Box' 
          and S.XLABELNOTE = 'DISP: GPO-CRM-US-ATT-20160415-09' 
          and s.xprojectenddate < to_date('05/25/2006', 'mm-dd-yyyy') 
order by ...)
where rownum <= 5000;