私はZohar Peledに同意しますが、これはおそらくこれらのテーブルを正規化する正しい方法ではありません。まだこれが必要な場合:
string_split()
を使用できます。
このバージョンより前。ジェフ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
);
挿入文:
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文字の制限のために設計されています。さらに必要な場合は、上記のリンクに代替があります。
いくつかのサンプル入力と期待される出力を表示してください。 – TheGameiswar
実際、私には標準化されていないようです。 1つのレコードの中にノート全体を入力することができるとき、各テキスト行を別々のレコードにする点は何ですか? –
私は、この事例ではおそらく最善の方法ではないことに同意します。これは、フリーフォームのテキストフィールド(これは私たちの組織で一般的です)を保持するために*常に* VARCHAR(MAX)を使用する必要はないことを示すPOCの詳細です。 – Dark1