Trap Both Primary Key and CHECK Constraint Violations : Constriant violation « Constraints « 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 » Constraints » Constriant violation 
7. 12. 1. Trap Both Primary Key and CHECK Constraint Violations
8CREATE TABLE T1(
9>   pk_col    int NOT NULL PRIMARY KEY CHECK (pk_col > 0),
10>   ident_col int NOT NULL IDENTITY (1,1)
11)
12>
13> DECLARE
14>   @myerror    AS int
15INSERT INTO T1 VALUES(0-- violate the check constraint
16> SET @myerror = @@ERROR
17> IF @myerror = 2627
18>   PRINT 'PRIMARY KEY constraint violation'
19> ELSE IF @myerror = 547
20>   PRINT 'CHECK constraint violation'
21> GO
Msg 547, Level 16, State 1, Server J\SQLEXPRESS, Line 15
The INSERT statement conflicted with the CHECK constraint "CK__T1__pk_col__39788055". The conflict occurred in database "master", table "dbo.T1", column 'pk_col'.
The statement has been terminated.
CHECK constraint violation
1>
2> --Attempt to Capture @@IDENTITY, @@ROWCOUNT, and @@ERROR
3> DECLARE
4>   @myerror    AS int,
5>   @myrowcount AS int,
6>   @myidentity AS int
7INSERT INTO T1 VALUES(10-- PK violation
8SELECT @myidentity = @@IDENTITY,
9>        @myrowcount = @@ROWCOUNT,
10>        @myerror    = @@ERROR
11> PRINT '@myidentity: ' + CAST(@myidentity AS varchar)
12> PRINT '@myrowcount: ' + CAST(@myrowcount AS varchar)
13> PRINT '@myerror   : ' + CAST(@myerror AS varchar)
14> GO

(rows affected)

(rows affected)
@myidentity: 2

(rows affected)
@myrowcount: 1

(rows affected)
@myerror   : 0
1> --Output of Successful Attempt to Capture @@IDENTITY, @@ROWCOUNT, and @@ERROR
2>
3> drop table T1;
4> GO
7. 12. Constriant violation
7. 12. 1. Trap Both Primary Key and CHECK Constraint Violations
7. 12. 2. Exception in a transaction
7. 12. 3. A NOT NULL phrase adds a constraint to restrict the input of rows with a null value.
7. 12. 4. Update statement and check-constraints
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.