Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Issue ,Getting Only One Child Node by sp_xml_preparedocument.


Issue ,Getting Only One Child Node by sp_xml_preparedocument.

Author
Message
harsimranjeetsinghwasson
harsimranjeetsinghwasson
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 233
Hi,

I am using Below logic , but getting only one Office ID for Doctor 74962 and its corresponding workdays.


USE DoctorPayroll3
GO



DECLARE @Payrollhandle int
DECLARE @Payrolldoc VARCHAR(MAX)=
('<Payroll StartDate="2013-10-30" EndDate="2013-10-31">
<Doctor ID="74962">
<WorkDays>
<Office ID="60101">23</Office>
<Office ID="60102">23</Office>
</WorkDays>
<AsstDays>
<Office ID="60102">23</Office>
</AsstDays>
<PTODays>
<Office ID="60101">23</Office>
</PTODays>
</Doctor>
<Doctor ID="74961">
<WorkDays>
<Office ID="60101">23</Office>
<Office ID="60102">23</Office>
</WorkDays>
<AsstDays>
<Office ID="60102">23</Office>
</AsstDays>
<PTODays>
<Office ID="60101">23</Office>
</PTODays>
</Doctor>
</Payroll>')

EXEC SP_xml_preparedocument @Payrollhandle OUTPUT,@Payrolldoc
SELECT

A.*

FROM

OPENXML(@Payrollhandle,'/Payroll/Doctor',8)
WITH (DoctorID INT '@ID',
    StartDate DATE '../@StartDate',
    EndDate DATE '../@EndDate',
    WorkDaysOfficeID VARCHAR(10) 'WorkDays/Office/@ID',
    WorkDays INT 'WorkDays//Office'--,
    --AsstDaysOfficeID VARCHAR(10) 'AsstDays/Office/@ID',
    --AsstDays INT 'AsstDays/Office' ,
    --PTODaysOfficeID VARCHAR(10) 'PTODays/Office/@ID'   ,
    --PTODays INT 'PTODays/Office'   
    )
    AS A
   
   
EXEC sp_xml_removedocument @Payrollhandle
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 22778
DECLARE @PayrollXML XML = CAST(@Payrolldoc AS XML)

SELECT doctor.r1.value('@ID','INT') AS DoctorID,
doctor.r1.value('../@StartDate','DATETIME') AS StartDate,
doctor.r1.value('../@EndDate','DATETIME') AS EndDate,
workdays.r2.value('@ID','INT') AS WorkDaysOfficeID,
workdays.r2.value('.','INT') AS WorkDays
FROM @PayrollXML.nodes('/Payroll/Doctor') AS doctor(r1)
CROSS APPLY doctor.r1.nodes('WorkDays/Office') AS workdays(r2)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




harsimranjeetsinghwasson
harsimranjeetsinghwasson
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 233
Can this be acheived through sp_xml_preparedocument
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 22778
Like this


SELECT 

A.*

FROM

OPENXML(@Payrollhandle,'/Payroll/Doctor/WorkDays/Office',8)
WITH (DoctorID INT '../../@ID',
StartDate DATE '../../../@StartDate',
EndDate DATE '../../../@EndDate',
WorkDaysOfficeID VARCHAR(10) '@ID',
WorkDays INT '.'--,
--AsstDaysOfficeID VARCHAR(10) 'AsstDays/Office/@ID',
--AsstDays INT 'AsstDays/Office' ,
--PTODaysOfficeID VARCHAR(10) 'PTODays/Office/@ID' ,
--PTODays INT 'PTODays/Office'
)
AS A



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




harsimranjeetsinghwasson
harsimranjeetsinghwasson
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 233
yes, but can you please tell if i mention rowpattern in OPEN XML like '/Payroll/Doctor' Only then will be the corresponding path for below i used "//" but getting only one row.


I am saying to fix rowpattern to that only because i don't know what nodes can be multiple

AS ASSDAYS can have multiple days with respect to office.

FROM

OPENXML(@Payrollhandle,'/Payroll/Doctor/',8)
WITH (DoctorID INT '.@ID',
    StartDate DATE '../@StartDate',
    EndDate DATE '../@EndDate',
    WorkDaysOfficeID VARCHAR(10) 'WorkDays//Office//@ID',
    WorkDays INT 'WorkDays//Office',
    AsstDaysOfficeID VARCHAR(10) 'AsstDays/Office/@ID',
    AsstDays INT 'AsstDays/Office' ,
    PTODaysOfficeID VARCHAR(10) 'PTODays/Office/@ID'   ,
    PTODays INT 'PTODays/Office'   
    )
    AS A
harsimranjeetsinghwasson
harsimranjeetsinghwasson
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 233
Not Able to get Assistant days now by open xml



DECLARE @Payrollhandle int
DECLARE @Payrolldoc VARCHAR(MAX)=
('<Payroll StartDate="2013-10-30" EndDate="2013-10-31">
<Doctor ID="74962">
<WorkDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</WorkDays>
<AsstDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</AsstDays>
</Doctor>
</Payroll>')

EXEC SP_xml_preparedocument @Payrollhandle OUTPUT,@Payrolldoc
SELECT
* FROM    

OPENXML(@Payrollhandle,'Payroll/Doctor/WorkDays/Office',8)
WITH (DoctorID INT '../../@ID',
WorkDaysOfficeID VARCHAR(10) './@ID',
    AsstDaysOfficeID VARCHAR(10) '../../AsstDays/Office/@ID'
   
    )
    AS A
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2236 Visits: 7418
harsimranjeetsinghwasson (10/24/2013)
Not Able to get Assistant days now by open xml



DECLARE @Payrollhandle int
DECLARE @Payrolldoc VARCHAR(MAX)=
('<Payroll StartDate="2013-10-30" EndDate="2013-10-31">
<Doctor ID="74962">
<WorkDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</WorkDays>
<AsstDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</AsstDays>
</Doctor>
</Payroll>')

EXEC SP_xml_preparedocument @Payrollhandle OUTPUT,@Payrolldoc
SELECT
* FROM    

OPENXML(@Payrollhandle,'Payroll/Doctor/WorkDays/Office',8)
WITH (DoctorID INT '../../@ID',
WorkDaysOfficeID VARCHAR(10) './@ID',
    AsstDaysOfficeID VARCHAR(10) '../../AsstDays/Office/@ID'
   
    )
    AS A


Using the solution I posted here: Not Able to Get Multiple Rows
you could do this:


DECLARE @Payrollhandle int
DECLARE @Payrolldoc VARCHAR(MAX)=
('<Payroll StartDate="2013-10-30" EndDate="2013-10-31">
<Doctor ID="74962">
<WorkDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</WorkDays>
<AsstDays>
<Office ID="60101" Days="23"/>
<Office ID="60102" Days="23"/>
</AsstDays>
</Doctor>
</Payroll>')

EXEC SP_xml_preparedocument @Payrollhandle OUTPUT,@Payrolldoc;

SELECT   A.DoctorID,
      A.WorkDaysOfficeID,
      B.AsstDaysOfficeID,
      A.OfficeDays,
      B.AsstDays,
      A.StartDate,
      A.EndDate
FROM
OPENXML(@Payrollhandle,'Payroll/Doctor/WorkDays/Office',8)
WITH
(   StartDate date '../../../@StartDate',
   EndDate date '../../../@EndDate',
   DoctorID int '../../@ID',
   WorkDaysOfficeID VARCHAR(10) '@ID',
   OfficeDays int '@Days'
   Wink AS A
OUTER APPLY
OPENXML(@Payrollhandle,'Payroll/Doctor/AsstDays/Office',8)
WITH
(   DoctorID INT '../../@ID',
   AsstDaysOfficeID VARCHAR(10) '@ID',
   AsstDays int '@Days') AS B
WHERE A.DoctorID=B.DoctorID AND A.WorkDaysOfficeID=B.AsstDaysOfficeID



The above query gives you everything you need to get any attribute in your XML file.

The most important thing to note here is that I am breaking this up into two tables and joining them on //Workdays/Office/@ID=//AsstDatys/Office/@ID.

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search