SQL XML查询模式?
我在一个141k XML文档中有一些如下所示的XML。 是否有人可以显示SQL Server 2008 XQuery以将其插入两个临时表,即具有“状态”子关系的“国家”表 谢谢SQL XML查询模式?,xml,sql-server-2008,tsql,xpath,xquery,Xml,Sql Server 2008,Tsql,Xpath,Xquery,我在一个141k XML文档中有一些如下所示的XML。 是否有人可以显示SQL Server 2008 XQuery以将其插入两个临时表,即具有“状态”子关系的“国家”表 谢谢 <?xml version="1.0" encoding="utf-8"?> <countries author="Michael John Grove" title="Country, State-Province selections" date="2008-Feb-05"> <co
<?xml version="1.0" encoding="utf-8"?>
<countries author="Michael John Grove" title="Country, State-Province selections"
date="2008-Feb-05">
<country name="Afghanistan">
<state>Badakhshan</state>
<state>Badghis</state>
<state>Baghlan</state>
</country>
<country name="Albania">
<state>Berat</state>
<state>Bulqize</state>
<state>Delvine</state>
etc
巴达克山
巴德吉斯省
巴格兰省
斥责
布尔奇泽
德尔文
等
试试这个:
declare @x xml = '<?xml version="1.0" encoding="utf-8"?>
<countries author="Michael John Grove" title="Country, State-Province selections"
date="2008-Feb-05">
<country name="Afghanistan">
<state>Badakhshan</state>
<state>Badghis</state>
<state>Baghlan</state>
</country>
<country name="Albania">
<state>Berat</state>
<state>Bulqize</state>
<state>Delvine</state>
</country>
</countries>'
declare @StateTable table(StateId int, [State] nvarchar(100))
declare @CountryTable table(CountryId int, [Country] nvarchar(100))
declare @CountryState table(CountryId int, StateId int)
declare @tempTable table(StateId int, [State] nvarchar(100), CountryId int, [Country] nvarchar(100))
insert @tempTable
select ROW_NUMBER() over(order by ta.[state], ta.country) [stateId]
, ta.state
, DENSE_RANK() over(order by ta.country) [countryId]
, ta.country
from
(
select t.s.value('.', 'nvarchar(100)') [state]
, t.s.value('../@name', 'nvarchar(100)') [country]
from @x.nodes('countries/country/state') t(s)
)ta
insert @StateTable
select c.stateId, c.state
from @tempTable c
insert @CountryTable
select distinct c.countryId, c.country
from @tempTable c
insert @CountryState
select c.countryId, c.stateId
from @tempTable c
select * from @StateTable
select * from @CountryTable
select * from @CountryState
试试这个:
declare @x xml = '<?xml version="1.0" encoding="utf-8"?>
<countries author="Michael John Grove" title="Country, State-Province selections"
date="2008-Feb-05">
<country name="Afghanistan">
<state>Badakhshan</state>
<state>Badghis</state>
<state>Baghlan</state>
</country>
<country name="Albania">
<state>Berat</state>
<state>Bulqize</state>
<state>Delvine</state>
</country>
</countries>'
declare @StateTable table(StateId int, [State] nvarchar(100))
declare @CountryTable table(CountryId int, [Country] nvarchar(100))
declare @CountryState table(CountryId int, StateId int)
declare @tempTable table(StateId int, [State] nvarchar(100), CountryId int, [Country] nvarchar(100))
insert @tempTable
select ROW_NUMBER() over(order by ta.[state], ta.country) [stateId]
, ta.state
, DENSE_RANK() over(order by ta.country) [countryId]
, ta.country
from
(
select t.s.value('.', 'nvarchar(100)') [state]
, t.s.value('../@name', 'nvarchar(100)') [country]
from @x.nodes('countries/country/state') t(s)
)ta
insert @StateTable
select c.stateId, c.state
from @tempTable c
insert @CountryTable
select distinct c.countryId, c.country
from @tempTable c
insert @CountryState
select c.countryId, c.stateId
from @tempTable c
select * from @StateTable
select * from @CountryTable
select * from @CountryState
具有整数标识列作为主键的版本
declare @Country table
(
CountryID int identity primary key,
Name varchar(50)
)
declare @State table
(
StateID int identity primary key,
CountryID int,
Name varchar(50)
)
insert into @Country (Name)
select C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (CountryID, Name)
select Country.CountryID, S.S.value('.', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
inner join @Country as Country
on Country.Name = C.C.value('@name', 'varchar(50)')
declare @Country table
(
CountryName varchar(50) primary key
)
declare @State table
(
StateName varchar(50) primary key,
CountryName varchar(50)
)
insert into @Country (CountryName)
select distinct C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (StateName, CountryName)
select S.S.value('.', 'varchar(50)'),
C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
以及将名称用作主键的版本
declare @Country table
(
CountryID int identity primary key,
Name varchar(50)
)
declare @State table
(
StateID int identity primary key,
CountryID int,
Name varchar(50)
)
insert into @Country (Name)
select C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (CountryID, Name)
select Country.CountryID, S.S.value('.', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
inner join @Country as Country
on Country.Name = C.C.value('@name', 'varchar(50)')
declare @Country table
(
CountryName varchar(50) primary key
)
declare @State table
(
StateName varchar(50) primary key,
CountryName varchar(50)
)
insert into @Country (CountryName)
select distinct C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (StateName, CountryName)
select S.S.value('.', 'varchar(50)'),
C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
具有整数标识列作为主键的版本
declare @Country table
(
CountryID int identity primary key,
Name varchar(50)
)
declare @State table
(
StateID int identity primary key,
CountryID int,
Name varchar(50)
)
insert into @Country (Name)
select C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (CountryID, Name)
select Country.CountryID, S.S.value('.', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
inner join @Country as Country
on Country.Name = C.C.value('@name', 'varchar(50)')
declare @Country table
(
CountryName varchar(50) primary key
)
declare @State table
(
StateName varchar(50) primary key,
CountryName varchar(50)
)
insert into @Country (CountryName)
select distinct C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (StateName, CountryName)
select S.S.value('.', 'varchar(50)'),
C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
以及将名称用作主键的版本
declare @Country table
(
CountryID int identity primary key,
Name varchar(50)
)
declare @State table
(
StateID int identity primary key,
CountryID int,
Name varchar(50)
)
insert into @Country (Name)
select C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (CountryID, Name)
select Country.CountryID, S.S.value('.', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
inner join @Country as Country
on Country.Name = C.C.value('@name', 'varchar(50)')
declare @Country table
(
CountryName varchar(50) primary key
)
declare @State table
(
StateName varchar(50) primary key,
CountryName varchar(50)
)
insert into @Country (CountryName)
select distinct C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
insert into @State (StateName, CountryName)
select S.S.value('.', 'varchar(50)'),
C.C.value('@name', 'varchar(50)')
from @xml.nodes('/countries/country') as C(C)
cross apply C.C.nodes('state') as S(S)
我建议对此任务使用XML批量加载(SQLXML 4.0)。过程如下: 1.创建临时选项卡 2.创建XML和临时表结构之间的映射
3.使用单个调用执行实际加载我建议使用XML批量加载(SQLXML 4.0)来完成此任务。过程如下: 1.创建临时选项卡 2.创建XML和临时表结构之间的映射 3.使用单个调用执行实际加载