我在XML文件下面。
<steps>
<step debug="Checking dialed number" Seq="63" dbtime="0" calltime="05/01/2014 20:00:49" />
<step name="Playing welcome" Seq="2" Language="spanish" calltime="05/01/2014 20:00:50" />
<step debug="Message played" Seq="64" playtime="31" calltime="05/01/2014 20:00:50" />
<step debug="Check message" Seq="63" dbtime="0" calltime="05/01/2014 20:00:50" />
<step name="Prompting" Seq="3" calltime="05/01/2014 20:00:50" />
<step debug="Message played" Seq="64" playtime="9158" calltime="05/01/2014 20:00:59" />
<step debug="Input received" Seq="65" usertime="18486" calltime="05/01/2014 20:01:25" />
<step name="User Input : 1" Seq="12" Input="1" calltime="05/01/2014 20:01:25" />
<step debug="Validate ID" Seq="63" dbtime="203" calltime="05/01/2014 20:01:25" />
<step debug="Check Multi" Seq="63" dbtime="16" calltime="05/01/2014 20:01:25" />
<step name="Prompting for" Seq="20" calltime="05/01/2014 20:01:25" />
<step debug="Message played" Seq="64" playtime="31" calltime="05/01/2014 20:01:25" />
<step debug="Input received" Seq="65" usertime="3495" calltime="05/01/2014 20:01:29" />
<step name="User Input : 61" Seq="21" DutyCode="61" calltime="05/01/2014 20:01:29" />
</steps>
想要在下表中按列插入数据。
create table xmltest
( debug varchar(40),
name varchar(40),
lang varchar(15),
paytime int,
dutycode int,
usertime int,
seq int,
dbtime int,
calltime datetime
);
由于
答案 0 :(得分:1)
试试这个:
-- Assuming you have your XML in a SQL variable of type XML ....
DECLARE @Input XML = '<steps>
<step debug="Checking dialed number" Seq="63" dbtime="0" calltime="05/01/2014 20:00:49" />
<step name="Playing welcome" Seq="2" Language="spanish" calltime="05/01/2014 20:00:50" />
<step debug="Message played" Seq="64" playtime="31" calltime="05/01/2014 20:00:50" />
<step debug="Check message" Seq="63" dbtime="0" calltime="05/01/2014 20:00:50" />
<step name="Prompting" Seq="3" calltime="05/01/2014 20:00:50" />
<step debug="Message played" Seq="64" playtime="9158" calltime="05/01/2014 20:00:59" />
<step debug="Input received" Seq="65" usertime="18486" calltime="05/01/2014 20:01:25" />
<step name="User Input : 1" Seq="12" Input="1" calltime="05/01/2014 20:01:25" />
<step debug="Validate ID" Seq="63" dbtime="203" calltime="05/01/2014 20:01:25" />
<step debug="Check Multi" Seq="63" dbtime="16" calltime="05/01/2014 20:01:25" />
<step name="Prompting for" Seq="20" calltime="05/01/2014 20:01:25" />
<step debug="Message played" Seq="64" playtime="31" calltime="05/01/2014 20:01:25" />
<step debug="Input received" Seq="65" usertime="3495" calltime="05/01/2014 20:01:29" />
<step name="User Input : 61" Seq="21" DutyCode="61" calltime="05/01/2014 20:01:29" />
</steps>'
-- insert into the target table
INSERT INTO dbo.xmltest(debug, name, lang, paytime, dutycode, usertime, seq, dbtime, calltime)
-- select from all <step> elements in the XML
SELECT
debug = xc.value('@debug', 'varchar(40)'),
name = xc.value('@name', 'varchar(40)'),
lang = xc.value('@Language', 'varchar(15)'),
paytime = xc.value('@playtime', 'int'),
dutycode = xc.value('@DutyCode', 'int'),
usertime = xc.value('@usertime', 'int'),
seq = xc.value('@Seq', 'int'),
dbtime = xc.value('@dbtime', 'int'),
calltime = xc.value('@calltime', 'DateTime')
FROM
@Input.nodes('/steps/step') AS XT(XC)