当我使用row_number()时选择不运行更新

时间:2013-12-05 03:25:09

标签: sql-server

我有像这样的选择

的查询更新
update a 
set a.DocRefNumber = b.code + '-' + rtrim(ltrim(ROW_NUMBER() OVER(ORDER BY a.transid DESC)))  
from tblCollateralTransaction a
inner join tblMappingDocumentType b
on b.DocumentDetail = a.documents

当我运行此查询时,我收到如下错误:

  

Msg 4108,Level 15,State 1,Line 2
  窗口函数只能出现在SELECT或ORDER BY子句中。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

不是tesetd,但你可以尝试这样。

With UpdateData As
(
select rtrim(ltrim(ROW_NUMBER() OVER(ORDER BY a.transid DESC)))  as RN
from tblCollateralTransaction a
)    
Update a
set a.DocRefNumber = b.code + '-' + RN
From tblCollateralTransaction a
inner join tblMappingDocumentType b
on b.DocumentDetail = a.documents