0

我在名为 dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG 的表中名为 response 的 xml 列中有以下 xml。我在正确检索错误和消息元素的属性代码和描述时遇到了困难。谁能解释我做错了什么?我得到 1727 行错误,而且比这少得多。我玩弄

select response.query('declare namespace Error="http://imodules.com/WebServices/EmailCategoryQuery/Error";/Error:EmailCategoryUpdateResults/EmailCategoryUpdateMemberResult/Error[@Code[1]]') as result
FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG 

这只是返回空白。

提前致谢。

/* xml 示例

<EmailCategoryUpdateResults xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="true" Constituent_Id="105933" Column="Email_Category_Omit_for_Travel_Program">
    <Message Code="2207" Description="The column 105933 already contains the specified update value." />
  </EmailCategoryUpdateMemberResult>
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="false" Constituent_Id="105939" Column="Email_Category_Do_Not_Email">
    <Error Code="2500" Description="Could not find column Email_Category_Do_Not_Email." />
  </EmailCategoryUpdateMemberResult>
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="true" Constituent_Id="105939" Column="Email_Category_Omit_AYA_Email">
    <Message Code= "2207" Description="The column 105939 already contains the specified update value." />
  </EmailCategoryUpdateMemberResult>
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="true" Constituent_Id="105939" Column="Email_Category_Omit_for_Travel_Program">
    <Message Code="2207" Description="The column 105939 already contains the specified update value." />
  </EmailCategoryUpdateMemberResult>
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="true" Constituent_Id="106016" Column="Email_Category_Omit_Travel_Email">
    <Message Code="2207" Description="The column 106016 already contains the specified update value." />
  </EmailCategoryUpdateMemberResult>
  <EmailCategoryUpdateMemberResult xmlns="http://imodules.com/WebServices/EmailCategoryQuery/" Community_Id="2" Success="true" Constituent_Id="106016" Column="Email_Category_Omit_Travel_Email">
  </EmailCategoryUpdateMemberResult>
</EmailCategoryUpdateResults>

*/

/*
create some temp tables
*/
If Object_Id('tempdb..#xml_transaction') IS NOT NULL BEGIN DROP TABLE #xml_transaction END; 
If Object_Id('tempdb..#xml_message') IS NOT NULL BEGIN DROP TABLE #xml_message END; 
If Object_Id('tempdb..#xml_error') IS NOT NULL BEGIN DROP TABLE #xml_error END; 

create table  #xml_transaction (
    TRANSACTIONID uniqueidentifier,
    LOOKUPID nvarchar(10),
    TRANSFERDATE nvarchar(25),
    CATEGORY nvarchar(155),
    LASTUPDATED nvarchar(25),
    COMMUNITYID nvarchar(1),
    OPTIN nvarchar(10),
    SUCCESS nvarchar(10)            
)

create table  #xml_message (
    MESSAGEID uniqueidentifier,
    LOOKUPID nvarchar(10),
    CATEGORY nvarchar(155),
    CODE nvarchar(25),
    DESCRIPTION nvarchar(100)
)           

create table  #xml_error (
    ERRORID uniqueidentifier,
    LOOKUPID nvarchar(10),
    CATEGORY nvarchar(155),
    CODE nvarchar(25),
    DESCRIPTION nvarchar(100)
)       

/* get the requests and responses for each transaction - some have messages or errors, and some do not. */  

INSERT INTO #xml_transaction
(
    TRANSACTIONID,
    TRANSFERDATE,
    LOOKUPID,
    CATEGORY,
    COMMUNITYID,
    LASTUPDATED,
    OPTIN,
    SUCCESS
)
SELECT
     id as trans_id
    ,transferdate   
    ,request.value(   
      'declare namespace Req="http://www.w3.org/2001/XMLSchema-instance";
        (/ArrayOfAllMemberOptOutResults/AllMemberOptOutResults/@Constituent_Id)[1]','varchar(20)') as lookup_id 
    ,request.value(
        'declare namespace Req="http://www.w3.org/2001/XMLSchema-instance";
         (/ArrayOfAllMemberOptOutResults/AllMemberOptOutResults/@Column)[1]','varchar(100)') as req_email_category  
    ,request.value(
        'declare namespace Req="http://www.w3.org/2001/XMLSchema-instance";
         (/ArrayOfAllMemberOptOutResults/AllMemberOptOutResults/@Community_Id)[1]','varchar(100)') as req_community_id              
    ,request.value(
        'declare namespace Req="http://www.w3.org/2001/XMLSchema-instance";
         (/ArrayOfAllMemberOptOutResults/AllMemberOptOutResults/@Last_Updated)[1]','varchar(100)') as req_last_updated
    ,request.value(
        'declare namespace Req="http://www.w3.org/2001/XMLSchema-instance";
         (/ArrayOfAllMemberOptOutResults/AllMemberOptOutResults/@OptIn)[1]','varchar(100)') as req_opt_in       
    ,response.value(
        'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/";                
                                                            (//@Success)[1]','varchar(100)') as response_successOrFailure                       
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG

/* get any messages */

INSERT INTO #xml_message
(
    MESSAGEID,
    LOOKUPID,
    CATEGORY,
    CODE,
    DESCRIPTION 
)
    select 
            id as message_id
           ,response.value(
          'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Message";               
                (//@Constituent_Id)[1]','varchar(100)') as message_lookupid 
            ,response.value(
          'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Message";               
                (//@Column)[1]','varchar(100)') as message_category   
            ,response.value(
            'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Message";             
                (//@Code)[1]','varchar(100)') as message_code
                ,response.value(
            'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Message";             
                (//@Description)[1]','varchar(100)') as message_description     
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG     

/*  Get any errors */

INSERT INTO #xml_error
(
    ERRORID,
    LOOKUPID,
    CATEGORY,
    CODE,
    DESCRIPTION 
)                                                       
    select 
           id as error_id
          ,response.value(
          'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/EmailCategoryUpdateMemberResult/Error";             
                (//@Constituent_Id)[1]','varchar(100)') as error_lookupid
          ,response.value(
          'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/EmailCategoryUpdateMemberResult/Error";             
                (//@Column)[1]','varchar(100)') as error_category
            ,response.value(
            'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Error";               
                (//@Code)[1]','varchar(100)') as error_code
                ,response.value(
            'declare namespace Detail="http://imodules.com/WebServices/EmailCategoryQuery/Error";               
                (//@Description)[1]','varchar(100)') as error_description       
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG;
4

1 回答 1

0

为了更简单地进行查询,您可以*对任意命名空间使用符号。例如,此查询返回错误元素:

    SELECT  
      response.query('/*:EmailCategoryUpdateResults/*:EmailCategoryUpdateMemberResult/*:Error')
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG

具有显式命名空间的下一个查询将给出相同的结果:

    ;WITH XMLNAMESPACES('http://imodules.com/WebServices/EmailCategoryQuery/' AS q)
    SELECT
      response.query('/EmailCategoryUpdateResults/q:EmailCategoryUpdateMemberResult/q:Error')
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG

下一个查询将提取错误代码:

    SELECT response.value('(/*:EmailCategoryUpdateResults/*:EmailCategoryUpdateMemberResult/*:Error/@Code)[1]', 'varchar(100)')
    FROM dbo.USR_YALE_IMODULES_EMAIL_WEBSERVICE_LOG

我希望这有帮助。

于 2016-04-06T17:38:09.130 回答