-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_product.sql
67 lines (61 loc) · 1.3 KB
/
update_product.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
USE [ISYS4283];
GO
IF OBJECT_ID('dbo.update_product', 'p') IS NULL
EXEC('CREATE PROCEDURE update_product AS SELECT 1')
GO
ALTER PROCEDURE update_product
@product INT,
@name NVARCHAR(255) = NULL,
@price MONEY = NULL,
@description NVARCHAR(MAX) = NULL,
@image VARCHAR(255) = NULL
AS
SET NOCOUNT ON
-- validate product ID belongs to vendor
DECLARE @pid INT
SET @pid = (
SELECT id
FROM products
WHERE id = @product
AND vendor = SYSTEM_USER
)
IF @pid IS NULL
THROW 51000, 'The product does not exist.', 16;
IF @name IS NOT NULL
BEGIN TRY
UPDATE products SET name = @name
WHERE id = @product
END TRY
BEGIN CATCH
THROW;
END CATCH
IF @price IS NOT NULL
BEGIN TRY
UPDATE products SET price = @price
WHERE id = @product
END TRY
BEGIN CATCH
THROW;
END CATCH
IF @description IS NOT NULL
BEGIN TRY
UPDATE products SET description = @description
WHERE id = @product
END TRY
BEGIN CATCH
THROW;
END CATCH
IF @image IS NOT NULL
BEGIN TRY
UPDATE products SET image = @image
WHERE id = @product
END TRY
BEGIN CATCH
THROW;
END CATCH
-- audit
INSERT INTO products_audit (product, name, price, description, image, vendor)
VALUES (@product, @name, @price, @description, @image, SYSTEM_USER)
GO
GRANT EXECUTE ON update_product TO ISYS4283vendors;
GO