2009-09-04 3 views
5
<game xmlns="http://my.name.space" ></game> 

이것은 내 루트 요소입니다. 필자는 요소를 삽입하기 위해 저장 프로 시저를 작성했습니다. 저장 프로 시저를 요약하면, 여기에 SQL 이제 결과가 지금은 모두XQuery 네임 스페이스없는 삽입 SQL2008

사용 해봤이

<move xmlns=""> 
    <player>black</player> 
    <piece>king</piece> 
    <start>E7</start> 
    <end>E6</end> 
</move> 

그래서 MSSQL 빈 공간도 삽입 삽입을 수행 할 때

UPDATE ChessGame SET GameHistory.modify('insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/game)[0]') WHERE Id = @GameId; 

에게있어

WITH XMLNAMESPACES(DEFAULT 'http://my.name.space') 

GameHistory.modify('declare default element namespace "http://my.name.space"; insert ...') 

하지만 어디서든 접두어로 끝나고 각 요소에 대한 네임 스페이스 선언이 끝납니다.

MSSQL이 넣는 접두사를 처리 할 논리가없는 코드 라이브러리에서 문제가 발생합니다. 결국에는 XML 루트에 새 요소를 삽입하고 네임 스페이스를 비워두기를 원합니다 (루트 기본값을 사용 하시겠습니까?). 저는이 모든 것을 처음 접했지만 이해할 때 루트 요소에 네임 스페이스가 있으면 모든 자식 노드가 루트의 기본 네임 스페이스를 갖지 않아야합니까?

답변

2
declare @x xml; 
select @x='<game xmlns="http://my.name.space" ></game>'; 
set @x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece> 
    <start>E7</start><end>E6</end></move> as last into (/game)[1]'); 
select @x; 

이 생성됩니다

<game xmlns="http://my.name.space"> 
    <move> 
    <player>black</player> 
    <piece>pawn</piece> 
    <start>E7</start> 
    <end>E6</end> 
    </move> 
</game> 
모두 SQL 2005 SP2에

및 SQL 2008 SP1.

또한이 테이블 업데이트는 잘 작동 :

declare @t table (x xml); 
insert into @t (x) values ('<game xmlns="http://my.name.space" ></game>'); 
update @t 
set x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece> 
     <start>E7</start><end>E6</end></move> as last into (/game)[1]'); 
select * from @t; 
3

확인이 나를 위해 작동합니다 게시물의

DECLARE @x XML; 
SET @x = '<game xmlns="http://my.name.space" ></game>'; 

select @x 

SET @x.modify(
    ' declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/*:game)[1]' 
    ) 

select @x 
+0

모두 가 그래도 난 내 파괴하는 문제를 발견 100 % 작동합니다. 마지막으로 (/ game) [0]은 (/ game) [1]에 마지막으로 입력해야합니다. [0]이 첫 번째 항목 항목을 나타내지 않습니까? 제로 기반 어레이와 비슷합니까? – Matt

+1

@RBarry : 왜'/ * : node'를 선호합니까? * 어떤 * 네임 스페이스가있는 게임 노드를 의미합니까? –

+0

매트 : 아니오, 서수 : [1]은 첫 번째 인스턴스입니다. – RBarryYoung