3

I have a query that runs without a variable. It works as expected:

INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
FROM OPENROWSET(BULK 'C:\xml\hamlet.xml', SINGLE_BLOB) AS x;

However when I add in a variable. It doesn't work (I replaced a string with a variable). I get this error => Incorrect syntax near '@path'.

DECLARE @path varchar(50) = 'C:\xml\hamlet.xml';

INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
FROM OPENROWSET(BULK @path, SINGLE_BLOB) AS x;

Does anyone know what is wrong here?

1 Answer 1

3

The parametrized dynamic SQL query may not work. Try concatenating the path explicitly:

DECLARE @path varchar(50) = 'C:\xml\hamlet.xml', 
        @sql nvarchar(max)= ''

set @sql = '
INSERT INTO XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE() 
FROM OPENROWSET(BULK ''' + @path +''', SINGLE_BLOB) AS x;'

exec sp_executesql @sql,N''
Sign up to request clarification or add additional context in comments.

2 Comments

Incorrect syntax near 'C:'. I it doesnt recognizes the variable as a string in this situation
@JaxPatočka forgot to include single quotes. Try the updated answer. Note, I trust your original query works fine.

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.