简体   繁体   中英

Saving data in table using OpenXML

i want to save xml data into a table in sp,passed as string,here is my code

alter PROCEDURE usp_be_insertXML 
-- Add the parameters for the stored procedure here
@xml varchar(1000) 

AS
BEGIN
DECLARE @idoc int
DECLARE @doc varchar(1000)
SET NOCOUNT ON;

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml

BEGIN TRY       
INSERT INTO testing ([Name] ,[Fname] )
SELECT  Column1,Column2
FROM OPENXML (@idoc, 'NewDataSet/Sheet1', 1)
WITH ([Column1] [nvarchar](50),
[Column2] [nvarchar](50))

END TRY
BEGIN CATCH

END CATCH
EXECUTE sp_xml_removedocument @idoc
END

This inserts NULL only in table,any help? and here is XML

 <NewDataSet>
    <Sheet1>
      <Column1>Name</Column1>
      <Column2>Fname</Column2>
    </Sheet1>
    <Sheet1>
      <Column1>khan</Column1>
      <Column2>dd</Column2>
    </Sheet1>
    <Sheet1>
      <Column1>mytest</Column1>
      <Column2>ff</Column2>
    </Sheet1>
 </NewDataSet> 

Problem is you're specifying to use an "attribute-centric" mapping (the "1" in the last parameter of OPENXML), but your data elements are actually elements. You can get your desired result one of two ways:

1) Change the parameter to "2" 2) Change your WITH clause to explicitly state the child element you're looking for:

WITH (
[Column1] [nvarchar](50) 'Column1'
, [Column2] [nvarchar](50) 'Column2' 
)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM