我们的想法是通过选择上次同步的开始时间来检查系统上次同步的时间。问题是保存同步信息的表是新的,当它移动到生产时,表将为空,因此该查询会破坏系统,因为它不会返回任何内容:
select ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and SYNC_ID = (select MAX(SYNC_ID)
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN')
我尝试过使用案例的路线来检查是否存在这样的事情:
select case
when exists (select ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF')
then ST_DT_TM
else ''
end as ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and SYNC_ID = (select MAX(SYNC_ID)
from [XREFDB].[dbo].[SYNCJOBLOG]
where SYS_ID = 'MF' and STAT_CDE = 'FN'))
当表格中存在某些内容时,它会起作用,但如果没有,则会返回一组空行并打破系统。如果我取出外部where子句并且表中没有任何内容,那么它将返回空行,但如果表中存在某些内容,那么它将返回多行而我不能拥有该行。有没有其他方法可以检查表中是否存在任何内容,如果没有,则返回空白行,否则只返回最大SYNC_ID行?
答案 0 :(得分:0)
我会更改您的系统代码以检查返回的记录数,并以这种方式解决您的问题。如果不可能,请尝试:
select ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and SYNC_ID = (select MAX(SYNC_ID)
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN')
union
select ''
where not exists(
select 1
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and SYNC_ID = (select MAX(SYNC_ID)
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'))
答案 1 :(得分:0)
试试这个:
select top 1 ST_DT_TM
from (
select ST_DT_TM,
0 as IsAlternate
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and SYNC_ID = (select MAX(SYNC_ID)
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN')
union all
select 0 as ST_DT_TM, 1 as IsAlternate
) t
order by IsAlternate;
答案 2 :(得分:0)
你不能将外部联接回到同一个表或CTE吗?
with CTE as (
select MAX(SYNC_ID) as sync_id
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN')
select s1.ST_DT_TM
from SYNCJOBLOG s1
left outer join CTE
on s1.sync_id = cte.sync_id
where s1.SYS_ID = 'MF' and s1.STAT_CDE = 'FN'
答案 3 :(得分:0)
似乎有sys-id =' MF'但是st_dt_tm为空,所以这应该解决它:
select case
when exists (select ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF')
then ST_DT_TM
else ''
end as ST_DT_TM
from SYNCJOBLOG
where SYS_ID = 'MF' and STAT_CDE = 'FN'
and isnull(st_dt_tm,'')<>''
and SYNC_ID = (select MAX(SYNC_ID)
from [XREFDB].[dbo].[SYNCJOBLOG]
where SYS_ID = 'MF' and STAT_CDE = 'FN'))
答案 4 :(得分:0)
SELECT ISNULL(ST_DT_TM,'') AS 'ST_DT_TM'
FROM SYNCJOBLOG
基本上,ISNULL
将测试第一个参数,如果它为null,它将返回第二个参数中的任何内容(在本例中为空字符串)。