Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Import without SSIS Expand / Collapse
Author
Message
Posted Thursday, May 2, 2013 9:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 1:15 PM
Points: 4, Visits: 5
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
Post #1449034
Posted Friday, May 3, 2013 3:57 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 9:58 PM
Points: 1,207, Visits: 928
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.

Manie 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)
Post #1449110
Posted Friday, May 3, 2013 6:04 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1449158
Posted Friday, May 3, 2013 6:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1449162
Posted Friday, May 3, 2013 8:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 1:15 PM
Points: 4, Visits: 5
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
Post #1449380
Posted Friday, May 3, 2013 8:32 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:14 PM
Points: 12,952, Visits: 32,476
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1449382
Posted Saturday, May 4, 2013 11:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 1:15 PM
Points: 4, Visits: 5
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?
Post #1449444
Posted Saturday, May 4, 2013 11:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 1:15 PM
Points: 4, Visits: 5
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?
Post #1449445
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse