如何仅将新记录插入具有多个联合的表中?

时间:2014-02-18 09:21:19

标签: sql sql-server

我创建了一个表

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[ODSCustomerContact]') AND type in (N'U'))
DROP TABLE [ODSCustomerContact]
Go

Create Table ODSCustomerContact 
(
    CustomerContactInt int NOT NULL identity PRIMARY KEY,
    ContactID           varchar(20) NULL,
    SiteID              varchar(20) NULL,
    ClientName          nvarchar(max) NULL,
    ContactFirstName    nvarchar(max) NULL,
    ContactLastName     nvarchar(max) NULL,
    Position            nvarchar(max) NULL,
    Phone               nvarchar(max) NULL,
    MobilePhone         nvarchar(max) NULL,
    EmailAddress        nvarchar(max) NULL
)

我已使用此脚本在此表中插入了初始或第一批数据。

insert into SEC_ODS.dbo.ODSCustomerContact 
(ContactID,
SiteID,
ClientName,
ContactFirstName,
ContactLastName,
Position,
Phone,
MobilePhone,
EmailAddress)

   select 
       CONVERT(int, CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
      'EXCL_E4SE' as SiteID,
      Clientname, 
      Contact_FirstName, 
      Contact_LastName ,
      Position,
      Phone,
      MobilePhone,
      EmailAdress
   from (  
       select distinct 
          e.BusinessEntityID,
          e.ContactID,
          d.SoldToShortName as Clientname, 
          e.FirstName as Contact_FirstName, 
          e.LastName as Contact_LastName ,
          e.Position,
          e.Phone,
          e.MobilePhone,
          e.EmailAddr as EmailAdress
       from 
          EXCL_E4SE.dbo.projectrule d
       left join 
          EXCL_E4SE.dbo.BusinessEntityAssociation BEA on d.soldtoID = BEA.customerID 
       left join 
          EXCL_E4SE.dbo.BusinessEntityContact e on e.BusinessEntityID = BEA.BusinessEntityID 
       where 
          d.SoldToShortName IS NOT NULL
          and e.BusinessEntityID is not null) a --order by Clientname

UNION ALL

select CONVERT(int,CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
  'ASIA_E4SE' as SiteID,
  Clientname, 
  Contact_FirstName, 
  Contact_LastName ,
  Position,
  Phone,
  MobilePhone,
  EmailAdress
from (  
  select distinct e.BusinessEntityID,
    e.ContactID,
    d.SoldToShortName as Clientname, 
    e.FirstName as Contact_FirstName, 
    e.LastName as Contact_LastName ,
    e.Position,
    e.Phone,
    e.MobilePhone,
    e.EmailAddr as EmailAdress

  from ASIA_E4SE.dbo.projectrule d
  left join ASIA_E4SE.dbo.BusinessEntityAssociation BEA 
  on d.soldtoID = BEA.customerID 
  left join ASIA_E4SE.dbo.BusinessEntityContact e 
  on e.BusinessEntityID = BEA.BusinessEntityID 

  where d.SoldToShortName IS NOT NULL
     and e.BusinessEntityID is not null
     ) b --order by Clientname


UNION ALL


select CONVERT(int,CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
  'BGD_E4SE' as SiteID,
  Clientname, 
  Contact_FirstName, 
  Contact_LastName ,
  Position,
  Phone,
  MobilePhone,
  EmailAdress
from (  
  select distinct e.BusinessEntityID,
    e.ContactID,
    d.SoldToShortName as Clientname, 
    e.FirstName as Contact_FirstName, 
    e.LastName as Contact_LastName ,
    e.Position,
    e.Phone,
    e.MobilePhone,
    e.EmailAddr as EmailAdress

  from BGD_E4SE.dbo.projectrule d
  left join BGD_E4SE.dbo.BusinessEntityAssociation BEA 
  on d.soldtoID = BEA.customerID 
  left join BGD_E4SE.dbo.BusinessEntityContact e 
  on e.BusinessEntityID = BEA.BusinessEntityID 

  where d.SoldToShortName IS NOT NULL
     and e.BusinessEntityID is not null
     ) c order by Clientname

上面的插入脚本成功了。但是,下面的插入新记录的脚本不起作用。我运行此脚本时得到的错误是这个。

  

Msg 1033,Level 15,State 1,Line 125
  除非还指定了TOP或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

这是“仅插入新记录”脚本。有人可以帮我纠正这个吗?谢谢

实际上它取决于ALIAS字母M而不是Alias字母C,我这样做是为了避免框内的长脚本。休息确保从字母D到字母M的ALIAS与ALIAS表A,B和C一样,但具有不同的数据库源,例如EXCL_E4SE为A,ASIA_E4SE为B ....

insert into SEC_ODS.dbo.ODSCustomerContact 
(ContactID,
SiteID,
ClientName,
ContactFirstName,
ContactLastName,
Position,
Phone,
MobilePhone,
EmailAddress)

select 
x.ContactID,
x.SiteID,
x.ClientName,
x.ContactFirstName,
x.ContactLastName,
x.Position,
x.Phone,
x.MobilePhone,
x.EmailAddress
from
(
select CONVERT(int,CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
  'EXCL_E4SE' as SiteID,
  Clientname, 
  Contact_FirstName, 
  Contact_LastName ,
  Position,
  Phone,
  MobilePhone,
  EmailAdress
from (  
  select distinct e.BusinessEntityID,
    e.ContactID,
    d.SoldToShortName as Clientname, 
    e.FirstName as Contact_FirstName, 
    e.LastName as Contact_LastName ,
    e.Position,
    e.Phone,
    e.MobilePhone,
    e.EmailAddr as EmailAdress

  from EXCL_E4SE.dbo.projectrule d
  left join EXCL_E4SE.dbo.BusinessEntityAssociation BEA 
  on d.soldtoID = BEA.customerID 
  left join EXCL_E4SE.dbo.BusinessEntityContact e 
  on e.BusinessEntityID = BEA.BusinessEntityID 

  where d.SoldToShortName IS NOT NULL
     and e.BusinessEntityID is not null
     ) a --order by Clientname

UNION ALL


select CONVERT(int,CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
  'ASIA_E4SE' as SiteID,
  Clientname, 
  Contact_FirstName, 
  Contact_LastName ,
  Position,
  Phone,
  MobilePhone,
  EmailAdress
from (  
  select distinct e.BusinessEntityID,
    e.ContactID,
    d.SoldToShortName as Clientname, 
    e.FirstName as Contact_FirstName, 
    e.LastName as Contact_LastName ,
    e.Position,
    e.Phone,
    e.MobilePhone,
    e.EmailAddr as EmailAdress

  from ASIA_E4SE.dbo.projectrule d
  left join ASIA_E4SE.dbo.BusinessEntityAssociation BEA 
  on d.soldtoID = BEA.customerID 
  left join ASIA_E4SE.dbo.BusinessEntityContact e 
  on e.BusinessEntityID = BEA.BusinessEntityID 

  where d.SoldToShortName IS NOT NULL
     and e.BusinessEntityID is not null
     ) b --order by Clientname


UNION ALL


select CONVERT(int,CAST(BusinessEntityID AS varchar(20)) + CAST(ContactID AS varchar(20))) as ContactID,
  'BGD_E4SE' as SiteID,
  Clientname, 
  Contact_FirstName, 
  Contact_LastName ,
  Position,
  Phone,
  MobilePhone,
  EmailAdress
from (  
  select distinct e.BusinessEntityID,
    e.ContactID,
    d.SoldToShortName as Clientname, 
    e.FirstName as Contact_FirstName, 
    e.LastName as Contact_LastName ,
    e.Position,
    e.Phone,
    e.MobilePhone,
    e.EmailAddr as EmailAdress

  from BGD_E4SE.dbo.projectrule d
  left join BGD_E4SE.dbo.BusinessEntityAssociation BEA 
  on d.soldtoID = BEA.customerID 
  left join BGD_E4SE.dbo.BusinessEntityContact e 
  on e.BusinessEntityID = BEA.BusinessEntityID 

  where d.SoldToShortName IS NOT NULL
     and e.BusinessEntityID is not null
     ) c order by Clientname, ClientID

     )  x

     where not exists (select * from SEC_ODS.dbo.ODSCustomerContact)

2 个答案:

答案 0 :(得分:1)

正如您对其他ORDER BYs所做的那样

where d.SoldToShortName IS NOT NULL
 and e.BusinessEntityID is not null
 ) a --order by Clientname
....

 where d.SoldToShortName IS NOT NULL
 and e.BusinessEntityID is not null
 ) b --order by Clientname

您还需要注释掉以下内容

where d.SoldToShortName IS NOT NULL
 and e.BusinessEntityID is not null
 ) c -->order by Clientname, ClientID<--

 )  x

答案 1 :(得分:1)

INSERT/SELECT/UNION ALL的查询无效,因为ORDER BY子句只允许在指令末尾和外部查询中,如documentation中所述:

  

在使用UNION,EXCEPT或INTERSECT运算符的查询中,ORDER BY   仅在声明结尾处被允许。此限制适用   仅限于在顶级指定UNION,EXCEPT和INTERSECT时   查询而不是子查询。

您必须从查询中删除ORDER BY子句才能使此脚本正常运行。