将XML文件放入数据库时遇到问题。
我的XML文件如下所示:
<teryt>
<catalog name="SIMC" type="all" date="2014-01-01">
<row>
<col name="WOJ">12</col>
<col name="POW">10</col>
<col name="GMI">16</col>
<col name="RODZ_GMI">5</col>
<col name="RM">00</col>
<col name="MZ">1</col>
<col name="NAZWA">Zagórze</col>
<col name="SYM">1047995</col>
<col name="SYMPOD">0465897</col>
<col name="STAN_NA">2014-01-01</col>
</row>
<row>
<col name="WOJ">14</col>
<col name="POW">36</col>
<col name="GMI">02</col>
<col name="RODZ_GMI">2</col>
<col name="RM">00</col>
<col name="MZ">1</col>
<col name="NAZWA">Wyspa</col>
<col name="SYM">1058763</col>
<col name="SYMPOD">0631864</col>
<col name="STAN_NA">2014-01-01</col>
</row>
</catalog>
每个标记包含10个标记,其中包含应放入列的数据。那个标签是相同的,唯一不同的是name属性(它是列的名称 - 那个标签)。
我尝试将其放入一个SQL表中,其列名为:WOJ,POW,GMI,RODZ_GMI,RM,MZ,NAZWA,SYM,SYMPOD,STAN_NA。
这是我用2个游标尝试的。但它在第一行被某种方式阻止 - 这是我预测的错误。
DECLARE @DocID INT;
DECLARE @DocXML XML;
DECLARE @sqlstmt nVARCHAR(MAX);
DECLARE @results table (result xml)
--Build the Dynamic SQL Statement to get the data from the xml file
SET @sqlstmt= 'SELECT * FROM OPENROWSET ( BULK ''C:\SIMC.xml'', SINGLE_CLOB )AS xmlData'
-- Insert the results of the dynamic SQL Statement into the temporary table variable.
INSERT INTO @results EXEC (@sqlstmt)
--DECLARE @xmlDoc XML
SELECT @DocXML = result FROM @results
EXECUTE sp_xml_preparedocument @DocID OUTPUT, @DocXML;
Declare @WOJ integer, @POW integer, @GMI integer,
@RODZ_GMI integer, @RM varchar(max), @MZ integer,
@NAZWA varchar(max), @SYM varchar(max),@SYMPOD varchar(max),
@STAN_NA date
Declare @zmiennanacolumn xml
declare @rowid int
declare @dane varchar
set @rowid=1;
DECLARE kursorMain CURSOR LOCAL FORWARD_ONLY FOR
SELECT
*
from OPENXML(@DocID, 'teryt/catalog/row', 2)
Open KursorMain
fetch next from KursorMain into @rowid,@zmiennanacolumn
WHILE @@FETCH_STATUS = 0
BEGIN
--tutaj robić coś z tymi danymi które mam
DECLARE @h2 int
EXEC sp_xml_preparedocument @h2 output, @zmiennanacolumn
DECLARE kursorMainInside CURSOR LOCAL FORWARD_ONLY FOR
select [col]
from OPENXML(@h2,'',2) with [col] varchar
Open kursorMainInside
declare @columnnumber int
set @columnnumber=1
FETCH NEXT FROM KursorMainInside into @dane
WHILE @@FETCH_STATUS = 0
BEGIN
if(@columnnumber=1) set @WOJ = @dane
else if(@columnnumber=2) set @POW = @dane
else if(@columnnumber=3) set @GMI = @dane
else if(@columnnumber=4) set @RODZ_GMI = @dane
else if(@columnnumber=5) set @RM = @dane
else if(@columnnumber=6) set @MZ = @dane
else if(@columnnumber=7) set @NAZWA = @dane
else if(@columnnumber=8) set @SYM = @dane
else if(@columnnumber=9) set @SYMPOD = @dane
else set @STAN_NA = @dane
--tutaj patrzeć które przejście i dodawać do inta
set @columnnumber = @columnnumber +1
FETCH NEXT FROM KursorMainInside into @dane
Insert into [dbo.SIMC] values (@rowid, @WOJ , @POW , @GMI ,
@RODZ_GMI , @RM , @MZ ,
@NAZWA , @SYM ,@SYMPOD ,
@STAN_NA)
END
EXEC sp_xml_removedocument @h2
CLOSE KursorMainInside
DEALLOCATE KursorMainInside
set @rowid=@rowid+1
FETCH NEXT FROM KursorMain into @rowid,@zmiennanacolumn
END
EXEC sp_xml_removedocument @DocID
CLOSE KursorMain
DEALLOCATE KursorMain
答案 0 :(得分:2)
假设您在SQL Server变量@Input
中有有效 XML,您可以使用以下内容:
INSERT INTO dbo.YourTableName(WOJ, POW, GMI, RODZ_GMI, RM, MZ, NAZWA, SYM, SYMPOD, STAN_NA)
SELECT
WOJ = xc.value('(col[@name="WOJ"])[1]', 'varchar(50)'),
POW = xc.value('(col[@name="POW"])[1]', 'varchar(50)'),
GMI = xc.value('(col[@name="GMI"])[1]', 'varchar(50)'),
RODZ_GMI = xc.value('(col[@name="RODZ_GMI"])[1]', 'varchar(50)'),
RM =xc.value('(col[@name="RM"])[1]', 'varchar(50)'),
MZ =xc.value('(col[@name="MZ"])[1]', 'varchar(50)'),
NAZWA =xc.value('(col[@name="NAZWA"])[1]', 'varchar(50)'),
SYM =xc.value('(col[@name="SYM"])[1]', 'varchar(50)'),
SYMPOD =xc.value('(col[@name="SYMPOD"])[1]', 'varchar(50)'),
TAN_NA =xc.value('(col[@name="STAN_NA"])[1]', 'varchar(50)')
FROM
@input.nodes('/teryt/catalog/row') AS XT(XC)
答案 1 :(得分:0)
我还设法为大数据做这件事。这是大型xml文件的解决方案 - 您的工作适用于小型文件。
CREATE TABLE [dbo].[SIMC](
[ID] [int] IDENTITY(1,1) NOT NULL,
[WOJ] [int] NULL,
[POW] [int] NULL,
[GMI] [int] NULL,
[RODZ_GMI] [int] NULL,
[RM] [varchar](max) NULL,
[MZ] [int] NULL,
[NAZWA] [varchar](max) NULL,
[SYM] [varchar](max) NULL,
[SYMPOD] [varchar](max) NULL,
[STAN_NA] [date] NULL
) ON [PRIMARY]
GO
DECLARE @DocID INT;
DECLARE @DocXML XML;
DECLARE @sqlstmt nVARCHAR(MAX);
DECLARE @results table (result xml)
--Build the Dynamic SQL Statement to get the data from the xml file
SET @sqlstmt= 'SELECT * FROM OPENROWSET ( BULK ''C:\SIMC.xml'', SINGLE_BLOB )AS xmlData'
-- Insert the results of the dynamic SQL Statement into the temporary table variable.
INSERT INTO @results EXEC (@sqlstmt)
--DECLARE @xmlDoc XML
SELECT @DocXML = result FROM @results
EXECUTE sp_xml_preparedocument @DocID OUTPUT, @DocXML;
Declare @WOJ integer, @POW integer, @GMI integer,
@RODZ_GMI integer, @RM varchar(max), @MZ integer,
@NAZWA varchar(max), @SYM varchar(max),@SYMPOD varchar(max),
@STAN_NA date
DECLARE kursorMain CURSOR LOCAL FORWARD_ONLY FOR
SELECT *
FROM OPENXML (@DocID, 'teryt/catalog/row',2)
WITH
(
WOJ varchar(max) 'col[@name="WOJ"]',
POW varchar(max) 'col[@name="POW"]',
GMI varchar(max) 'col[@name="GMI"]',
RODZ_GMI varchar(max) 'col[@name="RODZ_GMI"]',
RM varchar(max) 'col[@name="RM"]',
MZ varchar(max) 'col[@name="MZ"]',
NAZWA varchar(max) 'col[@name="NAZWA"]',
SYM varchar(max) 'col[@name="SYM"]',
SYMPOD varchar(max) 'col[@name="SYMPOD"]',
STAN_NA varchar(max) 'col[@name="STAN_NA"]'
)
Open KursorMain
fetch next from KursorMain into @WOJ , @POW , @GMI ,
@RODZ_GMI , @RM , @MZ ,
@NAZWA , @SYM ,@SYMPOD ,
@STAN_NA
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO SIMC(WOJ, POW, GMI, RODZ_GMI, RM, MZ, NAZWA, SYM, SYMPOD, STAN_NA)
values (@WOJ , @POW , @GMI ,
@RODZ_GMI , @RM , @MZ ,
@NAZWA , @SYM ,@SYMPOD ,
@STAN_NA)
fetch next from KursorMain into @WOJ , @POW , @GMI ,
@RODZ_GMI , @RM , @MZ ,
@NAZWA , @SYM ,@SYMPOD ,
@STAN_NA
END
EXEC sp_xml_removedocument @DocID
CLOSE KursorMain
DEALLOCATE KursorMain