I need to insert data into an SQL table by reading multiple Excel spreadsheets. I've figured out how to code the loop but decided to test with a single sheet.
When I run the following code in Query Analyser I get:
Server: Msg 7202, Level 11, State 2, Line 32
Could not find server 'Excel' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
(line 32 is the INSERT).
However, it doesn't seem to be executing the code - I tried inserting a few random 'Print' statements before the INSERT and none were executed. sp_helpserver shows that the linked server has not been addded. If I execute the SET @SQLCommand and EXEC @SELCommand separately the linked server is addded and I can run select * from Excel...Data and see the spreadsheet data. Any advice gratefully received
The Code
Truncate Table ForecastArray
declare @WsheetName VarChar(100)
declare @WsheetPath VarChar(100)
declare @SQLCommand VarChar(600)
-- set path to worksheets
set @WsheetPath = 'C:\Test\'
set @WsheetName = 'Forecast01.xls'
-- Create a linked server connection to the worksheet.
SET @SQLCommand =
'EXEC master..sp_addlinkedserver '+
'''' + 'Excel' + '''' + ',' +
'''' + 'Jet 4.0' + '''' + ',' +
'''' + 'Microsoft.Jet.OLEDB.4.0' +
'''' + ',' + '''' + @Wsheetpath +
@WsheetName + '''' + ',' +
'NULL,' + '''' + 'Excel 8.0' + ''''
EXEC (@SQLCommand)
-- login to the linked server
EXEC sp_addlinkedsrvlogin 'Excel', FALSE
-- Now that the worksheet is "attached" insert its data into table ForecastArray. -
INSERT into ForecastArray select * from Excel...Data
-- Cleanup
EXEC sp_droplinkedsrvlogin 'Excel',NULL
EXEC sp_dropserver 'Excel', NULL
GO
When I run the following code in Query Analyser I get:
Server: Msg 7202, Level 11, State 2, Line 32
Could not find server 'Excel' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
(line 32 is the INSERT).
However, it doesn't seem to be executing the code - I tried inserting a few random 'Print' statements before the INSERT and none were executed. sp_helpserver shows that the linked server has not been addded. If I execute the SET @SQLCommand and EXEC @SELCommand separately the linked server is addded and I can run select * from Excel...Data and see the spreadsheet data. Any advice gratefully received
The Code
Truncate Table ForecastArray
declare @WsheetName VarChar(100)
declare @WsheetPath VarChar(100)
declare @SQLCommand VarChar(600)
-- set path to worksheets
set @WsheetPath = 'C:\Test\'
set @WsheetName = 'Forecast01.xls'
-- Create a linked server connection to the worksheet.
SET @SQLCommand =
'EXEC master..sp_addlinkedserver '+
'''' + 'Excel' + '''' + ',' +
'''' + 'Jet 4.0' + '''' + ',' +
'''' + 'Microsoft.Jet.OLEDB.4.0' +
'''' + ',' + '''' + @Wsheetpath +
@WsheetName + '''' + ',' +
'NULL,' + '''' + 'Excel 8.0' + ''''
EXEC (@SQLCommand)
-- login to the linked server
EXEC sp_addlinkedsrvlogin 'Excel', FALSE
-- Now that the worksheet is "attached" insert its data into table ForecastArray. -
INSERT into ForecastArray select * from Excel...Data
-- Cleanup
EXEC sp_droplinkedsrvlogin 'Excel',NULL
EXEC sp_dropserver 'Excel', NULL
GO