Consult xml file from sql server with OPENROWSET

1

I'm doing a query to an xml file from a store procedure and when placing the direct route it works. the problem is that the route I want to pass it by parameters and throws me error when placed as variable

DECLARE @XmlFile XML

SELECT @XmlFile = BulkColumn
FROM  OPENROWSET(BULK 'C:\example\mixml.xml', SINGLE_BLOB) x;

That's what I did and it works but when applying the change below and placing the path in a variable @file it says incorrect syntax.

DECLARE @XmlFile XML;
DECLARE @file varchar(max);

set @file = 'C:\example\mixml.xml';

SELECT @XmlFile = BulkColumn
FROM  OPENROWSET(BULK @file, SINGLE_BLOB) x;

Can someone help me with this?

    
asked by DeCo 18.12.2017 в 14:04
source

1 answer

1

Have you tried using a dynamic query? Something like this,

    DECLARE  @XmlFile   xml;
    DECLARE  @file      varchar(max);
    DECLARE  @Query     nvarchar(max)

    SELECT  @Query          =   'SELECT @varOut = BulkColumn
                        FROM  OPENROWSET(BULK ' + @file + ', SINGLE_BLOB) x;'

    EXECUTE sp_executesql @Query, N'@varOut xml out',@varOut=@XmlFile out

    SELECT @XmlFile
    
answered by 20.12.2017 в 00:14