-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery2.sql
35 lines (26 loc) · 919 Bytes
/
SQLQuery2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--use C21_DB1;
--CREATE TABLE Products (
-- ProductID INT PRIMARY KEY,
-- StockQuantity INT
--);
--Go
--INSERT INTO Products (ProductID, StockQuantity) VALUES (1, 100);
--INSERT INTO Products (ProductID, StockQuantity) VALUES (2, 50);
--INSERT INTO Products (ProductID, StockQuantity) VALUES (3, 75);
use C21_DB1;
declare @NewStockQty INT;
set @NewStockQty=-5;
-- Start a TRY block
BEGIN TRY
-- Check if NewStockQty is negative
IF @NewStockQty < 0
THROW 51000, 'Stock quantity cannot be negative.', 1;
-- Proceed with updating stock (example code)
UPDATE Products SET StockQuantity = @NewStockQty WHERE ProductID = 1;
END TRY
-- Start a CATCH block to handle the error
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH