使用联合在sql中合并两个表

时间:2015-02-18 10:29:53

标签: sql sql-server union

我想显示来自2个表的数据,因为我使用的是union,它不起作用。

 SELECT   
  CA.ContactAddressId,  
  CA.CompanyName AS CompanyName,  
  CA.ContactPerson AS ContactPerson,  
  CA.Address1 AS Street,  
  ISNULL(CA.Address2,'') AS Street2,  
  CA.City,  
  CA.[State] AS [State],  
  CA.Country AS CountryCode,  
  CASE  
   WHEN CA.Country = 1 THEN 'USA'  
   WHEN CA.Country = 2 THEN 'CANADA'  
   WHEN CA.Country = 4 THEN 'MEXICO'  
  END AS Country,  
  CA.ZipCode,  
  CA.WorkPhone AS Phone,  
  ISNULL(CA.Fax,'') AS Fax,  
  CA.Email AS [Email],  
  CA.AddressType,  
  CA.AddressCode,  
  CADS.PickupReadyTime AS PickupReadyTime,  
  CADS.PickupCloseTime AS PickupCloseTime,  
  CADS.DeliveryReadyTime AS DeliveryOpeningTime,  
  CADS.DeliveryCloseTime AS DeliveryClosingTime,  
  CAST(ISNULL(CAD.ContactAddressId,0) as bit) IsDefault,  
  CADS.PickupRemarks,  
  CADS.DeliveryRemarks  
 INTO #ContactAddressDetails  
 FROM CR2.ContactAddress AS CA WITH (NOLOCK)  
 INNER JOIN #ContactGroupList list  
  ON list.ContactGroupId = CA.ContactGroupId  
 LEFT JOIN CR2.ContactAddressDefaultSettings AS CADS WITH (NOLOCK)  
  ON CADS.ContactAddressId = CA.ContactAddressId  
 LEFT JOIN CR2.ContactAddressDefault CAD WITH (NOLOCK) ON CAD.ContactAddressId = CA.ContactAddressId   
  AND CAD.UserId = @LoggedInUserId   
  AND CA.IsDeleted = 0  
  AND CA.AddressType IN (3)  

这是我的第一张表,

 SELECT 
     CA.ID  
    ,CA.CompanyName AS CompanyName   
    ,CA.ContactName AS ContactPerson         
    ,CA.Street  
    ,ISNULL(CA.Street2,'') AS Street2  
    ,CA.City  
    ,CA.[StateCode] AS [State]  
    ,CA.Country AS CountryCode
    , CASE  
        WHEN CA.Country = 1 THEN 'USA'  
        WHEN CA.Country = 2 THEN 'CANADA'  
        WHEN CA.Country = 4 THEN 'MEXICO'  
        END AS Country
    ,CA.ZipCode  
    ,CA.PhoneNo AS Phone  
    ,ISNULL(CA.Fax,'') AS Fax  
    ,CA.EmailId
    ,CA.AddressType   
    ,CA.AddressCode
    ,CA.PickupReadyTime AS  PickupReadyTime 
    ,CA.PickupCloseTime AS  PickupCloseTime   
    ,CA.DeliveryReadyTime AS DeliveryReadyTime  
    ,CA.DeliveryCloseTime AS DeliveryCloseTime 
    , CAST(ISNULL(CA.ID,0) as bit) IsDefault
    ,CA.PickupRemarks   
    ,CA.DeliveryRemarks      
    INTO #RexnordAddressDetails
  FROM [GTCommon].[dbo].[CustomerAddresses] AS CA WITH(NOLOCK)  
  WHERE CA.IsBlocked = 0  
   AND CA.AddressType IN (1,2) AND CA.CustomerId=@LoggedInUserId

这是我的第二张桌子 我使用Union来显示表数据。我的做法是对的吗?

 SELECT *  
 FROM #ContactAddressDetails
 UNION 
 Select * 
 FROM  #RexnordAddressDetails

请建议

1 个答案:

答案 0 :(得分:0)

正如@frlan所说,你不需要使用临时表。您可以在一个声明中完成所有操作。请注意,输出的列 names 将是第一个SELECT中定义的列名。

SELECT   
  CA.ContactAddressId,  
  CA.CompanyName AS CompanyName,  
  CA.ContactPerson AS ContactPerson,  
  CA.Address1 AS Street,  
  ISNULL(CA.Address2,'') AS Street2,  
  CA.City,  
  CA.[State] AS [State],  
  CA.Country AS CountryCode,  
  CASE  
   WHEN CA.Country = 1 THEN 'USA'  
   WHEN CA.Country = 2 THEN 'CANADA'  
   WHEN CA.Country = 4 THEN 'MEXICO'  
  END AS Country,  
  CA.ZipCode,  
  CA.WorkPhone AS Phone,  
  ISNULL(CA.Fax,'') AS Fax,  
  CA.Email AS [Email],  
  CA.AddressType,  
  CA.AddressCode,  
  CADS.PickupReadyTime AS PickupReadyTime,  
  CADS.PickupCloseTime AS PickupCloseTime,  
  CADS.DeliveryReadyTime AS DeliveryOpeningTime,  
  CADS.DeliveryCloseTime AS DeliveryClosingTime,  
  CAST(ISNULL(CAD.ContactAddressId,0) as bit) IsDefault,  
  CADS.PickupRemarks,  
  CADS.DeliveryRemarks  
 FROM CR2.ContactAddress AS CA WITH (NOLOCK)  
 INNER JOIN #ContactGroupList list  
  ON list.ContactGroupId = CA.ContactGroupId  
 LEFT JOIN CR2.ContactAddressDefaultSettings AS CADS WITH (NOLOCK)  
  ON CADS.ContactAddressId = CA.ContactAddressId  
 LEFT JOIN CR2.ContactAddressDefault CAD WITH (NOLOCK) ON CAD.ContactAddressId = CA.ContactAddressId   
  AND CAD.UserId = @LoggedInUserId   
  AND CA.IsDeleted = 0  
  AND CA.AddressType IN (3)  

UNION ALL

 SELECT 
     CA.ID  
    ,CA.CompanyName AS CompanyName   
    ,CA.ContactName AS ContactPerson         
    ,CA.Street  
    ,ISNULL(CA.Street2,'') AS Street2  
    ,CA.City  
    ,CA.[StateCode] AS [State]  
    ,CA.Country AS CountryCode
    , CASE  
        WHEN CA.Country = 1 THEN 'USA'  
        WHEN CA.Country = 2 THEN 'CANADA'  
        WHEN CA.Country = 4 THEN 'MEXICO'  
        END AS Country
    ,CA.ZipCode  
    ,CA.PhoneNo AS Phone  
    ,ISNULL(CA.Fax,'') AS Fax  
    ,CA.EmailId
    ,CA.AddressType   
    ,CA.AddressCode
    ,CA.PickupReadyTime AS  PickupReadyTime 
    ,CA.PickupCloseTime AS  PickupCloseTime   
    ,CA.DeliveryReadyTime AS DeliveryReadyTime  
    ,CA.DeliveryCloseTime AS DeliveryCloseTime 
    , CAST(ISNULL(CA.ID,0) as bit) IsDefault
    ,CA.PickupRemarks   
    ,CA.DeliveryRemarks      
  FROM [GTCommon].[dbo].[CustomerAddresses] AS CA WITH(NOLOCK)  
  WHERE CA.IsBlocked = 0  
   AND CA.AddressType IN (1,2) AND CA.CustomerId=@LoggedInUserId

除非你正确理解它们是如何工作的,否则我会反对(NOLOCK)提示。