只能指定一个表达式

时间:2014-11-19 02:29:53

标签: sql-server

您好我正在尝试使用以下查询获取结果MSDB和我的本地数据库DXSH。 我收到错误消息

" Msg 116,Level 16,State 1,Line 12 当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。"

select (SELECT StoreID FROM dxsh..Store),(select distinct j.Name as "Job Name",h.run_date as LastStatusDate,case h.run_status 
when 0 then 'Failed' 
when 1 then 'Successful' 
when 3 then 'Cancelled' 
when 4 then 'Executing' 
end as JobStatus
from msdb.dbo.sysJobHistory h, msdb.dbo.sysJobs j
where j.job_id = h.job_id and h.run_date = (select max(hi.run_date) from msdb.dbo.sysJobHistory hi where h.job_id = hi.job_id)
and h.run_status = 0
and J.name = 'Clear Trays and Trolleys')

结果预期考虑Storeid:111

111 Clear Trays and Trolleys 20141119失败

请帮忙

2 个答案:

答案 0 :(得分:0)

将dxsh..store移动到应该可以修复问题,但是你需要与其他表一起使用一些连接。

select distinct S.StoreID, j.Name as "Job Name",h.run_date as LastStatusDate,case h.run_status 
when 0 then 'Failed' 
when 1 then 'Successful' 
when 3 then 'Cancelled' 
when 4 then 'Executing' 
end as JobStatus
from msdb.dbo.sysJobHistory h, msdb.dbo.sysJobs j, dxsh..Store S
where j.job_id = h.job_id and h.run_date = (select max(hi.run_date) from msdb.dbo.sysJobHistory hi where h.job_id = hi.job_id)
and h.run_status = 0
and J.name = 'Clear Trays and Trolleys'

答案 1 :(得分:0)

如果您不使用exists,则需要在Select子句中的第二个嵌套查询中仅选择一列。如果表dxsh..store与dbo.sysJobHistory和dbo.sysJobs有关系,则可以删除第一个嵌套的Select并直接选择该列。然后可以删除第二个嵌套的Select子句。

 select distinct str.StoreID, j.Name as "Job Name",h.run_date as LastStatusDate,case h.run_status 
    when 0 then 'Failed' 
    when 1 then 'Successful' 
    when 3 then 'Cancelled' 
    when 4 then 'Executing' 
    end as JobStatus
    from msdb.dbo.sysJobHistory h, msdb.dbo.sysJobs j, dxsh..Store str
    where j.job_id = h.job_id 
    and str.StorID = j.StoreID
    h.run_date = (select max(hi.run_date) from msdb.dbo.sysJobHistory hi where h.job_id = hi.job_id)
    and h.run_status = 0
    and J.name = 'Clear Trays and Trolleys'