如何有效地合并SQL Server数据?

时间:2014-10-14 19:38:56

标签: c# sql sql-server

我在SQL Server中有三个表,我需要将所有表中的所有匹配行组合成第四个MergedTable,它将包含基于U_ID列的三个单独表中的所有列。

有没有办法在存储过程中通过T-SQL执行此操作,还是应该在C#中创建循环函数?

底线是这将从网站的命令执行,因此它需要是我可以封装到MVC项目或组件中的东西。

以下是表格的示例。

表1:

U_ID       ClientNumber   OrderDate   Amount
---------------------------------------------
BB000Kw    1920384        5/14/2013   1093.39
AA000bM    3839484        12/8/2012   584.42
AA000gH    8294848        2/28/2014   4849.38
AA000md    3849484        4/31/2013   590.84
AA000mF    3998398        3/29/2013   448.82
AA000mG    9944848        11/28/2014  98.85
AA000mn    0292938        10/31/2012  300.48

表2:

U_ID       Name                 Date
------------------------------------------
AA000bM    "Krivis, Jeffrey"    7/1/2002
AA000bv    "Saydah, Michael"    7/30/2002
AA000cA    "Byrne, Richard"     4/21/2003
AA000dd    "McNeil, Joseph"     6/10/2003
AA000dH    "Greenberg, Arnold"  1/16/2003
AA000gH    "Rich, Elwood"       7/5/2003
AA000id    "O'Neill, Robert J." 11/20/2002
AA000jf    "Patsey, Richard"    4/22/2003
AA000jr    "Jones, Arthur"      7/1/2002
AA000jU    "Toff, Ronald"       7/15/2002
AA000k4    "Anderson, Carl"     8/14/2002
BB000Kw    "Wilson, Sam"        3/9/2003

表3:

U_ID       Name
-----------------------------
AA000bM    Acme Company
AA000jr    Stockwell Industries
BB000ke    Gensen Motors
BB999di    Falstaff Cards
BB000dl    B and R Printing
BB000Kw    Go Golf Carts
AA000gH    Rich's Sandwiches

产生的合并表

U_ID       ClientNumber   OrderDate   Amount     CustomerName         JoinDate     CompanyName
-------------------------------------------------------------------------------------------------------
BB000Kw    1920384        5/14/2013   1093.39    "Wilson, Sam"        3/9/2003     Go Golf Carts
AA000bM    3839484        12/8/2012   584.42     "Krivis, Jeffrey"    7/1/2002     Acme Company
AA000gH    8294848        2/28/2014   4849.38    "Rich, Elwood"       7/5/2003     Rich's Sandwiches

表1是其他匹配的主表。您可以从结果中看到,根据表1中匹配的表格,只有所有表的子集。

我将MVC与Entity Framework 6和Linq-to-Entities一起使用,但如果T-SQL脚本效率更高,那么我应该使用它。

获得此结果的更好方法是什么?

2 个答案:

答案 0 :(得分:1)

如果要创建新表,可以使用SELECT ... INTO ... FROM ...查询。在你的情况下,它看起来像这样:

SELECT t1.U_ID, t1.ClientNumber, t1.OrderDate, t1.Amount, 
       t2.Name as CustomerName, t2.Date as JoinDate, 
       t3.Name as CompanyName
INTO dbo.ResultingMergedTable
FROM Table1 t1 
INNER JOIN Table2 t2 ON t1.U_ID = t2.U_ID
INNER JOIN Table3 t3 ON t1.U_ID = t3.U_ID

请记住,如果您正在查看非常大的数据表,这将花费大量时间来执行。

答案 1 :(得分:0)

您可以创建第4个表来执行您提到的操作,但如果您使用的是sql,则可以创建一个视图来执行相同的操作。视图是虚拟表。我们在对数据进行分区以及如上所述的详细记录时使用它。

http://msdn.microsoft.com/en-us/library/ms187956.aspx

http://www.sqlinfo.net/sqlserver/sql_server_VIEWS_the_basics.php

CREATE VIEW DetailView AS
(
   SELECT
     -- table1
     t1.U_ID,
 t1.ClientNumber,
 t1.OrderDate,
 t1.Amount,
 -- table2
 t2.Name,
 t2.Date as [JoinDate],
 -- table3
 t3.Name as [Company]
   FROM 
     table1 t1
     LEFT JOIN  
   table2 t2
     ON t1.U_ID = t2.U_ID
 LEFT JOIN
   table3 t3
     ON t1.U_ID = t3.U_ID
   WHERE
      t1.U_ID = t2.U_ID
  and 
  t1.U_ID = t3.U_ID
)