Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are forced to do this in a stored procedure and your arrays are equal size you can join the two lists, split them, and then join on position (the number of elements in each array) to get the linked set you need. </p> <p>The below example uses a <a href="https://stackoverflow.com/questions/1393951/what-is-the-best-way-to-create-and-populate-a-numbers-table">number table</a>, but you can replace that split operation with any.</p> <pre><code>-- if you dont have a number table: /* create table [dbo].[Number](n int not null primary key clustered); insert into dbo.Number values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10), (11),(12),(13),(14),(15),(16),(17),(18),(19),(20) */ declare @lstid varchar(100) = '1,2,3,4,51', @lstvalue varchar(100) = '10,20,22,35,60' declare @Length tinyint, @Input varchar(8000), @Delimiter char(1) -- sanity check if len(@lstid)-len(replace(@lstid, ',', '')) &lt;&gt; len(@lstvalue)-len(replace(@lstvalue, ',', '')) begin raiserror('lists are not equal', 16, 1); return; end --count the numbers of elements in one of the arrays select @Length = len(@lstid)-len(replace(@lstid, ',', ''))+1; --join the two arrays into one select @Input = @lstid + ',' + @lstvalue; set @Delimiter = ','; ;with cte (i,s) as ( select row_number() over (order by n.n asc) [i], substring(@Delimiter + @Input + @Delimiter, n.n + 1, charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.n + 1) - n.n - 1) [s] from dbo.Number n where n.n = charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.n) and n.n &lt;= len(@Delimiter + @Input) ) select a.s, b.s from cte a join cte b on a.i+@Length = b.i order by a.i; return </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload