SQL Server 2012 - 从一个文本块插入多个记录

时间:2017-04-06 15:06:32

标签: sql-server tsql

在SQL Server 2012上。我已经规范化了包含“注释”的表。每个“note”记录可以使用外键将许多notel绑定到它。我正在寻找一个SQL语句来解析一个文本块,并为该文本中的每一行插入一个单独的记录。

我猜测每个文本块都有某种“WHILE循环”,但无法理解它是如何工作的。

要明确:最终的结果是将文本块粘贴到查询中并执行,这样我就可以将其中的每一行都放入注释中,而不会产生多个插入语句。

1 个答案:

答案 0 :(得分:0)

我同意Zohar Peled,这可能不是规范化这些表的正确方法。如果这仍然是你需要做的事情:

在SQL Server 2016+中,您可以使用string_split()

在该版本之前;使用Jeff Moden的CSV Splitter表值函数:

表格设置:

create table dbo.note (
    id int not null identity(1,1) primary key 
    , created datetime2(2) not null
    /* other cols */
);
create table dbo.note_lines (
    id int not null identity(1,1) primary key
  , noteId int not null foreign key references dbo.note(id)
  , noteLineNumber int not null
  , noteLineText varchar(8000) not null
);

插入陈述:

declare @noteId int; 
insert into dbo.note values (sysutcdatetime());
set @noteId = scope_identity();

declare @note_txt varchar(8000) = 'On SQL server 2012. I have normalized tables that will consist of "notes". Each "note" record can have many notelines tied to it with a foreign key. I''m looking for a SQL statement that will parse a block of text and, for each line within that text, insert a separate record.
I''m guessing some sort of "WHILE loop" for each block of text but can''t get my head around how it would work.
To be clear: The end result of this would be to just paste the block of text into the query and execute so that I can get each individual line of it into the note without messing around creating multiple insert statements.'

insert into dbo.note_lines (noteId, noteLineNumber, noteLineText)
select @noteId, s.ItemNumber, s.Item 
from  [dbo].[delimitedsplit8K](@note_txt, char(10)) s

插入后:

select * from note;
select * from note_lines;

rextester 演示http://rextester.com/SCODAG90159

分别返回:

+----+---------------------+
| id |       created       |
+----+---------------------+
|  1 | 2017-04-06 15:16:59 |
+----+---------------------+

+----+--------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | noteId | noteLineNumber |                                                                                                                                    noteLineText                                                                                                                                     |
+----+--------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  1 |      1 |              1 | On SQL server 2012. I have normalized tables that will consist of "notes". Each "note" record can have many notelines tied to it with a foreign key. I'm looking for a SQL statement that will parse a block of text and, for each line within that text, insert a separate record. |
|  2 |      1 |              2 | I'm guessing some sort of "WHILE loop" for each block of text but can't get my head around how it would work.                                                                                                                                                                       |
|  3 |      1 |              3 | To be clear: The end result of this would be to just paste the block of text into the query and execute so that I can get each individual line of it into the note without messing around creating multiple insert statements.                                                      |
+----+--------+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

拆分字符串参考:

注意:示例中使用的函数的设计限制为8000个字符,如果需要更多,则上面的链接中有其他选项。

相关问题