Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Import without SSIS


Import without SSIS

Author
Message
kev921hs
kev921hs
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
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
Manie Verster
Manie Verster
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1308 Visits: 986
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)
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14957 Visits: 38958
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!

Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14957 Visits: 38958
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!

kev921hs
kev921hs
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14957 Visits: 38958
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!

kev921hs
kev921hs
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
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?
kev921hs
kev921hs
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search