除非TOP,OFFSET或FOR XML为

时间:2017-08-28 04:28:03

标签: sql sql-server

我的UPDATE声明:

Update S 
set Status = 6 
Where Status = 5 
  and P_ID in (1, 3) 
  and ID in (Select dbo.T.sID 
             from dbo.T  
             Group By s 
             having Max(TDate) <= @t1 and Max(TDate) >= @t2 
                and sid is not null
             order by Max(TDate))

导致显示错误:

  

Msg 1033,Level 15,State 1,Line 6
  除非还指定了TOP,OFFSET或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

1 个答案:

答案 0 :(得分:0)

order by子句对于内联查询毫无意义。只需从查询中删除order by Max(TDate)部分即可。

Update S 
set Status = 6 
Where Status = 5 
  and P_ID in (1, 3) 
  and ID in (Select dbo.T.sID 
             from dbo.T  
             Group By s 
             having Max(TDate) <= @t1 and Max(TDate) >= @t2 
                and sid is not null )