2011-10-25 3 views
0

사용하는 방법이 내가 값 XML 요소를 삽입에 문제가 내 코드 하나 지금 내 코드는 마지막 만 삽입되어이SQL OPENXML 루프

DECLARE @x VARCHAR(8000) 
SET @x = 
    '<authors> 
    <author> 
     <firstname>Michael</firstname> 
     <lastname>Howard</lastname> 
    </author> 
    <author> 
     <firstname>David</firstname> 
     <lastname>LeBlanc</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    </authors>' 

DECLARE @h INT 
DECLARE @h1 VARCHAR(MAX) 
EXECUTE sp_xml_preparedocument @h OUTPUT, @x 

SELECT @h1= firstname 
FROM OPENXML(@h, '/authors/author',2) 
WITH(
    firstname VARCHAR(20) 

) 
select NAME from temps WHERE [email protected] 
if @@ROWCOUNT=0 
Begin 
INSERT INTO temps(NAME) VALUES (@h1) 
END 
EXECUTE sp_xml_removedocument @h 

에 대한 루프 넣어 어떻게 .. 로 XML 요소 나는 그것에게 다른 후 하나를 수행 할 .. 나에게 당신이 XML을 처리하는 이유 당신은 을 설명하지 않은

+1

당신은 당신이하고있는 일을 묘사했으나, 왜 그런 일을하고 있는지, 또는 당신이하고 싶은 것을 기술하지 않았습니다. – Ryan

+0

왜 이런 식으로하고 싶습니까? sp_xml_preparedocument는 더 이상 사용되지 않습니다. 여전히 SQL 2000을 사용하고 있습니까? 아니면 다른 방법을 사용하고 있습니까? 또한, 왜 당신은 한 번에 하나의 요소를 삽입하기 만 하는가? –

+0

내 코드를 지금 확인하십시오. 나는이 상황을 생각하고 싶습니다. 그래서 .. 이런 식으로 생각했습니다. 좋은 생각을 제안합니다. 2008 년 서버를 사용합니다 .. – Rocky111

답변

1
DECLARE @x XML = '<authors> 
    <author> 
    <firstname>Michael</firstname> 
    <lastname>Howard</lastname> 
    </author> 
    <author> 
    <firstname>David</firstname> 
    <lastname>LeBlanc</lastname> 
    </author> 
    <author> 
    <firstname>adad</firstname> 
    <lastname>asdad</lastname> 
    </author> 
    <author> 
    <firstname>adad</firstname> 
    <lastname>asdad</lastname> 
    </author> 
</authors>' 

SELECT T.c.value('./firstname[1]', 'varchar(200)') 
FROM @x.nodes('/authors/author') AS T(c) 
0

솔루션 또는 생각을 말해, 그래서 나는 생각합니다. temps 테이블에서 특정 firstname 값을 찾고있는 것으로 보이며, 발견되지 않으면 추가합니다. 테이블 변수 @names으로 시연 해 보겠습니다. LEFT OUTER 조인

-- Assume you have a table [temps] with 
-- a column [name], and you want to find 
-- the new/missing firstnames from an XML 
-- fragment and insert the new firstnames into 
-- the [temps] table 
-- Example: 
--DECLARE [temps] TABLE (name VARCHAR(20)) 
--INSERT [temps] (name) VALUES ('Michael') 

-- Create a temporary table to hold the XML firstname values 
DECLARE @names TABLE (firstname VARCHAR(20)) 
DECLARE @x xml 
SET @x = 
    '<authors> 
    <author> 
     <firstname>Michael</firstname> 
     <lastname>Howard</lastname> 
    </author> 
    <author> 
     <firstname>David</firstname> 
     <lastname>LeBlanc</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    </authors>' 

-- Extract the firstnames from the XML and insert the names 
-- into the temp table variable 
INSERT INTO @names(firstname) 
SELECT x.author.value('firstname[1]', 'varchar(20)') AS firstname 
FROM @x.nodes('/authors/author') AS x(author) 

-- Use a left outer join query to identify the new/missing names 
-- in the temp table and then insert the names into [temps]. 
-- The WHERE condition specifies rows that exist in @firstnames 
-- but do not exist (are NULL) in [temps]. 
INSERT temps (name) 
SELECT nam.firstname 
FROM @names  nam LEFT OUTER JOIN 
     temps  tmp ON (nam.firstname = tmp.name) 
WHERE tmp.name IS NULL 

는 @firstnames 나타난 변수 아닌 [firstnames 표에 신원 firstnames에 WHERE 조건으로 사용된다. 가능하면 SQL의 데이터를 반복하여 피하고 집합 기반 연산을 사용하십시오. 이 같은 것을 할 경우

+0

예 ... 한 번에 하나의 값을 가져 가고 싶습니다. @name 그리고 내가 2 또는 3 테이블을 체크 아웃해야 .. 그래서 내가 위의 예제에서 확인 할 수있는 전화 번호 pls 전화 번호 – Rocky111

+0

다시 내가 모두 선택하고 싶지 않아 – Rocky111

+0

당신은 무엇을 확인해야합니까 2 또는 3 테이블? 이름이 다른 테이블에 존재하는지 여부 거의 항상 세트 기반 연산을 사용하여 이것을 수행 할 수 있습니다.이 연산은 하나씩 반복하지 않고 SQL을 사용하여 작업하는 가장 좋은 방법입니다. 2 개 또는 3 개의 다른 테이블에서 수행중인 작업에 대한 자세한 정보를 제공하십시오. – Ryan

0

: 당신은 쉽게 루프에게 당신처럼 보였다 때문에 나는 ROW_NUM을 추가

참고을 수행하는 insert into ...을 대신 할 수있는

DECLARE @x xml 
SET @x = 
    '<authors> 
    <author> 
     <firstname>Michael</firstname> 
     <lastname>Howard</lastname> 
    </author> 
    <author> 
     <firstname>David</firstname> 
     <lastname>LeBlanc</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    </authors>' 

SELECT 
    ref.value('firstname[1]', 'NVARCHAR(20)') AS firstname , 
    ref.value('lastname[1]', 'NVARCHAR (10)') AS lastname , 
    ROW_NUMBER() over (order by ref.value('firstname[1]', 'NVARCHAR(20)')) AS rownum 

FROM @x.nodes('/authors/author') 
xmlData(ref) 

Which prints: 
firstname   lastname rownum 
-------------------- ---------- -------------------- 
adad     asdad  1 
adad     asdad  2 
David    LeBlanc 3 
Michael    Howard  4 

만 두 번째를 삽입하고 싶었다 귀하의 검색어에서 알 수있는 행의 가치

0

답변이 .........

DECLARE @x VARCHAR(8000), 
     @nItemCnt int 
DECLARE @ProductID VARCHAR(MAX), 
     @lcnt INT 
DECLARE @@getCurrentName CURSOR, 
@h INT, 
@h1 VARCHAR(MAX) 
SET @x = 
    '<authors> 
    <author> 
     <firstname>Michael</firstname> 
     <lastname>Howard</lastname> 
    </author> 
    <author> 
     <firstname>David</firstname> 
     <lastname>LeBlanc</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    <author> 
     <firstname>adad</firstname> 
     <lastname>asdad</lastname> 
    </author> 
    </authors>' 


EXECUTE sp_xml_preparedocument @h OUTPUT, @x 
SET @@getCurrentName = CURSOR FOR SELECT firstname FROM OPENXML(@h, '/authors/author',2)WITH(firstname VARCHAR(20)) 
OPEN @@getCurrentName 
FETCH NEXT FROM @@getCurrentName INTO @ProductID 
WHILE @@FETCH_STATUS = 0 
BEGIN 
select @lcnt=COUNT(name) from temps WHERE [email protected] 
if @lcnt=0 
insert into temps values(@ProductID) 
PRINT @ProductID 
FETCH NEXT 
FROM @@getCurrentName INTO @ProductID 
END 
CLOSE @@getCurrentName 
DEALLOCATE @@getCurrentName