One, requirements
recently customers have a requirement to guide multiple physical tables into EXECL tables, but the problem is that several tables have more than 20W of data, a execl sheet can only import 65,536 of data, using SQL Server's export function directly, The effect cannot be achieved.
Second, the solution to the idea
from the Internet search related solutions, the original is to have a special software to achieve this function, with Sogou did not find, only to find Jiangjian stored procedures, can be common to a table of data into a execl of multiple sheet, Related code you can search the network, here is not posted, but there is a problem is that if a table of data greater than 20W, this stored procedure execution will be wrong, "beyond the resources."
So I thought of a way to solve the problem of exporting to EXECL, the first step is to split a large data table into multiple tables according to the 6W table. The second step is to import data from multiple tables into a EXECL using the export capabilities of SQL Server itself.
Resolution
The first step is to split a physical table into multiple tables using a common stored procedure as follows:
--=========================================== = =
--Author: george
-description: in order to export a large amount of data to execl, split the table into multiple tables according to the size of 6w
--Sample:exec SP _splittable ' tablename '--split into multiple tables according to the original table name, and the split table name suffix passes through 1,2,3
--=============================================
Crteate PROCEDURE [dbo]. [Sp_splitetable]
@tableName varchar
as
BEGIN
declare @rows int, @temptable varchar, @sql varchar (500)
DECLARE @insertSql varchar (1000)
DECLARE @tablenum varchar (100)
Set @temptable = ' temp ' +convert (varchar), newid ())
Set @sql = ' Select Identity (int,1,1) as tempid,* into [' + @temptable + '] from ' + @tableName
EXEC (@sql)
Set @rows =@ @ROWCOUNT
If @rows =0 return
declare @tablecount int, @tablenow int, @recordcount int, @recordnow int
DECLARE @pagesize int
DECLARE @tableindex int
Set @pagesize = 60000--size of each table
Set @tableindex =1
Set @tablecount = CEILING (@rows/cast (@pagesize as float)
Set @tablenow = @tablecount
Set @recordnow = 0
IF @tablecount = 1 return
IF @tablecount > 1 begin
while @tablenow > 1 begin
set @tablenum = @tableName +rtrim (LTrim (str (@tableindex))
if @tablenow = @tablecount Begin--only one table
set @insertSql = ' select Top ' +rtrim ltrim ( STR (@pagesize))) + ' * into ' + @tablenum + ' from [' + @temptable + '] '
exec (@insertSql)
end
&NBSP
if @tablenow < @tablecount begin
set @insertSql = ' select Top ' +rtrim (LTrim (str (@pagesize)) + ' * into ' + @tablenum + ' from [' + @temptable + '] where tempid isn't in (select Top ' +rtrim (LTrim (str (@recordnow-@pagesize)) + ' Tempid from [' + @temptable + ']) '
exec (@insertSql)
end
SET @ Recordnow = @pagesize * (@tablecount-@tablenow +2)
Set @tablenow = @tablenow-1
Set @tableindex = @tableindex +1
end
end
set @sql = ' delete from [' + @temptable + '] '
exec (@sql)
End
The second step is to import data from multiple tables into a EXECL using the export functionality of SQL Server itself