views:

132

answers:

2

I'm trying to write this as part of stored procedure on SQL Server 2000. I keep getting a syntax error thrown on line starting Insert into OPENROWSET(.....

Help! I can't see the syntax error!

DECLARE @vDate Varchar(25) 
DECLARE @vCommand nvarchar(1000) 
DECLARE @fileName varchar(500)  

SET @vDate = LEFT(DATENAME(month, DATEADD(m, -1, GETDATE())), 3) + DATENAME(year, DATEADD(m, -1, GETDATE()))

SET @fileName = '\\SERVER\folder\subfolder\subfolder\Excel\JobRerun\JobRerun_' + @vDate + '.xls'

SET @vCommand =  'copy \\SERVER\folder\subfolder\subfolder\Excel\JobRerun\JobRerunTemplate.xls ' + @fileName

Exec master..xp_cmdshell @vCommand , NO_OUTPUT

INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=' + @fileName + ';', 
   'SELECT * FROM [RerunData$]')  
+1  A: 

Your INSERT statement is incorrect to me - you have INSERT INTO OPENROWSET(.... That would imply that you're trying to insert into the OPENROWSET query, but you're supplying "SELECT *..." within it. It should resemble:

INSERT INTO your_table
SELECT * FROM OPENROWSET(...
OMG Ponies
I'm trying to copy data from sql into the excel spreadsheet. I copied this code from a working example and I'm trying to modify it to suit my needs.
Marc
The syntax I've seen working is INSERT INTO OPENROWSET(<params>) <select statement>
Marc
@Marc: The INSERT statement reads as trying to insert into the Excel spreadsheet, without any selection from the database table. Based on the details, I don't think this is what you're after.
OMG Ponies
+1  A: 
Per-Frode Pedersen
This has got to be the problem! But I tried doing it on a separate statement and I got the same error there too. How else could I correct?
Marc
Modified suggestions using dynamic sql...
Per-Frode Pedersen
I'm not experienced with dynamic sql, can you point me in the right direction?
Marc
Create your statement as string, and execute using the exec() function, like this:exec ('INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', ''@providerstring + ''', ''SELECT * FROM[RerunData$]''')
Per-Frode Pedersen
Your right, the inline concat is what confused it. A) That SHOULD be allowed. B) I got around it by copying my template to a temporary file which always has the same name, then inserting into that, and copying that file to give it the correct name with date.
Marc