Cascade Update Triggers : Trigger for update « Trigger « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Trigger » Trigger for update 
22. 6. 3. Cascade Update Triggers
3>
4CREATE TABLE stores(
5>    stor_id        char(4)           NOT NULL,
6>    stor_name      varchar(40)           NULL,
7>    stor_address   varchar(40)           NULL,
8>    city           varchar(20)           NULL,
9>    state          char(2)               NULL,
10>    zip            char(5)               NULL
11)
12> GO
1insert stores values('1','B','56Ave.','Tustin',   'CA','92789')
2insert stores values('2','N','57St.', 'Los Gatos','CA','96745')
3insert stores values('3','T','67St.', 'Portland', 'OR','89076')
4insert stores values('4','F','89  St.', 'Fremont',  'CA','90019')
5> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3CREATE TABLE discounts(
4>    discounttype   varchar(40)       NOT NULL,
5>    stor_id        char(4NULL              ,
6>    lowqty         smallint              NULL,
7>    highqty        smallint              NULL,
8>    discount       dec(4,2)          NOT NULL
9)
10> GO
1>
2insert discounts values('Initial Customer',  NULL,   NULL, NULL, 10.5)
3insert discounts values('Volume Discount',   NULL,   100,  10006.7)
4insert discounts values('Customer Discount', '8042', NULL, NULL, 5.0)
5> GO

(rows affected)

(rows affected)

(rows affected)
1>
2>
3>     CREATE TRIGGER myTrigger ON stores
4>     FOR UPDATE
5>     AS
6>     DECLARE @intRowCount int
7>     SELECT @intRowCount = @@RowCount
8>     IF @intRowCount > 1
9>         BEGIN
10>             IF UPDATE(stor_id)
11>                 ROLLBACK TRANSACTION
12>         END
13>     ELSE
14>         IF @intRowCount = 1
15>             BEGIN
16>                 IF UPDATE(stor_id)
17>                     BEGIN
18>                         UPDATE sales
19>                             SET sales.stor_id = (SELECT stor_id FROM inserted)
20>                             FROM sales INNER JOIN deleted
21>                             ON sales.stor_id = deleted.stor_id
22>                         UPDATE discounts
23>                             SET discounts.stor_id = (SELECT stor_id FROM inserted)
24>                             FROM discounts INNER JOIN deleted
25>                             ON discounts.stor_id = deleted.stor_id
26>                     END
27>         END
28>     GO
1>
2> drop TRIGGER myTrigger;
3> drop table sales;
4> drop table discounts;
5> GO
22. 6. Trigger for update
22. 6. 1. create TRIGGER for update
22. 6. 2. The trigger rolls back any T-SQL that changes the advance column.
22. 6. 3. Cascade Update Triggers
22. 6. 4. CREATE TRIGGER FOR INSERT, UPDATE
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.