You've got an answer already, which is working fine, but this should be faster and easier:
You did not specify your SQL-Server's version, but - talking about STRING_SPLIT()
- I assume it's at least v2016. If this is correct, you can use OPENJSON. Your list of numbers needs nothing more than brackets to be a JSON-array ([1,2,3]
), while an array of words/letters can be transformed with some easy string operations (["a","b","c"]
).
Following the docs, OPENJSON returns the elements position in [key]
, while the element itself is returned in [value]
. You can simply JOIN these sets:
DECLARE @ParamList1 NVARCHAR(max) = '1,2,3,4,5';
DECLARE @ParamList2 NVARCHAR(max) = 'a,b,c,d,e';
SELECT p1.[key] AS FragmentNr
,p1.[value] AS P1
,p2.[value] AS P2
FROM OPENJSON(CONCAT('[',@ParamList1 + ']')) p1
INNER JOIN OPENJSON(CONCAT('["',REPLACE(@ParamList2,',','","'),'"]')) p2 ON p1.[key]=p2.[key] ;
In this answer you will find some details (UPDATE section 1 and 2).