Transactions and Error Handling in SQL Server
Miriam Antona
Software Engineer
INSERT INTO products (product_name, stock, price)
VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'unique_name'.
Cannot insert duplicate key in object 'dbo.products'.
The duplicate key value is (Trek Powerfly 5 - 2018).
หมายเลขข้อผิดพลาด

select * from sys.messages
| message_id | language_id | severity | ... | text |
|------------|-------------|----------|-----|-----------------------------------------------------------------|
| ... | ... | ... | ... | ... |
| 2627 | 1033 | 14 | ... | Violation of %ls constraint '%.*ls'. Cannot insert duplicate... |
| ... | ... | ... | ... | ... |
ระดับความรุนแรง

สถานะ

บรรทัด

โพรซีเดอร์

ระดับความรุนแรงต่ำกว่า 11 (11-19 ดักจับได้)
ระดับความรุนแรง 20 ขึ้นไปที่ตัดการเชื่อมต่อ
ข้อผิดพลาดการคอมไพล์: อ็อบเจกต์หรือคอลัมน์ที่ไม่มีอยู่
BEGIN TRY
SELECT non_existent_column FROM products;
END TRY
BEGIN CATCH
SELECT 'You are in the CATCH block' AS message;
END CATCH
Msg 207, Level 16, State 1, Line 2
Invalid column name 'non_existent_column'.
Transactions and Error Handling in SQL Server