选择子查询选择不起作用

时间:2013-03-24 19:24:21

标签: sql sql-server tsql

我一直在这段代码上收到错误。我没有使用EXISTS.Please帮助。

  

当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。

SQL:

    If ISDATE(@grpsearch) = 1
        SELECT     grp.GroupID,
                   vlanlist.InternetType, 
                   grp.GroupName, cast(grp.StartDateTime as date) as StartDate, 
                   convert(varchar,cast(grp.StartDateTime as time),100) as starttime,
                           (select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12 then datepart(hh,convert(datetime,grp.StartDateTime))-12
                            else case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0 then '12' else datepart(hh,convert(datetime,grp.StartDateTime)) end end as starthour,
                            datepart(mi,convert(datetime,grp.StartDateTime)) as startmin, case when datepart(hh,convert(datetime,grp.StartDateTime)) >= 12 then 'PM' else 'AM' end as startperiod),
                    cast(grp.enddatetime as date) as EndDate, 
                    convert(varchar,cast(grp.EndDateTime as time),100) as EndTime, 

                    grp.UserInitials, 
                    grp.UserComments,
                    roomlist.RoomName, 
                    jacklist.JackNumber


FROM        a_Cisco.dbo.grp_internet as grp left outer join
            dbo.jacklist as jacklist ON grp.intJack = jacklist.intJack left outer join
            dbo.roomlist as roomlist ON grp.intRoom = roomlist.intROom left outer join
            dbo.vlanlist as vlanlist ON grp.VlanID =  vlanlist.VlanID

WHERE  (convert(varchar,cast(grp.StartDateTime as date),100)  = @grpsearch)

1 个答案:

答案 0 :(得分:1)

问题是查询的这一部分:

      (select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
                   then datepart(hh,convert(datetime,grp.StartDateTime))-12
                   else case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0 
                             then '12' else datepart(hh,convert(datetime,grp.StartDateTime))
                        end
       end as starthour,

首先,您根本不需要select。其次,您缺少右括号())。我建议:

      (case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
            then datepart(hh,convert(datetime,grp.StartDateTime))-12
            else (case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0 
                       then '12'
                       else datepart(hh,convert(datetime,grp.StartDateTime))
                  end)
       end) as starthour,