• josetur12 (3/11/2011)


    Hi, I have a flat file with over 200 fixed length columns. I want to use SSIS to load the data into SQL Server.

    Is there a way to easily load them into SQL Server without parsing each column?

    I will appreciate any help. Thanks,

    You could use T-SQL. Start with a table that contains each column (order, name, starting position, size). Then, utilizing BULK INSERT, load the data into a #temp table. Run a select to get the data.

    This is a start... just expand upon it.

    DECLARE @Columns TABLE (ColumnOrder INT, ColumnName varchar(20), StartingPosition smallint, ColumnSize tinyint);

    INSERT INTO @Columns

    SELECT 1, 'Column1', 1, 10 UNION ALL

    SELECT 2, 'Column2', 11, 5 UNION ALL

    SELECT 3, 'Column3', 16, 12;

    DECLARE @TestData TABLE (RowID INT IDENTITY, RowData varchar(100));

    INSERT INTO @TestData

    SELECT 'y4gfbnenbeofnoihfgjq3fgwqgnq3ovqvqno' UNION ALL

    SELECT 'qgbughqofniufgonqgoinvoqfqnbqnqn3qfo';

    WITH cte AS

    (

    SELECT t.RowID, dt.*

    FROM @TestData t

    CROSS APPLY (SELECT ColumnOrder,

    ColumnName,

    ColumnValue = SubString(t.RowData, StartingPosition, ColumnSize)

    FROM @Columns) dt

    )

    SELECT RowID,

    Column1 = MAX(CASE WHEN ColumnName = 'Column1' THEN ColumnValue ELSE NULL END),

    Column2 = MAX(CASE WHEN ColumnName = 'Column2' THEN ColumnValue ELSE NULL END),

    Column3 = MAX(CASE WHEN ColumnName = 'Column3' THEN ColumnValue ELSE NULL END)

    FROM CTE

    GROUP BY RowID

    ORDER BY RowID;

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2