事情是这样的,今天查询前端数据显示有两条数据竟然为同一个入库单号(正常来说应该是唯一的),然后开始排查,使用嵌套查询时查出来死活有两条,但是不用嵌套查询时只有1条,最后顺藤摸瓜定位到isnull上,注释之后只查出来一条,取消注释后就有两条。
语法
ISNULL ( check_expression , replacement_value )
说明
如果 check_expression 为 NULL,则返回它的值;否则,将返回后者。
--两条
SELECT *FROM ( SELECT tb2.pig_form_id AS FFactory, tb2.MID AS MaterialCode, tb2.NUM, tb2.SWEIGHT AS Weight, 'I' AS TypeName, tb2.INTYPE AS TypeNo, tb2.INTYPENAME AS TypeNameDetail, tb2.INTIME as OutAndPutDate,tb2.LTIME,tb2.VENDORNAME, tb2.FErtificateDate,tb2.FSlaughterDate,tb2.FMaker, ISNULL(tb2.INBILLNO,tb2.Batchno) INBILLNO FROM dbo.T_asrs_inbill_feedback AS tb2 ) AS A where A.INBILLNO='CPCK--21110701276' --注意:这里的INBILLNO是tb2.INBILLNO和tb2.Batchno的集合,即(WHERE INBILLNO= 'CPCK--21110701276' OR Batchno = 'CPCK--21110701276')
--一条 select top 100*from T_asrs_inbill_feedback where INBILLNO='CPCK--21110701276' --注意:这里的INBILLNO只是INBILLNO
因为业务需要,我的INBILLNO和Batchno是一样的,为了防止其中一个为空而导致整条数据取不到,所以我用了isnull做防范。
但是问题就出在这里,我这样写的语法就相当于:
SELECT tb2.pig_form_id AS FFactory, tb2.MID AS MaterialCode, tb2.NUM, tb2.SWEIGHT AS Weight, 'I' AS TypeName, tb2.INTYPE AS TypeNo, tb2.INTYPENAME AS TypeNameDetail, tb2.INTIME AS OutAndPutDate, tb2.LTIME, tb2.VENDORNAME, tb2.FErtificateDate, tb2.FSlaughterDate, tb2.FMaker, tb2.INBILLNO, tb2.Batchno FROM dbo.T_asrs_inbill_feedback AS tb2 WHERE INBILLNO= 'CPCK--21110701276' OR Batchno = 'CPCK--21110701276'
用上述查询后确定到问题:
问题定位:
数据库本身存在两条一模一样的单号,是因为我的查询条件不一样导致!