Reputation: 119
I am having some serious problems with loading data from xml file. I want to declare it as a procedure with the .xml file name as parameter.
GO
ALTER PROCEDURE loadXML
@FileName varchar(50)
AS
BEGIN
DECLARE @XML AS XML,@hDoc AS INT
SELECT @XML = CONVERT(XML, BulkColumn)
FROM OPENROWSET(BULK 'D:\ '+@FileName , SINGLE_BLOB) AS x;
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
INSERT INTO [dbo].[Client]
SELECT *
FROM OPENXML(@hDoc, 'orderType/client')
WITH
(
OrderId varchar(200) '../../../sn',
Client_number varchar(200) 'client_number',
FirstName varchar(200) 'firstname',
LastName varchar(200) 'lastname',
E-Mail varchar(200) 'email'
)
EXEC sp_xml_removedocument @hDoc
END
exec loadXML 'order2.xml'
Obviously, this code won't work, because there's a syntax error near the @FilePath variable. I tried making string variables with the whole SELECT part, but it didn't work.
Upvotes: 0
Views: 3712
Reputation: 77926
Why don't pass the entire path along with drive name as parameter. I mean D:\folder1\file.xml
.
Assuming that @FileName
parameter has the full path like D:\folder1\file.xml
then you can use dynamic query to achieve what you are looking for like below. Give it a try
DECLARE @sqlCommand varchar(1000)
SET @sqlCommand = 'set @XML = (SELECT '+ @XML + ' = CONVERT(XML, BulkColumn)
FROM OPENROWSET(BULK ' + @FileName +' , SINGLE_BLOB) AS x)';
declare @xml xml;
EXEC sp_executesql @sqlCommand, N'@XYZ xml output', @xml output
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XYZ;
//rest of the code here
Upvotes: 1