sql >> Base de Datos >  >> RDS >> Sqlserver

Divida la cadena delimitada por comas e insértela en una tabla (int)

Mejor usa XML para esto,

Declare @Var nvarchar(MAX)

Set @Var ='188,189,190,191,192,193,194'

DECLARE @XML AS XML

DECLARE @Delimiter AS CHAR(1) =','

SET @XML = CAST(('<X>'+REPLACE(@Var,@Delimiter ,'</X><X>')+'</X>') AS XML)

DECLARE @temp TABLE (ID INT)

INSERT INTO @temp

SELECT N.value('.', 'INT') AS ID FROM @XML.nodes('X') AS T(N)

SELECT * FROM @temp