Je suis assez nouveau à ce sujet, alors s'il vous plaît ours avec moi. Je tente de transférer de nombreux fichiers XML dans Microsoft SQL Server en une seule fois. Malheureusement, je reçois une erreur disant que je dois convertir avec l'option de style 2. Je ne sais pas comment faire cela pour autant de fichiers.Utilisation de Convert Style Option 2 pour un grand nombre de fichiers XML à la fois dans Microsoft SQL Server
Voici ma question:
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;
Ce que je besoin est un moyen de résoudre ce qui traitera de tous les fichiers à la fois. Les erreurs indiquant qu'il ne peut pas charger en bloc en raison de l'inexistence du fichier n'est pas un problème car ces fichiers n'existent pas réellement. Dans l'image ci-dessous, les erreurs qui comptent sont entourées d'un symbole en violet. Ce ne sont pas les seuls cependant. Il y en a des centaines. Ce ne sont que quelques-uns de ceux que j'ai fait défiler. Les deux types d'erreur que vous voyez ci-dessous sont les seuls que l'exécution de ma requête a rapporté.
Les fichiers contiennent toutes les colonnes identiques, les mêmes types de données, mais des valeurs différentes pour les colonnes. Voici la version bébé d'un tel document XML.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rollcall-vote PUBLIC "-//US Congress//DTDs/vote v1.0 20031119 //EN" "http://clerk.house.gov/evs/vote.dtd">
<?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>6</rollcall-num>
<legis-num>H RES 5</legis-num>
<vote-question>On Agreeing to the Resolution</vote-question>
<vote-type>YEA-AND-NAY</vote-type>
<vote-result>Passed</vote-result>
<action-date>6-Jan-2015</action-date>
<action-time time-etz="17:30">5:30 PM</action-time>
<vote-desc>Adopting rules for the One Hundred Fourteenth Congress</vote-desc>
<vote-totals>
<totals-by-party-header>
<party-header>Party</party-header>
<yea-header>Yeas</yea-header>
<nay-header>Nays</nay-header>
<present-header>Answered “Presentâ€</present-header>
<not-voting-header>Not Voting</not-voting-header>
</totals-by-party-header>
<totals-by-party>
<party>Republican</party>
<yea-total>234</yea-total>
<nay-total>4</nay-total>
<present-total>1</present-total>
<not-voting-total>3</not-voting-total>
</totals-by-party>
<totals-by-party>
<party>Democratic</party>
<yea-total>0</yea-total>
<nay-total>168</nay-total>
<present-total>0</present-total>
<not-voting-total>5</not-voting-total>
</totals-by-party>
<totals-by-party>
<party>Independent</party>
<yea-total>0</yea-total>
<nay-total>0</nay-total>
<present-total>0</present-total>
<not-voting-total>0</not-voting-total>
</totals-by-party>
<totals-by-vote>
<total-stub>Totals</total-stub>
<yea-total>234</yea-total>
<nay-total>172</nay-total>
<present-total>1</present-total>
<not-voting-total>8</not-voting-total>
</totals-by-vote>
</vote-totals>
</vote-metadata>
<vote-data>
<recorded-vote>
<legislator name-id="A000374" sort-field="Abraham" unaccented-name="Abraham" party="R" state="LA" role="legislator">Abraham</legislator>
<vote>Yea</vote>
</recorded-vote>
<recorded-vote>
<legislator name-id="A000370" sort-field="Adams" unaccented-name="Adams" party="D" state="NC" role="legislator">Adams</legislator>
<vote>Nay</vote>
</recorded-vote>
<recorded-vote>
<legislator name-id="A000055" sort-field="Aderholt" unaccented-name="Aderholt" party="R" state="AL" role="legislator">Aderholt</legislator>
<vote>Yea</vote>
</recorded-vote>
</vote-data>
</rollcall-vote>
Dans ma réponse, j'utilise 'DECLARE @xmlString NVARCHAR (MAX)'. Si vous rencontrez des problèmes, essayez gthe sans le 'N' (simple' VARCHAR (MAX) '). J'ai eu quelques problèmes avec l'encodage de votre fichier sur le disque ... – Shnugo