count for DELETED : Deleted table « 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 » Deleted table 
22. 14. 2. count for DELETED
2CREATE TABLE Orders (
3>      OrderID int IDENTITY (11NOT NULL ,
4>      CustomerID nchar (5NULL ,
5>      EmployeeID int NULL ,
6>      OrderDate datetime NULL ,
7>      RequiredDate datetime NULL ,
8>      ShippedDate datetime NULL ,
9>      ShipVia int NULL ,
10>     Freight money NULL DEFAULT (0),
11>     ShipName nvarchar (40NULL ,
12>     ShipAddress nvarchar (60NULL ,
13>     ShipCity nvarchar (15NULL ,
14>     ShipRegion nvarchar (15NULL ,
15>     ShipPostalCode nvarchar (10NULL ,
16>     ShipCountry nvarchar (15NULL
17)
18> GO
1>    CREATE TRIGGER Order2DependsOnOrders
2>       ON Orders
3>       FOR INSERT, UPDATE, DELETE
4>    AS
5>       DECLARE @Count int
6>       SELECT @Count = COUNT(*FROM DELETED
7>       IF @Count > 0
8>       BEGIN
9>          IF NOT EXISTS
10>             (
11>              SELECT 'True'
12>              FROM Deleted d
13>              LEFT JOIN Orders o
14>                 ON d.OrderID = o.OrderID
15>              WHERE o.OrderID IS NULL
16>             )
17>          BEGIN
18>             RAISERROR('Record Exists In Orders Table. Delete Cancelled.',16,1)
19>             ROLLBACK TRAN
20>          END
21>       END
22>       IF @@ERROR != 0
23>          ROLLBACK TRAN
24>       SELECT @Count = COUNT(*FROM INSERTED
25>       SELECT 'Count is ' + CONVERT(varchar,@Count' Before Delete'
26>       IF @Count > 0
27>       BEGIN
28>          IF EXISTS
29>             (
30>              SELECT 'True'
31>              FROM Inserted i
32>              LEFT JOIN Orders o
33>                 ON i.OrderID = o.OrderID
34>              WHERE o.OrderID IS NULL
35>             )
36>          BEGIN
37>             RAISERROR('Inserted Record Must exist in the Orders Table',16,1)
38>             ROLLBACK TRAN
39>          END
40>       END
41>       IF @@ERROR != 0
42>          ROLLBACK TRAN
43>       GO
1>       drop TRIGGER Order2DependsOnOrders;
2>       GO
1>       drop table Orders;
2>       GO
22. 14. Deleted table
22. 14. 1. Trigger for delete using deleted table
22. 14. 2. count for DELETED
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.