【发布时间】:2021-08-06 01:53:38
【问题描述】:
我想创建触发器,在将经度和纬度插入表车辆后,从经度纬度创建点。
我的触发器看起来是这样的
create trigger [dbo].[t_points]
on [dbo].[Vehicle]
after insert
as
begin
SET NOCOUNT ON;
DECLARE @Point AS GEOGRAPHY
DECLARE @Longitude AS FLOAT
DECLARE @Latitude AS FLOAT
set @latitude = (select v.latitude from Vehicle v)
set @longitude = (select v.longitude from Vehicle v)
if @latitude is not null and @Longitude is not null
begin
set @Point = geography::Point(@latitude,@longitude,4326)
update Vehicle set locationVehicle = @Point
end
end
GO
但是当我插入我的表值时:
insert into Vehicle
(idGroupVehicle, brand, model, maxRange, weight, maxSpeed, pricePerSale,
latitude, longitude, locationName)
values ('1', 'Xiaomi', 'Mijia M365 PRO', '45', '14', '1900', '25',
'19.905203', '50.071597', 'Piastowska 49 Kraków')
我有错误
子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。
谁能解释一下为什么我有这个错误,我不能在我的表中插入这个值我的触发器只检查纬度和经度不为空创建一个点。
【问题讨论】:
-
为什么触发?只需创建一个计算列...最好使用persist。
-
为什么你的触发器没有引用伪表
inserted?另外,您的表Vehicle真的只有一行吗? -
我认为您还没有掌握触发器的概念。您正在从
Vehicle的每一行中选择所有值,并实质上为每个@variable分配一个随机值;然后,您将使用@point的值更新表中的所有行。LocationVehicle在这种情况下可能是一个计算列,无需尝试在触发器中重现基本相同的内容。
标签: sql-server tsql triggers geospatial spatial