Import without SSIS

  • Hello - I'm working in SQL Server 2012 Express. I have 400+ Excel files in a folder that I want to import data from into one of my tables. I realized tonight I can't save an SSIS package which seems to be the way to go, in combination with a ForEachLoop which apparently I also can't create. Is there any way to do a batch import without these tools, or do I need to just import each file manually using the wizard?

    Thanks in advance,

    Kevin

  • Unfortunately, I think, you will have to import each file manually using the wizard. Sql Server express does not leave you with a lot of options. Somebody correct me if I am wrong.

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

  • the devil is in the details, but if xp_cmdShell is available, you can use THAT via tsql to :

    1. get the list of files,

    2. create a cursor/loop for each of those files

    3.dynamically drop/create a linked server pointing to the [current] excel file.

    4. insert/update into your destination table from the lined server

    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!

  • and the details:

    first, you gotta have the ACE drivers installed, look at this post FIRST:

    http://www.sqlservercentral.com/Forums/FindPost1407497.aspx

    then here's a mockup of the code: there's a couple of issues...I'm ASSUMING every sheet is just named the defaultSheet1$, and have the same structure/number of columns, and no headers/comments before the data starts so tehy can be treated like a table in a linked server.

    --a table to loop thru filenames drop table ALLFILENAMES

    CREATE TABLE ALLFILENAMES(WHICHPATH VARCHAR(255),WHICHFILE varchar(255))

    --some variables

    declare @filename varchar(255),

    @path varchar(255),

    @sql varchar(8000),

    @cmd varchar(1000)

    --get the list of files to process:

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

    SET @path = 'C:\DB\'

    SET @cmd = 'dir ' + @path + '*.txt /b'

    INSERT INTO ALLFILENAMES(WHICHFILE)

    EXEC Master..xp_cmdShell @cmd

    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null

    SET @path = 'C:\DB2\'

    SET @cmd = 'dir ' + @path + '*.txt /b'

    INSERT INTO ALLFILENAMES(WHICHFILE)

    EXEC Master..xp_cmdShell @cmd

    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null

    SET @path = 'C:\DB3\'

    SET @cmd = 'dir ' + @path + '*.txt /b'

    INSERT INTO ALLFILENAMES(WHICHFILE)

    EXEC Master..xp_cmdShell @cmd

    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null

    SET @path = 'C:\DB4\'

    SET @cmd = 'dir ' + @path + '*.txt /b'

    INSERT INTO ALLFILENAMES(WHICHFILE)

    EXEC Master..xp_cmdShell @cmd

    UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null

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

    --cursor loop

    declare c1 cursor for SELECT WHICHPATH + WHICHFILE FROM ALLFILENAMES where WHICHFILE like '%.xls' or WHICHFILE like '%.xlsx'

    open c1

    fetch next from c1 into @path

    While @@fetch_status <> -1

    begin

    --===== Drop the text server

    EXEC dbo.sp_DropServer 'MyExcelACE', 'DropLogins'

    --===== create the Linked Server for the current file

    EXEC sp_addlinkedserver @server = 'MyExcelACE',

    @srvproduct ='',

    @provider = 'Microsoft.ACE.OLEDB.12.0',

    @datasrc = @path,

    @location = NULL,

    @provstr = 'Excel 12.0'

    EXEC dbo.sp_AddLinkedSrvLogin 'MyExcelACE', FALSE, NULL, Admin, NULL

    EXEC sp_tables_ex 'MyExcelACE'

    --INSERT INTO MyTable(ColumnList)

    --SELECT ColumnList FROM MyExcelACE...Sheet1$

    fetch next from c1 into @path

    end

    close c1

    deallocate c1

    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!

  • Thanks Lowell, I think I'm making progress. Using that code, two issues. First, I'm kind of new at this and not sure of the proper syntax in the SELECT statement. My columns did have headers, I deleted them from my test files, but I'm sure the following is incorrect:

    INSERT INTO dbo.myTable(Column_1, Column_2, Column_3)

    SELECT Column A, B, C FROM MyExcelACE...Sheet1$

    What is the proper SELECT syntax here?

    Second, I'm getting a message Invalid Object Name 'MyExcelACE...Sheet1$'.

    Thoughts?

    Thanks,

    Kevin

  • Sorrry kev i might have bben too brief on the explanation.

    The sheet nin the excel needs to have column names in the first row and data in the subsequent rows.

    I meant there cannot be five rows of comments explantions or anything above the real data

    By default an excel has three sheets in it named Sheet1 Sheet2 and Sheet3.

    If they are named differently than the default names you have to use the real sheet name.

    when referenced in sql you must add a dollar sign to thenname.

    Doing ex sp_tables_ex linkedservername retrieves the names of all sheets.

    I think thats the issue with object not found...the sheet has a different name?

    So if the sheet was renamed to "Expenses" it would be select * from MyExcelAce...Expenses$

    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!

  • Lowell - that was part of it. The sheets in question were named 'table'. So I fixed that.

    What I've got now is basically:

    INSERT INTO dbo.existingTable(ColumnName1, ColumnName2, ColumnName3)

    SELECT * FROM MyExcelACE...table$

    When I try to execute the query I get the following:

    Msg 7202, Level 11, State 2, Line 35

    Could not find server 'MyExcelACE' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

    Any thoughts?

  • Oops, hold on. Line 35 of that error references "begin" in the following:

    While @@fetch_status <> -1

    begin

    --===== Drop the text server

    EXEC dbo.sp_DropServer 'MyExcelACE', 'DropLogins'

    Does that make any more sense?

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply