我有一个存储过程,它接收“Survey”对象的集合并将它们分成适当的表。
“Survey”对象有两个子对象,“Erosions”和“Spans”。
以下是我可能使用的XML示例:
<Surveys>
<Survey>
<SurveyID>35</SurveyID>
<CulvertID>5</CulvertID>
<PRNo>587180</PRNo>
<MP>5.243</MP>
<RefMiles>1.500</RefMiles>
<PtID>ABCGEFGH</PtID>
<FWVersion>10</FWVersion>
<SurveyDate>2012-08-27</SurveyDate>
<FutureComments>7c36fe43-78cb-436e-81cf-f416aa63c8fc</FutureComments>
<Erosions>
<Erosion>
<ID>160</ID>
<SurveyID>35</SurveyID>
<Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
</Erosion>
<Erosion>
<ID>161</ID>
<SurveyID>35</SurveyID>
<Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
</Erosion>
</Erosions>
<Spans>
<Span>
<ID>88</ID>
<SurveyID>35</SurveyID>
<Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
</Span>
<Span>
<ID>89</ID>
<SurveyID>35</SurveyID>
<Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
</Span>
</Spans>
</Survey>
</Surveys>
我已将存储过程定义上传到以下位置(太大而无法粘贴)。 Download SQL
当我发送上面提供的XML示例时,该过程大约需要10秒钟才能完成,而且只需要一个非常小的XML字符串。更新/插入包含任何“VARBINARY(MAX)”列中的值的记录可能需要几分钟。
我是一个真正的Xquery新手......不确定我做错了什么,但表现绝对可怕。有什么建议吗?
使用解决方案进行编辑:
按照下面的@wBob建议,我为我的存储过程提出了this SQL。我最终重新定义了桌面上的PK,以便更加“准确”。它不是生成的int ID,而是基于实际的数据字段。这允许我使用merge来使用由实际数据值定义的外键来插入子对象,而不是使用我必须检索的生成的数字。
答案 0 :(得分:3)
我已经改变了你的(非常大的)过程。请记住这是演示原则的示例脚本(即带有OUTPUT子句的MERGE),由您来完成并测试它。例如,您需要测试所有列是否正确插入/更新,检查您的数据类型,以及我删除了NULLIF等的位置。我还会添加TRY / CATCH错误和事务处理。请注意,我还根据surveyId完成了MERGE - 不确定这是否正确?
如果我是你,我会花一些时间熟悉MERGE和OUTPUT条款,然后继续这样做,但祝你好运,让我们知道你是如何继续的。
我还在我的完整测试台上添加了link。此脚本在tempdb中是安全且完全可重新运行的。
ALTER PROCEDURE [dbo].[sp_SaveSurvey]
@XMLobject XML
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #tmp_survey ( surveyId INT PRIMARY KEY, erosions XML, spans XML )
BEGIN TRAN
------------------------------------------------------------------------------------------------
-- MERGE dbo.CULV_StreamCrossingSurvey START
------------------------------------------------------------------------------------------------
SET IDENTITY_INSERT dbo.CULV_StreamCrossingSurvey ON
;MERGE dbo.CULV_StreamCrossingSurvey AS target
USING
(
SELECT
CAST(NULLIF(X.survey.value('(SurveyID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS SurveyID,
CAST(NULLIF(X.survey.value('(CulvertID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS CulvertID,
NULLIF(X.survey.value('(County/text())[1]', 'VARCHAR(50)'), '') AS County,
NULLIF(X.survey.value('(RoadName/text())[1]', 'VARCHAR(50)'), '') AS RoadName,
CAST(NULLIF(X.survey.value('(PRNo/text())[1]', 'VARCHAR(50)'), '') AS INT) AS PRNo,
CAST(NULLIF(X.survey.value('(MP/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,3)) AS MP,
CAST(NULLIF(X.survey.value('(RefMiles/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,3)) AS RefMiles,
NULLIF(X.survey.value('(PtID/text())[1]', 'VARCHAR(8)'), '') AS PtID,
CAST(NULLIF(X.survey.value('(FWVersion/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS FWVersion,
CAST(NULLIF(X.survey.value('(Latitude/text())[1]', 'VARCHAR(200)'), '') AS FLOAT) AS Latitude,
CAST(NULLIF(X.survey.value('(Longitude/text())[1]', 'VARCHAR(200)'), '') AS FLOAT) AS Longitude,
NULLIF(X.survey.value('(Waterway/text())[1]', 'VARCHAR(50)'), '') AS Waterway,
NULLIF(X.survey.value('(SiteID/text())[1]', 'VARCHAR(50)'), '') AS SiteID,
NULLIF(X.survey.value('(Observers/text())[1]', 'VARCHAR(50)'), '') AS Observers,
CAST(NULLIF(X.survey.value('(SurveyDate/text())[1]', 'VARCHAR(200)'), '') AS DATE) AS SurveyDate,
NULLIF(X.survey.value('(AdjacentLandowner/text())[1]', 'VARCHAR(MAX)'), '') AS AdjacentLandowner,
NULLIF(X.survey.value('(LocationComments/text())[1]', 'VARCHAR(MAX)'), '') AS LocationComments,
CAST(NULLIF(X.survey.value('(CrossingType/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS CrossingType,
NULLIF(X.survey.value('(StructureShape/text())[1]', 'VARCHAR(50)'), '') AS StructureShape,
NULLIF(X.survey.value('(InletStructure/text())[1]', 'VARCHAR(50)'), '') AS InletStructure,
NULLIF(X.survey.value('(OutletStructure/text())[1]', 'VARCHAR(50)'), '') AS OutletStructure,
NULLIF(X.survey.value('(OutletType/text())[1]', 'VARCHAR(50)'), '') AS OutletType,
NULLIF(X.survey.value('(SubstrateInStructure/text())[1]', 'VARCHAR(50)'), '') AS SubstrateInStructure,
NULLIF(X.survey.value('(GeneralCondition/text())[1]', 'VARCHAR(50)'), '') AS GeneralCondition,
CAST(NULLIF(X.survey.value('(PluggedPercent/text())[1]', 'VARCHAR(MAX)'), '') AS SMALLINT) AS PluggedPercent,
NULLIF(X.survey.value('(PluggedLocation/text())[1]', 'VARCHAR(50)'), '') AS PluggedLocation,
CAST(NULLIF(X.survey.value('(CrushedPercent/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS CrushedPercent,
NULLIF(X.survey.value('(CrushedLocation/text())[1]', 'VARCHAR(50)'), '') AS CrushedLocation,
CAST(NULLIF(X.survey.value('(IsRustedThrough/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsRustedThrough,
NULLIF(X.survey.value('(StructureInterior/text())[1]', 'VARCHAR(50)'), '') AS StructureInterior,
CAST(NULLIF(X.survey.value('(StructureWaterDepthInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterDepthInlet,
CAST(NULLIF(X.survey.value('(StructureWaterDepthOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterDepthOutlet,
CAST(NULLIF(X.survey.value('(StructureEmbeddedDepthInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureEmbeddedDepthInlet,
CAST(NULLIF(X.survey.value('(StructureEmbeddedDepthOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureEmbeddedDepthOutlet,
CAST(NULLIF(X.survey.value('(StructureWaterVelocityInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityInlet,
CAST(NULLIF(X.survey.value('(StructureWaterVelocityOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityOutlet,
CAST(NULLIF(X.survey.value('(StructureWaterVelocityMeasured/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityMeasured,
NULLIF(X.survey.value('(StructureWaterVelocityMeasuredWith/text())[1]', 'VARCHAR(50)'), '') AS StructureWaterVelocityMeasuredWith,
CAST(NULLIF(X.survey.value('(IsPerched/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsPerched,
CAST(NULLIF(X.survey.value('(PerchHeight/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS PerchHeight,
NULLIF(X.survey.value('(StreamFlow/text())[1]', 'VARCHAR(50)'), '') AS StreamFlow,
CAST(NULLIF(X.survey.value('(IsScourPoolPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsScourPoolPresent,
CAST(NULLIF(X.survey.value('(ScourPoolLength/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolLength,
CAST(NULLIF(X.survey.value('(ScourPoolWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolWidth,
CAST(NULLIF(X.survey.value('(ScourPoolDepth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolDepth,
CAST(NULLIF(X.survey.value('(IsUpstreamPondPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsUpstreamPondPresent,
CAST(NULLIF(X.survey.value('(UpstreamPondLength/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS UpstreamPondLength,
CAST(NULLIF(X.survey.value('(UpstreamPondWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS UpstreamPondWidth,
CAST(NULLIF(X.survey.value('(RiffleWaterDepth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWaterDepth,
CAST(NULLIF(X.survey.value('(RiffleBankfullWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleBankfullWidth,
CAST(NULLIF(X.survey.value('(RiffleWettedWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWettedWidth,
CAST(NULLIF(X.survey.value('(RiffleWaterVelocity/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWaterVelocity,
NULLIF(X.survey.value('(RiffleMeasuredWith/text())[1]', 'VARCHAR(50)'), '') AS RiffleMeasuredWith,
NULLIF(X.survey.value('(RiffleSubstrate/text())[1]', 'VARCHAR(50)'), '') AS RiffleSubstrate,
NULLIF(X.survey.value('(RoadSurface/text())[1]', 'VARCHAR(50)'), '') AS RoadSurface,
NULLIF(X.survey.value('(RoadCondition/text())[1]', 'VARCHAR(50)'), '') AS RoadCondition,
CAST(NULLIF(X.survey.value('(RoadWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RoadWidth,
NULLIF(X.survey.value('(LocationOfLowPoint/text())[1]', 'VARCHAR(50)'), '') AS LocationOfLowPoint,
NULLIF(X.survey.value('(RunOffPath/text())[1]', 'VARCHAR(50)'), '') AS RunOffPath,
CAST(NULLIF(X.survey.value('(FillDepthUpStream/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS FillDepthUpStream,
CAST(NULLIF(X.survey.value('(FillDepthDownStream/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS FillDepthDownStream,
NULLIF(X.survey.value('(SlopeUpStream/text())[1]', 'VARCHAR(50)'), '') AS SlopeUpStream,
NULLIF(X.survey.value('(SlopeDownStream/text())[1]', 'VARCHAR(50)'), '') AS SlopeDownStream,
CAST(NULLIF(X.survey.value('(ApproachLengthLeft/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ApproachLengthLeft,
CAST(NULLIF(X.survey.value('(ApproachLengthRight/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ApproachLengthRight,
NULLIF(X.survey.value('(ApproachSlopeLeft/text())[1]', 'VARCHAR(50)'), '') AS ApproachSlopeLeft,
NULLIF(X.survey.value('(ApproachSlopeRight/text())[1]', 'VARCHAR(50)'), '') AS ApproachSlopeRight,
NULLIF(X.survey.value('(VegetationDitchLeft/text())[1]', 'VARCHAR(50)'), '') AS VegetationDitchLeft,
NULLIF(X.survey.value('(VegetationDitchRight/text())[1]', 'VARCHAR(50)'), '') AS VegetationDitchRight,
CAST(NULLIF(X.survey.value('(IsErosionPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsErosionPresent,
CAST(NULLIF(X.survey.value('(IsErosionCorrectable/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsErosionCorrectable,
NULLIF(X.survey.value('(ErosionExtent/text())[1]', 'VARCHAR(50)'), '') AS ErosionExtent,
NULLIF(X.survey.value('(ErosionNotes/text())[1]', 'VARCHAR(MAX)'), '') AS ErosionNotes,
CAST(NULLIF(X.survey.value('(IsPrioritySite/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsPrioritySite,
NULLIF(X.survey.value('(PriorityReason/text())[1]', 'VARCHAR(50)'), '') AS PriorityReason,
NULLIF(X.survey.value('(PriorityComments/text())[1]', 'VARCHAR(MAX)'), '') AS PriorityComments,
CAST(NULLIF(X.survey.value('(FutureVisit/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS FutureVisit,
NULLIF(X.survey.value('(FutureComments/text())[1]', 'VARCHAR(MAX)'), '') AS FutureComments,
CAST(NULLIF(X.survey.value('(NonNativeInvasiveSpecies/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS NonNativeInvasiveSpecies,
NULLIF(X.survey.value('(SpeciesObserved/text())[1]', 'VARCHAR(MAX)'), '') AS SpeciesObserved,
CAST(NULLIF(X.survey.value('(IsHeadChanged/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsHeadChanged,
NULLIF(X.survey.value('(HeadChangeComments/text())[1]', 'VARCHAR(MAX)'), '') AS HeadChangeComments,
CAST(NULLIF(X.survey.value('(IsBackwatered/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsBackwatered,
NULLIF(X.survey.value('(BackwaterComments/text())[1]', 'VARCHAR(MAX)'), '') AS BackwaterComments,
CAST(NULLIF(X.survey.value('(IsOvertopping/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsOvertopping,
NULLIF(X.survey.value('(OvertoppingComments/text())[1]', 'VARCHAR(MAX)'), '') AS OvertoppingComments,
CAST(NULLIF(X.survey.value('(IsSubstrateEntireLength/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsSubstrateEntireLength,
CAST(NULLIF(X.survey.value('(CalculatedPassability/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(4,1)) AS CalculatedPassability,
CAST(NULLIF(X.survey.value('(DefinedPassability/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(4,1)) AS DefinedPassability,
NULLIF(X.survey.value('(PassabilityComments/text())[1]', 'VARCHAR(MAX)'), '') AS PassabilityComments,
NULLIF(X.survey.value('(PhotoInlet/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoInlet,
NULLIF(X.survey.value('(PhotoInletFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoInletFileName,
NULLIF(X.survey.value('(PhotoOutlet/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoOutlet,
NULLIF(X.survey.value('(PhotoOutletFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoOutletFileName,
NULLIF(X.survey.value('(PhotoUpstream/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoUpstream,
NULLIF(X.survey.value('(PhotoUpstreamFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoUpstreamFileName,
NULLIF(X.survey.value('(PhotoDownstream/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoDownstream,
NULLIF(X.survey.value('(PhotoDownstreamFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoDownstreamFileName,
NULLIF(X.survey.value('(PhotoRoadApproachLeft/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoRoadApproachLeft,
NULLIF(X.survey.value('(PhotoRoadApproachLeftFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoRoadApproachLeftFileName,
NULLIF(X.survey.value('(PhotoRoadApproachRight/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoRoadApproachRight,
NULLIF(X.survey.value('(PhotoRoadApproachRightFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoRoadApproachRightFileName,
Erosions = (CASE WHEN CAST(X.survey.query('Erosions') AS VARCHAR(MAX)) = '' THEN NULL
ELSE X.survey.query('Erosions') END),
Spans = (CASE WHEN CAST(X.survey.query('Spans') AS VARCHAR(MAX)) = '' THEN NULL
ELSE X.survey.query('Spans') END)
FROM @XMLobject.nodes('Surveys/Survey') AS X(survey)
) AS source ON source.SurveyId = target.SurveyId
WHEN NOT MATCHED BY TARGET
THEN
INSERT ( surveyId, CulvertID, County, RoadName, PRNo, MP, RefMiles, PtID, FWVersion, Latitude, Longitude, Waterway, SiteID, Observers, SurveyDate, AdjacentLandowner, LocationComments, CrossingType, StructureShape, InletStructure, OutletStructure, OutletType, SubstrateInStructure, GeneralCondition, PluggedPercent, PluggedLocation, CrushedPercent, CrushedLocation, IsRustedThrough, StructureInterior, StructureWaterDepthInlet, StructureWaterDepthOutlet, StructureEmbeddedDepthInlet, StructureEmbeddedDepthOutlet, StructureWaterVelocityInlet, StructureWaterVelocityOutlet, StructureWaterVelocityMeasured, StructureWaterVelocityMeasuredWith, IsPerched, PerchHeight, StreamFlow, IsScourPoolPresent, ScourPoolLength, ScourPoolWidth, ScourPoolDepth, IsUpstreamPondPresent, UpstreamPondLength, UpstreamPondWidth, RiffleWaterDepth, RiffleBankfullWidth, RiffleWettedWidth, RiffleWaterVelocity, RiffleMeasuredWith, RiffleSubstrate, RoadSurface, RoadCondition, RoadWidth, LocationOfLowPoint, RunOffPath, FillDepthUpStream, FillDepthDownStream, SlopeUpStream, SlopeDownStream, ApproachLengthLeft, ApproachLengthRight, ApproachSlopeLeft, ApproachSlopeRight, VegetationDitchLeft, VegetationDitchRight, IsErosionPresent, IsErosionCorrectable, ErosionExtent, ErosionNotes, IsPrioritySite, PriorityReason, PriorityComments, FutureVisit, FutureComments, NonNativeInvasiveSpecies, SpeciesObserved, IsHeadChanged, HeadChangeComments, IsBackwatered, BackwaterComments, IsOvertopping, OvertoppingComments, IsSubstrateEntireLength, CalculatedPassability, DefinedPassability, PassabilityComments, PhotoInlet, PhotoInletFileName, PhotoOutlet, PhotoOutletFileName, PhotoUpstream, PhotoUpstreamFileName, PhotoDownstream, PhotoDownstreamFileName, PhotoRoadApproachLeft, PhotoRoadApproachLeftFileName, PhotoRoadApproachRight, PhotoRoadApproachRightFileName )
VALUES ( surveyId, CulvertID, County, RoadName, PRNo, MP, RefMiles, PtID, FWVersion, Latitude, Longitude, Waterway, SiteID, Observers, SurveyDate, AdjacentLandowner, LocationComments, CrossingType, StructureShape, InletStructure, OutletStructure, OutletType, SubstrateInStructure, GeneralCondition, PluggedPercent, PluggedLocation, CrushedPercent, CrushedLocation, IsRustedThrough, StructureInterior, StructureWaterDepthInlet, StructureWaterDepthOutlet, StructureEmbeddedDepthInlet, StructureEmbeddedDepthOutlet, StructureWaterVelocityInlet, StructureWaterVelocityOutlet, StructureWaterVelocityMeasured, StructureWaterVelocityMeasuredWith, IsPerched, PerchHeight, StreamFlow, IsScourPoolPresent, ScourPoolLength, ScourPoolWidth, ScourPoolDepth, IsUpstreamPondPresent, UpstreamPondLength, UpstreamPondWidth, RiffleWaterDepth, RiffleBankfullWidth, RiffleWettedWidth, RiffleWaterVelocity, RiffleMeasuredWith, RiffleSubstrate, RoadSurface, RoadCondition, RoadWidth, LocationOfLowPoint, RunOffPath, FillDepthUpStream, FillDepthDownStream, SlopeUpStream, SlopeDownStream, ApproachLengthLeft, ApproachLengthRight, ApproachSlopeLeft, ApproachSlopeRight, VegetationDitchLeft, VegetationDitchRight, IsErosionPresent, IsErosionCorrectable, ErosionExtent, ErosionNotes, IsPrioritySite, PriorityReason, PriorityComments, FutureVisit, FutureComments, NonNativeInvasiveSpecies, SpeciesObserved, IsHeadChanged, HeadChangeComments, IsBackwatered, BackwaterComments, IsOvertopping, OvertoppingComments, IsSubstrateEntireLength, CalculatedPassability, DefinedPassability, PassabilityComments, PhotoInlet, PhotoInletFileName, PhotoOutlet, PhotoOutletFileName, PhotoUpstream, PhotoUpstreamFileName, PhotoDownstream, PhotoDownstreamFileName, PhotoRoadApproachLeft, PhotoRoadApproachLeftFileName, PhotoRoadApproachRight, PhotoRoadApproachRightFileName )
WHEN MATCHED
THEN UPDATE
SET target.CulvertID = source.CulvertID
-- DELETE section
--WHEN NOT MATCHED BY SOURCE
-- THEN DELETE
OUTPUT source.surveyId, source.Erosions, source.Spans INTO #tmp_survey
--OUTPUT $action
;
-- MERGE dbo.CULV_StreamCrossingSurvey END
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-- DELETEs START
-- Remove existing erosion and span records
------------------------------------------------------------------------------------------------
DELETE e
FROM dbo.CULV_StreamCrossingErosion e
INNER JOIN #tmp_survey s ON e.SurveyID = s.surveyId
DELETE e
FROM dbo.Culvert_StreamCrossingSpan e
INNER JOIN #tmp_survey s ON e.SurveyID = s.surveyId
-- DELETEs END
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-- MERGE dbo.CULV_StreamCrossingErosion START
------------------------------------------------------------------------------------------------
;MERGE dbo.CULV_StreamCrossingErosion AS target
USING
(
SELECT
t.surveyId
, NULLIF(E.erosion.value('(Location/text())[1]', 'VARCHAR(50)'), '') AS Location
, E.erosion.value('(Length/text())[1]', 'NUMERIC(6,1)') AS Length
, E.erosion.value('(Width/text())[1]', 'NUMERIC(6,1)') AS Width
, E.erosion.value('(Depth/text())[1]', 'NUMERIC(6,1)') AS Depth
, E.erosion.value('(IsReachingStream/text())[1]', 'BIT') AS IsReachingStream
, NULLIF(E.erosion.value('(MaterialEroded/text())[1]', 'VARCHAR(50)'), '') AS MaterialEroded
FROM #tmp_survey t
CROSS APPLY erosions.nodes('Erosions/Erosion') AS E(erosion)
) AS source ON source.SurveyId = target.SurveyId
WHEN NOT MATCHED BY TARGET
THEN
INSERT ( surveyId, Location, Length, Width, Depth, IsReachingStream, MaterialEroded )
VALUES ( surveyId, Location, Length, Width, Depth, IsReachingStream, MaterialEroded )
WHEN MATCHED
THEN UPDATE
SET
target.Location = source.Location,
target.Length = source.Length,
target.Width = source.Width,
target.Depth = source.Depth,
target.IsReachingStream = source.IsReachingStream,
target.MaterialEroded = source.MaterialEroded
;
;MERGE dbo.Culvert_StreamCrossingSpan AS target
USING
(
SELECT
t.surveyId,
S.span.value('(SpanNumber/text())[1]', 'SMALLINT') AS SpanNumber,
S.span.value('(Length/text())[1]', 'NUMERIC(6,1)') AS Length,
S.span.value('(Width/text())[1]', 'NUMERIC(6,1)') AS Width,
S.span.value('(Height/text())[1]', 'NUMERIC(6,1)') AS Height,
NULLIF( S.span.value('(Material/text())[1]', 'VARCHAR(50)'), '') AS Material
FROM #tmp_survey t
CROSS APPLY spans.nodes('Spans/Span') AS S(span)
) AS source ON source.SurveyId = target.SurveyId
WHEN NOT MATCHED BY TARGET
THEN
INSERT ( surveyID, SpanNumber, Length, Width, Height, Material )
VALUES ( surveyID, SpanNumber, Length, Width, Height, Material )
WHEN MATCHED
THEN UPDATE
SET
target.SpanNumber = source.SpanNumber,
target.Length = source.Length,
target.Width = source.Width,
target.Height = source.Height,
target.Material = source.Material
;
-- MERGE dbo.CULV_StreamCrossingErosion END
------------------------------------------------------------------------------------------------
-- !!TODO Add TRY/CATCH error and transaction handling
COMMIT
RETURN
END
GO
答案 1 :(得分:1)
我可以推荐一些变化。首先,您不需要.query和.value。 .query返回XML,所以如果你想要XML,那么你使用.query。 .value返回一个标量值,因此这是你需要在这里使用的值,例如
SELECT
CAST(NULLIF(X.survey.value('(SurveyID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS SurveyID,
CAST(NULLIF(X.survey.value('(CulvertID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS CulvertID,
另外,添加text()构造函数和1 ordinal(如上所述),根据here,它应该更有效。
其次,我认为你不需要游标。 .nodes返回一个结果集,以便您可以直接切换到临时表,然后INSERT / UPDATE / DELETE到主表中。如果您使用的是SQL 2008,则可以使用MERGE,它可以将所有操作合二为一。你甚至可以,如果你感觉很勇敢,可以从.nodes结果集直接进入主表,如果这有意义吗?
如果您没有从这些更改中获得所需的性能,您可以考虑使用OPENXML,对于较大的XML文档,这有时会更快。请注意,OPENXML有一个众所周知的功能,它可以在调用时自动占用服务器内存的1/8,因此您必须始终记得调用sp_xml_removedocument。
继续进行这些更改并让我们知道您是如何获得的。
PS您正在使用正确的术语'shred',请忽略有关SAX的说明。 SQL Server内置了非常强大和快速的组件来粉碎XML。