Triggers for table synchronization update issues

Source: Internet
Author: User
Triggers | issues | Triggers 1SQL Server 2000 triggers, table sync Update issues
2 has three tables, a, b,c
3 A, B table contains: A1,b1,c1 three fields,
The 4C table holds the collection of A1, B1, C1 in Table A, B,
5 field types are nvarchar (10),
6 when table A's data is updated, deleted, inserted and reflected to the C table.
7 when table B's data is updated, deleted, and inserted, it is reflected to the C table.
8 assumes a unique index on the A1,B1,C1 in the A,b table
9
10
11 This problem, if it is purely theoretical, is easy to solve, because from the requirements, it is essentially the data stored in the C table is the set of a and B tables. Can be in a, B table on the same trigger, once a, B table changes, such as inserting, delete or update, that is, empty the C table data, and then the A, B table data union inserted into the C table can achieve the purpose: Oh ah ...
12
13 The following trigger principles are:
14
15 When a table inserts data, check whether there is a table in C table will be inserted data, if not, then insert this row of data into the C table, otherwise, do not need to operate.
16
17 When a table update, check whether there are updates in B table before this row of data, if there is, then the C table should retain this row of data and the a table in the updated data also inserted into the C table. If this row of data is not in table B before the update of table A, and there is no table in table C that is updated with this row of data, you will need to update the data in table A with the same data as in table A, if you do not have this row of data updated in table A, and there is this row of data in Table C, which is updated by table A, Then you need to remove the same row of data from table C as before update a table (since table A and table B do not have the same row of data as table A before update), this row of data is clearly not supposed to exist in table C.
18
19 when deleted in table A, check that table B still exists for this row of data to be deleted in table A, and if so, you cannot delete the same row in the C table as the data you want to delete in table A. Conversely, the delete operation is performed.
20
21st
The trigger in table B is the same as the principle in table A.
23
24
25CREATE TRIGGER sync_c_by_a
26ON A
27AFTER Insert,update,delete
28AS
29Declare @Dml TinyInt--1:insert 2:update 3:delete
30Declare @RowsD Int
31Declare @RowsI Int
32Declare @A1_D nvarchar (10)
33Declare @B1_D Nvarchar (10)
34Declare @C1_D Nvarchar (10)
35--determine what kind of DML operation &NB



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.