2017-04-06 122 views
1

在SQL Server 2012上。我規範化了包含「註釋」的表。每個「註釋」記錄可以有許多與外鍵相關的線索。我正在查找將解析一段文本的SQL語句,併爲該文本中的每一行插入單獨的記錄。SQL Server 2012 - 從一個文本塊插入多個記錄

我猜測某種「WHILE循環」爲每個文本塊,但不能讓我的頭周圍如何工作。

要清楚:最終的結果是隻將文本塊粘貼到查詢中並執行,這樣我就可以將它的每一行都寫入註釋中,而不會繞過創建多個插入語句。

+0

你可以請示出一些樣本輸入和預期的輸出 – TheGameiswar

+1

其實,這似乎並沒有對我規範化。當您可以在單個記錄中輸入整個記錄時,將每行文本設爲不同的記錄有什麼意義? –

+0

我同意在這種情況下,它可能不是做事的最佳方式。這更像是一個POC,表明我們並不總是必須使用VARCHAR(MAX)來保存自由格式的文本字段(這在我們的組織中很流行)。 – Dark1

回答

0

我同意Zohar Peled,這可能不是規範化這些表的正確方法。如果這仍然是您需要做的事情:

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

在此版本之前;使用CSV分離器表值函數由傑夫MODEN:

表設置:

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 
); 

插入語句:

select * from note; 
select * from note_lines; 

rextester 演示:

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 

而且插入後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個字符,如果您需要更多,則在上面的鏈接中有替代方案。

+0

我喜歡這個!我使用了Jeff Moden的分離器代碼,它工作得很好。自從我發佈以來,我有一個使用charindex的解決方案,但並不完全正確。然後我回到這裏,那個功能非常完美!謝謝 – Dark1

+0

@ Dark1樂於助人! – SqlZim

相關問題