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

INSERT INTO SELECT 使用 UNION 的奇怪順序

INSERT INTO SELECT strange order using UNION(INSERT INTO SELECT 使用 UNION 的奇怪順序)
本文介紹了INSERT INTO SELECT 使用 UNION 的奇怪順序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個典型的非規范化表 (tempTable),其中包含多個編號的列 (rep1,rep2,...).所以我寫了一個腳本將非規范化數據插入規范化表 (myTable):

I had a typical non-normalized table (tempTable) with multiple numbered columns (rep1,rep2,...). So i wrote a script to insert the non-normalized data into a normalized table (myTable):

insert into myTable
select idRep,rep FROM
(
    select idRep, ISNULL(rep1,'') as rep FROM tempTable
    union
    select idRep, ISNULL(rep2,'') as rep FROM tempTable
    union
    select idRep, ISNULL(rep3,'') as rep FROM tempTable
    union
    select idRep, ISNULL(rep4,'') as rep FROM tempTable
    union
    select idRep, ISNULL(rep5,'') as rep FROM tempTable
) as t

注意:表 myTable 還包含一個自動遞增的 IDENTITY 列作為它的 PRIMARY KEY.

Note: The table myTable also contains an auto-incremented IDENTITY column as its PRIMARY KEY.

在我的場景中,rep1、rep2、rep3、rep4、rep5 的順序很重要.奇怪的是,當我執行腳本時,數據沒有以正確的順序插入,例如自動生成的 id '1000' 的值來自 'rep3',而 id '1001' 的值來自 'rep1'.

The order rep1, rep2, rep3, rep4, rep5 is important in my scenario. Strangely, when I executed the script, the data wasn't inserted in the correct order such as the auto-generated id '1000' had the value from 'rep3' and the id '1001' had the value from 'rep1'.

這是為什么?腳本是如何執行的?

Why is that? How was the script executed?

推薦答案

在使用 UNION 時它沒有按照您期望的順序進行的原因是 union 試圖強加唯一性,因此它正在處理所有這些行并帶來按照對引擎最方便的順序排列.

The reason it is not going in the order you expect when using UNION is that union attempts to impose uniquness, so it is processing all of those rows together and bringing them out in the order most convenient for the engine.

如果您像 Parado 建議的那樣切換到 UNION ALL(它不會嘗試強加唯一性),它將不會進行處理,并且它們將按照您放入的順序進入表,幾乎每時每刻.然而,這并不是絕對的,其他進程中發生的某些非常不尋常的情況(尤其是那些以某種方式觸及您的臨時表的情況)可能會影響它.

If you switch to UNION ALL (which does not try to impose uniqueness) as Parado suggested it will not do the processing and they will go into the table in the order you put them in, almost all the time. This however is not gaurunteed and certain very unusual circumstances going on in other processes (especially ones that somehow touch on your tempTable) can affect it.

如果您按照 Kash 的建議使用 order by,那么這將保證 id 的順序(這可能很重要),但從技術上講,不會確定插入行的順序(這在實踐中很少有影響).

If you use an order by as Kash suggests then that will gauruntee the order of the ids (which can matter), but not technically the order that the rows get inserted (which very rarely matters in practice).

對其中的一些內容進行了很好的總結MSDN.

所以,這就說明了原因.至于如何獲得您真正想要的東西,我會使用 Kash 的建議,即添加一列與 order by 子句一起使用,但我會使用 UNION ALL 而不是 UNION.使用 UNION 就像添加和隱含的distinct"要求一樣,會占用處理器周期并使查詢計劃更加復雜.

So, that takes care of the why. As for the how to get what you actually want, I would use Kash's suggestion of adding a column to use with an order by clause, but I would use UNION ALL instead of UNION. Using UNION is like adding and implicit "distinct" requirement, which takes up processor cycles and makes the query plan more complicated.

這篇關于INSERT INTO SELECT 使用 UNION 的奇怪順序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产一区二区三区久久久久久久久 | 成人在线观看网站 | 免费在线观看成人 | 亚洲精品一级 | 日韩欧美国产一区二区 | 嫩草研究影院 | 欧美精品一区二区三区在线 | 精品福利一区二区三区 | 亚洲激精日韩激精欧美精品 | 国产精品一区二区欧美黑人喷潮水 | 欧美性大战久久久久久久蜜臀 | 国产精品久久久久久中文字 | www.亚洲.com| 久久亚洲综合 | 皇色视频在线 | 亚洲成人精品 | www.日日操| 国产乱码精品一区二区三区五月婷 | 伊人影院在线观看 | 欧美一区在线看 | 国产精品婷婷 | 超碰综合 | 中文字幕精品一区 | 四虎影院免费在线播放 | 波多野结衣精品在线 | 日韩精品一区二区在线观看 | 久久久免费毛片 | 国产精品色 | 欧美午夜精品 | 国产欧美日韩在线观看 | 国产精品一区视频 | 亚洲影音先锋 | 懂色tv| 日本成人福利 | 成人免费一区二区三区牛牛 | 色综合色综合 | 在线高清免费观看视频 | 免费三级黄 | 免费观看av网站 | 99精品久久 | 福利精品 |