Comment stocker une longue string séparée par un point-virgule dans plusieurs colonnes?

Fondamentalement en raison de la ressortingction sur le server en direct, je ne peux pas utiliser de file text, donc j'ai besoin de coder en dur datatables dans le code T-SQL.

Donc d'abord j'ai créé une string à partir du file text comme ceci:

("001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24", .... ) 

J'ai cette structure de table:

 DECLARE @Product TABLE(ProductCode INT NOT NULL, Description nvarchar(100) NOT NULL) 

Je dois d'abord stocker le code et la description dans une variable de table. Une fois cela fait, je peux facilement le mapper à la table physique et mettre à jour les loggings.

Comment puis-je get quelque chose de similaire à:

 insert into @Product(ProductCode, Description) values ("001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24", .... ) 
  Code Description 001122 Sale Item1 001123 Sale Item2 001124 Sale Item3 

J'ai mis en caisse un échantillon pour vous, veuillez vérifier ceci

 declare @Questions varchar(100)= '"001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24"' DECLARE @myXML AS XML = N'<H><r>' +Replace(@Questions, ',', '</r><r>') + '</r></H>' select @myXML ;WITH cte AS ( SELECT CAST(N'<H><r>' + Replace(Vals.id.value('.', 'NVARCHAR(50)') ,';' , '</r><r>') + '</r></H>' as XML) AS val FROM @myXML.nodes('/H/r') AS Vals(id) ) ,mycte1 as ( SELECT distinct Replace( Savalue('(/H/r)[1]', 'NVARCHAR(50)') , '"', '') AS c1, Replace( Savalue('(/H/r)[2]', 'NVARCHAR(50)') , '"', '') AS c2 FROM cte CROSS APPLY val.nodes('/H/r') S(a) ) select * from mycte1 

La sortie sera

 c1 c2 001123 Sale Item 23 001124 Sale Item 24 001122 Sale Item 1 

Si vous avez un format fixe comme dans l'exemple, vous pouvez get la sortie désirée simplement en utilisant CHARINDEX et SUBSTRING

 SELECT SUBSTRING(description, 0, CHARINDEX(';', DESCRIPTION, 0)) code, SUBSTRING(description, CHARINDEX(';', DESCRIPTION, 0)+1, LEN(DESCRIPTION)) Description FROM @Product 

SORTIE:

  code Description ------------------------ 001122 Sale Item 1 001123 Sale Item 23 001124 Sale Item 24 

Ici nous allons matey:

 DECLARE @MySsortingng nvarchar(max) = '"001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24"'; DECLARE @delimiter1 nvarchar(1) , @delimiter2 nvarchar(1) , @Start1 int , @End1 int , @Length int , @cNEXTVALUE nvarchar(1000) , @cNEXTVALUE2 nvarchar(1000) , @SsortingngLength int; DECLARE @Product TABLE ( ProductCode int NOT NULL , Description nvarchar(100) NOT NULL ); SET @MySsortingng = RTRIM(LTRIM(@MySsortingng)); SET @MySsortingng = REPLACE(@MySsortingng, CHAR(13), ''); SET @MySsortingng = REPLACE(@MySsortingng, CHAR(10), ''); SET @delimiter1 = ';'; SET @delimiter2 = ','; SET @MySsortingng = REPLACE(@MySsortingng, '"', '')+@delimiter2; SET @SsortingngLength = LEN(@MySsortingng); SELECT @Start1 = 0 , @End1 = CHARINDEX(@delimiter1, @MySsortingng, 1); SELECT @Length = @End1 - @Start1; WHILE @Length > 0 BEGIN SET @cNEXTVALUE = SUBSTRING(@MySsortingng, @Start1, @Length); SET @Start1 = @End1 + 1; SET @End1 = CHARINDEX(@delimiter2, @MySsortingng, @Start1); SELECT @Length = @End1 - @Start1; SET @cNEXTVALUE2 = SUBSTRING(@MySsortingng, @Start1, @Length); IF LEN(RTRIM(LTRIM(@cNEXTVALUE))) > 0 BEGIN INSERT INTO @Product ( ProductCode , Description ) VALUES ( @cNEXTVALUE, @cNEXTVALUE2 ); END; SET @Start1 = @End1 + 2; SET @End1 = CHARINDEX(@delimiter1, @MySsortingng, @Start1); SELECT @Length = @End1 - @Start1; END; SELECT * FROM @Product;