This question already has answers here:
Result order of string_split? (3 answers)
Closed last month.
In SQL (T-SQL) I have 3 lists of numbers, in the form of strings (I take them from an Excel file):
DECLARE @article_list VARCHAR(MAX) = 1060226, 20653, 39986, 1041443, 1060303, 1057353, 1050423, 1057354, 1059541, 1050570';
DECLARE @store_list VARCHAR(MAX) = '1046, 1046, 1046, 1046, 1107, 1107, 1107, 1107, 1107, 1107';
DECLARE @order_list VARCHAR(MAX) = '1012406, 2008322, 2008322, 2008322, 2011092, 1020199, 1020199, 1020199, 1020176, 2010731';
And I want to create a table like this:
DECLARE @rows_to_delete TABLE (rn INT IDENTITY(1,1), art INT, ord INT, stor INT);
Now I need to load the values of the lists, in the same order, into the table @rows_to_delete, like {1060226, 1046, 1012406}, {20653, 1046, 2008322}
(it's not JSON, I just want to describe the table rows..)
Any ideas?
2条答案
按热度按时间yhived7q1#
Here is a JSON based solution, using set based operations.
It will work starting from SQl Server 2016 onwards.
We are converting input strings into JSON arrays with guaranteed sequential order.
OPENJSON()
is converting JSON arrays into rectangular data sets with [Key] and [Value] columns.SQL
Output
6jjcrrmo2#
If looking for a little more brevity.