On SQL Server triggers

Source: Internet
Author: User

Following the last SQL Server spatial task,

The author also received a new task:

When the Database property field changes, the spatial data is automatically updated without changing the business code.

The first solution that comes to mind is the trigger.

Basic ideas:

After the data has been updated or inserted, the spatial field data is updated when the value of any one of the X, Y fields of the table changes.


The following is an example of camera table Videopointinfo

varchar
Field
type
videoid
varchar
primary key ID
videoname
camera name
clientx
number

Longitude

clienty
Number
latitude
geom
geometry
actual coordinates
--  add a coordinate field to the table alter table videopointinfo add geom geometry;--  create or modify a trigger,  Created with Create, modified with Alteralter trigger tri_videopointinfo on videopointinfoafter update, insert as--  call If update (clientx)  or update (clienty) when Clientx or clienty has an update begin     --  set Geom to null when either Clientx or clienty has a null value     if  (select clientx  from inserted)  is null or  (select clienty from inserted)  is  null    begin        update v set      geom=null    from videopointinfo v     where exists (Select 1 from inserted where videoflag=v.videoflag)         Return    end         update&nBsp;v set     geom=geometry::stgeomfromtext (' point ' (' +convert (Varchar,CLIENTX) + ')   ' +convert (varchar,clienty) + ') ', 4326     from videopointinfo v     where exists (Select 1 from inserted where videoflag=v.videoflag) End

Finally, you can try to update a few data to test the effect.

Another way to open and disable a trigger statement

Disable trigger Trigdb on database--disable trigger enable trigger TRIGDB on database--open trigger

Reference Document: Msdn-create Trigger

On SQL Server triggers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.