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

sqlserver中查出表中的数据同时按某个字段分类插入多个表中

数据库中有表(anst)部分字段如下,要求把“For_item”字段中的数据分别导出为不同的EXCEL表中。如“缭ET”到一个表中,“2829”到另一个表,“00005”到一个表,表中有40万数据,大概有5000个分类,也就是说要导出到5000个EXCEL表中,请教各位大哥?
表中部分字段:
类别 For_item BY_ITEM
注塑色粉 缭ET
注塑色粉 缭ET
注塑色粉 缭ET
注塑色粉 缭ET
车缝唛头纸 2829 2829-83
车缝唛头纸 2829 2829-83
裁片件 00000
裁片件 00000
裁片件 00000
裁片件 00005
裁片件 00005
裁片件 00005
裁片件 00005
说明书 00001
说明书 00001


我的做法如下:
select   distinct   For_item   into   testa   from   anst   order   by   For_item

declare   @i   int    
declare   @item   nvarchar(255)
set   @i=1  
set   @item= 'item1 '
while   @i <4192
begin
'select   *   into '+@item+   'from   anst   where   For_item   in   (select   For_item   from   testa   where   id=@i) '
set   @i=@i+1
set   @item= 'item '+   @i
end

但是数据库始终提示
Msg   102,   Level   15,   State   1,   Line   7
'select   *   into '   附近有语法错误。
Msg   102,   Level   15,   State   1,   Line   10
'end '   附近有语法错误。

------解决方案--------------------
少了exec了吧