2013-11-04 167 views
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,行鄰近 'R1' 33
不正確的語法。

腳本:

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) 
         ) 

如何解決在上面這個腳本,它只是插入最新數據

+0

提示:不要使用'char(50)' - 這將創建一個固定長度的列,將**總是s **長達50個字符 - 無論你在裏面儲存什麼。無論您存儲什麼,都會用空格填充到固定長度。 'char(n)'對**短**字符串是有效的,就像代碼(例如3位數的ISO國家代碼等) - 但它對於更長的字符串非**有用 - 使用'varchar(50)'代替。 –

+0

嗨marc_s,我在創建表時使用的數據類型是基於我們當前ERP系統中現有的源表。感謝您對此的看法。謝謝 – BA82283

回答

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) 
) 
+1

謝謝dasblinkenlight。我沒有注意到我錯過了那些AND。也許我可以在做這個之前使用一些早餐。 – BA82283

+0

謝謝dasblinkenlight! – BA82283

相關問題