2017-04-05 145 views
1

我有一個XML formatted like this查詢XML在SQL Server

<Maximum.Edm.CodeIncludedInCalculation > 
    <CalculationsByProvince> 
    <Maximum.Edm.IncludedInByProvince Province="QC"> 
     <RevenueCodeCalculations CalculationListType="RevenueCode"> 
     <Maximum.Edm.CalculationIncluded Code="@Vacation" IsSelected="True" /> 
     <Maximum.Edm.CalculationIncluded Code="@RRQ" IsSelected="True" /> 
     <Maximum.Edm.CalculationIncluded Code="@EmploymentInsurance" IsSelected="True" /> 
     [...] 
     </RevenueCodeCalculations> 
     <TaxableBenefitCalculations CalculationListType="TaxableBenefits"> 
     <Maximum.Edm.CalculationIncluded Code="1" IsSelected="False" /> 
     <Maximum.Edm.CalculationIncluded Code="AV. VIE FE" IsSelected="True" /> 
     </TaxableBenefitCalculations> 
     <DeductionCodeCalculations CalculationListType="DeductionCode"> 
     <Maximum.Edm.CalculationIncluded Code="123" IsSelected="False" /> 
     <Maximum.Edm.CalculationIncluded Code="456" IsSelected="True" /> 
     <Maximum.Edm.CalculationIncluded Code="AC" IsSelected="False" /> 
     [...] 
     </DeductionCodeCalculations> 
    </Maximum.Edm.IncludedInByProvince> 
    <Maximum.Edm.IncludedInByProvince Province="ON"> 
     [...] 
    </Maximum.Edm.IncludedInByProvince> 
    </CalculationsByProvince> 
</Maximum.Edm.CodeIncludedInCalculation> 

我想創建一個簡單的查詢將返回各CalculationIncludedcodeIsSelected值。我的最終目標是創建一個存儲過程,該存儲過程將收到codeprovinceCalculationListType並返回IsSelected值。

我試圖用從hereherehere的例子,但我不斷收到null或空值。

查詢我試着用的例子之一進行:

select * from 
    (select 
    pref.value('(text())[1]', 'varchar(32)') as RoleName 
    from 
     Payroll.RevenueCode CROSS APPLY 
IncludeInCalculation.nodes('/Maximum.Edm.CodeIncludedInCalculation/CalculationsByProvince/Maximum.Edm.IncludedInByProvince/RevenueCodeCalculations') AS IncludeInCalculation(pref) 
) as Result 

回答

2

我的最終目標是創建一個將獲得一個代碼,一個省,一個CalculationListType並返回IsSelected值的存儲過程。

這不應該需要一個存儲過程...

下面將返回1(= true)或0(= FALSE)或NULL(=不存在)

DECLARE @Province NVARCHAR(100)='QC'; 
DECLARE @ListType NVARCHAR(100)='RevenueCode'; 
DECLARE @Code NVARCHAR(100)='@EmploymentInsurance' 

SELECT @xml.value(N'(/Maximum.Edm.CodeIncludedInCalculation 
        /CalculationsByProvince 
        /Maximum.Edm.IncludedInByProvince[@Province=sql:variable("@Province")] 
        /RevenueCodeCalculations[@CalculationListType=sql:variable("@ListType")] 
        /Maximum.Edm.CalculationIncluded[@Code=sql:variable("@Code")]/@IsSelected)[1]',N'bit') 
+0

該作品頂尖 !!非常感謝 –

+0

男孩我誤解了問題+1 –