如果您以后需要继续更新此内容,您可以试试这个。这是几个步骤,但会修复它并设置它以供将来使用。 (可能需要检查我的语法 - 我现在更多地使用 ORacle,所以我可能混淆了一些东西 - 但逻辑应该有效。)
首先,创建一个表来包含每个序列的当前计数器级别:
Create newTable (counter int, sequence varchar)
然后,用这样的数据填充它:
插入新表
(选择不同的0作为计数器,序列
从表)
这会将每个序列号放入表中一次,每个序列号的计数器将设置为 0。
然后,使用两个更新语句和一些额外的逻辑创建一个更新过程:
Create procedere counterUpdater(@sequence varchar) as
Declare l_counter as int;
select l_counter = counter
from newTable
where sequence = @sequence
--assuming you have a primary key in the table.
Declare @id int;
Select top 1 @id = id from table
where sequence = @sequence
and counter is null;
--update the table needing edits.
update table
set counter = l_counter + 1
where id = @id
--update the new table so you can keep track of which
--counter you are on
update newTable
set counter = l_counter + 1
where id = @id
然后运行一个 proc 来为表中的每条记录执行这个 proc。
现在您应该有一个“newTable”,其中填充了表中每条记录当前使用的计数器。设置您的插入过程,以便在任何时候创建新记录时,如果它是尚未在 newTable 中的序列,则将其添加为 1,并将计数 1 放入主表中。如果序列确实存在,请使用上述逻辑(增加已使用“newTable”的计数,并将该计数作为计数器值放在 newTable 和 mainTable 中。
基本上,此方法决定使用内存来代替查询现有表。如果您有一个包含大量重复序列号的大表,这将变得最有益。如果您的序列号只出现两到三次,您可能希望在更新然后插入时进行查询:
首先,更新:
--找出计数器值
声明 l_counter int
选择 l_counter = 最大值(计数器)
从表中序列 = @sequence
update table
set counter = l_counter + 1
where id = (select top 1 id from table where sequence = @sequence
and counter is null)
然后为每条记录运行它。
那么,在插入新记录时:
Declare l_counter int
select l_counter = max(counter)
from table where sequence = @sequence
IsNull(l_counter, 0)
Insert into table
(counter, sequence) values (l_counter + 1, @sequence)
再次,我很肯定我在这里混合并匹配了我的语法,但这些概念应该有效。当然,这是一种“一次一个”的方法,而不是基于集合的方法,所以它可能效率有点低,但它会起作用。