SQL Server 2008:删除后重新增加表

本文关键字:增加 新增加 2008 Server 删除 SQL | 更新日期: 2023-09-27 18:10:14

使用SQL Server 2008,使用MS Visual Studio 2012 c# .NET4.5

我上周问了一个类似的问题,用下面的查询解决了:

DECLARE @from int = 9, @to int = 3
UPDATE MainPareto 
SET pareto = m.new_pareto
FROM (
SELECT pKey, -- this is your primary key for the table
new_pareto = row_number() 
over(ORDER BY   CASE WHEN pareto = @from THEN @to ELSE pareto END, 
                CASE WHEN pareto = @from THEN 0 ELSE 1 END)
FROM MainPareto
-- put in any conditions that you want to restrict the scores by.
WHERE PG = @pg AND pareto IS NOT NULL
-- end condtions
) as m
INNER JOIN MainPareto ON MainPareto.pKey = m.pKey
WHERE MainPareto.pareto <> m.new_pareto

正如你所看到的,这个工作得很好,当改变时增加"联盟"。
现在在某些功能用户请求删除和恢复后。

在我的winform上,用户可以右键单击网格并删除"部件"编号。

如果需要,用户也可以恢复。

但是,我需要一个存储过程,它将像这个方法一样在从另一个存储过程中删除后使用网格和更新,我的Winform将对该部分进行排序,但我确实需要一个过程,可以做我当前的删除过程。

希望你们能理解,如果不能问我,我会尽我所能澄清。

SQL Server 2008:删除后重新增加表

我不完全确定这是否是您正在寻找的,但这就是您可以重新播种主键列的方式(如果您的主键也是identity)。注意,截断后的插入不包括列1(主键列)。

select *
into #temp
from MainPareto
truncate table MainPareto
insert into MainPareto (col2, col3, col4) --...
    select col2, col3, col4 --...
    from #temp