1

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$]')  
2
  • What exactly are you trying to do here? Commented Aug 2, 2010 at 16:36
  • I'm trying to reference a filename that will be different each time the procedure runs (once a month). Commented Aug 2, 2010 at 16:43

2 Answers 2

1

You can't do inline concatenation of string like you do in that line: 'Excel 5.0;Database=' + @fileName + ';'

Try moving that to a separate statement

You could try something like:

exec ('INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', ''' + @providerstring + ''', ''SELECT * FROM [RerunData$]''')

Sign up to request clarification or add additional context in comments.

4 Comments

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?
I'm not experienced with dynamic sql, can you point me in the right direction?
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$]''')
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.
1

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(...

3 Comments

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.
The syntax I've seen working is INSERT INTO OPENROWSET(<params>) <select statement>
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.