【问题标题】:Only one expression can be specified只能指定一个表达式
【发布时间】:2014-11-19 02:29:53
【问题描述】:

您好,当我尝试使用以下查询获取结果 MSDB 和我的本地 DB 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 清除托盘和手推车 20141119 失败

请帮忙

【问题讨论】:

  • 这是错误的:select (SELECT StoreID FROM dxsh..Store),... 应该类似于 select storeID,... 然后在 from 中有表 Store 并将其与其他表 sysJobssysJobsHistory 连接起来

标签: sql-server


【解决方案1】:

将 dxsh..store 移动到 from 应该可以解决问题,但您需要对其他表使用一些连接。

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'

【讨论】:

    【解决方案2】:

    您只需要在 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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 2018-04-14
      • 2013-12-21
      相关资源
      最近更新 更多