Tabella e dati di prova
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')
Ottieni l'xml
declare @xml xml
set @xml = (select *
from tableABC
for xml auto, elements)
Dati XML
<tableABC>
<A>1</A>
<B>1</B>
<C>Row 1</C>
</tableABC>
<tableABC>
<A>2</A>
<B>0</B>
<C>Row 1</C>
</tableABC>
Inserisci in un'altra tabellaABC
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)
Modifica Copia l'intera dichiarazione per verificare se funziona
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
Il risultato sono righe duplicate in tableABC
A B C
----------- ----- ----------
1 1 Row 1
2 0 Row 1
1 1 Row 1
2 0 Row 1