这里我有2个触发器,它们在插入或更新值后更新字段。我的问题是如何确保我只更新已插入或更新的行上的值,我的更新不包含任何WHERE子句
CREATE TRIGGER LocUpdated
ON [SqlDistance].[dbo].[DistantTest]
AFTER UPDATE
AS
IF ( UPDATE (Lat) OR UPDATE (Lon))
BEGIN
UPDATE [SqlDistance].[dbo].[DistantTest]
SET [Geography2] = geography::STGeomFromText('POINT(' + CAST([Lon] AS VARCHAR(20)) + ' ' +
CAST([lat] AS VARCHAR(20)) + ')', 4326)
END;
GO
CREATE TRIGGER LocInserted
ON [SqlDistance].[dbo].[DistantTest]
AFTER INSERT
AS
BEGIN
UPDATE [SqlDistance].[dbo].[DistantTest]
SET [Geography2] = geography::STGeomFromText('POINT(' + CAST([Lon] AS VARCHAR(20)) + ' ' +
CAST([lat] AS VARCHAR(20)) + ')', 4326)
END;
GO
答案 0 :(得分:1)
您想JOIN
inserted
UPDATE D
SET [Geography2] = geography::STGeomFromText('POINT(' + CAST(i.[Lon] AS VARCHAR(20)) + ' ' +
CAST(i.[lat] AS VARCHAR(20)) + ')', 4326)
FROM [dbo].[DistantTest] D --no need to declare the database, we're already in it.
JOIN inserted i ON D.UniqueIdColumn = i.UniqueIdColumn;
。这是伪sql,在没有表DDL的情况下,这应该可以让你走上正轨:
{{1}}