|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 8:28 AM
Points: 40,
Visits: 89
|
|
Hi,
could any one shed some light to read data from Excel sheet using SQL code.
Requirements 1)Excel sheet will be placed in some server path 2)Read the data available in particular column and from particular excel sheet.(say sheet 1 or sheet2 etc) 3)do we have any challenges while reading data from excel 2007 or 2010
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 4:46 AM
Points: 138,
Visits: 549
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 8:28 AM
Points: 40,
Visits: 89
|
|
Hey Hi,
I know we can use SSIS to get data from excel and load in to SQL table from where i can achieve requirements.
I thought of using only SQL to get data from excel and achieve the requirements so that we need not to go for SSIS.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Yesterday @ 8:48 AM
Points: 64,
Visits: 317
|
|
subramani.rudrappa 78855 (11/16/2012) Hey Hi,
I know we can use SSIS to get data from excel and load in to SQL table from where i can achieve requirements.
I thought of using only SQL to get data from excel and achieve the requirements so that we need not to go for SSIS.
I'm not aware of a way to use SQL directly. That is why Microsoft developed SSIS.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Yesterday @ 12:57 PM
Points: 39,
Visits: 1,107
|
|
add excel file as a linked server, then
INSERT INTO A_Table SELECT c1, c2 ... FROM LinkedExcelName...Sheet1$ WHERE something
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 11,648,
Visits: 27,764
|
|
you can create a linked server to point to an excel spreadsheet, and then reference it just like shanghaigirl-1125377's example.
here's my detailed notes and example on getting it set up and ready to rock :
a couple of prerequisites: install the AccessDatabaseEngine_x64.exe from microsoft: http://www.microsoft.com/en-us/download/details.aspx?id=13255make sure you open an Administrative command prompt window, and run it with the c:\Downloads\AccessDatabaseEngine_x64.exe /passivecommand line flag; this will force the install of the drivers, even if you have 32 bit office installed; otherwise you get some error about 32 bit Office preventing the install. After that is isntalled: --Required settings for the provider to work correctly as a linked server EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 GO EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
and then the code for the linked server: --################################################################################################# --Linked server Syntax for Excel with ACE 64 driver --################################################################################################# GO DECLARE @srvproduct nvarchar(256), @provider nvarchar(256), @location nvarchar(100), @provstr nvarchar(100), @catalog sysname, @sql varchar(1000) --add an excel spreadsheet as a linked server. SET @server = 'MyExcelACE' SET @srvproduct = '' SET @provider = 'Microsoft.ACE.OLEDB.12.0' set @provstr = 'Excel 12.0' SET @datasrc ='C:\Data\BlockGroups_2010\AKblockgroup.xls' EXEC sp_addlinkedserver @server,@srvproduct,@provider,@datasrc,NULL,@provstr EXEC dbo.sp_AddLinkedSrvLogin @server, FALSE, NULL, Admin, NULL --what spreadsheets (table equivilents are available? EXEC sp_tables_ex 'MyExcelACE' --you MUST know the name of the spreadsheet; --spreadsheet name has a dollar sign at the end of it! --I've personally never gor a spreadsheet that has a space in it --for example "Activity Data" =ActivityData$ never got [Activity Data$] or [Activity Data]$ to work --to work, so I end up editing the spreadsheet to remove spaces if that happens. SELECT * FROM MyExcelACE...ActivityData$; SELECT * FROM MyExcelACE...Sheet1$;
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
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 7:43 AM
Points: 656,
Visits: 2,921
|
|
subramani.rudrappa 78855 (11/16/2012) Hey Hi,
I know we can use SSIS to get data from excel and load in to SQL table from where i can achieve requirements.
I thought of using only SQL to get data from excel and achieve the requirements so that we need not to go for SSIS.
If you are importing data on a routine basis as part of a inter-application data transfer, then SSIS is a good choice.
For one-off ad hoc importing, however, I much prefer to use OPENROWSET() queries. There are few fiddly bits to get right or the process fails: - The spreadsheet cannot be open while you run the import. - You must know the name and path of the spreadsheet and the name of the worksheet you want to import and hard-code them into your query (unless you resort to dynamic SQL). - You have to enable ad hoc distributed queries on your server. - If you have mixed data types in the same column, the type conversion can fail if the process guesses wrong about how to type the column. Google for "Excel IMEX=1" for more information and a work-around.
Once you get all that working once, it's easy to re-do for other uses. I don't know what "best practice" is considered here, but I do a lot of one-time imports from Excel, and this is my favorite tool in the box.
Rich
|
|
|
|