SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Better Excel Insert procedure?


Better Excel Insert procedure?

Author
Message
Jayded
Jayded
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 36
We recently purchased a third party tool to run in side by side with our SQL based product. In strategizing the best way to deliver this product to our existing base, we deemed having templates be provided that map the tables in Excel would be the easier procedure to proceed with.

One limitation I'm running into is the Insert statement I typically use with other Excel to SQL imports:

INSERT INTO MAPPINGTABLE
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\Documents and Settings\user\My Documents\Length Adjustments\Mapping\Mapping Table.xls', 'SELECT * FROM [Sheet1$]')

Using this statement requires that I have all of the columns in Excel in the same order as the tables I am inserting into. Is there an easy way to tie the SQL column titles to the column titles I'd use in Excel so that I can move the columns in Excel around to make my Excel workflow easier for data entry versus the way that the programmer has the SQL tables aligned?
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10058 Visits: 10574
Jayded (4/15/2010)
INSERT INTO MAPPINGTABLE
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\Documents and Settings\user\My Documents\Length Adjustments\Mapping\Mapping Table.xls', 'SELECT * FROM [Sheet1$]')


Try:
INSERT INTO MAPPINGTABLE (Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\Documents and Settings\user\My Documents\Length
Adjustments\Mapping\Mapping Table.xls', 'SELECT * FROM [Sheet1$]')



This way you can grab whatever columns you want from the spreadsheet, and put them into specific column in the table.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Jayded
Jayded
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 36
Thanks Wayne! In this instance when I'm making the lists do I have to have a consistency that SQL column 2 points to Excel column 3 such that if someone were to re-sort the Excel spreadsheet and move that column to column 4 that it would no longer work?

Would there be a way to tie the two via the column ID in SQL?

Ex. Column sCodeID in SQL is currently in Excel column 2 and row 1 is labled as sCodeID but should someone move that to column 8 it would still be ok as the system realizes that the column headed sCodeID belongs with SQL sCodeID regardless of where it is.
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10058 Visits: 10574
Jayded (4/15/2010)
Thanks Wayne! In this instance when I'm making the lists do I have to have a consistency that SQL column 2 points to Excel column 3 such that if someone were to re-sort the Excel spreadsheet and move that column to column 4 that it would no longer work?

Would there be a way to tie the two via the column ID in SQL?

Ex. Column sCodeID in SQL is currently in Excel column 2 and row 1 is labled as sCodeID but should someone move that to column 8 it would still be ok as the system realizes that the column headed sCodeID belongs with SQL sCodeID regardless of where it is.

If you are using a Header row in Excel, that becomes the column name. In this case, it will follow.
If you don't have a Header row, the columns get names like F1...F255. In this case, you will need to know what column it has been changed to.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

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