我有一個應用程序,其中兩列可以從多個不同的應用程序填充;一,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
第二次嘗試嘗試更新整個表;與插入的表完全沒有關係。除非你的表是空的(否則你會注意到整個表已經被更新),我會再次建議,也許你正在運行'UPDATE'語句,並且這個觸發器沒有觸發,因爲它是'INSTEAD OF INSERT'觸發器。 – 2012-02-11 01:31:49