Testtabell och data
create table tableABC(A int, B bit, C varchar(10))
insert into tableABC values(1, 1, 'Row 1')
insert into tableABC values(2, 0, 'Row 1')
Skaffa xml
declare @xml xml
set @xml = (select *
from tableABC
for xml auto, elements)
XML-data
<tableABC>
<A>1</A>
<B>1</B>
<C>Row 1</C>
</tableABC>
<tableABC>
<A>2</A>
<B>0</B>
<C>Row 1</C>
</tableABC>
Infoga i en annan tabellABC
insert into tableABC(A, B, C)
select
r.value('A[1]', 'int'),
r.value('B[1]', 'bit'),
r.value('C[1]', 'varchar(10)')
from @xml.nodes('tableABC') t(r)
Redigera Kopiera hela detta uttalande för att testa om det fungerar
use tempdb
go
create table tableABC(A int, B bit, C varchar(10))
go
insert into tableABC values(1, 1, 'Row 1')
insert into tableABC values(2, 0, 'Row 1')
declare @xml xml
set @xml = (select *
from tableABC
for xml auto, elements)
insert into tableABC(A, B, C)
select
r.value('A[1]', 'int'),
r.value('B[1]', 'bit'),
r.value('C[1]', 'varchar(10)')
from @xml.nodes('tableABC') t(r)
select *
from tableABC
go
drop table tableABC
Resultatet är dubblerade rader i tabellABC
A B C
----------- ----- ----------
1 1 Row 1
2 0 Row 1
1 1 Row 1
2 0 Row 1