0

I am trying to create a simple stored procedure:

CREATE PROCEDURE SP_Test @FilePath int
AS
    SELECT
       LastName, FirstName
    INTO 
       tmp_tblPerson
    FROM 
       OPENROWSET('MSDASQL','Driver={Microsoft Access Text Driver (.txt, .csv)}','SELECT * FROM ' + @FilePath + "'")
GO

But I get a syntax error which I don't understand..?

Msg 102, Level 15, State 1, Procedure SP_Test, Line 12
Incorrect syntax near '+'.

Any ideas?

2

2 Answers 2

2

You can't use dynamic SQL when using using OPENROWSET. A workaround is to make the entire block use dynamically created SQL like this:

CREATE PROCEDURE SP_Test @FilePath int
AS
    DECLARE @sql NVARCHAR(MAX) = 
        'SELECT LastName, FirstName 
        INTO tmp_tblPerson 
        FROM OPENROWSET(
            ''MSDASQL'',
            ''Driver={Microsoft Access Text Driver (.txt, .csv)}'', 
            ''SELECT * FROM '' + @FilePath)'

    EXEC(@sql)

As always with dynamic SQL, make sure you are not vulnerable to SQL injection attacks.

Additionally, your query appears to be incorrect as I doubt you have a table with an integer as a name.

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

1 Comment

Great thanks. This will work. Oh and ummm the int datatype is my own typo..hehe...it is meant to be a VARCHAR
0

@filepath is int, you probably want something like

'SELECT * FROM ' + convert(varchar,@FilePath)

1 Comment

Bad habits to kick : declaring VARCHAR without (length) - you should always provide a length for any varchar variables and parameters that you use

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.