insert value from xml to the table

  • Hi

    follwing is the code i had use

    DECLARE @GroupID int = 1

    DECLARE @Agency xml

    SET @Agency = '<Agency>

    <AgencyID>1</AgencyID>

    <AgencyID>2</AgencyID>

    <AgencyID>3</AgencyID>

    </Agency>'

    CREATE TABLE #temp (id int identity,AgencyID int,GroupID int )

    INSERT INTO #temp

    (

    AgencyID

    ,GroupID

    )

    SELECT

    V.y.value('(//AgencyID)[1]','int') AS AgencyID

    ,@GroupID

    FROM @Agency.nodes('/Agency/AgencyID') AS V(y)

    SELECT * FROM #temp

    DROP TABLE #temp

    this resul is coming while executing the above code, the agency id is not coming right instaed of coming 1,2,3 it is repeating 1

    id AgencyID GroupID

    111

    211

    311

    i want the following result

    id AgencyID GroupID

    111

    221

    331

  • Try this

    CREATE TABLE #temp (id int identity,AgencyID int,GroupID int )

    INSERT INTO #temp

    (

    AgencyID

    ,GroupID

    )

    SELECT

    -- V.y.value('(//AgencyID)[1]','int') AS AgencyID

    V.y.value('.','int')

    ,@GroupID

    FROM @Agency.nodes('/Agency/AgencyID') AS V(y)

    SELECT * FROM #temp

    DROP TABLE #temp

  • thanks srikant

    can u explain why you r using '.' instead of tags

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply