我正在尝试解析一个简单的 XML 文件。一旦我取消注释插入语句,它就会给我无效的列错误。
drop table #TEMP
drop table #TEMP_T
declare @XMl_DATA AS XML
set @XMl_DATA =
'<DocumentElement>
<Att_Table>
<L_ATTR_CD>GAS_FLOW_START_DATE</L_ATTR_CD>
<L_ATTR_DESC>GAS FLOW START DATE</L_ATTR_DESC>
<L_ATTR_VALUE>01/01/2012</L_ATTR_VALUE>
<R_ATTR_CD>EX_CTRCT_CO_ID</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE GID(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>GAS_FLOW_END_DATE</L_ATTR_CD>
<L_ATTR_DESC>GAS FLOW END DATE</L_ATTR_DESC>
<L_ATTR_VALUE>01/31/2012</L_ATTR_VALUE>
<R_ATTR_CD>EX_CTRCT_NBR</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE CONTRACT NUMBER(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>CTRCT_CO_ID</L_ATTR_CD>
<L_ATTR_DESC>GID(S)</L_ATTR_DESC>
<L_ATTR_VALUE />
<R_ATTR_CD>EX_RATE_CMPNT_CD</R_ATTR_CD>
<R_ATTR_DESC>EXCLUDE RATE COMPONENT CODE(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
<Att_Table>
<L_ATTR_CD>CTRCT_NBR</L_ATTR_CD>
<L_ATTR_DESC>DART STYLE CONTRACT NUMBER(S)</L_ATTR_DESC>
<L_ATTR_VALUE />
<R_ATTR_CD>EX_PT_ID_NBR</R_ATTR_CD>
<R_ATTR_DESC>EXCLIDE POINT ID NUMBER(S)</R_ATTR_DESC>
<R_ATTR_VALUE />
</Att_Table>
</DocumentElement>'
温度表:
CREATE TABLE #TEMP_T
(
ID INT IDENTITY(1,1),
ATT_CD VARCHAR(50),
ATT_CD_VALUE VARCHAR(1000)
)
SELECT
cast(Colx.query('data(L_ATTR_CD)') as varchar(max))as L_ATTR_CD,
cast(Colx.query('data(L_ATTR_VALUE)') as varchar(max))as L_ATTR_CD_VALUE,
cast(Colx.query('data(R_ATTR_CD)') as varchar(max)) as R_ATTR_CD,
cast(Colx.query('data(R_ATTR_VALUE)') as varchar(max))as R_ATTR_CD_VALUE
INTO #TEMP
FROM @XMl_DATA.nodes('DocumentElement/Att_Table') AS T(Colx)
--INSERT INTO #TEMP_T(ATT_CD,ATT_CD_VALUE)
--SELECT LTRIM(RTRIM(L_ATT_CD)),LTRIM(RTRIM(L_ATT_CD_VALUE))
--FROM #TEMP
--WHERE L_ATT_CD_VALUE <> 'NO_DATA'
--INSERT INTO #TEMP_T(ATT_CD,ATT_CD_VALUE)
--SELECT LTRIM(RTRIM(R_ATT_CD)),LTRIM(RTRIM(R_ATT_CD_VALUE))
--FROM #TEMP
--WHERE R_ATT_CD_VALUE <>'NO_DATA'
输出:
select * from #TEMP_T
select * from #TEMP