I am trying to insert "DocumentFields" values from XML into SQL Server 2008 but no luck.
Can you please help me with this? Below is the XML and the stored procedure used.
CREATE PROCEDURE [dbo].[SP_Test]
(@xmlData As XML)
AS
BEGIN
DECLARE @idoc int
SET @xmlData='<?xml version="1.0" encoding="UTF-8"?>
<ImportSession>
<Documents>
<Document DocumentClassName="RDOCCLASS" Processed="0" Priority="2">
<DocumentFields>
<DocumentField Name="DocumentID" Value="419" />
<DocumentField Name="MessageID" Value="apap-12w-asqwe" />
<DocumentField Name="AccountName" Value="John Thomas" />
<DocumentField Name="AccountNumber" Value="1234567890" />
<DocumentField Name="Contact" Value="00012736782345" />
</DocumentFields>
</Document>
</Documents>
</ImportSession>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlData;
INSERT INTO dbo.[Account]([DocumentID], [MessageID], [AccountName], [AccountNumber], [Contact])
SELECT *
FROM OPENXML(@idoc,/ImportSession/Documents/Document/DocumentFields',1)
WITH (DocumentID varchar(20),MessageID varchar(30),
AccountName varchar(20),AccountNumber varchar(20),
Contact varchar(20)
);
EXEC sp_xml_removedocument @xmlData;
END
Thanks Arsh
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!