1

我用这个创建了一个表

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

(RegionViewKey int NOT NULL identity Primary Key,                              
RegionView          varchar(10),
RegionViewCode      varchar(10),
ActiveYear              smallint,
SublocationString       varchar(7)NULL,
SubLocationCode     char(10)NOT NULL,
LocationCode            char(10)NULL,
RegionCode          char(10)NULL,
RegionGrpCode           char(10)NULL,
DivisionCode            char(10)NOT NULL,
DivisionGrpCode     char(10)NULL,
SubLocationDescription      char(50)NULL,
LocationDescription     char(50)NULL,
RegionDescription       char(50)NULL,
RegionGrpDescription        char(50)NULL,
DivisionDescription     char(50)NULL,
DivisionGrpDescription      char(50)NULL)

我使用这个脚本插入数据

insert into DWResourceTask.dbo.DimRegion --(2013)

Select
'Region1' as RegionView,
'R1' as RegionViewCode,
'2013' as ActiveYear, 
sl.sublocationstring, 
sl.subLocationCode, 
l.locationcode,
r.regioncode,
rg.RegionGrpCode,  
d.DivisionCode, 
dg.DivisionGrpCode,
sl.SubLocationDescription, 
l.LocationDescription, 
r.regiondescription,
rg.RegionGrpDescription, 
d.divisionDescription, 
dg.DivisionGrpDescription
from SCSubLocation sl, 
SCLocation l, 
SCRegion r, 
SCRegionGrp rg,
SCDivision d,
SCDivisionGrp dg
where l.LocationCode = sl.LocationCode
and r.RegionCode = l.RegionCode
and r.RegionGrpCode = rg.RegionGrpCode
and d.divisioncode = rg.divisioncode
and d.divisiongrpcode = dg.divisiongrpcode

但是当我在下面创建这个脚本时,它只会插入新的和最新的数据,它给了我一个错误,比如

消息 102,级别 15,状态 1,第 33 行
“R1”附近的语法不正确。

脚本:

insert into DWResourceTask.dbo.DimRegion --(2013)

Select
'Region1' as RegionView,
'R1' as RegionViewCode,
'2013' as ActiveYear, 
sl.sublocationstring, 
sl.subLocationCode, 
l.locationcode,
r.regioncode,
rg.RegionGrpCode,  
d.DivisionCode, 
dg.DivisionGrpCode,
sl.SubLocationDescription, 
l.LocationDescription, 
r.regiondescription,
rg.RegionGrpDescription, 
d.divisionDescription, 
dg.DivisionGrpDescription
from SCSubLocation sl, 
SCLocation l, 
SCRegion r, 
SCRegionGrp rg,
SCDivision d,
SCDivisionGrp dg
where l.LocationCode = sl.LocationCode
and r.RegionCode = l.RegionCode
and r.RegionGrpCode = rg.RegionGrpCode
and d.divisioncode = rg.divisioncode
and d.divisiongrpcode = dg.divisiongrpcode
and not exists(select * from DWResourceTask.dbo.DimRegion x
                  where(Region1=x.RegionView
                        R1=x.RegionViewCode
                        2013=x.ActiveYear 
                        sl.sublocationstring=x.sublocationstring
                        sl.subLocationCode=x.subLocationCode
                         l.locationcode=x.locationcode
                         r.regioncode=x.regioncode
                        rg.RegionGrpCode=x.RegionGrpCode
                         d.DivisionCode=x.DivisionCode
                        dg.DivisionGrpCode=x.DivisionGrpCode
                        sl.SubLocationDescription=x.SubLocationDescription
                         l.LocationDescription=x.LocationDescription 
                         r.regiondescription=x.regiondescription
                        rg.RegionGrpDescription=x.RegionGrpDescription
                         d.divisionDescription=x.divisionDescription 
                        dg.DivisionGrpDescription=x.DivisionGrpDescription)
                        )

如何在仅插入最新数据的顶部修复此脚本

4

1 回答 1

2

看起来您的WHERE子句缺少AND条件部分之间的 s :

...
where(Region1=x.RegionView
     AND  R1=x.RegionViewCode
     AND  2013=x.ActiveYear 
     AND  sl.sublocationstring=x.sublocationstring
     AND  sl.subLocationCode=x.subLocationCode
     AND  l.locationcode=x.locationcode
     AND  r.regioncode=x.regioncode
     AND  rg.RegionGrpCode=x.RegionGrpCode
     AND   d.DivisionCode=x.DivisionCode
     AND  dg.DivisionGrpCode=x.DivisionGrpCode
     AND  sl.SubLocationDescription=x.SubLocationDescription
     AND   l.LocationDescription=x.LocationDescription 
     AND   r.regiondescription=x.regiondescription
     AND  rg.RegionGrpDescription=x.RegionGrpDescription
     AND   d.divisionDescription=x.divisionDescription 
     AND  dg.DivisionGrpDescription=x.DivisionGrpDescription)
 )
于 2013-11-04T01:29:03.857 回答