0

我不断收到此代码的错误。我没有使用 EXISTS。请帮助。

当不使用 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)
4

1 回答 1

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,
于 2013-03-24T19:41:25.583 回答