标题:删除后怎样让ID递减
取消只看楼主
yjyuyu96
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2012-2-20
结帖率:85.71%
已结贴  问题点数:20 回复次数:2 
删除后怎样让ID递减
列表中存了信息 删除后怎样让ID递减
ID 是自增的
    ID
如   1   1条信息。。。。
     2   2条信息。。。。
     3   3条信息。。。。
     4   4条信息。。。。
删除  1 后
怎么变为
     1   2条信息。。。。
     2   3条信息。。。。
     3   4条信息。。。。

     
搜索更多相关主题的帖子: 信息 
2012-03-07 14:12
yjyuyu96
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2012-2-20
得分:0 
回复 3楼 tanghuawei
public  class Ac_Delete
    {
        DataTable dt = new DataTable();
        OperateBusi ob = new OperateBusi();

        public DataTable Delete(string ID)
        {
            string sql = "Pay_Delete ";
            sql += "'" + ID + "'";
         
            dt = ob.ExecBusiOrder(sql);

            return dt;
        }
    }
///操作类

create proc Pay_Delete
   
    @ID int
   
    as
    set nocount on
        delete
        from Pay
        where D_ID=@ID
        
    if @@ERROR<>0
        begin
        select -3,'删除表失败'
        return -3
        end
        
        select 999 '操作成功'
        
        go
///存储过程
        string ID = txt_dh.Value;
        DataTable dt = new DataTable();
        dt = ac_delete.Delete(ID);
        if (Message_Alert_web.IsExecSuccess(dt))
        {
            Message_Alert_("删除成功", "../CW/Query.aspx");
        }
///代码

哪里需要改进
2012-03-07 17:17
yjyuyu96
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2012-2-20
得分:0 
以下是引用tanghuawei在2012-3-7 16:13:26的发言:

可以在数据库内加个触发器,然后去更新自增列CREATE TRIGGER DELID ON TABLE  FOR DELETE
            AS
              BEGIN
                      IF @@ROWCOUNT=1
                        UPDATE TABLE SET ID=ID-1 WHERE ID>(SELECT ID FROM DELETED)
                     ELSE
                        BEGIN
                               DECLARE @COUNT INT,@MIN INT ,@II INT
                               SELECT @MIN=1
                               SELECT @COUNT=COUNT(*) FROM TABLE WHERE ID>@MIN
                               SELECT @II=MIN(ID) FROM DELETED
                               WHILE @MIN<=@COUNT
                                      BEGIN
                                                  IF (SELECT COUNT(*) FROM TABLE WHERE ID=@II)=0
                                                     BEGIN
                                                             UPDATE TABLE SET ID=@II WHERE ID=(SELECT MIN(ID) FROM TABLE WHERE ID>@II)
                                                                    SET @II=@II+1
                                                                    SET @MIN=@MIN+1
                                                    END
                                                ELSE
                                                    SET @II=(SELECT MIN(ID) FROM DELETED WHERE ID>@II)
                                      END
                        END
                END当然也可以在删除之后,将表内的自增列删除,然后再新增自增列,可以看看TRUNCATE的用法
触发器不太懂
2012-03-07 17:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362466-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050652 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved