Home Forums SQL Server 2005 Development Bulk insert from file having varying number of columns RE: Bulk insert from file having varying number of columns

  • here's my linked server example for a 64 bit folder full of text files that may help:

    a couple of prerequisites:

    install the AccessDatabaseEngine_x64.exe from microsoft:

    http://www.microsoft.com/en-us/download/details.aspx?id=13255

    make sure you open an Administrative command prompt window, and run it with the c:\Downloads\AccessDatabaseEngine_x64.exe /passive

    command line flag;

    this will force the install of the drivers, even if you have 32 bit office installed;

    otherwise you get some error about 32 bit Office preventing the install.

    After that is installed:

    --Required settings for the provider to work correctly as a linked server

    EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1

    GO

    EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1

    GO

    and then the code for the text based linked server:

    --#################################################################################################

    --Linked server Syntax for Folder Full Of Text Files

    --#################################################################################################

    --add a folder as a linked server to access all .txt and .csv files in the folder

    DECLARE @server sysname,

    @srvproduct nvarchar(256),

    @provider nvarchar(256),

    @datasrc nvarchar(100),

    @location nvarchar(100),

    @provstr nvarchar(100),

    @catalog sysname,

    @sql varchar(1000)

    SET @server = N'TxtSvr'

    SET @srvproduct = N'OLE DB Provider for ACE'

    SET @provider = N'Microsoft.ACE.OLEDB.12.0'

    SET @datasrc = N'C:\Data\'

    SET @provstr ='Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Data\;Extended Properties="text;HDR=YES;FMT=Delimited" '

    set @provstr = 'Text'

    EXEC sp_addlinkedserver @server,@srvproduct,@provider,@datasrc,@provstr,@provstr

    GO

    --===== Set up login mappings.

    EXEC dbo.sp_AddLinkedSrvLogin TxtSvr, FALSE, NULL, Admin, NULL

    GO

    --===== List the tables in the linked server which is really a list of

    -- file names in the directory. Note that the "#" sign in the

    -- Table_Name is where the period in the filename actually goes.

    EXEC dbo.sp_Tables_Ex TxtSvr

    GO

    --===== Query one of the files by using a four-part name.

    SELECT *

    FROM TxtSvr...[LegalName_NickName_List#txt]

    --===== Drop the text server

    EXEC dbo.sp_DropServer 'TxtSvr', 'DropLogins'

    GO

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!