读书人

sqlserver中查出表中的数据同时按某个

发布时间: 2012-02-16 21:30:36 作者: rapoo

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

数据库中有表(anst)部分字段如下,要求把“For_item”字段中的数据分别导出为不同的EXCEL表中。如“缭ET”到一个表中,“2829”到另一个表,“00005”到一个表,表中有40万数据,大概有5000个分类,也就是说要导出到5000个EXCEL表中,请教各位大哥?
表中部分字段:
类别For_itemBY_ITEM
注塑色粉缭ET
注塑色粉缭ET
注塑色粉缭ET
注塑色粉缭ET
车缝唛头纸28292829-83
车缝唛头纸28292829-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了吧

读书人网 >SQL Server

热点推荐