如何触发检查纬度和经度,如果纬度和经度正确我们可以插入数据库?
How to do trigger which check the latitude and longitude and if latitude and longitude is correct we can insert to database?
我想创建触发器,它将在插入数据库之前检查经度和纬度,如果经度不正确,触发器打印一条消息“错误的经度,经度必须在
间隔所以我的触发器是
create trigger t_point_betweenU2
on UserApp
INSTEAD OF insert
as
begin
SET NOCOUNT ON;
declare @latitude as float
declare @longitude as float
declare @Message Varchar(100)
set @latitude = (select u.latitude from UserApp u)
set @longitude = (select u.longitude from UserApp u)
Select @latitude = i.latitude, @longitude = i.longitude
from inserted i
if((@latitude >= 19.7922354 and @latitude <= 20.2173455))
begin
Set @Message = 'This latitude is incorrect, latitude must be between 19.7922354 and 20.2173455'
RaisError(@Message, 16, 1)
RollBack
end
else if((@longitude>= 49.9676667 and @longitude <= 50.1261338))
begin
Set @Message = 'This longitude is incorrect , longitude must be between 49.9676667 and 50.1261338'
RaisError(@Message, 16, 1)
RollBack
end
end
GO
但是当我有这个触发器时,我无法插入到我的数据库中
有人可以解释为什么我有 thistrigger 时无法插入数据库吗?
正如我提到的,根本不要使用 TRIGGER
,只需使用几个 CONSTRAINT
:
ALTER TABLE dbo.UserApp ADD CONSTRAINT chk_latitude CHECK (latitude >= 19.7922354 and latitude <= 20.2173455);
ALTER TABLE dbo.UserApp ADD CONSTRAINT chk_longitude CHECK (longitude >= 49.9676667 and longitude <= 50.1261338);
我想创建触发器,它将在插入数据库之前检查经度和纬度,如果经度不正确,触发器打印一条消息“错误的经度,经度必须在 间隔所以我的触发器是
create trigger t_point_betweenU2
on UserApp
INSTEAD OF insert
as
begin
SET NOCOUNT ON;
declare @latitude as float
declare @longitude as float
declare @Message Varchar(100)
set @latitude = (select u.latitude from UserApp u)
set @longitude = (select u.longitude from UserApp u)
Select @latitude = i.latitude, @longitude = i.longitude
from inserted i
if((@latitude >= 19.7922354 and @latitude <= 20.2173455))
begin
Set @Message = 'This latitude is incorrect, latitude must be between 19.7922354 and 20.2173455'
RaisError(@Message, 16, 1)
RollBack
end
else if((@longitude>= 49.9676667 and @longitude <= 50.1261338))
begin
Set @Message = 'This longitude is incorrect , longitude must be between 49.9676667 and 50.1261338'
RaisError(@Message, 16, 1)
RollBack
end
end
GO
但是当我有这个触发器时,我无法插入到我的数据库中 有人可以解释为什么我有 thistrigger 时无法插入数据库吗?
正如我提到的,根本不要使用 TRIGGER
,只需使用几个 CONSTRAINT
:
ALTER TABLE dbo.UserApp ADD CONSTRAINT chk_latitude CHECK (latitude >= 19.7922354 and latitude <= 20.2173455);
ALTER TABLE dbo.UserApp ADD CONSTRAINT chk_longitude CHECK (longitude >= 49.9676667 and longitude <= 50.1261338);