求循环插入的问题
已知起始日期,截至日期   共M天 
 如:起始日期2007-01-03    
          截至日期2007-01-12 
 已知客户 
 a 
 b 
 c 
 d 
 e 
 f 
 共N个    
 M大于N   
 现在要将客户顺序插入日期表形成如下格式 " 
 日期                              客户      第几次循环 
 2007-01-03               a                  1 
 2007-01-04               b                  1 
 2007-01-05               c                  1 
 2007-01-06               d                  1 
 2007-01-07               e                  1 
 2007-01-08               f                  1 
 2007-01-09               a                  2 
 2007-01-10               b                  2 
 2007-01-11               c                  2 
 2007-01-12               d                  2     
------解决方案----------------------原始数据 
 declare @数据表 table (日期 varchar(10), 客户 varchar(20), 循环 int) 
 insert @数据表(日期) 
 select  '2007-01-03 ' union all 
 select  '2007-01-04 ' union all 
 select  '2007-01-05 ' union all 
 select  '2007-01-06 ' union all 
 select  '2007-01-07 ' union all 
 select  '2007-01-08 ' union all 
 select  '2007-01-09 ' union all 
 select  '2007-01-10 ' union all 
 select  '2007-01-11 ' union all 
 select  '2007-01-12 ' 
 select top 1 * from @数据表   
 --原始数据 
 declare @客户表 table (客户ID int identity, 客户 varchar(20)) 
 insert @客户表 
 select  'a ' union all 
 select  'b ' union all 
 select  'c ' union all 
 select  'd ' union all 
 select  'e ' union all 
 select  'f ' 
 select top 1 * from @客户表   
 --结果 
 declare @MinDate datetime, @MaxID int 
 select @MinDate = min(日期) from @数据表 
 select @MaxID = max(客户ID) from @客户表 
 update a set  
   a.客户 = b.客户, 
   a.循环 = datediff(day, @MinDate, 日期) / @MaxID + 1 
 from @数据表 a, @客户表 b 
 where datediff(day, @MinDate, 日期) % @MaxID = b.客户ID - 1 
 select * from @数据表   
 /* 
 1.客户ID是必须而且是有序连续的 
 2.日期也必须是有序连续的 
 3.如果不是有序连续的,添加有序的字段对应,比如自增字段 
 4.如果不想加字段,用游标一行一行Update,慢,不建议 
 */