SQL ORDER BY语句检索较少的记录

时间:2018-09-25 14:32:08

标签: sql db2-400 rpgle

我有一个运行嵌入式SQL的程序,可以从四个连接的文件中检索数据。当我尝试按一个字段(而不是关键字段)对记录进行排序时,检索到的记录数从9,999(子文件的最大值)下降到113。我运行了相同的SQL语句(带有或不带有ORDER BY子句) )在SQL Server上,它们都返回了相同数量的记录(超过9,999条)。其他人是否遇到过此问题,或者我只是想做太多事情?

SELECT  cawlaml0.WMAWID,cawlabl0.WBBLCD,cawlaml0.WMCODE,
        SUBSTRING(cawlael0.WEFKEY,1,32) as WEFKEY32,
        SUBSTRING(cawlael0.WETKEY,1,32) as WETKEY32,
        cawlaml0.WMEFFR,cawlaml0.WMEFTO,cawlaml0.WMEFLG,
        cawlaml0.WMUSED,cawladl0.WDEAAL,cawladl0.WDLORG,
        cawladl0.WDLBAL,cawlael0.WEAWLN,cawladl0.WDAWPR

FROM cawlaml0

INNER JOIN cawlabl0
        on cawlaml0.WMAWID = cawlabl0.WBAWID
INNER JOIN cawlael0
        on cawlaml0.WMAWID = cawlael0.WEAWID
LEFT OUTER JOIN cawladl0
        on cawlaml0.WMAWID = cawladl0.WDAWID


WHERE
         // The '@' fields are user input fields.
        (cawlael0.WEFKEY = :@WEFKEY or :@WEFKEY = ' ') and
        (cawlael0.WETKEY = :@WETKEY or :@WETKEY = ' ') and
        (cawlabl0.WBBLCD = :@WBBLCD or :@WBBLCD = ' ') and
        (cawlaml0.WMCODE = :@WMCODE or :@WMCODE = ' ') and
         cawlael0.WEAWLN = cawladl0.WDAWLN

ORDER BY WEFKEY32;

1 个答案:

答案 0 :(得分:0)

好吧,看来问题不在于SQL本身,而在于一些数据。虽然SQL可以检索所有记录,但我的访存语句是导致错误的原因。我将字段放到屏幕上,但没有意识到有记录(准确地说是第114个记录)的日期(WMEFFR和WMEFTO)超出了有效范围。

获取语句:

EXEC SQL
     FETCH NEXT FROM R1
       INTO :WMAWID,:WBBLCD,:WMCODE,:WEFKEY32,:WETKEY32,
       :WMEFFRA,:WMEFTOA,:WMEFLG,:WMUSED,:WDEAAL,
       :WDLORG,:WDLBAL,:WEAWLN,:WESTAT,:WEAWEX; 

SQL表:

HUFFN Data Table

由于该记录在未排序列表中排得很远,所以直到我尝试进行排序时,我才真正遇到它,因此该记录在我身上。

谢谢大家的回答!