sql server

时间:2015-06-23 10:20:53

标签: sql-server

我需要使用sql server 2008中的order by子句更新表,例如

Update tblTempChek Set TmpCheckIn='15:50:03' 
Where TempID IN (
    Select TempID 
    From tblTempChek 
    Where convert(date, TmpDate)='2015-06-23' AND UserID='1' 
    Order By TempID Desc
)

但这会产生错误

    Msg 1033, Level 15, State 1, Line 3
    The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

2 个答案:

答案 0 :(得分:2)

你需要使用TOP:

    Update tblTempChek Set TmpCheckIn='15:50:03' 
    Where TempID in (
        Select TOP 1 TempID 
        From tblTempChek 
        Where Convert(date, TmpDate)='2015-06-23' AND UserID='1'
        Order By TempID Desc
    )

答案 1 :(得分:0)

替代方案可以是使用max,例如:

CoordinateReferenceSystem crs = null;
try {

    crs = CRS.decode("epsg:26910");
    System.out.println("got "+crs);

} catch (FactoryException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这假设TempID是一些递增值。