Mar 25, 2011

Attach excel to SQL Server DB as linked server

Query to attach excel to DB as linked server.

EXEC sp_addlinkedserver 'ImportData',
                        'Jet 4.0', 'Microsoft.Jet.OLEDB.4.0',
                        'D:\Working\Web_Analytics_Requirements.xls',
                        NULL,
                        'Excel 8.0'
GO
--ImportData is the name of new linked server
--D:\Working\Web_Analytics_Requirements.xls is the path to excel
--Excel 8.0 is for MS office 2003

To insert the spreadsheet as a table in database

INSERT INTO dbo.ImportTest
SELECT *
FROM OPENQUERY(ImportData, 'SELECT * FROM [Summary$]')
--ImportData is the name of linked server
--Summary is the name the spreadsheet

To directly fetch values from linked server (excel file)

SELECT * FROM ImportData...Summary$

No comments: