I want to create trigger which after insert longitude and latitude to table Vehicle create point from longitude latitude.
My trigger looks that
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
but when I insert to my table values:
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')
I have error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Can someone explain me why I have this error and I can't insert to my table this value my trigger only check is latitude and longitude is not null create a point.
You must query the Inserted
pseudo table to get the inserted rows inside an insert trigger. This table might return several rows. Instead of looping through the rows, you can update the location column like this
CREATE TRIGGER [dbo].[t_points]
ON [dbo].[Vehicle]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE v
SET
v.locationVehicle = geography::Point(i.latitude, i.longitude, 4326)
FROM
dbo.Vehicle v
INNER JOIN inserted i
ON v.idVehicle = i.idVehicle
WHERE
i.latitude IS NOT NULL AND
i.longitude IS NOT NULL
END
Assuming that idGroupVehicle
is the primary key of the table. If it is not, replace it by the primary key (every table should have a primary key).
UPDATE According to your comment I replaced idGroupVehicle
by idVehicle
in the join.