我全献给你了,来吧!200分,大不了我再注册。(200分)

  • 主题发起人 xxxxxxxxx
  • 开始时间
X

xxxxxxxxx

Unregistered / Unconfirmed
GUEST, unregistred user!
sqlserver中有很多记录,其中一字段内容是整型,是连续的值,但由于
某种原因,有的值不连续,如 1,2,3,4, 6,7, 9 其中少了 5 ,9 以至于
不连续了,请问如何用sql语句将它们找出。
 
1.用 store procedure
2.提供一个思路(可能没用):
将数字反向连接在一起,比如:
12 -> 1221
123 -> 123321
1234 -> 12344321

这样的数有一个规律:
1221 div 11 = 111
123321 div 111 = 1111
12344321 div 1111 = 11111
即结果全由1组成,且位数比原数字位数多1

不连续的数字没有这个规律.

这个方法不实用,还是用存储过程把!


 
根据你数据库的大小,可以采取不同的算法:
1、select * where your_fildname is not in (1...maxvalue)
 
不如另建一个数据库包含所有的min。。max整数,用
select b.yourfield from
b
where b.yourfield not in
(select a.yourfield from a)
或者用
for I:=0 to max-1 do
if not table.locate(...,..,I) then
recordnumber;
我想不会很慢
 
你们说的都与我说的意思不对。
我再阐述一片:
我现在将印有身份证号的表100份发到地区,这100份身份证是连号,但收回时只有
98份,中间少了98,56两号,我还是将这98份表录入库中,但我最后要将这缺的两号查出。用sql怎么查?
 
用个循环判断记录如果一条记录字段值+1 <> 上一条记录字段值则不连续
了, 缺的个数就是两者之差.
大概程序如下:
with query do
begin
sql.text := 'select yourfield from yourtable order by yourfield';
open;
first;
v := fieldbyname('yourfield').asinteger;
next;
while not eof do
begin
if fieldbyname('yourfield').asinteger +1 <> v then
break;
inc(v);
next;
end;
if not eof then showmessage('数据不连续');
close;
end;
 
sunlei说得有道理,我这库可是有1200多万条记录,速度怎么样?有没有更好的办法?
 
除了遍历数据库, 没有好方法 sunlei的方法只能取到字段值为0的记录, 不能得知其他记录该字段值数字是否连续(比如: maxvalue为5, 两个记录值分别
为2, 4, 他们不连续, 但他们都 in (1.. maxvalue).)
 
最好用stored procedure
Create Procedure test2
As
declare @id int
declare @i int

declare card_cursor cursor for
select id from idtable order by id
open card_cursor
select @i=1
If Exists (Select * From sysobjects Where name = 'temp_table' And user_name(uid) = 'dbo')
Drop Table dbo.temp_table
Create Table dbo.temp_table
(
nofound int
)
fetch card_cursor into @id
while (@@fetch_status=0)
begin
while (@i<@id)
begin
insert into temp_table (nofound) values (@i)
select @i=@i+1
end
select @i=@id+1
fetch card_cursor into @id
end
select * from temp_table
Drop Table dbo.temp_table

return
 
var n:word;
begin
query1.sql.add('select num');
query1.sql.add('from temp');
query1.sql.add('group by num');
query1.sql.add('order by num');
query1.open;
query1.first;
n:=1;
while not query1.eof do
begin
while n<query1.fieldbyname('num').asinteger do
begin
listbox1.items.add(inttostr(n));
n:=n+1;
end;
n:=n+1;
query1.next;
end;
query1.close;
end;
 
Another_eYes: 你说得有道理,但1200万条记录,这方法不行吧?
crytown: 你说的也有道理,用stored procedure是要快,但有没有其它更好的?
 
不如这样: 在发送时把需要发送的纪录保存到一个临时表中, 返回时再来比较.
 
当然有更好的办法:
添加和删除记录时多写点代码. 防止出现上述问题. 省得以后麻烦.
或者定时清理数据库(比如每天一次, 找没人用的时候做).
 
Another_eYes: 我添加时可不是按号的顺序录入的,有可能打乱了号,如果是按递增的顺序录入当然好处理。你说的定时清理是一种办法。我会采用的,谢谢。我还想听听不同的方法。
 
顺便问一下,网际精灵原文件在哪里下,我怎么下的都是升级版(它说的是原版,
有一两兆,但我下的都是6k)
 
除了stored procedure其它都会慢一些:-(
另外可以建立两个库, a是正式库, b是号库, 输入号时先判断b中是否>=max(no),
如果>= 那么添加入b>max(no) &amp; &amp;lt;input_no内的数和input_no+1的数, 添加
input_no到a中, 如果&amp;lt;max(no), 那么查找是否有此数, 如果有delete b中的
数, insert 到a中.
当删除a.deleteno时, insert deleteno到b中.
所有这些可以通过stored procedure做, 可以避免冲突:)
这样是最快了.
 
cytown: 接受了,我全献给你了.200 分.后会有期.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
975
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
962
SUNSTONE的Delphi笔记
S
顶部