Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Xml With Key

Author  Topic 

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-05-27 : 01:49:12
Hi I need to generate Primary key for each responseId
and This primary key should be foreign key for each Sourcename
My XML is
'<Response>
<Header ResponseId="017224878" ParticipentId="RXHUB" ReceiverId="T00000000021041" ResponseDate="20110523" ResponseTime="1006">
<Source PBMName="RXHUBPBM" PBMId="T00000000001000">
<Receiver ProviderId="4321012352" LastName="Jones" FirstName="Mark" Suffix="MD" Address="" City="" State="" PostalCode="" Country="" CountrySubDivisionCode="">
<Subscriber SubscriberId="B000000%111111110%001" LastName="PALTROW" FirstName="BRUCE" MiddleName="K" Address="2645 MULBERRY LANE" City="TOLEDO" State="OH" PostalCode="54360" Country="US" Gender="Male" DateOfBirth="19450201" Relatioship="Self">
<DateTimePeriod Code="" Description=""/>
<Reference Name="Identification Card Number" Value="111111110" />
<Reference Name="Family Unit Number" Value="001" />
<Eligibility>
<Eligibility Code="1" Description="Active Coverage" HealthPlanName="HEALTH PLAN NAME" ServiceTypeCode="30">
<Reference Name="Group Number" Value="B000000" Description="HONEYWELL" />
<Reference Name="Plan Number" Value="PLAN ID" />
<Reference Name="Drug Formulary Number" Value="FSL101" />
<Reference Name="Alternative List ID" Value="ALT101" />
<DateTimePeriod Code="291" Description="Plan" />
</Eligibility>

<Eligibility Code="1" Description="Active Coverage" ServiceTypeCode="88">
<Reference Name="Plan Number 2" Value="PLAN ID 2" />
<Reference Name="Drug Formulary Number 2" Value="FSL106" />
<DateTimePeriod Code="" />
</Eligibility>

<Eligibility Code="1" Description="Active Coverage" ServiceTypeCode="90">
<DateTimePeriod Code="" />
</Eligibility>
</Eligibility>
</Subscriber>
</Receiver>
</Source>


<Source PBMName="RXHUBPBM" PBMId="T00000000001000">
<Receiver ProviderId="4321012352" LastName="Jones" FirstName="Mark" Suffix="MD" Address="" City="" State="" PostalCode="" Country="" CountrySubDivisionCode="">
<Subscriber SubscriberId="B000000%111111110%001" LastName="PALTROW" FirstName="BRUCE" MiddleName="K" Address="2645 MULBERRY LANE" City="TOLEDO" State="OH" PostalCode="54360" Country="US" Gender="Male" DateOfBirth="19450201" Relatioship="Self">
<DateTimePeriod Code="" Description=""/>
<Reference Name="Identification Card Number" Value="111111110" />
<Reference Name="Family Unit Number" Value="001" />
<Eligibility>
<Eligibility Code="1" Description="Active Coverage" HealthPlanName="HEALTH PLAN NAME" ServiceTypeCode="30">
<Reference Name="Group Number" Value="B000000" Description="HONEYWELL" />
<Reference Name="Plan Number" Value="PLAN ID" />
<Reference Name="Drug Formulary Number" Value="FSL101" />
<Reference Name="Alternative List ID" Value="ALT101" />
<DateTimePeriod Code="291" Description="Plan" />
</Eligibility>
<Eligibility Code="1" Description="Active Coverage" HealthPlanName="planx" ServiceTypeCode="88">
<Reference Name="Plan Number 2" Value="PLAN ID 2" />
<Reference Name="Drug Formulary Number 2" Value="FSL106" />
<DateTimePeriod Code="" />
</Eligibility>
<Eligibility Code="1" Description="Active Coverage" HealthPlanName="y" ServiceTypeCode="90">
<DateTimePeriod Code="" />
</Eligibility>
</Eligibility>
</Subscriber>
</Receiver>
</Source>

</Header>
</Response>'


Output should be
In First Table #t
Sourceid ResponseId ParticipentId ReceiverId ...
1 017224878 RXHUB T00000000021041 ...

In Second Table #tt

Sourceid Sid PBMName ...
1 1 RXHUBPBM ...
1 2 RXHUBPBM ...






In Love... With Me!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-27 : 08:31:53
Would something like this work for you?

DECLARE @x XML;
SET @x = 'Your XML here';

SELECT
IDENTITY(int,1,1) AS SourceId,
c.value('@ResponseId','varchar(32)') ResponseId,
c.value('@ParticipentId','varchar(32)') ParticipentId,
c.value('@ReceiverId','varchar(32)') ReceiverId
INTO #tmp1
FROM
@x.nodes('//Header') T(c);

DECLARE @sourceId INT = SCOPE_IDENTITY();
SELECT
@sourceId AS SourceId,
IDENTITY(INT,1,1) AS SID,
c.value('@PBMName','varchar(32)') AS PBMName
INTO #tmp2
FROM
@x.nodes('//Source') T(c)

SELECT * FROM #tmp1;
SELECT * FROM #tmp2;
DROP TABLE #tmp1;
DROP TABLE #tmp2;
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-27 : 08:38:41
On second thoughts, what I posted earlier would not work as expected if you have a lot of elements. It would work fine for the example data that you posted, but if you had many Response nodes, the scope_identity() would pick up only one of those nodes.

A better approach might be to get the data into a temp table like what I am showing below and then use that as the source to populate your two target tables.

DECLARE @x XML;
SET @x = 'Your XML here';

SELECT
c.value('@ResponseId','varchar(32)') ResponseId,
c.value('@ParticipentId','varchar(32)') ParticipentId,
c.value('@ReceiverId','varchar(32)') ReceiverId,
c2.value('@PBMName','varchar(32)') PBMName
INTO #tmp
FROM
@x.nodes('//Header') T(c)
CROSS APPLY c.nodes('//Source') S(c2)
SELECT * FROM #tmp;

---
-- Populate the two target tables here from data in #tmp
---
DROP TABLE #tmp;
Go to Top of Page
   

- Advertisement -