日期:2014-05-16  浏览次数:20476 次

把字段为null变成指定连续的数据 论坛问题处理

原论坛贴子地址:点击打开链接

   

 /********************************************************************************           
 *主题: SQl 2008/2005 论坛问题解答           
 *说明:本文是个人学习的一些笔记和个人愚见            
 *      有很多地方你可能觉得有异议,欢迎一起讨论            
            
 *作者:Stephenzhou(阿蒙)            
 *日期: 2012.08.7          
 *Mail:szstephenzhou@163.com            
 *另外:转载请著名出处。            
**********************************************************************************/ 


 
--字段A有空也有带数值的
--如何把空值的部分加上数值
--要求
--1、不能重复
--2、按2000000000001起增加,2000000000002,2000000000003....
--3、效率要高,因为字段A可能有十万行数据。
--4、执行语句遇到死机等特殊情况时,能否对数据库避免产生影响。

--SQL2005 谢谢!

 


--输入测试数据

use DBText
go
if OBJECT_ID('tb_test') is not null
drop table tb_test
go

create table tb_test(id int identity primary key,A varchar(50))
go

declare @i int;
set @i=0;
while @i<300
begin
if right (DATEPART(ms,GETDATE()),1)=0
insert into tb_test values('test'+cast(@i as varchar));
else
 insert into tb_test values(null);
 set @i=@i+1
 end
 go
   --查看测试数据
   select * from tb_test
  
   /*
   id          A
----------- --------------------------------------------------
1           test0
2           test1
3           test2
4           test3
5           test4
6           test5
7           NULL
8           NULL
9           NULL
10          NULL
11          NULL
12          NULL
13          NULL
14          NULL
15          NULL
16          NULL
17          NULL
18          NULL
19          NULL
20          NULL
21          NULL
22          NULL
23          NULL
24          NULL
25          test24
26          test25
27          test26
28          test27
29          test28
30          test29
31          test30
32          test31
33          test32
34          test33
35          test34
36          test35
37   &nb