如何使用具有唯一 ID 的数百条记录更新现有 table

How do I update an existing table with a few hundred records with a unique ID

我在使用唯一 ID

更新现有 table(有几百条记录)时遇到问题

table 被称为 BHIVE.ProjectDataGroupDetail 并且有几百条记录。 我要更新的字段称为 ProjectDataGroupDetailID。我还更新了值为“1”的第二个字段。

我的存储过程是:

ALTER PROCEDURE [BHIVE].[PopulateProjectDataGroupDetail]  
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

UPDATE BHIVE.ProjectDataGroupDetail
SET ProjectDataGroupDetailID = Row_Number() OVER (ORDER BY SampleProjects), ProjectDataGroup = 1 
    
End

我遇到错误:

Windowed functions can only appear in the SELECT or ORDER BY clauses

你能试试吗

UPDATE
BHIVE.ProjectDataGroupDetail

SET ProjectDataGroupDetailID = RowNumber
FROM
(
SELECT
Row_Number() OVER (ORDER BY SampleProjects) Rownumber
FROM
BHIVE.ProjectDataGroupDetail 
) source

您可以为此使用 MERGE statement

db<>fiddle

merge into ProjectDataGroupDetail as t
using (
  select id, row_number() over(partition by some_grouping_col order by id) as rn
  from ProjectDataGroupDetail
) as s
  on t.id = s.id
when matched then update
  set
    ProjectDataGroupDetailID = s.rn,
    ProjectDataGroup = 1
;