[英]update column with incremented value based on each where clause
I like to update each row with incremented value based on the where condition. 我喜欢根据where条件用递增的值更新每一行。
exp: select ClientId from [Document] Where ClientId 10 Shows 3 result then I need to update each matterID with 1 to 3. exp:从[文档]中选择ClientId,其中ClientId 10显示3个结果,那么我需要将每个issueID更新为1到3。
1 clientId has one or many MatterID so Need to update each matterID based on number of matterID for each client. 1个clientId具有一个或多个MatterID,因此需要根据每个客户端的subjectID数量更新每个subjectID。 IF client 10 holds 3 matterID then matterIDs would be 1 - 2 - 3
如果客户端10拥有3个IssueID,则subjectID将为1-2-3
Not: It is not updating correctly I am getting matterIDs for this example ramdom number not 1-39-39. 否:更新不正确,此示例随机编号不是1-39-39,我正在获取问题ID。
Stored Procedure
储存程序
Alter PROCEDURE dbo.CustomIdProcedure
@clientId varchar(255) = ''
AS
Begin
update e set MatterId = rn from (select * ,rn=row_number() over (order by id) from [Document] where ClientId = @clientId ) e
END
** **
To run the Procedure
运行过程
** exec CustomIdProcedure (select ClientId from [Document] Where ClientId <>'') ** exec CustomIdProcedure(从[Document]中选择ClientId,其中ClientId <>'')
You can use a subquery or updatable CTE for this: 您可以为此使用子查询或可更新的CTE:
with toupdate as (
select d.*,
row_number() over (partition by clientid order by id) as rn
from [Document] d
where ClientId = @clientid
)
update toupdate
set MatterId = rn;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.