2011-10-19 51 views
1

在以下代碼中,此語句;刪除重複且真正多餘的名稱空間

declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd 

重複6次,使代碼非常的混亂和難以遵循:

SELECT XW_PK, xw_ExeVersion, xw_enterpriseCode, xw_DatabaseCode, xw_CompanyCode, 
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
    (pd:action/@name)[1]', 'varchar(100)') Name, 
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
    (pd:action/@started)[1]', 'varchar(100)') [Started], 
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
    (pd:action/@ended)[1]', 'varchar(100)') [Ended], 
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
    (pd:action/@Elapsed)[1]', 'varchar(100)') Elapsed, 
    Poodle.Love.value('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
    (pd:action/@elapsedWithChildren)[1]', 'varchar(100)') elapsedWithChildren 
    FROM stmusage 
     CROSS APPLY xw_rawData.nodes('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
     /pd:performanceMeasurement/pd:action') as Poodle(Love) 

我想要做的就是聲明命名空間只有一次,並用它做。問題在於每個xquery都被嵌入到一個字符串中 - 而我並不完全確定 - 但是多年來我選擇的經驗給了我絕大多數的印象,即這些字符串不會與每個字符串進行交互其他時間很快。

回答

0
SELECT 
      XMLTable.actions.value('(@name)', 'varchar(100)') Name, 
      XMLTable.actions.value('(@started)', 'datetime') [Started], 
      XMLTable.actions.value('(@started)', 'datetime') + 
      convert(float, replace (replace (XMLTable.actions.value('(@elapsedWithChildren)', 'varchar(100)'),'PT',''),'S',''))/(60 * 60 * 24) Ended, --dodgy floating point rounding errors 
      convert(float, replace (replace (XMLTable.actions.value('(@elapsed)', 'varchar(100)'),'PT',''),'S','')) Elapsed, 
      convert(float, replace (replace (XMLTable.actions.value('(@elapsedWithChildren)', 'varchar(100)'),'PT',''),'S','')) ElapsedWithChildren, 
      XMLTable.actions.value('@moduleId','varchar(100)') moduleid, 
      XMLTable.actions.value('@actionCount','int') actionCount, 
      XW_PK, XW_ExeVersion, XW_EnterpriseCode, XW_DatabaseCode, XW_CompanyCode, ExportType 
      FROM StmUsage 
       CROSS APPLY xw_rawData.nodes('declare namespace pd="http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd"; 
       /pd:performanceMeasurement/pd:action') XMLTable(actions) 
      cross join (select 'Elapsed' ExportType union select 'ElapsedWithChildren') ExportTypeAlias 

簡單!

0

我不知道你正在寫的編程語言,但在任何其他語言,我會寫一個函數,字符串「名」或「開始」作爲參數PPPPP併產生

貴賓犬。 Love.value('declare namespace d =「http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd」; (pd:action/@ {PPPPP})[1]','varchar( 100)')

作爲其結果。