Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Reference LinkedServer (Excel) in Dynamic SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-10-08 : 09:27:00
Gordon writes "I'm trying to import data from Excel using linked servers to the workbook. I need to import data from several sheets within the workbook. Rather than repeat the same static code over and over again, I want to do it in a loop with some dynamic SQL. The import happens overnight, so performance isn't an issue.

My code is as follows (I haven't included the code to link to the servers)

---------------------
DECLARE @S varchar(255)
DECLARE @SQLImport varchar(255)

SET @S = ''
SET @S = @S + 'INSERT INTO '
SET @S = @S + ' #drawings '
SET @S = @S + '( '
SET @S = @S + ' DrgNum, '
SET @S = @S + ' Title, '
SET @S = @S + ' JobNum, '
SET @S = @S + ' CreatedBy, '
SET @S = @S + ' CreatedDate, '
SET @S = @S + ' JobType '
SET @S = @S + ') '
SET @S = @S + 'SELECT '
SET @S = @S + ' LEFT(DrgNum, 7), '
SET @S = @S + ' Title, '
SET @S = @S + ' JobNum, '
SET @S = @S + ' CreatedBy, '
SET @S = @S + ' CreatedDate, '
SET @S = @S + ' JobType '
SET @S = @S + 'FROM '

SET @SQLImport = @S + 'ExcelDrgReg...RegA$'
EXECUTE @SQLImport

------------------------

I was then planning on repeating the last two lines for each sheet and for each workbook (I actually have two separate files).

When I do the exec, it reports that it can't see the linked server, but it definitely exists.

This is SQL 7 running on NTS 4.0"
   

- Advertisement -