ALTER PROCEDURE GetSingersGenere
(@SingerData ntext)
AS
BEGIN
DECLARE @hDoc int
exec sp_xml_preparedocument @hDoc OUTPUT,@SingerData
IF OBject_id('SingerTable') IS NOT NULL
BEGIN
DROP TABLE SingerTable
END
CREATE TABLE SingerTable
(
SingerName varchar(200)
)
INSERT INTO SingerTable
(
SingerName
)
SELECT * FROM OpenXML (@hDoc,'/Singers/Singer')
WITH (SingerName varchar(200)) XMLSinger
SELECT * FROM SingerTable
END
and the way I am executing is this:-
EXEC GetSingersGenere
'<Singers>
<Singer>
Joe
</Singer>
<Singer>
ACDC
</Singer>
</Singers>'
I see NULL getting inserted in the table. Could anyone point out the mistake?
By default the OPENXML will look at attribute values or child elements for the data. If you write your select as:
It should work ok. Note the addition of ‘text()’ in to the schema mapping to specify that we just want the text value of the node instead of any attribute value.