views:

163

answers:

3

Hi guys,

I have a huge excel with thousands of rows and I need to generate an sql query to insert the data into a sql server database.

The format of the excel is the following

1 | string1 | another string
    string2
    string3
2 | AAA AAA | ZZZZZZZ
    BB BBBB
    CCCC CC

The first column is a row counter, it doesn't matter. The second column is a group of strings separated by break-lines The third column is a string that has to be associated to each of the strings on column 2.

So I need to generate the following queries:

INSERT INTO SomeTable VALUES ('string1', 'another string')
INSERT INTO SomeTable VALUES ('string2', 'another string')
INSERT INTO SomeTable VALUES ('string3', 'another string')
INSERT INTO SomeTable VALUES ('AAA AAA', 'ZZZZZZZ')
INSERT INTO SomeTable VALUES ('BB BBBB', 'ZZZZZZZ')
INSERT INTO SomeTable VALUES ('CCCC CC', 'ZZZZZZZ')

Is it clear what I need to do?

Unfortunately my excel habilities are so poor so I can't figure a way to do this.

Any help?

Thank you so much!

A: 

What kinds of DB do you plan to use?

Are you familiar with the tool 'TOAD'?

exiter2000
The DB already exists, it's SQL Server 2005. No, I'm not familiar with that tool. I just need to generate those queries... or if I could split the second column into multiple rows using the break line as delimiter, that would help me a lot.
emzero
TOAD AFAIK is specific to Oracle
CResults
A: 

First create a linkedServer to the Excel worksheet in question

declare @server varchar(100),@dropLogins varchar(20)
select @server='myExcelBook',@dropLogins='droplogins'

Exec sp_dropServer @server,@droplogins=@dropLogins    


    EXEC sp_addlinkedserver @server,
       'ACE 12.0',
       'Microsoft.ACE.OLEDB.12.0',
       'c:\MyExcelBooks\myExcelBool.xls',
       NULL,
       'Excel 12.0'

exec sp_linkedServers

ACE 12.0,Excel 12.0 and Microsoft.ACE.Oledb.12.0 are provider names for Excel 2007 If you have different version of excel you have to replace those literals with correct ones. It is also required that the excel is installed on your database server.

Once you have done above.. you can treat each worksheet in the Workbook as a Table

so inserting data to SomeTable goes like this

Insert SomeTable(column1,Column2,Column3)
Select Column1,Column2+Column3,Column3 from myExcelbook..Sheet1$

Note that the $ sign is to be appended to Sheet that you are refering.

After adding the link server, you can see the Worksheets / and the Ranges under the Server Objects / LinkedServers / myExcelBook/Catalogs/Defautlt/Tables

TonyP
Sorry, but how is that going to help me?
emzero
you could do this by concatentating columns within the select statement I have edited the answer to reflect that
TonyP
+2  A: 

Here you go,

Assuming the number is in column A, and the two strings in columns B and C respectively

Put this in cell F2

=IF(C2="",C1,C2)

and this in cell I2

="INSERT INTO SomeTable VALUES('" & B2 & "','" & F2 & "')"

then copy the formulas up and down in columns F and I.

Hopefully you should see how it works

CResults
I came up with something similar, but that doesn't work because that generates 1 insert per row, and in the example, the first row needs to generate 3 inserts, because there are 3 strings delimited by break lines on column 2. If I could split column2 in different rows using the break line as delimiter that would help a lot.
emzero
Is the number of delimited items always the same? Or at least within a range?
CResults
Unfortunately no. Could be 1 upto infinity =P
emzero
OK, alternative idea. Would the total length of the cell containing the delimted string always be less than 4000 characters long? If so you could always build an SP, take that cell as a parameter and split it within SQL
CResults
By the way I realise that infinity is usually > 4000 chars but worth an ask!
CResults
Haha, yes, in this case infinity < 4000 =PSo there's no way to split it within excel?
emzero
You can split in Excel from the Data menu and then select Text to Columns - it will then split the cell by the delimiter. But I think at that point writing the formula to write the SQL query may be more trouble than its worth.
CResults