从 xml 导入到标识列
Import from xml to identity column
我尝试将一些数据从 XML 导入临时 table。
我的代码:
DECLARE @xmlData XML
SET @xmlData = '
<PersonsInfo>
<Person BussEntityId="1">
<Name>
<First>Ken</First>
<Middle>J</Middle>
<Last>Sánchez</Last>
</Name>
</Person>
<Person BussEntityId="2">
<Name>
<First>Terri</First>
<Middle>Lee</Middle>
<Last>Duffy</Last>
</Name>
</Person>
</PersonsInfo>'
CREATE TABLE #Person
(
PersonId int IDENTITY PRIMARY KEY,
FirstName varchar(20),
MiddleName varchar(20),
LastName varchar(20)
)
SET IDENTITY_INSERT #Person ON;
INSERT INTO #Person
SELECT
T.e.value('@BussEntityId', 'int') AS PersonId,
P.elem.value('First[1]', 'varchar(20)') AS FirstName,
P.elem.value('Middle[1]', 'varchar(20)') AS MiddleName,
P.elem.value('Last[1]', 'varchar(20)') AS LastName
FROM
@xmlData.nodes('/PersonsInfo/Person') AS T(e)
OUTER APPLY
T.e.nodes('Name') AS P(elem)
SET IDENTITY_INSERT #Person OFF
SELECT * FROM #Person
DROP TABLE #Person
我收到一个错误:
An explicit value for the identity column in table '#Person' can only be specified when a column list is used and IDENTITY_INSERT is ON
我设置了IDENTITY_INSERT ON
- 为什么会出现问题?
正如@DanGuzman 已经指出的,需要为 INSERT INTO ...
子句明确指定列列表。
出于性能原因,我还调整了 XPath 表达式。
SQL
USE tempdb;
GO
DECLARE @xmlData XML
SET @xmlData = '
<PersonsInfo>
<Person BussEntityId="1">
<Name>
<First>Ken</First>
<Middle>J</Middle>
<Last>Sánchez</Last>
</Name>
</Person>
<Person BussEntityId="2">
<Name>
<First>Terri</First>
<Middle>Lee</Middle>
<Last>Duffy</Last>
</Name>
</Person>
</PersonsInfo>'
CREATE TABLE #Person (
PersonId int identity primary key
,FirstName varchar(20)
,MiddleName varchar(20)
,LastName varchar(20)
)
SET IDENTITY_INSERT #Person ON;
INSERT INTO #Person (PersonId, FirstName, MiddleName, LastName)
SELECT e.value('@BussEntityId', 'int') AS PersonId
,elem.value('(First/text())[1]', 'varchar(20)') AS FirstName
,elem.value('(Middle/text())[1]', 'varchar(20)') AS MiddleName
,elem.value('(Last/text())[1]', 'varchar(20)') AS LastName
FROM @xmlData.nodes('/PersonsInfo/Person') AS T(e)
OUTER APPLY T.e.nodes('Name') AS P(elem);
SET IDENTITY_INSERT #Person OFF;
SELECT * FROM #Person;
DROP TABLE #Person;
输出
+----------+-----------+------------+----------+
| PersonId | FirstName | MiddleName | LastName |
+----------+-----------+------------+----------+
| 1 | Ken | J | Sánchez |
| 2 | Terri | Lee | Duffy |
+----------+-----------+------------+----------+
我尝试将一些数据从 XML 导入临时 table。
我的代码:
DECLARE @xmlData XML
SET @xmlData = '
<PersonsInfo>
<Person BussEntityId="1">
<Name>
<First>Ken</First>
<Middle>J</Middle>
<Last>Sánchez</Last>
</Name>
</Person>
<Person BussEntityId="2">
<Name>
<First>Terri</First>
<Middle>Lee</Middle>
<Last>Duffy</Last>
</Name>
</Person>
</PersonsInfo>'
CREATE TABLE #Person
(
PersonId int IDENTITY PRIMARY KEY,
FirstName varchar(20),
MiddleName varchar(20),
LastName varchar(20)
)
SET IDENTITY_INSERT #Person ON;
INSERT INTO #Person
SELECT
T.e.value('@BussEntityId', 'int') AS PersonId,
P.elem.value('First[1]', 'varchar(20)') AS FirstName,
P.elem.value('Middle[1]', 'varchar(20)') AS MiddleName,
P.elem.value('Last[1]', 'varchar(20)') AS LastName
FROM
@xmlData.nodes('/PersonsInfo/Person') AS T(e)
OUTER APPLY
T.e.nodes('Name') AS P(elem)
SET IDENTITY_INSERT #Person OFF
SELECT * FROM #Person
DROP TABLE #Person
我收到一个错误:
An explicit value for the identity column in table '#Person' can only be specified when a column list is used and IDENTITY_INSERT is ON
我设置了IDENTITY_INSERT ON
- 为什么会出现问题?
正如@DanGuzman 已经指出的,需要为 INSERT INTO ...
子句明确指定列列表。
出于性能原因,我还调整了 XPath 表达式。
SQL
USE tempdb;
GO
DECLARE @xmlData XML
SET @xmlData = '
<PersonsInfo>
<Person BussEntityId="1">
<Name>
<First>Ken</First>
<Middle>J</Middle>
<Last>Sánchez</Last>
</Name>
</Person>
<Person BussEntityId="2">
<Name>
<First>Terri</First>
<Middle>Lee</Middle>
<Last>Duffy</Last>
</Name>
</Person>
</PersonsInfo>'
CREATE TABLE #Person (
PersonId int identity primary key
,FirstName varchar(20)
,MiddleName varchar(20)
,LastName varchar(20)
)
SET IDENTITY_INSERT #Person ON;
INSERT INTO #Person (PersonId, FirstName, MiddleName, LastName)
SELECT e.value('@BussEntityId', 'int') AS PersonId
,elem.value('(First/text())[1]', 'varchar(20)') AS FirstName
,elem.value('(Middle/text())[1]', 'varchar(20)') AS MiddleName
,elem.value('(Last/text())[1]', 'varchar(20)') AS LastName
FROM @xmlData.nodes('/PersonsInfo/Person') AS T(e)
OUTER APPLY T.e.nodes('Name') AS P(elem);
SET IDENTITY_INSERT #Person OFF;
SELECT * FROM #Person;
DROP TABLE #Person;
输出
+----------+-----------+------------+----------+
| PersonId | FirstName | MiddleName | LastName |
+----------+-----------+------------+----------+
| 1 | Ken | J | Sánchez |
| 2 | Terri | Lee | Duffy |
+----------+-----------+------------+----------+