select job_id from sysjobhistory where 
message like '%The job failed%' 
这句话查询结果2,3,4,5 select job_id from sysjobactivity where stop_execution_date between dateadd(day,-1,getdate()) and getdate() 
这句话查询结果1,2,3 若得出查询结果取得公共部分,也就是2,3  SQL语句怎么写? 谢谢。

解决方案 »

  1.   

    也就是怎么把这两句SQL语句合并起来,查询出结果为2,3
      

  2.   

    select job_id from sysjobhistory where 
    message like '%The job failed%' 
    intersect--交集
    select job_id from sysjobactivity where stop_execution_date between dateadd(day,-1,getdate()) and getdate() 
      

  3.   

    select job_id from sysjobhistory where 
    message like '%The job failed%' 
    union
    select job_id from sysjobactivity where stop_execution_date between dateadd(day,-1,getdate()) and getdate() 
      

  4.   

    --这样?select job_id from sysjobhistory where 
    message like '%The job failed%' and job_id in (
      select job_id from sysjobactivity where stop_execution_date between dateadd(day,-1,getdate()) and getdate() 
     )
      

  5.   

    5楼的我曾经测试过,不可以的,看一下2楼的吧。 这个SQL语句看似不难,所以造成了我眼高手低。 谢谢各位!