У меня есть данные в исходной таблице, которая выглядит, как показано ниже. По сути, это повествовательное поле, разделенное на 47 частей. В данных существует соглашение о том, что если пользователь вводит новую строку, она заменяется символом `и этим заканчивается эта строка.
CREATE TABLE narrdata (parentindex integer, linenum integer, linetext varchar(48))
Я хочу объединить данные из отдельных строк в одну varchar ( max) поле. Раньше я делал это с помощью курсора и .WRITE вот так.
Мне интересно, есть ли другой способ сделать это без .WRITE.
CREATE TABLE #target_table (myindex integer, narrative varchar(max))
/* some code here to initialize rows in target_table - so assume it already has a row */
DECLARE @item integer
DECLARE @line integer
DECLARE @chunk varchar(54)
DECLARE @crlf bit
DECLARE @crlf_last bit
DECLARE @item_last integer
DECLARE cur_narr CURSOR FOR
SELECT
parentindex,
linenum,
CASE
WHEN linetext IS NOT NULL THEN
CASE
WHEN RIGHT(RTRIM(linetext), 1) = '`' THEN
SUBSTRING(linetext, 1, LEN(linetext) - 1) + CHAR(13) + CHAR(10)
ELSE
RTRIM(linetext)
END
ELSE ''
END,
CASE
WHEN linetext IS NOT NULL THEN
CASE WHEN RIGHT(RTRIM(linetext), 1) = '`' THEN 1 ELSE 0 END
ELSE 0
END
FROM narrdata
WHERE parentindex IN ( /* some condition here */ )
ORDER BY parentindex, linenum
SET @item_last = -1
SET @crlf_last = 1
OPEN cur_narr
FETCH NEXT FROM cur_narr
INTO @item, @line, @chunk, @crlf
WHILE @@FETCH_STATUS = 0
BEGIN
IF @item <> @item_last
BEGIN
SET @crlf_last = 1
SET @item_last = @item
END
IF @crlf_last = 0
SET @chunk = ' ' + @chunk
UPDATE #target_table
SET narrative .WRITE(@chunk, NULL, 0)
WHERE myindex = @item
SET @crlf_last = @crlf
FETCH NEXT FROM cur_narr
INTO @item, @line, @chunk, @crlf
END
CLOSE cur_narr
DEALLOCATE cur_narr
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 1, 'This is a narrative which should be separated')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 2, 'into several lines in the source data. I have')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 3, 'no idea which line this part will be in. This')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 4, 'however should be before a newline.`')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 5, 'A new line just started here.`')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 6, 'Another new line here. Then some blank lines.`')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 7, '`')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 8, '`')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 9, 'How about this now? Is this enough sample')
INSERT INTO narrdata (parentindex, linenum, linetext) VALUES (37791, 10, 'data.')
Ожидаемый результат
This is a narrative which should be separated into several lines in the source data. I have no idea which line this part will be in. This however should be before a newline.
A new line just started here.
Another new line here. Then some blank lines.
How about this now? Is this enough sample data.
Вот пример, который вы можете запустить для себя:
CREATE TABLE #narrdata (parentindex integer, linenum integer, linetext varchar(48))
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 1, 'This is a narrative which should be separated')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 2, 'into several lines in the source data. I have')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 3, 'no idea which line this part will be in. This')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 4, 'however should be before a newline.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 5, 'A new line just started here.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 6, 'Another new line here. Then some blank lines.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 7, '`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 8, '`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 9, 'How about this now? Is this enough sample')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37791, 10, 'data.')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 1, 'This is a narrative which should be separated')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 2, 'into several lines in the source data. I have')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 3, 'no idea which line this part will be in. This')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 4, 'however should be before a newline.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 5, 'A new line just started here.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 6, 'Another new line here. Then some blank lines.`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 7, '`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 8, '`')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 9, 'How about this now? Is this enough sample')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37792, 10, 'data.')
INSERT INTO #narrdata (parentindex, linenum, linetext) VALUES (37793, 1, 'A simple line example.')
CREATE TABLE #target_table (myindex integer, narrative varchar(max))
INSERT INTO #target_table (myindex, narrative)
SELECT DISTINCT parentindex, ''
FROM #narrdata
DECLARE @item integer
DECLARE @line integer
DECLARE @chunk varchar(54)
DECLARE @crlf bit
DECLARE @crlf_last bit
DECLARE @item_last integer
DECLARE cur_narr CURSOR FOR
SELECT
parentindex,
linenum,
CASE
WHEN linetext IS NOT NULL THEN
CASE
WHEN RIGHT(RTRIM(linetext), 1) = '`' THEN
SUBSTRING(linetext, 1, LEN(linetext) - 1) + CHAR(13) + CHAR(10)
ELSE
RTRIM(linetext)
END
ELSE ''
END,
CASE
WHEN linetext IS NOT NULL THEN
CASE WHEN RIGHT(RTRIM(linetext), 1) = '`' THEN 1 ELSE 0 END
ELSE 0
END
FROM #narrdata
ORDER BY parentindex, linenum
SET @item_last = -1
SET @crlf_last = 1
OPEN cur_narr
FETCH NEXT FROM cur_narr
INTO @item, @line, @chunk, @crlf
WHILE @@FETCH_STATUS = 0
BEGIN
IF @item <> @item_last
BEGIN
SET @crlf_last = 1
SET @item_last = @item
END
IF @crlf_last = 0
SET @chunk = ' ' + @chunk
UPDATE #target_table
SET narrative .WRITE(@chunk, NULL, 0)
WHERE myindex = @item
SET @crlf_last = @crlf
FETCH NEXT FROM cur_narr
INTO @item, @line, @chunk, @crlf
END
CLOSE cur_narr
DEALLOCATE cur_narr
SELECT * FROM #target_table
DROP TABLE #target_table
DROP TABLE #narrdata