日期:2014-05-19  浏览次数:20527 次

很急,高人帮忙(插入数据后更新)
有一数据库   (SQL   SERVER   2000)
非常频繁的插入数据
字段:序号     时间   (其它不列举)
插入的时候,需要按时间来排序,因为时间在我的系统里是个变量,说不准是个什么时间,但必须按升序来排列,并且,前面的序号要根据实际位置也要跟着变,数据量不小,我用我的办法效率实在是太低下,我想问的是有没有一个高效的办法来解决这个问题?

------解决方案--------------------
好像思路有点问题吧...
------解决方案--------------------
--参考以下

create table 表 (序号 int,时间 datetime)

----建个触发器
create trigger tr_updateID
on 表
for insert
as
if object_id( '表bak ') is not null
drop table 表bak
select 序号=identity(int,1,1) , 时间 into 表bak from 表 order by 时间



insert into 表 (时间)
select getdate()
select * from 表bak

/*
序号 时间
----------------------------------------------------
1 2007-07-27 12:01:35.497
2 2007-07-27 12:01:41.000
3 2007-07-27 12:05:22.840
4 2007-07-27 12:06:19.700
5 2007-07-27 12:06:31.200
6 2007-07-27 12:06:45.140
7 2007-07-27 12:07:07.170
8 2007-07-27 12:07:16.950
9 2007-07-27 12:07:17.937
10 2007-07-27 12:07:18.810
11 2007-07-27 12:07:19.890
12 2007-07-27 12:07:20.700
13 2007-07-27 12:08:22.340
14 2007-07-27 12:08:23.500
15 2007-07-27 12:08:26.750
*/
------解决方案--------------------
--你这么干就可以了,在要插入的表中设序号col为自增就可用来,干嘛还要现处理?!
create table t1(col int identity,col2 datetime)
create table t2(col int,col2 datetime)
insert into t2 select 2, '2007-07-07 '
insert into t2 select 1, '2007-07-17 '
insert into t2 select 3, '2007-07-27 '

insert into T1
select col2 from t2

select * from t1
------解决方案--------------------
插入的时候,需要按时间来排序?

有这个必要吗?插入进数据库按主键位置放就好了

你查询的时候按时间order by不就够了,或者在时间这个字段建一个升序的索引


------解决方案--------------------
在时间 上建立聚集索引
create clustered index IN_时间 on table1 (时间)
序號非主鍵
insert table1 values (1,@date)
在table1 上建trigger
create trigger tr_test on table1 after insert
as
update table1 set 序號 = (select top 1 a.序號 from table1 a where a.时间 > inserted.时间 ) where table1.key = inserted.key
update table1 set 序號 = 序號 + 1 where 时间 > inserted.時間

如果序号為主鍵
建議另外 加一個字段 num 來記錄行位置
也用上面的trigger 更新num字段即可
------解决方案--------------------
a是表名,inserted是你刚刚插入的纪录