Skip to content

Instantly share code, notes, and snippets.

@mbmccormick
Created September 20, 2011 17:42
Show Gist options
  • Save mbmccormick/1229762 to your computer and use it in GitHub Desktop.
Save mbmccormick/1229762 to your computer and use it in GitHub Desktop.
Bulk INSERT LINQ collection via XML serialization and stored procedures
SET ANSI_NULL ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.InsertStopTimes
@SerializedData xml,
@NewPartitionKey uniqueidentifier,
@OldPartitionKey uniqueidentifier
AS
INSERT INTO StopTimes(RowKey, PartitionKey, StopID, TripID, ArrivalTime, DepartureTime, StopSequence)
SELECT T.c.value('./RowKey[1]', 'uniqueidentifier'),
T.c.value('./PartitionKey[1]', 'uniqueidentifier'),
T.c.value('./StopID[1]', 'varchar(50)'),
T.c.value('./TripID[1]', 'varchar(50)'),
T.c.value('./ArrivalTime[1]', 'datetime'),
T.c.value('./DepartureTime[1]', 'datetime'),
T.c.value('./StopSequence[1]', 'int')
FROM @SerializedData.nodes('/ArrayOfStopTime/StopTime') T(c)
UPDATE Agency SET PartitionKey = @NewPartitionKey WHERE PartitionKey = @OldPartitionKey
DELETE FROM StopTimes WHERE PartitionKey = @OldPartitionKey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment