日期:2014-05-18  浏览次数:20577 次

如何写这句SQL语句,请教高人???

如图所示的数据库,左边最左边ID这列本是自增列,从1004开始下面的数字都是9999,nh字段从这列开始都是2008,但是我想重新排列ID字段接着从1004往下排,替换掉9999,而其他列保持不变,应该如何写这句SQL语句,谢~

------解决方案--------------------
SET IDENTITY_INSERT tablename ON
GO

------解决方案--------------------
SQL code
SELECT   IDENTITY   (int,   1005,   1)   AS   ID,   nh,   swh,   wh,   zrz,   tm,   rq,   ztc,   dah,   zj_id,   mj,   swrq,   
            files,   bz,   ys,   pt 
INTO   #temp 
WHERE   nh   =   '2008'    [color=#FF0000]---这句要写在下面的那个from语句下面[/color]
FROM   wj1 
                    DELETE   FROM   wj1 
                WHERE   ID   =   '9999' 
                                    SELECT   * 
                                INTO   wj1 
                                FROM   #temp 
                                                    SELECT   * 
                                                FROM   wj1

--------------------试试下面的-------------------------

SELECT   IDENTITY   (int,   1005,   1)   AS   ID,   nh,   swh,   wh,   zrz,   tm,   rq,   ztc,   dah,   zj_id,   mj,   swrq,   
            files,   bz,   ys,   pt 
INTO   #temp 
FROM   wj1 
WHERE   nh   =   '2008'    

DELETE   FROM   wj1 
                WHERE   ID   =   '9999' 
                                    SELECT   * 
                                INTO   wj1 
                                FROM   #temp 
                                                    SELECT   * 
                                                FROM   wj1

------解决方案--------------------
是这样吗
SQL code

declare @t table(id int )
insert into @t select 1
insert into @t select 2
insert into @t select 3
insert into @t select 2008

insert into @t select 2009
insert into @t select 2010
insert into @t select 2011



declare @i int
set @i =3
update @t 
set id = @i ,@i=@i+1
where id>2007

select * from @t
-----------
1
2
3
4
5
6
7

(7 row(s) affected)