Update data through views

Source: Internet
Author: User
-Example-Test Data Create   Table Table 1 (ID Int   Identity (1, 1), Varchar (10), B Varchar (10), c Varchar (10), d Varchar (10 ), Constraint Idx _ TABLE ‑ Unique (A) Insert table 1 Select   'A' , 'B' , 'C' , 'D'  Union   All   Select   'A2' , 'B' , 'C' , 'D2'  Union    All   Select   'A3' , 'C' , 'B' , 'A'  Create   Table Table 2 (ID Int   Identity (1, 1), E Varchar (10),Varchar (10), F Varchar (10), g Varchar (10 ), Constraint Idx _ TABLE 2_a Unique (A) Insert table 2 Select   'M' , 'A' , 'W' , 'Q'  Union    All   Select   'X' , 'A2' , 'P' , 'K' Union    All   Select   'U' , 'A3' , 'I' , 'R'  Go -- View Create   View View name As  Select A. *, B. G From Table 1 Join Table 2 B On A. A = B. Go -- Processed trigger Create   Trigger Tr_process On View name instead Of Insert, Update , Delete  As  If   Exists ( Select 1 From Inserted) If   Exists ( Select 1 From Deleted) Begin  Select Id = Identity (Int , 1, 1), a, B, c, d, g Into # I From Inserted Select Id = Identity ( Int , 1, 1), Into # D From Deleted Update Table 1 Set A = I _a, B = I _ B, c = I _c, D = I _d From Table 1 Join ( Select I _a = I. A, I _ B = I. B, I _c = I. C, I _d = I. D, d_a = d. From # IJoin # D On I. ID = D. id) B On A. A = d_a Update Table 2 Set A = I _a, G = I _g From Table 2 Join ( Select I _a = I. A, I _g = I. G, d_a = d. From # I Join # D On I. ID = D. id) B On A. A = d_a End  Else  Begin Insert table 1 (a, B, c, d)Select A, B, c, d From Insertedinsert table 2 (A, G) Select A, G From Inserted End  Else  Begin  Delete Table 1 From Table 1 Join Deleted d On A. A = d. Delete Table 2 From Table 2 Join Deleted d On A. A = d. End Go -- Display view Select * From View name Go -- Test the trigger Update View name Set B = B + 'Bb' , G = G + 'Gg' Insert View name Select 1, 'A' , 'B' , 'C' , 'D' , 'G'  Delete View name Where A ='A2'  Go -- Display the processing result Select * From View name Go -- Delete test Drop   View View name Drop   Table Table 1, Table 2/* -- test result -- View effect ID a B c d G --------- ---------------------------------------------------- 1 a B c d Q2 A2 B c D2 K3 A3 c B a r (3 rows affected) -- trigger processing result ID a B c d G ------------------------------------------------------------- 1 a bbb c d qgg3 A3 cbb B a rgg4 AA B c d G (3 rows affected )--*/

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.