Sélectionner pour renvoyer plusieurs lignes à partir d'une seule ligne

Je travaille sur une requête qui renvoie une list d'éléments affichés ci-dessous:

SELECT ID, Description FROM TableA WHERE ID = 10001 

Résultats:

 ID Description 10001 Item1 Item2 Item3 Item4 

Cependant, ce que je veux, c'est pouvoir faire le return de plusieurs lignes.

 ID Description 10001 Item1 10001 Item2 10001 Item3 10001 Item4 

Y a-t-il un moyen facile d'y parvenir?

Je vous remercie!

Voici comment vous pouvez faire ceci:

 SELECT id, Split.a.value('.', 'VARCHAR(100)') AS Ssortingng FROM (SELECT id, CAST ('<M>' + REPLACE(description, ' ', '</M><M>') + '</M>' AS XML) AS n from TableA where id = 1001) AS A CROSS APPLY n.nodes ('/M') AS Split(a); 

Fiddle http://sqlfiddle.com/#!3/152e3/1

Vous pouvez également créer une fonction pour le faire en tant que tel

 CREATE FUNCTION [dbo].[RowToManyRows] ( @SsortingngInput VARCHAR(8000) ) RETURNS @OutputTable TABLE ( [Ssortingng] VARCHAR(10) ) AS BEGIN DECLARE @Ssortingng VARCHAR(10) WHILE LEN(@SsortingngInput) > 0 BEGIN SET @Ssortingng = LEFT(@SsortingngInput, ISNULL(NULLIF(CHARINDEX(' ', @SsortingngInput) - 1, -1), LEN(@SsortingngInput))) SET @SsortingngInput = SUBSTRING(@SsortingngInput, ISNULL(NULLIF(CHARINDEX(' ', @SsortingngInput), 0), LEN(@SsortingngInput)) + 1, LEN(@SsortingngInput)) INSERT INTO @OutputTable ( [Ssortingng] ) VALUES ( @Ssortingng ) END RETURN END GO 

Vous interrogez ensuite cette fonction en tant que telle

 SELECT ID, Ssortingng FROM myTable CROSS APPLY [dbo].RowToManyRows(myTable.Description) 

Exemple:

 CREATE TABLE myTable ( ID INT, Description VARCHAR(50) ) INSERT INTO dbo.myTable ( ID, Description ) VALUES ( 1, -- ID - int 'AB' -- Description - varchar(50) ), (2, 'C D') 

Résultats

 1 A 1 B 2 C 2 D