-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBikeStores - StoredProcedures.sql
80 lines (76 loc) · 1.56 KB
/
BikeStores - StoredProcedures.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
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
These stored procedures are simple created without error checking.
*/
CREATE OR ALTER PROCEDURE CSP_AddBikeBrand
@BrandName VARCHAR(255)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO production.brands (brand_name) SELECT @BrandName;
SELECT SCOPE_IDENTITY();
END
GO
--GetBikeBrandID
CREATE OR ALTER PROCEDURE CSP_GetBikeBrandID
@BrandName VARCHAR(255),
@ID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT
@id=BRAND_ID
FROM production.brands
WHERE brand_name = @BrandName;
SELECT @ID
END
GO
-- getstoreID
CREATE OR ALTER PROCEDURE CSP_GetStoreID
@storeName VARCHAR(255),
@ID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT
@ID=store_ID
FROM sales.stores
WHERE store_name = @storeName;
SELECT @ID
END
-- Add Products
CREATE OR ALTER PROCEDURE CSP_NewProduct
@ProductName VARCHAR(255),
@brandID INT,
@CatID INT,
@ModelYear SMALLINT,
@listPrice Decimal(10,2)
AS
BEGIN
INSERT INTO [production].[products]
([product_name]
,[brand_id]
,[category_id]
,[model_year]
,[list_price])
SELECT
@ProductName
,@brandid
,@catid
,@modelyear
,@listPrice;
SELECT SCOPE_IDENTITY();
END
GO
-- Add Stock
CREATE OR ALTER PROCEDURE CSP_AddStock
@StoreID AS INT
, @ProductID as INT
, @QT as INT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO production.stocks(store_id, product_id, quantity) SELECT @StoreID, SUM(@ProductID), sum(@Qt);
--INSERT INTO production.stocks(store_id, product_id, quantity) SELECT @StoreID, SUM(500+@ProductID), sum(2*@Qt);
SELECT SCOPE_IDENTITY();
END
GO