Trigger (when the corresponding values in two tables change, one field in one table is changed at the same time)

Source: Internet
Author: User
Create trigger GetAllPointOnUserInfoChange
On UserInfo
For update, insert
As


Declare @ point int, @ point_bank int, @ point_mno int, @ point_present int, @ point_BabaoVoteYY int, @ userid nvarchar

Select @ point = inserted. point, @ point_bank = inserted. point_bank, @ point_mno = inserted. point_mno, @ point_present = inserted. point_present, @ userid = inserted. userid, @ point_BabaoVoteYY = BabaoVoteYY. point from inserted left join BabaoVoteYY on inserted. userid collate Chinese_PRC_CI_AS = BabaoVoteYY. userid collate Chinese_PRC_CI_AS

If @ point_BabaoVoteYY is null
Set @ point_BabaoVoteYY = 0

Update UserInfo set allpoint = @ point + @ point_bank + @ point_mno + @ point_present + @ point_BabaoVoteYY where userid = @ userid

**************************************** **************************************** **************
Create trigger GetAllPointOnBabaoVoteYYChange
On BabaoVoteYY
For update, insert
As

Declare @ point_UserInfo int, @ point_bank_UserInfo int, @ point_mno_UserInfo int, @ point_present_UserInfo int, @ point int, @ userid nvarchar

Select @ point_UserInfo = UserInfo. point, @ point_bank_UserInfo = UserInfo. point_bank, @ point_mno_UserInfo = UserInfo. point_mno, @ point_present_UserInfo = UserInfo. point_present, @ userid = inserted. userid, @ point = inserted. point from inserted, UserInfo where inserted. userid collate Chinese_PRC_CI_AS = UserInfo. userid collate Chinese_PRC_CI_AS

Update UserInfo set allpoint = @ point_UserInfo + @ point_bank_UserInfo + @ point_mno_UserInfo + @ point_present_UserInfo + @ point where userid = @ userid

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.