当前位置: 代码迷 >> Sql Server >> 怎么写循环让内码自动加1
  详细解决方案

怎么写循环让内码自动加1

热度:183   发布时间:2016-04-24 08:44:57.0
如何写循环让内码自动加1
我有两张表A、B

想把 A里某段日期的记录插入到B,但是要找到B的最大内码ID,比如找到的是88,那么
我插进去的那些记录的ID第一条要改成89,后面的的依次是90、91、92。。。,然后再插入。
不知道这个循环怎么写,A和B本身就有内码ID

select * into #A from  A
where fdate>='2015-9-1' and fdate<'2015-10-1'


然后再去修改临时表 ?
------解决思路----------------------
with A as
(
select CONVERT(datetime, '2015/11/01') dt union all
select CONVERT(datetime, '2015/11/02') dt union all
select CONVERT(datetime, '2015/11/03') dt union all
select CONVERT(datetime, '2015/11/04') dt union all
select CONVERT(datetime, '2015/11/05') dt union all
select CONVERT(datetime, '2015/11/06') dt union all
select CONVERT(datetime, '2015/11/07') dt union all
select CONVERT(datetime, '2015/11/08') dt union all
select CONVERT(datetime, '2015/11/09') dt union all
select CONVERT(datetime, '2015/11/10') dt union all
select CONVERT(datetime, '2015/11/11') dt union all
select CONVERT(datetime, '2015/11/12') dt union all
select CONVERT(datetime, '2015/11/13') dt
)
, B as
(
select 85 id union all
select 86 id union all
select 87 id union all
select 88 id 
)
select dt
, (ROW_NUMBER() over(order by dt))+(select MAX(id) from b) as rn 
from A 
where dt between '2015/11/05' and '2015/11/10'
  相关解决方案