1. 程式人生 > >Union All拼接Sql語句做批量新增

Union All拼接Sql語句做批量新增

    上篇文章《多種方法實現Excel批量匯入資料庫》說到,批量匯入用了拼接Insert語句的方式進行,這種方式簡單來說是把要匯入的每條資料都拼接成一條Insert語句, 然後同時執行實現的。但是經過領導提醒,又用了另外一種方式,那就是用Union All做合併,合併完一張表之後,整張表匯入。下面就簡單介紹一下。

    Union All是做聯表查詢的,平常的用法是:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">select * from Table1 union all select * from Table2</span></span>
union all在這裡的用法是,查詢Table1的時候,先查詢Table2的所有欄位,將所有欄位和Table2合併起來,然後顯示出來。union同樣也能做這個功能,不過二者有一點區別。union合併的時候,是兩張表相同的欄位合併為一個欄位,而union all做到的是整張表所有的欄位都合併給Table1。

    在我們這個系統中,是將union all放到了insert語句中,簡單介紹一下程式碼:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">        #region 批量新增實體
        /// <summary>
        /// 批量新增實體
        /// </summary>
        /// <param name="settingevaluation">要匯入資料庫的資料組成的泛型集合</param>
        /// <returns></returns>
        public bool Insert(List<YzSettingEvaluationEntity> settingevaluation)
        {
            //開啟資料庫連線
            SQLHelper sqlHelper = new SQLHelper("YzEvaluationSystemEntities");
            StringBuilder sbSql = new StringBuilder();

            //迴圈實體list,進行拼接
            if (settingevaluation.Count > 1)
            {
                sbSql.Append("insert into YzSettingEvaluationEntity(ID,EvaluatorID,CriticID,IsEvaluated,Weight,Year,EvaluationInfoID,IsUsed) ");
                for (int i = 0; i < settingevaluation.Count - 1; i++)
                {
                    sbSql.Append("select ");
                    sbSql.Append("'" + settingevaluation[i].ID + "',");
                    sbSql.Append("'" + settingevaluation[i].EvaluatorID + "',");
                    sbSql.Append("'" + settingevaluation[i].CriticID + "',");
                    sbSql.Append(0 + ",");
                    sbSql.Append("'" + settingevaluation[i].Weight + "',");
                    sbSql.Append("'" + settingevaluation[i].Year + "',");
                    sbSql.Append("'" + settingevaluation[i].EvaluationInfoID + "',");
                    sbSql.Append(1);
                    sbSql.Append(" union all ");
                }
                sbSql.Append("select ");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].ID + "',");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].EvaluatorID + "',");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].CriticID + "',");
                sbSql.Append(0 + ",");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].Weight + "',");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].Year + "',");
                sbSql.Append("'" + settingevaluation[settingevaluation.Count - 1].EvaluationInfoID + "',");
                sbSql.Append(1);
                sbSql.Append(";");
            }

            YzSettingEvaluationEntity set = new YzSettingEvaluationEntity();
            var lists = new[] { "ID", "EvaluatorID", "CriticID", "IsEvaluated", "Weight", "Year", "EvaluationInfoID", "IsUsed" };

            //呼叫sqlhelper方法進行新增。
            int flag = sqlHelper.ExecuteNonQuery(sbSql.ToString(), System.Data.CommandType.Text);
            if (flag > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion</span></span>
直接拼接Insert語句,得到的SQL語句是這個樣子的:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">insert into YzStaffEntity(ID,StaffName,StaffPassword,StaffID,Sex,IdentityCard,Subject,WorkDate,EngageDate,jobQualification,DivisionID,SeriesID,PositionID,IsUsed) values ('813a3a46-df83-49bf-8b78-d39f6ee73fa8','何志強','151035','1','男','132827196412151035','','1985/8/1 0:00:00','','中教高階','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','dc1166b7-d394-437c-bf12-a86e488ae930','1' )

insert into YzStaffEntity(ID,StaffName,StaffPassword,StaffID,Sex,IdentityCard,Subject,WorkDate,EngageDate,jobQualification,DivisionID,SeriesID,PositionID,IsUsed) values ('8572db21-930f-4c53-8282-d8dbafd0b9c5','劉懷增','054414','2','男','132801196306054414','','1982/4/1 0:00:00','','','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','cf0ade2f-3ed7-4fbb-b693-70b98e014ea8','1' )

insert into YzStaffEntity(ID,StaffName,StaffPassword,StaffID,Sex,IdentityCard,Subject,WorkDate,EngageDate,jobQualification,DivisionID,SeriesID,PositionID,IsUsed) values ('0e6b61d4-6fb5-4d83-88bd-e20ba0e2afb3','劉文俊','170819','3','男','132801196110170819','','1986/7/1 0:00:00','','中教高階','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','cf0ade2f-3ed7-4fbb-b693-70b98e014ea8','1' )</span></span>
而用union all拼接完的SQL語句的樣子是這樣的:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">insert into YzSettingEvaluationEntity(ID,EvaluatorID,CriticID,IsEvaluated,Weight,Year,EvaluationInfoID,IsUsed) select 'd000917f-9ebd-45e5-a574-0f2fa57539ad','63eff2c1-0dc8-4822-8664-1e54d9e632ec','439b6794-7467-4623-a8f6-0756c12e3eb5',0,'f87680ce-1cb4-d2d0-7aa5-04a6c5c3edf6','621541d6-1697-fb82-fc14-8540ff1cf000','0b2bc4cd-ac5f-412e-a227-c3b4ac61ddc5',1 union all 

select '6977b00d-94ea-442f-b22e-fd296c54d2e1','63eff2c1-0dc8-4822-8664-1e54d9e632ec','51c11fd2-65ca-4c61-9e13-0c44c735e4bd',0,'f87680ce-1cb4-d2d0-7aa5-04a6c5c3edf6','621541d6-1697-fb82-fc14-8540ff1cf000','0b2bc4cd-ac5f-412e-a227-c3b4ac61ddc5',1 union all 

select '303de911-52cc-4e20-ba88-6b301c59728a','63eff2c1-0dc8-4822-8664-1e54d9e632ec','09bd256e-eda4-47e4-bbe6-0e68eb472659',0,'f87680ce-1cb4-d2d0-7aa5-04a6c5c3edf6','621541d6-1697-fb82-fc14-8540ff1cf000','0b2bc4cd-ac5f-412e-a227-c3b4ac61ddc5',1 union all; 
</span></span>
    關於執行速度上面,還是Insert語句快一點點,經過測試,直接拼接Insert語句的,插入6400條資料花費21秒;而union all這種方式插入5550條資料花費了19秒,效能上二者相差不是很大,簡單的匯入用哪種方式都是可以的。

    在我們拼接SQL語句的過程中,還用過一種拼接Insert語句,拼接完的效果是:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">insert into YzStaffEntity(ID,StaffName,StaffPassword,StaffID,Sex,IdentityCard,Subject,WorkDate,EngageDate,jobQualification,DivisionID,SeriesID,PositionID,IsUsed) values ('813a3a46-df83-49bf-8b78-d39f6ee73fa8','何志強','151035','1','男','132827196412151035','','1985/8/1 0:00:00','','中教高階','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','dc1166b7-d394-437c-bf12-a86e488ae930','1' ),

 ('8572db21-930f-4c53-8282-d8dbafd0b9c5','劉懷增','054414','2','男','132801196306054414','','1982/4/1 0:00:00','','','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','cf0ade2f-3ed7-4fbb-b693-70b98e014ea8','1' ),

 ('0e6b61d4-6fb5-4d83-88bd-e20ba0e2afb3','劉文俊','170819','3','男','132801196110170819','','1986/7/1 0:00:00','','中教高階','3b253d62-54f9-4dd2-9897-7c8da7107693','e1a5eec0-d61f-4d55-86e2-fbffae1a4fdd','cf0ade2f-3ed7-4fbb-b693-70b98e014ea8','1' )
</span></span>
    這樣的SQL語句有一個弊病,那就是插入的資料超過千條後,就提示插入不成功,因為一條Insert語句插入的資料量是不能超過一千條的,所以如果有這個問題,可以選擇上面介紹的任意一種方式來解決。

總結

    記憶很深的有一節米老師講的課,主題是人生如程式碼,程式碼如人生,越來越能體會到這句話的含義。你的程式碼就像你的人生一樣,人生路上有很多個岔路口,每經過一個岔路口都需要你做出選擇,程式碼也一樣。實現同一個功能的方式有很多,關鍵是要選擇最適合的那個。而且程式碼需要用心去寫,就像你的路需要用心去走一樣。