SET XACT_ABORT ON
3> IF EXISTS (SELECT * FROM sysobjects WHERE name='show_error'
4> AND type='U')
5> DROP TABLE show_error
6> GO
1>
2> CREATE TABLE show_error
3> (
4> col1 smallint NOT NULL PRIMARY KEY,
5> col2 smallint NOT NULL
6> )
7> GO
1>
2> SET XACT_ABORT ON
3> BEGIN TRANSACTION
4>
5> INSERT show_error VALUES (1, 1)
6> INSERT show_error VALUES (1, 2)
7> INSERT show_error VALUES (2, 2)
8>
9> COMMIT TRANSACTION
10> GO
(1 rows affected)
Msg 2627, Level 14, State 1, Server J\SQLEXPRESS, Line 6
Violation of PRIMARY KEY constraint 'PK__show_error__78159CA3'. Cannot insert duplicate key in object 'dbo.show_error'.
1>
2> SELECT * FROM show_error
3> GO
col1 col2
------ ------
(0 rows affected)
1>
2> drop table show_error;
3> GO
Related examples in the same category