2012-02-11 129 views
0

我有一個應用程序,其中兩列可以從多個不同的應用程序填充;一,XY位置(stgeometry)由應用程序確定:二,XY位置由用戶鍵入單獨的x和y列。而不是觸發器不工作

我想出了以下觸發器來填充場景1中的X和Y列,並填充場景2中的形狀列。它似乎是循環並且不填充任一列。我哪裏做錯了?

ALTER trigger [dbo].[update_location_geometry] 
    --Trigger to allow non-gis input of XY data in X_Coord and Y_Coord columns, yet still populate the WKB 
    --shape column with coordinate using 26917 EPSG so that new location will be available to the gis without 
    -- additional user intervention 
    on [dbo].[TBL_LOCATIONS] 
    instead of insert as 
    begin 
--this block populates stgeometry.shape col with user-input XY and is not using the application to 
-- determine xy 
    if exists (select top 1 * from inserted where shape is not null) 

    update dbo.tbl_locations 
    set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
         CAST(Y_Coord AS VARCHAR(20)) + ')', 26917) 
    WHERE (UPDATE(X_Coord) OR UPDATE(Y_Coord)) 
       AND objectId IN (SELECT ins.objectId FROM inserted ins) 


    --this block populates the XY column from stgeometry.shape when the application calculated XY 
    update dbo.tbl_locations 
    set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 
    WHERE (UPDATE(Shape)) 
       AND objectId IN (SELECT ins.objectId FROM inserted ins) 
    End 

試過這個問題,以及,還在循環:

ALTER trigger [dbo].[update_location_geometry] 
    --Trigger to allow non-gis input of XY data in X_Coord and Y_Coord columns, yet still populate the WKB 
    --shape column with coordinate using 26917 EPSG so that new location will be available to the gis without 
    -- additional user intervention 
    on [dbo].[TBL_LOCATIONS] 
    instead of insert as -- Aaron changed update to insert here 


IF UPDATE(X_Coord) OR UPDATE(Y_Coord) 

Begin 
update dbo.tbl_locations 
set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
CAST(Y_Coord AS VARCHAR(20)) + ')', 26917) 

END 
IF UPDATE(Shape) 
Begin 
update dbo.tbl_locations 
set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 

End 

並且這產生語法錯誤:

instead of update as 

IF update (X_Coord) OR UPDATE(Y_Coord) AND objectId IN (SELECT ins.objectId FROM inserted ins) 

Begin 
update dbo.tbl_locations 
set SHAPE=Geometry::STPointFromText('POINT(' + CAST(X_Coord AS VARCHAR(20)) + ' ' + 
CAST(Y_Coord AS VARCHAR(20)) + ')', 26917) 

END 
IF UPDATE(Shape)AND objectId IN (SELECT ins.objectId FROM inserted ins) 
Begin 
update dbo.tbl_locations 
set X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY 

End 


Msg 207, Level 16, State 1, Procedure update_location_geometry, Line 10 
Invalid column name 'objectId'. 
Msg 207, Level 16, State 1, Procedure update_location_geometry, Line 18 
Invalid column name 'objectId'. 

最終解決方案:

ALTER TRIGGER [dbo].[update_location_geometry] ON [dbo].[TBL_LOCATIONS] 
INSTEAD OF INSERT 
AS BEGIN  
IF EXISTS (SELECT 1 FROM inserted WHERE SHAPE IS NOT NULL)  
BEGIN   
INSERT dbo.TBL_Locations(SHAPE, X_Coord, Y_Coord,objectid, loc_name)    
SELECT SHAPE, X_Coord = SHAPE.STX,Y_Coord = SHAPE.STY,objectid, loc_name    
FROM inserted;  
END  
ELSE  
BEGIN   
INSERT dbo.TBL_Locations(SHAPE, X_Coord, Y_Coord,objectid,loc_name)    
SELECT SHAPE=Geometry::STPointFromText('POINT(' 
+ CAST(X_Coord AS VARCHAR(20)) + ' ' 
+ CAST(Y_Coord AS VARCHAR(20)) + ')', 26917), 
X_Coord, Y_Coord,objectid,loc_name 
FROM inserted; 
END 
END 

插入件的實施例:

INSERT INTO [ATBI].[dbo].[TBL_LOCATIONS] 
      ([OBJECTID] 
      ,[X_Coord] 
      ,[Y_Coord] 
      ,Loc_Name, 
      Notes) 

    VALUES 
      (368 
      ,324999.997 
      ,3970798.079 
      ,'blahblah' 
      ,'notes of test') 

生產表:

CREATE TABLE [dbo].[TBL_LOCATIONS](
    [OBJECTID] [int] NOT NULL, 
    [FCategory] [nvarchar](16) NULL, 
    [MapMethod] [nvarchar](4) NULL, 
    [HError] [nvarchar](50) NULL, 
    [MapSource] [nvarchar](255) NULL, 
    [SourceDate] [datetime2](7) NULL, 
    [EditDate] [datetime2](7) NULL, 
    [Notes] [nvarchar](255) NULL, 
    [Site_ID] [uniqueidentifier] NULL, 
    [Meta_MID] [nvarchar](50) NULL, 
    [X_Coord] [numeric](38, 8) NULL, 
    [Y_Coord] [numeric](38, 8) NULL, 
    [Coord_Units] [nvarchar](50) NULL, 
    [Coord_System] [nvarchar](50) NULL, 
    [UTM_Zone] [nvarchar](50) NULL, 
    [Accuracy_Notes] [nvarchar](max) NULL, 
    [Unit_Code] [nvarchar](12) NULL, 
    [Loc_Name] [nvarchar](100) NULL, 
    [Loc_Type] [nvarchar](25) NULL, 
    [Updated_Date] [nvarchar](50) NULL, 
    [Loc_Notes] [nvarchar](max) NULL, 
    [Datum] [nvarchar](5) NULL, 
    [Watershed] [nvarchar](50) NULL, 
    [StreamName] [nvarchar](50) NULL, 
    [NHDReachCode] [nvarchar](14) NULL, 
    [TOPO_NAME] [nvarchar](50) NULL, 
    [Trail] [nvarchar](100) NULL, 
    [Road] [nvarchar](50) NULL, 
    [Elevation] [numeric](38, 8) NULL, 
    [LAT] [numeric](38, 8) NULL, 
    [LON] [numeric](38, 8) NULL, 
    [Year_] [nvarchar](4) NULL, 
    [County] [nvarchar](30) NULL, 
    [State] [nvarchar](30) NULL, 
    [IsExtant] [nvarchar](3) NULL, 
    [IsSenstive] [nvarchar](3) NULL, 
    [Eco_Notes] [nvarchar](50) NULL, 
    [EcoGroup] [nvarchar](50) NULL, 
    [ELCode] [smallint] NULL, 
    [Validation] [nvarchar](50) NULL, 
    [LocationDescription] [nvarchar](max) NULL, 
    [LocationDirections] [nvarchar](max) NULL, 
    [VerbatimLocation] [nvarchar](255) NULL, 
    [PlaceName] [nvarchar](75) NULL, 
    [SHAPE] [geometry] NULL, 
    [Location_ID] [uniqueidentifier] NOT NULL, 
CONSTRAINT [PK_TBL_LOCATIONS] PRIMARY KEY CLUSTERED 
(
    [OBJECTID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[TBL_LOCATIONS] WITH CHECK ADD CONSTRAINT [g2_ck] CHECK (([SHAPE].[STSrid]=(26917))) 
GO 

ALTER TABLE [dbo].[TBL_LOCATIONS] CHECK CONSTRAINT [g2_ck] 
GO 

ALTER TABLE [dbo].[TBL_LOCATIONS] ADD CONSTRAINT [DF__TBL_LOCAT__Globa__12C8C788] DEFAULT (newsequentialid()) FOR [Location_ID] 
GO 
+1

第二次嘗試嘗試更新整個表;與插入的表完全沒有關係。除非你的表是空的(否則你會注意到整個表已經被更新),我會再次建議,也許你正在運行'UPDATE'語句,並且這個觸發器沒有觸發,因爲它是'INSTEAD OF INSERT'觸發器。 – 2012-02-11 01:31:49

回答

3

INSERT觸發器中,並檢查UPDATE有意義嗎?你的意思是使它成爲INSTEAD OF UPDATE觸發器而不是INSTEAD OF INSERT

而且,假設它應該是一個UPDATE觸發,我通常會檢查這個方法而不是在WHERE條款:

IF UPDATE(X_Coord) OR UPDATE(Y_Coord) 
BEGIN 
    -- first update without first where clause 
END 

IF UPDATE(Shape) 
BEGIN 
    -- second update without first where clause 
END 

好了,所以我被卡的事實,你嘗試使用替代插入觸發器的更新。問題是,代替觸發器,您需要自己繼續執行插入操作,因爲觸發器正在運行而不是插入。試試這個(注意,如果有其他欄目正在與SHAPEX_Coord/Y_Coord一起插入,你需要拉那些inserted以及):

CREATE TRIGGER [dbo].[update_location_geometry] 
ON [dbo].[TBL_LOCATIONS] 
INSTEAD OF INSERT 
AS 
BEGIN 
    INSERT dbo.TBL_Locations(ObjectID, SHAPE, X_Coord, Y_Coord) 
     SELECT ObjectID, SHAPE, X_Coord = SHAPE.STX, Y_Coord = SHAPE.STY 
     FROM inserted WHERE SHAPE IS NOT NULL; 

    INSERT dbo.TBL_Locations(ObjectID, SHAPE, X_Coord, Y_Coord) 
     SELECT ObjectID, SHAPE=Geometry::STPointFromText('POINT(' 
      + CAST(X_Coord AS VARCHAR(20)) + ' ' 
      + CAST(Y_Coord AS VARCHAR(20)) + ')', 26917), 
      X_Coord, Y_Coord 
     FROM inserted WHERE SHAPE IS NULL; 
END 
GO 

我試圖與以下INSERT的陳述,他們填充表格就好了:

INSERT dbo.TBL_Locations(ObjectID, X_Coord, Y_Coord) 
    SELECT 1, 20, 30; 

INSERT dbo.TBL_Locations(ObjectID, Shape) 
    SELECT 2, 0x25690000010C00000000000034400000000000003E30; 
+0

當我添加'AND objectId IN(SELECT ins.objectId FROM插入的插件)'時,我得到了錯誤行。我肯定在這裏缺少一些非常明顯的語法。一直盯着這幾個小時。在這種特殊情況下數據的來源總是插入。 – tpcolson 2012-02-11 01:36:06

+0

是的,你是正確的,如果其他列由用戶或插入應用程序填充,他們從觸發器中的缺席導致它失敗。在全球範圍內包括觸發器中的所有列是否會太雜亂,性能明智?用戶可以選擇在編輯時不填寫任何或所有其他文件,除了即將使用的內置觸發器,可以自動執行大量操作(例如,狀態是什麼?)感謝您的幫助! – tpcolson 2012-02-11 02:04:08

+0

不,如果您需要在插入前執行這些計算,那麼添加用戶可能指定的其他列(儘管我仍然不知道「失敗」的含義;通常會發布實際的錯誤消息以便我們說同樣的話)。如果他們可以選擇指定或不指定所需的列,那麼您將需要使用'COALESCE'來使用默認值或標記值填充它們(或者如果您希望它們能夠將這些列從「NULL」更改爲「NOT NULL」將它們留作'NULL')。儘管您可能會考慮使用AFTER觸發器來測試更新功能。 – 2012-02-11 02:06:39