Utilisation d'une table de nombres auxiliaire, vous pouvez diviser la colonne balises en lignes tout en gardant associée à le VideoURL:
CREATE TABLE NumberPivot (NumberID INT PRIMARY KEY)
DECLARE @intLoopCounter INT
SELECT @intLoopCounter =0
SET NOCOUNT ON
WHILE @intLoopCounter <=999 BEGIN
INSERT INTO NumberPivot
VALUES (@intLoopCounter)
SELECT @intLoopCounter = @intLoopCounter +1
END
GO
SELECT
ContentPageID,
Substring(',' + Tags + ','
, numberID + 1
, Charindex(',', ',' + Tags + ',', numberID + 1) - numberid - 1) AS value
FROM dbo.NumberPivot AS np,
Staging AS S
WHERE numberid <= Len(',' + Tags + ',') - 1
AND Substring(',' + Tags + ',', numberID, 1) = ','
Voici donc, nous remplissons la Balises table avec des étiquettes uniques:
;WITH X AS (
SELECT
VideoURL,
Substring(',' + Tags + ',', numberID + 1, Charindex(',', ',' + Tags + ',', numberID + 1) - numberid - 1) AS Tag
FROM dbo.NumberPivot AS np,
Staging AS S
WHERE numberid <= Len(',' + Tags + ',') - 1
AND Substring(',' + Tags + ',', numberID, 1) = ','
)
INSERT Tag (Tag)
SELECT DISTINCT Tag FROM X;
Suivant remplir la table Vidéos:
INSERT Video (VideoURL, Rating, Length, Thumbnail)
SELECT VideoURL, Rating, Length, Thumbnail
FROM Staging;
remplissage Enfin VideoTag:
INSERT VideoTag (VideoURL, Tag)
SELECT
VideoURL,
Substring(',' + Tags + ',', numberID + 1, Charindex(',', ',' + Tags + ',', numberID + 1) - numberid - 1) AS Tag
FROM dbo.NumberPivot AS np,
Staging AS S
WHERE numberid <= Len(',' + Tags + ',') - 1
AND Substring(',' + Tags + ',', numberID, 1) = ','
Got la chaîne fractionnée à l'aide de la table numérique de here
Je suis confus s'il vous plaît préciser - Est-ce uniquement dans SQL? Dans ce cas, vous devez supprimer les balises ef et C#. –
Oups, j'ai oublié de les enlever. En fait, je m'intéresse à une solution SQL ou Entity Framework, mais EF ne peut finalement pas se rapprocher des performances de SQL direct. – BrazenTongue