从范围中向键添加新行

时间:2014-02-19 10:15:01

标签: sql sql-server key sql-server-2012 range

您好我有一个问题如何使用两列之间的数据扩展我的键列

SELECT tt.Nr_V, 
       tt.journalnumber_end, 
       tt.journalnumerstart 
 FROM transactiontable tt 


 SalesTransaction 

 Nr_V    JournalnumberStart JournalNumberEnd 

 100       1001002             1003000

 101       1003001             1004000 etc..


 OutPutTable 

 Nr_V JournalNumber 

 100    1001002 

 100     1001003

 100     1001004

 etc..

 100     1003000 etc..



 101     1004000

2 个答案:

答案 0 :(得分:0)

你可以使用CTE递归

这是一个例子

create table r ( id int, st int, ed int )
insert into r values( 1, 5,7), (2 ,8,12)

with nums as (
  select r.id , r.st no
  from r 
  union all
  select nums.id,nums.no +1 from nums join r   
  on r.id = nums.id and r.ed > no
)
select * from nums
order by id, no

请参阅http://sqlfiddle.com/#!6/77a21/8

答案 1 :(得分:0)

您好我发现自动生成一个Int从最小开始到最大开始在一个while循环和连接在开始和结束之间 - 为我做了!谢谢你的帮助!我真的很喜欢你们的帮助 - 但我找到了自己的方式。这是美好的一天。