2014-11-17 3 views
1

나는 빈 쿼리에 포함하기 전에 쿼리SQL Server 쿼리 빈 XML 요소 값

... 
<publicationreference> 
    ... 
    <referencedpublicationid /> 

을 위반 한 경우가 있었다. 수정 쿼리 후

은 포함 :

reference.value(
    '(./Field[@fieldName="referencedpublicationid"]/text())[1]','uniqueidentifier') 
    AS [referencedpublicationid], 

누군가가 그 선은 어떻게 다른가 설명 할 수 <referencedpublicationid xsi:nil="true" />

의 결과?

답변

1

두 개의 쿼리가 다른 XML 소스에 대한 것 같습니다. 처음에는 요소 이름으로 referencedpublicationid이 있고 두 번째 요소에는 @fieldName이라는 특성이 있습니다. 나는 또한 결과 XML이 이것과 관련이 있는지 이해하는 데 어려움을 겪고있다. for xml path, elements xsinil을 사용하여 XML을 만들면 xsi:nil="true"이 생성됩니다.

대신에 내가 생각하는 문제를 해결하려고 노력할 것입니다.

이 코드를보십시오 :

declare @XML xml = '<referencedpublicationid />' 
select @XML.value('./referencedpublicationid[1]', 'nvarchar(50)') 
select @XML.value('(./referencedpublicationid/text())[1]', 'nvarchar(50)') 

첫 번째 쿼리는 빈 문자열과 두 번째 반환 NULL를 반환합니다.

대신 데이터 형식 uniqueidentifier을 지정하면 빈 문자열을 uniqueidentifier로 캐스팅 할 수 없으므로 첫 번째 오류가 발생합니다. 두 번째 파일은 NULL이므로 잘 변환됩니다.

SQL Server에서 형식화되지 않은 XML의 모든 값은 값 함수에서 지정한 데이터 형식으로 변환되는 문자열입니다.

./referencedpublicationid[1]을 사용하면 모든 sub text() 값의 연결 값이 반환됩니다. 어떤 이유에서든 SQL Server는 text() 노드가 발견되지 않으면 빈 문자열을 제공합니다.

이 표현식 (./referencedpublicationid/text())[1]은 referencedpublicationid의 첫 번째 text() 노드를 지정하고 아무 것도 없기 때문에 NULL의 결과를 얻습니다.