久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將逗號分隔的值拆分為行

Split comma separated values to rows(將逗號分隔的值拆分為行)
本文介紹了將逗號分隔的值拆分為行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何從 SQL Server 中的逗號分隔字符串獲取值到行中,以便將它們插入表中?

How can I get values from a comma separated string in SQL Server in to rows, in order to insert them into a table?

例如,使用此數據:

 Declare @string as nvarchar(max);
 Declare @substring as nvarchar(50);
 set @string = "Apple, Banana, Cherry, Orange, Mango"

我目前已經硬編碼 set @last = 2,對于這個例子,但是 @last 應該包含字符串中的單詞數.參數 @substring 將包含循環中的每個水果,我想用它來插入目標表.

I have currently hard-coded set @last = 2, for this example but @last should contain the number of words in the string. The parameter @substring will contain each fruit one by one in the loop, which I want to use to insert into a target table.

這是我當前的代碼,但我不知道如何將 @last 設置為所需的值:

Here's my current code, but I'm stuck with how to set @last to the required value:

DECLARE @first AS INT
SET @first = 1
DECLARE @step AS INT
SET @step = 1
DECLARE @last AS INT
SET @last = 2

BEGIN TRANSACTION
WHILE(@first <= @last)
BEGIN

INSERT INTO tbFruit(Name)   
VALUES(@substring);

SET @first += @step
END
COMMIT TRANSACTION

推薦答案

您可以一次性完成所有工作,而不是使用 WHILE 循環.因此,在此代碼中,它會將值推送到臨時表的行中,然后使用它INSERT 到目標表中:

You can do it all in one go rather than use a WHILE loop. So in this code, it will push the values into rows of a temp table, before using it to INSERT into a target table:

用于將逗號分隔值拆分為以下行的示例代碼:

Sample code for splitting comma separated values to rows taken from:

DECLARE @string AS NVARCHAR(MAX);
DECLARE @substring AS NVARCHAR(50);
SET @string = 'Apple, Banana, Cherry, Orange, Mango'

SELECT Split.a.value('.', 'VARCHAR(100)') AS Fruits
INTO   #fruits
FROM   
   (
     SELECT CAST ('<M>' + REPLACE(@string, ', ', '</M><M>') + '</M>' AS XML) AS String
   ) AS A
     CROSS APPLY String.nodes('/M') AS Split ( a ); 

-- show what's in the temp table
SELECT *
FROM   #fruits

此時,您在臨時表中的行中有值,您可以使用它來填充目標表,如下所示:

At this point you have the values in rows in a temp table, which you can use to populate your target table like so:

INSERT INTO tbFruit ( Name )
SELECT Fruits FROM #fruits

-- show what's in the target table
SELECT * FROM   #target_table

-- tidy up
DROP TABLE #fruits

SQL 小提琴演示

TSQL 代碼:

 DECLARE @string AS NVARCHAR(MAX) = 'Apple, Banana, Cherry, Orange, Mango'
 DECLARE @substring AS NVARCHAR(50)

 SELECT Split.a.value('.', 'VARCHAR(100)') AS Fruits
 INTO   #fruits
 FROM   ( SELECT    CAST ('<M>' + REPLACE(@string, ', ', '</M><M>') + '</M>' AS XML) AS String
        ) AS A
        CROSS APPLY String.nodes('/M') AS Split ( a )   

 CREATE TABLE #target_table ( Fruits NVARCHAR(50) )

 INSERT INTO #target_table
        ( fruits )
        SELECT  *
        FROM    #fruits

 SELECT * FROM   #target_table

 DROP TABLE #fruits
 DROP TABLE #target_table

結果:

| FRUITS |
|--------|
|  Apple |
| Banana |
| Cherry |
| Orange |
|  Mango |

這篇關于將逗號分隔的值拆分為行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 在线色| 色偷偷888欧美精品久久久 | 亚洲精品4 | 中文字幕国产视频 | 成人片免费看 | 视频一区在线观看 | 亚洲婷婷六月天 | 日本一区二区三区在线观看 | 日本特黄a级高清免费大片 成年人黄色小视频 | 精品真实国产乱文在线 | av免费网址 | 亚洲婷婷六月天 | 亚洲在线免费观看 | 国产在线二区 | 天天操天天射天天 | 亚洲国产精品久久久久 | 高清视频一区二区三区 | 久久久久久国产精品免费免费 | 日日夜夜91 | 不卡视频一区 | 成人超碰| 成人免费看黄网站在线观看 | 国产在线一区二区 | 婷婷久久网 | 国产精品婷婷 | 国产999精品久久久 精品三级在线观看 | 欧美日韩精品在线一区 | 国产黄色一级片 | 国产精品美女久久久久aⅴ国产馆 | 91在线色视频 | 国产精品久久网 | 91天堂网| 日韩中文字幕免费在线 | 蜜桃在线播放 | 久久国产精品久久 | 久久精品国产一区二区电影 | 中文字幕国产精品 | 九九久久国产 | 久久久久无码国产精品一区 | 欧美国产日韩一区二区三区 | 国产高清免费 |