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

Sql server SQL Server 2012-从一个文本块插入多条记录,sql-server,tsql,Sql Server,Tsql,在SQL server 2012上。我有由“注释”组成的规范化表。每个“note”记录都可以有许多用外键绑定的注释行。我正在寻找一个SQL语句,它将解析一个文本块,并为该文本中的每一行插入一条单独的记录 我猜每个文本块都有某种“WHILE循环”,但我不知道它是如何工作的 需要明确的是:这样做的最终结果是将文本块粘贴到查询中并执行,这样我就可以将它的每一行都放到注释中,而不必费心创建多个insert语句。我同意Zohar Peled的观点,这可能不是规范这些表的正确方法。如果这仍然是您需要做的事情

在SQL server 2012上。我有由“注释”组成的规范化表。每个“note”记录都可以有许多用外键绑定的注释行。我正在寻找一个SQL语句,它将解析一个文本块,并为该文本中的每一行插入一条单独的记录

我猜每个文本块都有某种“WHILE循环”,但我不知道它是如何工作的


需要明确的是:这样做的最终结果是将文本块粘贴到查询中并执行,这样我就可以将它的每一行都放到注释中,而不必费心创建多个insert语句。

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

在SQL Server 2016+中,您可以使用
string\u split()

在该版本之前;使用Jeff Moden提供的CSV拆分器表值函数:

表格设置:

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演示

报税表(分别):

拆分字符串引用:


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

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

在SQL Server 2016+中,您可以使用
string\u split()

在该版本之前;使用Jeff Moden提供的CSV拆分器表值函数:

表格设置:

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演示

报税表(分别):

拆分字符串引用:


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

您能否实际显示一些示例输入和预期输出,这对我来说似乎不正常。当你可以在一条记录中输入整条注释时,将每行文本变成一条不同的记录有什么意义?我同意在这种情况下,这可能不是最好的方法。这更像是一个POC,表明我们不必总是使用VARCHAR(MAX)来保存自由格式的文本字段(这在我们的组织中很普遍)。您能不能实际显示一些示例输入和预期输出,这对我来说似乎不正常。当你可以在一条记录中输入整条注释时,将每行文本变成一条不同的记录有什么意义?我同意在这种情况下,这可能不是最好的方法。这更像是一个POC,表明我们不必总是使用VARCHAR(MAX)来保存自由格式的文本字段(这在我们的组织中很普遍)。我喜欢这个!我使用了Jeff Moden的拆分器代码,效果非常好。自从我发布后,我有了一个使用charindex的解决方案,但它并不完全正确。然后我回到这里,这个功能非常完美!Thanks@Dark1很乐意帮忙!我喜欢这个!我使用了Jeff Moden的拆分器代码,效果非常好。自从我发布后,我有了一个使用charindex的解决方案,但它并不完全正确。然后我回到这里,这个功能非常完美!Thanks@Dark1很乐意帮忙!