1 SQL server 2000 trigger, table synchronization update problem
2. There are three tables, A, B, and C.
Table 3A and Table B contain three fields: A1, B1, and C1,
Set A1, B1, and C1 in Table A and Table B in table 4C,
5. All fields are nvarchar (10 ),
6. Data in Table A should be updated, deleted, and inserted to Table C.
7. Data in Table B should be updated, deleted, and inserted to Table C.
8 assume that there is A unique index on a1, b1, and c1 in Table A and Table B.
9
10
11 This problem can be easily solved theoretically, because we can see from the requirements that the data stored in Table C is essentially the union of tables A and B. You can create the same trigger on Table A and Table B. Once Table A and Table B change, such as insert, delete, or update, the data in Table C can be cleared, then insert the union data in Table A and Table B into table C to achieve the goal: haha...
12
13 the following trigger implementation principle is:
14
15 when table A inserts data, check whether Table C contains the data to be inserted in Table A. If not, insert this row into table C. Otherwise, no operation is required.
16
17 When Table A is updated, check whether the row of data before update exists in Table B. If yes, this row of data should be retained in Table C and the updated data in Table A should be inserted into table C. If Table B does not have this row of data before Table A's update and Table C does not have this row of data after table A's update, you need to use the updated data of Table A to update the data of the same row in Table C as that of Table A before the update; if Table B does not have this row of data updated in Table A and Table C has this row of data updated in Table, you need to delete the row data that is the same as that of Table A before update from Table C (because after table A is updated, neither table A nor Table B has the row data before Table A is updated, this row of data should not exist in Table C ).
18
19 when Table A is deleted, check whether Table B still has this row of data to be deleted. If yes, the same row of data in Table C and table A cannot be deleted. Otherwise, delete the instance.
20
21
22. The trigger principle in Table B is the same as that in Table.
23
24
25 create trigger SYNC_C_BY_A
26ON
27 after insert, UPDATE, DELETE
28AS
29 Declare @ Dml TinyInt --
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.