Merge views with .nodes xml data type method

I have two views listed below that get attributes at there respective levels ‘//BHILeads/Lead/Contact’ and ‘//BHILeads/Lead/PropertyInterest'.What I would like to do is make one view, but I don’t know how.I looked at the examples in AdventureWorks and they all use target name spaces.Do I have to use target name spaces to merge these views?

ALTERVIEW [dbo].[Is_vwBHI_Contact]AS

SELECTIsLead_Id,

ContRef.value('@FirstName','varchar(max)')as FirstName,

ContRef.value('@LastName','varchar(max)')as LastName,

ContRef.value('@Email','varchar(max)')as Email,

ContRef.value('@Phone','varchar(max)')as Phone,

ContRef.value('@StreetAddress','varchar(max)')as StreetAddress,

ContRef.value('@City','varchar(max)')as City,

ContRef.value('@State','varchar(2)')as State,

ContRef.value('@PostalCode','varchar(max)')as PostalCode

FROMIntegration.dbo.[Is.Lead]

CROSSAPPLYIsLead_XmlWellFormed.nodes('//BHILeads/Lead/Contact')AS R(ContRef)

ALTERVIEW [dbo].[Is_vwBHI_PropertyInterest]AS

SELECTIsLead_Id,

PrpIntRef.value('@StateName','varchar(max)')as I_State,

PrpIntRef.value('@MarketName','varchar(max)')as I_City,

PrpIntRef.value('@BuilderName','varchar(max)')as Market,

PrpIntRef.value('@CommunityNumber','varchar(max)')as I_Comm_ID,

PrpIntRef.value('@CommunityName','varchar(max)')as I_Comm_Name,

PrpIntRef.value('@MasterCommunity','varchar(max)')as I_Comm_Name2,

PrpIntRef.value('@PlanNumber','varchar(max)')as FP_ID,

PrpIntRef.value('@PlanName','varchar(max)')as FP_Name,

PrpIntRef.value('@Options','varchar(max)')as Comment3,

PrpIntRef.value('@SpecNumber','varchar(max)')as Comment4,

PrpIntRef.value('@SpecAddress','int')as Comment5,

PrpIntRef.value('@Price','decimal(18, 0)')as Price

FROMIntegration.dbo.[Is.Lead]

CROSSAPPLYIsLead_XmlWellFormed.nodes('//BHILeads/Lead/PropertyInterest')AS T(PrpIntRef)

[17621 byte] By [FreddieTripples] at [2008-2-28]
# 1

What exactly do you mean with merging the views? What is the relationship between the Contact information and the PropertyInterest?

If the relationship is 1-1, then you probably could write:

IsLead_XmlWellFormed.nodes('//BHILeads/Lead') AS R(LeadRef)

and then use either (Contact/@attribute)[1] or (PropertyInterest/@attribute)[1] in the value method calls.

If you have more Property Interests than Contact or vice versa, it depends on whether you want to denormalize the two views into a single view or whether you want to build a combining view over the two views. In either case, you would need to join them over the parent element...

HTH

Michael

MRys at 2007-10-7 > top of Msdn Tech,SQL Server,SQL Server XML...
# 2
By merging the two views, I mean there is a 1:1 relationship between them
I tried what you said
SELECT IsLead_Id,
ContRef.value(
'Contact/@FirstName[1]', 'varchar(max)') as FirstName,
ContRef.value(
'Contact/@LastName[1]', 'varchar(max)') as LastName,
ContRef.value(
'Contact/@Email[1]', 'varchar(max)') as Email,
ContRef.value(
'Contact/@Phone[1]', 'varchar(max)') as Phone,
ContRef.value(
'Contact/@StreetAddress[1]', 'varchar(max)') as StreetAddress,
ContRef.value(
'Contact/@City[1]', 'varchar(max)') as City,
ContRef.value(
'Contact/@State[1]', 'varchar(2)') as State,
ContRef.value(
'Contact/@PostalCode[1]', 'varchar(max)') as PostalCode,
ContRef.value(
'PropertyInterest/@StateName[1]', 'varchar(max)') as StateName
FROM Integration.dbo.[Is.Lead]
CROSS APPLY IsLead_XmlWellFormed.nodes('//BHILeads/Lead') AS R(ContRef)
And I got this error.
Msg 2389, Level 16, State 1, Line 4
XQuery [Integration.dbo.Is.Lead.IsLead_XmlWellFormed.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
As you can probably tell, I am new to this stuff
Thanks
FreddieTripples at 2007-10-7 > top of Msdn Tech,SQL Server,SQL Server XML...
# 3

Since [1] is only applied to the last step, and you can theoretically have more than one Contact per Lead unless you have a schema that says otherwise, you have to put the whole expression into parens:

.value('(Contact/@FirstName)[1]', 'varchar(max)')

etc.

Best regards

Michael

MRys at 2007-10-7 > top of Msdn Tech,SQL Server,SQL Server XML...

SQL Server

Site Classified