2

I'm trying to write a stored procedure in which I insert a few things from an XML string. This is to avoid making lots of stored procedure calls from inside a loop.

This is what I got so far, but its giving me errors:

Msg 207, Level 16, State 1, Procedure newsMapper_prc, Line 22
Invalid column name 'headline'.
Msg 207, Level 16, State 1, Procedure newsMapper_prc, Line 22
Invalid column name 'story'.
Msg 207, Level 16, State 1, Procedure newsMapper_prc, Line 25
Invalid column name 'entity'.
Msg 207, Level 16, State 1, Procedure newsMapper_prc, Line 27
Invalid column name 'entity'.

Can't really figure this out, some help would be greatly appreciated

<newsfile>
    <headline>THIS IS A NEWS HEADLINE</headline>
    <story>THIS IS A NEWS STORY</story>
    <entity>  1234</entity>
    <entity>1111</entity>
    <entity>2222</entity>
</newsfile>

Stored procedure code:

CREATE PROCEDURE newsMapper_prc
-- Add the parameters for the stored procedure here
  (@xmlString xml)
AS
 declare @criteriaTable table (criterianame varchar(100), parm varchar(MAX))

insert into @criteriaTable 
Select 
    criteriaValues.parm.value('../@type','varchar(MAX)'),
    criteriaValues.parm.value('.','varchar(MAX)')
from @xmlString.nodes('/newsfile/headline') as headline(parm),
 @xmlString.nodes('/newsfile/headline/story') as story(parm),
 @xmlString.nodes('/newsfile/headline/story/entity') as entity(parm)

insert into News (newsHeadline, newsStory, newsDate) values ((select headline from            @criteriaTable),(select story from @criteriaTable), GETDATE())
declare @newsID int
SET @newsID = scope_identity()
while exists (select entity from @criteriaTable)
BEGIN
insert into NewsEntities(newsID,entityID) values (@newsID,( Select entity from @criteriaTable))
END
1
  • 1
    I presume this is meant to be SQL Server? Which version? Commented Aug 23, 2011 at 20:35

1 Answer 1

3

Given your XML

DECLARE @input XML
SET @input = '<newsfile>
    <headline>THIS IS A NEWS HEADLINE</headline>
    <story>THIS IS A NEWS STORY</story>
    <entity>  1234</entity>
    <entity>1111</entity>
    <entity>2222</entity>
</newsfile>'

using this T-SQL statement, you can "rip apart" your XML into rows and columns:

SELECT
    NF.Ent.value('(.)[1]', 'int') AS 'Entity',
    @input.value('(/newsfile/headline)[1]', 'varchar(100)') AS 'Headline',
    @input.value('(/newsfile/story)[1]', 'varchar(100)') AS 'Story'
FROM
    @input.nodes('/newsfile/entity') AS NF(Ent)

this gives you:

Entity  Headline                  Story
1234    THIS IS A NEWS HEADLINE   THIS IS A NEWS STORY
1111    THIS IS A NEWS HEADLINE   THIS IS A NEWS STORY
2222    THIS IS A NEWS HEADLINE   THIS IS A NEWS STORY

Now what do you want to do with this information?? I can't quite understand what your stored proc is trying to do.....

If I understand correctly - you want to insert the headline and the story into the News table and get the IDENTITY ID back:

INSERT INTO dbo.News(newsHeadline, newsStory, newsDate) 
    SELECT
        @input.value('(/newsfile/headline)[1]', 'varchar(100)') AS 'Headline',
        @input.value('(/newsfile/story)[1]', 'varchar(100)') AS 'Story',
        GETDATE()

DECLARE @NewsID INT
SET @NewsID = SCOPE_IDENTITY()

and then you want to add an entry into the NewsEntities table for each value of <entity> in your XML - correct?

INSERT INTO dbo.NewsEntities(NewsID, EntityID)
    SELECT
        @NewsID,
        NF.Ent.value('(.)[1]', 'int')
FROM
    @input.nodes('/newsfile/entity') AS NF(Ent)

If you put this all together - does that solve your problem?=

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.