I am a little new at this so please bear with me. I am attempting to generate tables from an xml document. Due to some of the contents of the XML document I had to use the convert method, which I am not very familiar with. As a result I get an error. I am not sure why though. I am using Microsoft SQL Server 2016.
Here is my query:
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
Here is a baby version of my XML document
<?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
UPDATE
This is where I am at now:
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
UPDATE
This is my 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)'')
,#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;
UPDATE
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;
UPDATE
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;
NEW ERROR
update
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;
ANOTHER UPDATE
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;
In the picture below you can see the error. The files that don't exist have the same old bulk file not found message. But the files that do exist (like 006, 012, and 014) have a different message.
As the essence of all your passed questions I pick
There are many xml files in the same directory (well, many in one and many in another)
All files have the same structure
You want to read them into a table
My Assumptions (which you have to adapt to your needs)
The names are computable (in my case "xml1", "xml2", "xml3" ...)
For my testscenario I created three XML files (called "xml1.xml", "xml2.xml" and "xml3.xml" with the content of your baby version. Therefore the loop stops at 4
Try this
I create a staging table with the fitting structure (do not bother about datetime conversion in this first step!)
I use a counter to compute the file's name within a loop
I use a WHILE loop
Within this loop the full statement is dynamically created. It will read your XML from the file into a variable and push all the values into the table.
With EXEC this command is executed
Check the result with a simple SELECT
This is the code
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;
UPDATE
If the file names are computeable, but there are gaps, just change the simple EXEC(#cmd) to this:
BEGIN TRY
EXEC(#command);
END TRY
BEGIN CATCH
--Might make sense to write some error meta data into a log table
END CATCH
This will ignore all file-not-found errors...
I have a Microsoft SQL Server R2 2008. And i see it first time in my life.
I have sql procedure:
DECLARE #RC int
DECLARE #Id uniqueidentifier
DECLARE #Segment_ID uniqueidentifier
DECLARE #SDate datetime
DECLARE #EDate datetime
DECLARE #withBig bit
DECLARE #withKm bit
DECLARE #withGeo bit
DECLARE #withDescr bit
-- TODO: задайте здесь значения параметров.
EXECUTE #RC = [Request_BusStation]
#Id
,#Segment_ID
,#SDate
,#EDate
,#withBig
,#withKm
,#withGeo
,#withDescr
GO
How i understand its just calling of procedure not thetself. But procedure too bit to copy it here.
AND have a table:
CREATE TABLE [BusStation](
[Id] [uniqueidentifier] NOT NULL,
[Segment_ID] [uniqueidentifier] NOT NULL,
[Dist] [decimal](18, 4) NOT NULL,
[Kod_Spr012] [smallint] NOT NULL,
[Square] [decimal](18, 6) NULL,
[OperationStartDate] [date] NULL,
[BallanceCost] [decimal](18, 6) NULL,
[DepreciatedCost] [decimal](18, 6) NULL,
[ChargesNorm] [decimal](18, 6) NULL,
[DocumentName] [varchar](100) NULL,
[DocumentNum] [varchar](100) NULL,
[DocumentDate] [date] NULL,
[Authority] [varchar](100) NULL,
[Kod_Spr091] [smallint] NOT NULL,
[HasWaysideStop] [bit] NOT NULL,
[HasLanding] [bit] NOT NULL,
[HasSpeedTransitArea] [bit] NOT NULL,
[LenSpeedTransitArea] [decimal](18, 6) NULL,
[YearBuilt] [smallint] NULL,
[YearMajorOverhaul] [smallint] NULL,
[Kod_Spr019] [smallint] NOT NULL,
[TechCond] [varbinary](max) NULL,
[LandCont] [varbinary](max) NULL,
[LandContDate] [date] NULL,
[LandContStartDate] [date] NULL,
[LandContEndDate] [date] NULL,
[Kod_Spr120] [smallint] NULL,
[E_Date_Begin] [datetime] NOT NULL,
[E_Date_End] [datetime] NULL,
[E_Date_Stop] [datetime] NULL,
Now i want to call this procedure for each row of table.
Its possible?
Yes, you can use a cursor that selects all the rows in the table and iteratively calls the stored procedure.
I would suggest that you may have a design issue before going down that route though. If the stored procedure needs to be called for every row in the table, you may be able to write a stored procedure that simply does what your current sp does to all the rows instead of a single row operation.
You have not provided what the sp is doing so I can only speculate here.
As mentioned in my comment, the only way I would know how to do that is using a CURSOR. Here is some sample code (untested of course):
DECLARE #ID INT
DECLARE #Segment_ID uniqueidentifier
DECLARE #getAccountID CURSOR
SET #BusStationCursor = CURSOR FOR
SELECT Id, Segment_ID --(etc: all the fields you need)
FROM BusStation
OPEN #BusStationCursor
FETCH NEXT FROM #BusStationCursor INTO #ID, #Segment_ID
WHILE ##FETCH_STATUS = 0
BEGIN
--CALL YOUR SP HERE
PRINT #ID
PRINT #Segment_ID
FETCH NEXT FROM #BusStationCursor INTO #ID, #Segment_ID
END
CLOSE #BusStationCursor
DEALLOCATE #BusStationCursor
This should help as well:
http://msdn.microsoft.com/en-us/library/ms180169.aspx
Good luck.