| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 586 人关注过本帖
标题:删除后怎样让ID递减
只看楼主 加入收藏
yjyuyu96
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2012-2-20
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:4 
删除后怎样让ID递减
列表中存了信息 删除后怎样让ID递减
ID 是自增的
    ID
   1   1条信息。。。。
     2   2条信息。。。。
     3   3条信息。。。。
     4   4条信息。。。。
删除  1 后
怎么变为
     1   2条信息。。。。
     2   3条信息。。。。
     3   4条信息。。。。

     
搜索更多相关主题的帖子: 信息 
2012-03-07 14:12
w024680
Rank: 4
等 级:业余侠客
帖 子:53
专家分:259
注 册:2010-7-27
收藏
得分:8 
给你个思路
   string str1 = "1条信息";
   string str2 = int.Parse(str1.Substring(0, 1)) - 1 + str1.Substring(1, str1.Length - 1);

ら.花舞花落泪 | ら.花哭花瓣飞
2012-03-07 15:35
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:12 
可以在数据库内加个触发器,然后去更新自增列
程序代码:
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的用法


[ 本帖最后由 tanghuawei 于 2012-3-7 16:16 编辑 ]

汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2012-03-07 16:13
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
快速回复:删除后怎样让ID递减
数据加载中...
 
   



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

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