简体   繁体   English

使用SSIS导出SQL Server中的多个文本文件

[英]Using SSIS to Export Multiple Text Files in SQL Server

I run a monthly process which requires that I take a table with 4-5 million records and divide it up into .txt files of 240,000 records each. 我每月运行一个流程,该流程要求我建立一个包含4-5百万条记录的表,并将其分成每个包含240,000条记录的.txt文件。 I use this code in SQL Server 2012 to create the paging of the file and then use a SSIS package to run this stored procedure and create the .txt file. 我在SQL Server 2012中使用此代码创建文件分页,然后使用SSIS包运行此存储过程并创建.txt文件。 I change the @PageNumber to 2, recompile the stored procedure, run the SSIS package. 我将@PageNumber更改为2,重新编译存储过程,运行SSIS包。 Increment the @PageNumber to 3 and so on until I run out of records which at 4 million records takes around 20x. 将@PageNumber递增到3,依此类推,直到我用完记录(400万条记录大约需要20倍)。

I was wondering if anyone knows a way to loop through the code and create the multiple .txt files in one pass without having to change the @PageNumber value 20x? 我想知道是否有人知道一种循环遍历代码并一次性创建多个.txt文件的方法,而不必将@PageNumber值更改为20x?

CREATE PROCEDURE [dbo].[p_ExportAllDMCSBorrowersPaging] 
AS
BEGIN
DECLARE @PageNumber AS INT, @RowspPage AS INT
SET @PageNumber = 1
SET @RowspPage = 240000

SELECT  [SSN]
        ,[DOB]
        ,[LastName]
        ,[FirstName]
        ,[CustomerRecordID]
        ,[ADDate]
        ,[MiddleName] FROM (
         SELECT ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER,
                 [SSN]
                ,[DOB]
                ,[LastName]
                ,[FirstName]
                ,[CustomerRecordID]
                ,[ADDate]
                ,[MiddleName]
        FROM [dbo].[All Borrowers 20160919]
               ) AS TBL
        WHERE 
            NUMBER BETWEEN ((@PageNumber - 1) * @RowspPage + 1) AND (@PageNumber * @RowspPage)
        ORDER BY 
            NUMBER

I would use a For Loop for this. 我将为此使用For循环。

Use package variables to hold the number of rows in the table and the current row counter. 使用包变量保存表中的行数和当前行计数器。

First put a script in the loop that changes the connection string of your flat file destination to whatever you want to name it for that iteration (eg: Page1, Page2, etc) 首先,将一个脚本放入循环中,该脚本可将平面文件目标的连接字符串更改为您要为该迭代命名的名称(例如:Page1,Page2等)

Then put a dataflow in the For Loop that uses the counter variable to get 240k rows starting at the value of the variable. 然后将数据流放入For循环中,该循环使用计数器变量从变量的值开始获得24万行。

After each iteration of the loop, increment the counter by 240k, until it exceeds the number of rows in the table, then exit the loop. 循环的每次迭代之后,将计数器增加240k,直到其超过表中的行数,然后退出循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从特定文件夹中选择动态文件,然后使用SSIS导出到SQL Server - How to pick dynamic files from a specific folder and then export to SQL server using SSIS 如何将数据从SQL表导出到SSIS中的多个excel文件? - How to export data from SQL table to multiple excel files in SSIS? 使用 SSIS,如何将 SQL 结果导出到多个 CSV 文件? - With SSIS, how do you export SQL results to multiple CSV files? 将SQL Server视图导出到文本文件中 - Export SQL Server view into text files 使用SSIS将Sharepoint上的xml文件中的数据导出到sql服务器 - export data from a xml file on sharepoint to sql server using ssis 使用SSIS将数据从SQL Server导出到Excel - Export data from sql server to excel using SSIS SSIS Sql Server配置以在多个目录中加载多个平面文件 - SSIS Sql Server Configuration to load multiple flat files in multiple directories 使用SSIS包将多个Excel文件导入SQL Server 2008 R2? - Import multiple Excel files into SQL Server 2008 R2 using SSIS Packages? 使用SSIS将多个文本文件导入到多个表中 - Importing multiple text files into multiple tables using ssis 如何使用 SQL 与查询合并多个 excel 文件而不使用 SSIS? - How to merge multiple excel files using SQL with a query and not using SSIS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM