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

大表数据按星期导入小表(详见说明,分不够可以开贴加,望高手不吝赐教)

小弟初次使用SQLSERVER数据库   也是初次来此版块求救   还望大家多多帮忙,感激不尽!

说明:某张大表里有一时间的字段,要求是   将所有数据按星期导入不同的子表中!
            也就是说每个星期就是一张表(按数据的时间动态生成的),所有这个星期的数据插入此表中!
实现方法是SQLSERVER结合JAVA代码来实现~数据量比较大,近一千万条数据!!

希望大家给些思路,最好能给出些代码实现,再次谢过了!!

------解决方案--------------------
1。动态生成各个星期表
你可以用datepart(year,时间字段),datepart(week,时间字段)来得到要生成哪些表
并动态生成。

2。分别写入数据。


当然,如果可以在各个动态表上以时间字段值建个主建的话,可以
考虑用分区视图来进行写入


------解决方案--------------------
我认为前台用java 控制时间段(或周)发出建表指令,可能比用job可行
数据量大,应该有聚集索引,时间字段应该也建立了非聚集索引
sqlserver中select * into 新表 from 大表 where 的形式就可自动建立新表,你的要求是将它写成动态sql以控制表名(自动 exec( 'select * into '+(getdate())+ 'from ')),控制where中的条件,这些放在存储过程中或者直接在前台调sql也行
------解决方案--------------------
drop table tbtest
go
create table tbtest(dt datetime)
insert into tbtest
select '2003-05-15 10:01:04 '
union all select '2003-05-29 15:30:13 '
union all select '2003-05-29 16:02:55 '
union all select '2003-05-30 15:46:19 '
union all select '2003-05-30 16:01:23 '
union all select '2003-05-30 16:30:35 '
union all select '2003-06-02 16:40:44 '
union all select '2003-06-02 16:52:57 '
union all select '2003-06-17 09:39:57 '
union all select '2003-06-17 09:40:37 '
union all select '2003-06-17 09:40:55 '
union all select '2003-06-26 09:46:06 '
union all select '2003-06-26 09:46:33 '
union all select '2003-07-11 10:09:13 '
union all select '2003-11-20 17:16:57 '
union all select '2003-12-30 17:50:12 '
union all select '2004-02-19 09:56:12 '
union all select '2004-02-24 14:56:42 '
union all select '2004-02-25 09:49:45 '
union all select '2004-06-10 15:29:14 '
union all select '2004-07-30 09:38:01 '
union all select '2004-07-30 10:18:09 '
union all select '2005-01-25 14:47:27 '
union all select '2005-05-27 15:01:39 '
union all select '2005-11-01 09:50:01 '
union all select '2005-11-01 09:50:22 '
union all select '2006-08-14 09:47:36 '
union all select '2006-08-21 11:07:57 '
union all select '2006-10-27 15:25:47 '
union all select '2006-10-27 15:26:21 '
union all select '2007-01-19 14:52:53 '
union all select '2007-03-28 16:11:30 '

declare @week int
declare @sql varchar(8000)
declare cur_tmp cursor for
select datepart(week,dt) from tbtest group by datepart(week,dt)
open cur_tmp
fetch next from cur_tmp into @week
while @@fetch_status=0
begin
set @sql= 'select dt into table_week '+rtrim(@week)+ ' from tbtest where datepart(week,dt)= '+rtrim(@week)
exec(@sql)
fetch next from cur_tmp into @week
end
close cur_tmp
deallocate cur_tmp

select name from sysobjects where name like 'table_week% '
/*
name
---------------
table_week13
table_week20
table_week22
table_week23
table_week24
table_week25
table_week26
table_week28
table_week3
table_week31
table_week33
table_week34
table_week43
table_week45
table_week47
table_week5
table_week53
table_week8
table_week9

(所影响的行数为 19 行)
*/