好幾天沒來了.把下面的在你的Query Analyzer中執行,你就明白該怎麼做了:
create table why
(
id1 int identity(1,1),
name1 varchar(100)
);
go
insert into why values ('ning');
insert into why values ('parker');
insert into why values ('wzp');
insert into why values ('huiler');
insert into why values ('lxw');
go
select * from why;
go
delete from why where id1 = 3;
go
select * from why;
go
select * into #temp1 from why;
go
truncate table why;
go
insert into why select name1 from #temp1;
go
select * from why;
go
create table why
(
id1 int identity(1,1),
name1 varchar(100)
);
go
insert into why values ('ning');
insert into why values ('parker');
insert into why values ('wzp');
insert into why values ('huiler');
insert into why values ('lxw');
go
select * from why;
go
delete from why where id1 = 3;
go
select * from why;
go
select * into #temp1 from why;
go
truncate table why;
go
insert into why select name1 from #temp1;
go
select * from why;
go
解决方案 »
- 请问如何从一张表中同时读出两条不同的记录
- 问下几个数据的插入问题
- 如何让SQL Server 2008 R2 开发者版与Visual Studio 2010 旗舰版连接上?
- 开机后没做任何操作,sqlserver进程就长期站用cpu时间!导致就像死机了一样!是中毒了吗?怎么解决?
- 为什么sqlserveragent无法启动?
- sql server中怎麼在BOM取得一個產品所有層次的物料
- 有一个字段,是varchar型的,里面比如说一段话
- 求助:在bat文件中调用osql执行TRUCATE操作,返回-100的问题
- 在SQL数据库中的Text类型中无法存储32k的文本,请帮忙看一下,有什么好的方法吗???
- Insert 数据报错,错误信息identify off。
- VFP的支持者们,让我们一同来向CSDN抗议吧!
- 在用ADOquery控件参数化查询SQL SERVER数据库时,怎样给参数赋值?
假設刪除的紀錄為第3條.delete from why where id1 = 3;
select * into #temp1 from why where id1 > 3;
delete from why where id1 > 3;
dbcc checkident(why,reseed,2);
insert into why select name1 from #temp1;
如果考慮多用戶,那麼對表加鎖.
操作完就釋放鎖.
在删除了后有什么方法可以设置IDENTITY的增长基数的?因为如果用上面的办法的话,再INSERT数据进去就会出现断层,有什么方法可以解决吗,还有就是如何在触发器中进行加锁?如何写?
--強制重置表why的identity字段的當前值為2,如果你建表時設的增長幅度為1的話,那麼下一條插入的輸入為3
--強制重置表why的identity字段的當前值為2,如果你建表時設的增長幅度為1的話,那麼下一條插入的數值為3
比如你的数据库中有1、2、3。500条记录,
删除第三条后,用
update why set 序号 =序号-1 where 序号>3
就可以了,不算太复杂。
其实用我的办法也有不好的地方,因为要用update ,涉及的记录很多,又都是要改关键字段,一般都需要锁定这些记录的,锁定记录也会增加服务器的开销,经常这样做,我怕服务器会吃不消的。所以想问一下hackerning(勤学),不知道为什么一定要这样处理ID号?能用其它方法避免这个问题最好。
http://www.csdn.net/expert/topic/320/320829.shtm
如果解决了一定重谢:)各位兄弟,谢谢了!!!
能留下你的QQ号码或者其他什么的吗?一起交流一下?QQ:764332
当然了,前提是不用identity,或者执行上一句时,首先关掉identity,完了,在开启
where 序号=(select min(T.序号) from Table T where (T.序号 < 序号))
子查询虽然效率低一些,但总比一个一个更新要有效。
实在要连号,不如写一个程序,再每周或每天定期执行。