删除重复且真正冗余的命名空间

时间:2011-10-19 00:38:34

标签: sql xml tsql namespaces xquery

在以下代码中声明;

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)

我想要做的只是声明命名空间一次并完成它。问题是每个xqueries都嵌入了一个字符串中 - 我并不完全确定 - 但是这些年来我所接受的经验给了我压倒性的印象,即这些字符串不会与每个字符串交互其他任何时候都很快。

2 个答案:

答案 0 :(得分:0)

我不知道您正在编写的编程语言,但在任何其他语言中,我会编写一个函数,将字符串“name”或“started”作为参数PPPPP并生成

Poodle.Love.value('declare namespace d =“http://cargowise.com/ediEnterprise/2011/10/11/systemUsage.xsd”;     (pd:action / @ {PPPPP})[1]','varchar(100)')

结果。

答案 1 :(得分: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

轻松!