使用convert进行从XML到SQL Server的数据传输后,无法生成表

时间:2017-02-21 19:57:12

标签: sql sql-server xml data-conversion sql-convert

我对此有点新意,请耐心等待。我试图从xml文档生成表。由于XML文档的一些内容,我不得不使用convert方法,我不是很熟悉。结果我收到一个错误。我不知道为什么。我正在使用Microsoft SQL Server 2016。

这是我的问题:

WITH XmlFile (Contents) AS 
(
    SELECT 
        CONVERT (XML, BulkColumn,2)
    FROM 
        OPENROWSET (BULK 'C:\Users\Owner\Documents\congress\House votes\114 congress 2015\passage\roll705.xml', SINGLE_BLOB) AS roll705
)
SELECT *
FROM XmlFile
GO

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, xmlfile

SELECT *
FROM OPENXML (@hdoc, '/rollcall-vote/vote-metadata', 1)
WITH (
    congress tinyint,
    [session] char(3),
    chamber varchar(40),
    [rollcall-num] smallint,
    [legis-num] varchar(20),
    [vote-question] varchar(1000),
    [vote-result] varchar (20),
    [action-date] date,
    [vote-desc] varchar(1000)
)

EXEC sp_xml_removedocument @hdoc

以下是我的XML文档的婴儿版本

<?xml-stylesheet type="text/xsl" href="http://clerk.house.gov/evs/vote.xsl"?>
<rollcall-vote>
  <vote-metadata>
    <majority>R</majority>
    <congress>114</congress>
    <session>1st</session>
    <chamber>U.S. House of Representatives</chamber>
    <rollcall-num>705</rollcall-num>
    <legis-num>H R 2029</legis-num>
    <vote-question>On Concurring in Senate Amdt with Amdt Specified in Section 3(a) of H.Res. 566</vote-question>
    <vote-type>YEA-AND-NAY</vote-type>
    <vote-result>Passed</vote-result>
    <action-date>18-Dec-2015</action-date>
    <action-time time-etz="09:49">9:49 AM</action-time>
    <vote-desc>Making appropriations for military construction, the Department of Veterans Affairs, and related agencies for the fiscal year ending September 30, 2016, and for other purposes</vote-desc>
</rollcall-vote>

a picture of my error

更新

这就是我现在所处的位置:

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' +  CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[legislator],[state],[party],[vote],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],)
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''../maority[1]'',N''nvarchar(max)'') 
          ,v.value(N''../congress[1]'',N''int'') 
          ,v.value(N''../session[1]'',N''nvarchar(max)'') 
          ,v.value(N''../chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''../rollcall-num[1]'',N''int'') 
          ,v.value(N''../legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''../vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''../vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''../vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''../action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''../action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''../vote-desc[1]'',N''nvarchar(max)'') 
          ,@xml
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

this is a screenshot of my data in my folder this is a picture of my alterations to the catch block

更新

这是我的疑问。

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' + REPLACE(STR(@Counter,3),' ','0') + CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],)
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''majority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
          ,@xml
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH print 'error'
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

Result of executing above query

更新

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' + REPLACE(STR(@Counter,3),' ','0') + CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],)
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''majority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
          ,v.value(N''sourceXML[1]'',N''XML)'')
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH print 'error'
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

upon execution

folder with xmls message

更新

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' + REPLACE(STR(@Counter,3),' ','0') + CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],)
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''majority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
          ,@xml
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH print ERROR_MESSAGE()
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

the error

新错误

enter image description here enter image description here

更新

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' + REPLACE(STR(@Counter,3),' ','0') + CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],[sourceXML])
    SELECT ' +
         ',v.value(N''majority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
          ,@xml
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH print ERROR_MESSAGE()
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

另一个更新

CREATE TABLE [dbo].[staagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL,
    [sourceXML] [XML] NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<800
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\Users\Owner\Documents\congress\House votes\114 congress 2015\Passage\roll' + REPLACE(STR(@Counter,3),' ','0') + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.staagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc],[sourceXML])
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''majority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
          ,@xml
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH print ERROR_MESSAGE()
END CATCH;
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.staagingTable;
GO
DROP TABLE dbo.staagingTable;

在下图中,您可以看到错误。不存在的文件具有相同的旧批量文件未找到消息。但是存在的文件(如006,012和014)有不同的信息。

screenshot

1 个答案:

答案 0 :(得分:1)

作为我所选择的所有问题的本质

  • 同一目录中有许多xml文件(好吧,很多在一个,很多在另一个目录中)
  • 所有文件具有相同的结构
  • 您想将它们读入表格

我的假设(你必须适应你的需要)

  • 名称是可计算的(在我的情况下是“xml1”,“xml2”,“xml3”......)
  • 对于我的测试场景,我创建了三个XML文件(称为“xml1.xml”,“xml2.xml”和“xml3.xml”,其中包含 baby version 的内容。因此循环停止于4

试试这个

  • 我使用拟合结构创建 staging 表(在第一步中不要担心日期时间转换!)
  • 我使用计数器来计算循环中的文件名
  • 我使用WHILE循环
  • 在此循环中,动态创建完整语句。它会将文件中的XML读入变量并将所有值都推送到表中。
  • 执行EXEC此命令
  • 使用简单的SELECT
  • 检查结果

这是代码

CREATE TABLE [dbo].[stagingTable](
    [Counter] INT NOT NULL,
    [majority] [nvarchar](max) NULL,
    [congress] [int] NULL,
    [session] [nvarchar](max) NULL,
    [chamber] [nvarchar](max) NULL,
    [rollcall-num] [int] NULL,
    [legis-num] [nvarchar](max) NULL,
    [vote-question] [nvarchar](max) NULL,
    [vote-type] [nvarchar](max) NULL,
    [vote-result] [nvarchar](max) NULL,
    [action-date] [nvarchar](max) NULL,
    [action-time] [nvarchar](max) NULL,
    [vote-desc] [nvarchar](max) NULL
);
GO

DECLARE @Counter INT=1;
DECLARE @command VARCHAR(MAX);

WHILE @Counter<4
BEGIN
    SET @command=
    '
    DECLARE @xml XML=
    (
    SELECT BulkColumn
    FROM OPENROWSET (BULK ''C:\StackOverflow\xml' +  CAST(@Counter AS VARCHAR(10)) + '.xml'', SINGLE_BLOB) AS c
    );

    INSERT INTO dbo.stagingTable(Counter,majority,congress,[session],chamber
              ,[rollcall-num],[legis-num],[vote-question],[vote-type]
              ,[vote-result],[action-date],[action-time],[vote-desc])
    SELECT ' +  CAST(@Counter AS VARCHAR(10)) + 
         ',v.value(N''maority[1]'',N''nvarchar(max)'') 
          ,v.value(N''congress[1]'',N''int'') 
          ,v.value(N''session[1]'',N''nvarchar(max)'') 
          ,v.value(N''chamber[1]'',N''nvarchar(max)'')
          ,v.value(N''rollcall-num[1]'',N''int'') 
          ,v.value(N''legis-num[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-question[1]'',N''nvarchar(max)'')
          ,v.value(N''vote-type[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-result[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-date[1]'',N''nvarchar(max)'') 
          ,v.value(N''action-time[1]'',N''nvarchar(max)'') 
          ,v.value(N''vote-desc[1]'',N''nvarchar(max)'') 
    FROM @xml.nodes(N''/rollcall-vote/vote-metadata'') AS A(v);
    ';
    EXEC(@command);
    SET @Counter=@Counter+1;
END
SELECT * FROM dbo.stagingTable;
GO
DROP TABLE dbo.stagingTable;

更新

如果文件名是可计算的,但存在空白,只需将简单EXEC(@cmd)更改为:

BEGIN TRY
EXEC(@command);
END TRY
BEGIN CATCH
--Might make sense to write some error meta data into a log table
END CATCH

这将忽略所有文件未找到错误......