CREATE TABLE [dbo].[CustomerTrack](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [uniqueidentifier] NULL,
[Channel] [int] NULL,
[OrderIndex] [int] NULL,
[Value] [nvarchar](50) NULL,
CONSTRAINT [PK_CustomerTrack] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
[ID] [int] IDENTITY(1,1) NOT NULL,
[CustomerID] [uniqueidentifier] NULL,
[Channel] [int] NULL,
[OrderIndex] [int] NULL,
[Value] [nvarchar](50) NULL,
CONSTRAINT [PK_CustomerTrack] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
---------------
上表增删改之后,下述脚本更新orderindex
with r_SomeTable
as
(
select *
as
(
select *
, row_number() over(partition by IDCol order by ValueCol) as rnk
from SomeTable
)
update r_SomeTable
set RANKCol = rnk
from SomeTable
)
update r_SomeTable
set RANKCol = rnk