2017-04-06 15 views
1

SQLサーバー2012年。 "ノート"で構成されるテーブルを正規化しました。各「メモ」レコードには、外部キーを使用して多くのノーラインが関連付けられています。私は、テキストブロックを解析するSQL文を探しています。そのテキスト内の各行について、別々のレコードを挿入します。SQL Server 2012 - 1つのテキストブロックから複数のレコードを挿入する

私はテキストの各ブロックに対して何らかの「WHILEループ」を推測していますが、どのように動作するかについて私の頭を浮かべることはできません。

明確にする:この結果は、テキストブロックをクエリに貼り付けて実行するだけで、複数の挿入ステートメントを作成することなく、それぞれの行をメモに取り込むことができます。

+0

いくつかのサンプル入力と期待される出力を表示してください。 – TheGameiswar

+1

実際、私には標準化されていないようです。 1つのレコードの中にノート全体を入力することができるとき、各テキスト行を別々のレコードにする点は何ですか? –

+0

私は、この事例ではおそらく最善の方法ではないことに同意します。これは、フリーフォームのテキストフィールド(これは私たちの組織で一般的です)を保持するために*常に* VARCHAR(MAX)を使用する必要はないことを示すPOCの詳細です。 – Dark1

答えて

0

私は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文字の制限のために設計されています。さらに必要な場合は、上記のリンクに代替があります。

+0

私はこれが大好き!私はJeff Modenのスプリッターコードを使用しました。私が投稿して以来、私はcharindexを使用したソリューションを持っていましたが、それはまったく正しいものではありませんでした。それから私はここに戻り、その機能は完璧でした!ありがとう – Dark1

+0

@ Dark1手伝って幸いです! – SqlZim

関連する問題