2013-03-24 40 views
0

我不斷收到此代碼的錯誤。我沒有使用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) 
+0

請發佈完整的查詢,否則不可能說什麼可能是問題。如果我不得不猜測你在WHERE語句中使用了一個IN或NOT IN子句,它有一個返回多個列的子查詢。 – jpw 2013-03-24 19:30:16

+0

對不起,我沒有使用,並沒有認爲這是需要的 – Henry 2013-03-24 19:36:13

回答

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, 
+0

謝謝解決你的建議代碼一​​切似乎好一個小錯誤,是編碼 – Henry 2013-03-24 19:49:14

+0

@Henry醜陋的一部分。 。 。我非常小心縮進我的代碼並使用括號來幫助防止這些類型的錯誤。 – 2013-03-24 19:53:21