Initial commit

This commit is contained in:
user
2026-07-12 13:56:05 +04:00
commit 04bf3a4b58
850 changed files with 9057 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\functions\function-does_barcode_exist.sql ^
.\functions\function-does_product_exist_with_name.sql ^
.\functions\function-does_product_exist_with_product_type_id.sql ^
.\functions\function-does_product_type_exist_with_name.sql ^
.\functions\function-subgroup_products_join_product_types.sql ^
.\functions\function-same_products_join_product_types.sql ^
.\functions\function-product_type_id.sql ^
-r1 1> NUL
+15
View File
@@ -0,0 +1,15 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\procedures\specific-procedures\procedure-products_join_product_types.sql ^
.\procedures\specific-procedures\procedure-rename_product.sql ^
.\procedures\specific-procedures\procedure-rename_product_type.sql ^
.\procedures\specific-procedures\procedure-specify_product_type.sql ^
.\procedures\specific-procedures\procedure-update_barcode.sql ^
.\procedures\standard-procedures\removal-procedures\procedures-delete_product.sql ^
.\procedures\standard-procedures\removal-procedures\procedure-delete_product_type.sql ^
.\procedures\standard-procedures\insertion-procedures\procedure-insert_product.sql ^
.\procedures\standard-procedures\insertion-procedures\procedure-insert_product_type.sql ^
.\procedures\standard-procedures\insertion-procedures\procedure-insert_product_attribute.sql ^
.\procedures\standard-procedures\insertion-procedures\procedure-insert_product_value.sql ^
-r1 1> NUL
+6
View File
@@ -0,0 +1,6 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\triggers\ddl-trigger-trg_drop_table.sql ^
.\triggers\dml-trigger-trg_insert_product_types.sql ^
-r1 1> NUL
+5
View File
@@ -0,0 +1,5 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\users\create-a-user-user.sql ^
-r1 1> NUL
+5
View File
@@ -0,0 +1,5 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\deleting\delete-tables.sql ^
-r1 1> NUL
+26
View File
@@ -0,0 +1,26 @@
USE [Database]
IF OBJECT_ID('[Tables].[product_write_off_records]', 'U') IS NOT NULL
DELETE [Tables].[product_write_off_records];
IF OBJECT_ID('[Tables].[additional_variables]', 'U') IS NOT NULL
DELETE [Tables].[additional_variables];
IF OBJECT_ID('[Tables].[price_history_records]', 'U') IS NOT NULL
DELETE [Tables].[price_history_records];
IF OBJECT_ID('[Tables].[product_outflow_records]', 'U') IS NOT NULL
DELETE [Tables].[product_outflow_records];
IF OBJECT_ID('[Tables].[product_inflow_records]', 'U') IS NOT NULL
DELETE [Tables].[product_inflow_records];
IF OBJECT_ID('[Tables].[write_off_reasons]', 'U') IS NOT NULL
DELETE [Tables].[write_off_reasons];
IF OBJECT_ID('[Tables].[product_values]', 'U') IS NOT NULL
DELETE [Tables].[product_values];
IF OBJECT_ID('[Tables].[product_attributes]', 'U') IS NOT NULL
DELETE [Tables].[product_attributes];
IF OBJECT_ID('[Tables].[attributes]', 'U') IS NOT NULL
DELETE [Tables].[attributes];
IF OBJECT_ID('[Tables].[suppliers]', 'U') IS NOT NULL
DELETE [Tables].[suppliers];
IF OBJECT_ID('[Tables].[products]', 'U') IS NOT NULL
DELETE [Tables].[products];
IF OBJECT_ID('[Tables].[product_types]', 'U') IS NOT NULL
DELETE [Tables].[product_types];
+5
View File
@@ -0,0 +1,5 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\drop-create\drop-create-Database.sql ^
-r1 1> NUL
@@ -0,0 +1,195 @@
USE [master];
IF EXISTS (SELECT 1 FROM sys.databases WHERE [name] = N'Database')
BEGIN
EXECUTE sp_executesql
@stmt =
N'USE [Database];
ALTER DATABASE [Database]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
USE [tempdb];
DROP DATABASE [Database];'
END;
GO
CREATE DATABASE [Database];
GO
USE [Database];
GO
CREATE SCHEMA [Tables] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Views] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Functions] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Procedures] AUTHORIZATION [dbo];
GO
CREATE TABLE [Tables].[product_types](
[id] INT NOT NULL IDENTITY(1, 1),
[parent_id] INT DEFAULT(NULL),
[name] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_product_types
PRIMARY KEY([id]),
CONSTRAINT FK_product_types FOREIGN KEY([parent_id])
REFERENCES [Tables].[product_types]([id])
);
CREATE TABLE [Tables].[products](
[id] INT NOT NULL IDENTITY(1, 1),
[barcode] BIGINT NOT NULL UNIQUE,
[name] NVARCHAR(128) NOT NULL UNIQUE,
[product_type_id] INT NOT NULL,
CONSTRAINT PK_products
PRIMARY KEY([id]),
CONSTRAINT CHECK_products
CHECK([barcode] > -1 AND [barcode] < 10000000000000),
CONSTRAINT FK_product_type_id FOREIGN KEY([product_type_id])
REFERENCES [Tables].[product_types]([id])
);
CREATE TABLE [Tables].[suppliers](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
[address] NVARCHAR(128) NOT NULL,
[telephone_number] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_suppliers
PRIMARY KEY([id])
);
--CREATE TABLE [Tables].[additional_variables](
-- [id] INT NOT NULL IDENTITY(1, 1),
-- [name] NVARCHAR(128) NOT NULL UNIQUE,
-- [value] NVARCHAR(128) NOT NULL
-- CONSTRAINT PK_additionalVariables
-- PRIMARY KEY([id])
--);
CREATE TABLE [Tables].[attributes](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
CONSTRAINT PK_attributes
PRIMARY KEY([id])
);
CREATE TABLE [Tables].[product_attributes](
[product_id] INT NOT NULL,
[attribute_id] INT NOT NULL,
[description] NVARCHAR(128)
CONSTRAINT PK_product_attributes
PRIMARY KEY([product_id], [attribute_id]),
CONSTRAINT FK_product_attributes_product_id FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id]),
CONSTRAINT FK_product_attributes_attribute_id FOREIGN KEY([attribute_id])
REFERENCES [Tables].[attributes]([id])
);
CREATE TABLE [Tables].[product_values](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[attribute_id] INT NOT NULL,
[value] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_product_values
PRIMARY KEY([id]),
CONSTRAINT FK_product_values FOREIGN KEY([product_id], [attribute_id])
REFERENCES [Tables].[product_attributes]([product_id], [attribute_id])
);
CREATE TABLE [Tables].[price_history_records](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME()),
[price] MONEY NOT NULL,
CONSTRAINT PK_price_history_records
PRIMARY KEY([id]),
CONSTRAINT FK_price_history_records_product_id FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id]),
CONSTRAINT CHECK_price_history_records
CHECK([price] >= 0)
);
-- CREATE TABLE [Tables].[barcode_history]...
CREATE TABLE [Tables].[product_outflow_records](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL, -- without reference (temp)
[barcode] BIGINT NOT NULL,
[count] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_outflow_records
PRIMARY KEY([id]),
CONSTRAINT CHECK_product_outflow_records
CHECK([count] > 0),
CONSTRAINT FK_product_outflow_records FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id])
);
CREATE TABLE [Tables].[product_inflow_records](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[barcode] BIGINT NOT NULL, -- without reference (temp)
[supplier_id] INT NOT NULL,
[count] INT NOT NULL,
[full_price] MONEY NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_inflow_records
PRIMARY KEY([id])
CONSTRAINT FK_product_inflow_records_supplier_id FOREIGN KEY([supplier_id])
REFERENCES [Tables].[suppliers]([id]),
CONSTRAINT CHECK_product_inflow_records
CHECK([count] > 0 AND [full_price] >= 0),
CONSTRAINT FK_product_inflow_records FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id])
);
CREATE TABLE [Tables].[write_off_reasons](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_write_off_reasons
PRIMARY KEY([id]),
);
CREATE TABLE [Tables].[product_write_off_records](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[barcode] BIGINT NOT NULL, -- without reference (temp)
[reason_id] INT NOT NULL,
[count] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_write_off_records
PRIMARY KEY([id])
CONSTRAINT FK_product_write_off_records_reason_id FOREIGN KEY([reason_id])
REFERENCES [Tables].[write_off_reasons]([id]),
CONSTRAINT CHECK_product_write_off_records
CHECK([count] > 0),
CONSTRAINT FK_product_write_off_records FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id])
);
GO
CREATE VIEW [Views].[product_properties]
WITH SCHEMABINDING
AS
SELECT [PA].[product_id], [PA].[attribute_id], [A].[name], [PV].[value]
FROM [Tables].[product_attributes] AS [PA]
LEFT OUTER JOIN [Tables].[product_values] AS [PV]
ON [PV].[product_id] = [PA].[product_id]
AND [PV].[attribute_id] = [PA].[attribute_id]
JOIN [Tables].[attributes] AS [A]
ON [A].[id] = [PA].[attribute_id];
GO
CREATE VIEW [Views].[available_product_types]
WITH SCHEMABINDING
AS
SELECT [B].[id], [B].[parent_id], [B].[name]
FROM [Tables].[product_types] AS [A]
RIGHT JOIN [Tables].[product_types] AS [B]
ON [B].[id] = [A].[parent_id]
WHERE [A].[id] IS NULL;
GO
@@ -0,0 +1,161 @@
USE [master];
IF EXISTS (SELECT 1 FROM sys.databases WHERE [name] = N'Database')
BEGIN
EXECUTE sp_executesql
@stmt =
N'USE [Database];
ALTER DATABASE [Database]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
USE [tempdb];
DROP DATABASE [Database];'
END;
GO
CREATE DATABASE [Database];
GO
USE [Database];
GO
CREATE SCHEMA [Tables] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Functions] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Procedures] AUTHORIZATION [dbo];
GO
CREATE TABLE [Tables].[product_types](
[id] INT NOT NULL IDENTITY(1, 1),
[parent_id] INT DEFAULT(NULL),
[name] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_product_types
PRIMARY KEY([id]),
CONSTRAINT FK_product_types FOREIGN KEY([parent_id])
REFERENCES [Tables].[product_types]([id])
);
CREATE TABLE [Tables].[products](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
[product_type_id] INT NOT NULL,
CONSTRAINT PK_products
PRIMARY KEY([id]),
CONSTRAINT FK_product_type_id FOREIGN KEY([product_type_id])
REFERENCES [Tables].[product_types]([id])
);
CREATE TABLE [Tables].[suppliers](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
[address] NVARCHAR(128) NOT NULL,
[telephone_number] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_suppliers
PRIMARY KEY([id])
);
CREATE TABLE [Tables].[additional_variables](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
[value] NVARCHAR(128) NOT NULL
CONSTRAINT PK_additionalVariables
PRIMARY KEY([id])
);
CREATE TABLE [Tables].[attributes](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL UNIQUE,
CONSTRAINT PK_attributes
PRIMARY KEY([id])
);
CREATE TABLE [Tables].[product_attributes](
[product_id] INT NOT NULL,
[attribute_id] INT NOT NULL,
CONSTRAINT PK_product_attributes
PRIMARY KEY([product_id], [attribute_id]),
CONSTRAINT FK_product_attributes_product_id FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id]),
CONSTRAINT FK_product_attributes_attribute_id FOREIGN KEY([attribute_id])
REFERENCES [Tables].[attributes]([id])
);
CREATE TABLE [Tables].[product_values](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[attribute_id] INT NOT NULL,
[value] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_product_values
PRIMARY KEY([id]),
CONSTRAINT FK_product_values FOREIGN KEY([product_id], [attribute_id])
REFERENCES [Tables].[product_attributes]([product_id], [attribute_id])
);
CREATE TABLE [Tables].[price_history_records](
[id] INT NOT NULL IDENTITY(1, 1),
[product_id] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME()),
[price] MONEY NOT NULL,
CONSTRAINT PK_price_history_records
PRIMARY KEY([id]),
CONSTRAINT FK_price_history_records_product_id FOREIGN KEY([product_id])
REFERENCES [Tables].[products]([id]),
CONSTRAINT CHECK_price_history_records
CHECK(price >= 0)
);
CREATE TABLE [Tables].[product_outflow_records](
[id] INT NOT NULL IDENTITY(1, 1),
[barcode_product_value_id] INT NOT NULL,
[count] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_outflow_records
PRIMARY KEY([id]),
CONSTRAINT FK_product_outflow_records FOREIGN KEY([barcode_product_value_id])
REFERENCES [Tables].[product_values]([id]),
CONSTRAINT CHECK_product_outflow_records
CHECK([count] > 0)
);
CREATE TABLE [Tables].[product_inflow_records](
[id] INT NOT NULL IDENTITY(1, 1),
[barcode_product_value_id] INT NOT NULL,
[supplier_id] INT NOT NULL,
[count] INT NOT NULL,
[full_price] MONEY NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_inflow_records
PRIMARY KEY([id])
CONSTRAINT FK_product_inflow_records_barcode_id FOREIGN KEY([barcode_product_value_id])
REFERENCES [Tables].[product_values]([id]),
CONSTRAINT FK_product_inflow_records_supplier_id FOREIGN KEY([supplier_id])
REFERENCES [Tables].[suppliers]([id]),
CONSTRAINT CHECK_product_inflow_records
CHECK([count] > 0 AND [full_price] >= 0)
);
CREATE TABLE [Tables].[write_off_reasons](
[id] INT NOT NULL IDENTITY(1, 1),
[name] NVARCHAR(128) NOT NULL,
CONSTRAINT PK_write_off_reasons
PRIMARY KEY([id]),
);
CREATE TABLE [Tables].[product_write_off_records](
[id] INT NOT NULL IDENTITY(1, 1),
[barcode_product_value_id] INT NOT NULL,
[reason_id] INT NOT NULL,
[count] INT NOT NULL,
[datetime] DATETIME NOT NULL DEFAULT(SYSDATETIME())
CONSTRAINT PK_product_write_off_records
PRIMARY KEY([id])
CONSTRAINT FK_product_write_off_records_barcode_id FOREIGN KEY([barcode_product_value_id])
REFERENCES [Tables].[product_values]([id]),
CONSTRAINT FK_product_write_off_records_reason_id FOREIGN KEY([reason_id])
REFERENCES [Tables].[write_off_reasons]([id]),
CONSTRAINT CHECK_product_write_off_records
CHECK([count] > 0)
);
GO
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_barcode_exist]') IS NOT NULL
DROP FUNCTION [Functions].[does_barcode_exist];
GO
CREATE FUNCTION [Functions].[does_barcode_exist](
@barcode AS BIGINT
)
RETURNS BIT
AS
BEGIN
IF EXISTS(
SELECT *
FROM [Tables].[products]
WHERE [barcode] = @barcode
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_exist_with_name]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_exist_with_name];
GO
CREATE FUNCTION [Functions].[does_product_exist_with_name](
@name AS NVARCHAR(128)
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[products]
WHERE [name] = @name
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_exist_with_product_type_id]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_exist_with_product_type_id];
GO
CREATE FUNCTION [Functions].[does_product_exist_with_product_type_id](
@product_type_id AS INT
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[products]
WHERE [product_type_id] = @product_type_id
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Functions].[does_product_type_exist_with_name]') IS NOT NULL
DROP FUNCTION [Functions].[does_product_type_exist_with_name];
GO
CREATE FUNCTION [Functions].[does_product_type_exist_with_name](
@name AS NVARCHAR(128)
)
RETURNS BIT
AS
BEGIN
IF EXISTS (
SELECT *
FROM [Tables].[product_types]
WHERE [name] = @name
)
RETURN CAST(1 AS BIT);
RETURN CAST(0 AS BIT);
END
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Functions].[product_type_id]') IS NOT NULL
DROP FUNCTION [Functions].[product_type_id];
GO
CREATE FUNCTION [Functions].[product_type_id](
@product_id AS INT
)
RETURNS INT
BEGIN
DECLARE @product_type AS INT;
SET @product_type = (
SELECT [product_type_id]
FROM [Tables].[products]
WHERE [id] = @product_id);
RETURN @product_type;
END
@@ -0,0 +1,26 @@
USE [Database]
IF OBJECT_ID('[Functions].[same_products_join_product_types]') IS NOT NULL
DROP FUNCTION [Functions].[same_products_join_product_types];
GO
CREATE FUNCTION [Functions].[same_products_join_product_types](
@product_type_id AS INT
)
RETURNS TABLE
RETURN
WITH [result] AS (
SELECT [id], [parent_id], [name]
FROM [Tables].[product_types]
WHERE [id] = @product_type_id
)
SELECT [P].[id] AS [product_id],
[P].[barcode] AS [product_barcode],
[P].[product_type_id],
[P].[name] AS [product_name],
[PT].[name] AS [product_type_name]
FROM [result] AS [R]
JOIN [Tables].[products] AS [P]
ON [P].[product_type_id] = [R].[id]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [P].[product_type_id]
@@ -0,0 +1,34 @@
USE [Database]
IF OBJECT_ID('[Functions].[subgroup_products_join_product_types]') IS NOT NULL
DROP FUNCTION [Functions].[subgroup_products_join_product_types];
GO
CREATE FUNCTION [Functions].[subgroup_products_join_product_types](
@product_type_id AS INT
)
RETURNS TABLE
RETURN
WITH [result] AS
(
SELECT [id], [parent_id], [name]
FROM [Tables].[product_types]
WHERE [id] = @product_type_id
UNION ALL
SELECT [PT].[id], [PT].[parent_id], [PT].[name]
FROM [result] AS [R]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[parent_id] = [R].[id]
)
SELECT [P].[id] AS [product_id],
[P].[barcode] AS [product_barcode],
[P].[product_type_id],
[P].[name] AS [product_name],
[PT].[name] AS [product_type_name]
FROM [result] AS [R]
JOIN [Tables].[products] AS [P]
ON [P].[product_type_id] = [R].[id]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [P].[product_type_id]
@@ -0,0 +1,16 @@
USE [Database]
IF OBJECT_ID('[Functions].[fill_CHAR_128]') IS NOT NULL
DROP FUNCTION [Functions].[fill_CHAR_128];
GO
CREATE FUNCTION [Functions].[fill_CHAR_128](
@string AS NVARCHAR(128),
@symbol AS NVARCHAR,
@width AS INT
)
RETURNS NVARCHAR(128) AS
BEGIN
RETURN CONCAT(REPLICATE(@symbol, @width - LEN(@string)), @string);
END
GO
@@ -0,0 +1,39 @@
USE [Database]
IF OBJECT_ID('[Functions].[product_with_barcode_stock]') IS NOT NULL
DROP FUNCTION [Functions].[product_with_barcode_stock];
GO
CREATE FUNCTION [Functions].[product_with_barcode_stock](
@barcode AS BIGINT
)
RETURNS INT AS
BEGIN
DECLARE @product_inflow_sum AS INT;
SET @product_inflow_sum = (
SELECT ISNULL(SUM([count]), 0) AS [sum]
FROM [Tables].[product_inflow_records]
WHERE [barcode] = @barcode
);
DECLARE @result AS INT;
WITH [counts] AS (
SELECT [POR].[count] AS [outflow_count], [PWOR].[count] AS [write_off_count]
FROM [Tables].[products] AS [P]
LEFT OUTER JOIN [Tables].[product_outflow_records] AS [POR]
ON [POR].[barcode] = [P].[barcode]
LEFT OUTER JOIN [Tables].[product_write_off_records] AS [PWOR]
ON [PWOR].[barcode] = [P].[barcode]
GROUP BY
GROUPING SETS
(
([POR].[count]),
([PWOR].[count])
)
)
SELECT @result = (
@product_inflow_sum - ISNULL(SUM([outflow_count]), 0) - ISNULL(SUM([write_off_count]), 0)
)
FROM [counts];
RETURN @result;
END
GO
+5
View File
@@ -0,0 +1,5 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\Initializing\initialize-all.sql ^
-r1 1> NUL
@@ -0,0 +1,88 @@
USE [Database]
SET IDENTITY_INSERT [Tables].[product_types] ON;
INSERT INTO [Tables].[product_types]([id], [parent_id], [name])
VALUES
(1, DEFAULT, N'Íåïðîäîâîëüñòâåííàÿ ïðîäóêöèÿ'),
(2, 1, N'Îäåæäà'),
(3, 1, N'Äåòñêèå òîâàðû'),
(4, 1, N'Äåêîð è èíòåðüåð'),
(5, 1, N'Õîááè è òâîð÷åñòâî'),
(6, 5, N'Íàñòîëüíûå è êàðòî÷íûå èãðû'),
(7, 6, N'Èãðàëüíûå êàðòû'),
(8, 6, N'Àêñåññóàðû'),
(9, 6, N'Íàñòîëüíûå èãðû'),
(10, 5, N'Ïàçëû è ãîëîâîëîìêè'),
(11, 1, N'Èãðû è êîíñîëè'),
(12, 1, N'Àêñåññóàðû'),
(13, 12, N'Áèæóòåðíûå óêðàøåíèÿ'),
(14, 13, N'Áðåëîêè è çíà÷êè'),
(15, DEFAULT, N'Ïðîäîâîëüñòâåííàÿ ïðîäóêöèÿ'),
(16, 15, N'Ñîêè, âîäû, íàïèòêè'),
(17, 16, N'Âîäà'),
(18, 15, N'Âûïå÷êà è ñëàäîñòè'),
(19, 18, N'Êîíôåòû'),
(20, 19, N'Äðàæå'),
(21, 18, N'Ïå÷åíüå, ïðÿíèêè è âàôëè'),
(22, 21, N'Ïå÷åíüå'),
(23, 18, N'Æåâàòåëüíàÿ ðåçèíêà');
SET IDENTITY_INSERT [Tables].[product_types] OFF;
SET IDENTITY_INSERT [Tables].[attributes] ON;
INSERT INTO [Tables].[attributes]([id], [name])
VALUES
(1, 'Article number'),
(2, 'Date of manufacture'),
(3, 'Expiration date'),
(4, 'Unit');
SET IDENTITY_INSERT [Tables].[attributes] OFF;
SET IDENTITY_INSERT [Tables].[products] ON;
INSERT INTO [Tables].[products] ([id], [barcode], [name], [product_type_id])
VALUES(1, 1111111111111, N'Hobby World Êàðòî÷íàÿ èãðà Óæàñ Àðêõýìà', 9),
(2, 2222222222222, N'Æåâàòåëüíàÿ ðåçèíêà Orbit Mega Êëóáíèêà, áåç ñàõàðà, 16,4 ã', 23),
(3, 3333333333333, N'Æåâàòåëüíàÿ ðåçèíêà Mentos Pure White âêóñ Êëóáíèêà, 15,5 ã', 23);
SET IDENTITY_INSERT [Tables].[products] OFF;
INSERT INTO [Tables].[product_attributes] ([product_id], [attribute_id])
VALUES (2, 2), (2, 3), (3, 2), (3, 3);
INSERT INTO [Tables].[product_values] ([product_id], [attribute_id], [value])
-- Äàòà èçãîòîâëåíèÿ; ñðîê ãîäíîñòè
VALUES (2, 2, N'1900-01-01'), (2, 3, N'3650'), (3, 2, N'1900-01-01'), (3, 3, N'30');
SET IDENTITY_INSERT [Tables].[suppliers] ON;
INSERT INTO [Tables].[suppliers]([id], [name], [telephone_number], [address])
VALUES
(1, N'First supplier', N'First telephone number', N'First address'),
(2, N'Second supplier', N'Second telephone number', N'Second address'),
(3, N'Thied supplier', N'Third telephone number', N'Third address');
SET IDENTITY_INSERT [Tables].[suppliers] OFF;
SET IDENTITY_INSERT [Tables].[product_inflow_records] ON;
INSERT INTO [Tables].[product_inflow_records] ([id], [product_id], [barcode], [supplier_id], [count], [full_price], [datetime])
VALUES
(1, 1, 1111111111111, 1, 5, 10, N'1900-01-01'),
(2, 1, 1111111111111, 2, 5, 5, N'1900-01-01'),
(3, 2, 2222222222222, 3, 5, 10, N'1900-01-01');
SET IDENTITY_INSERT [Tables].[product_inflow_records] OFF;
SET IDENTITY_INSERT [Tables].[product_outflow_records] ON;
INSERT INTO [Tables].[product_outflow_records] ([id], [product_id], [barcode], [count], [datetime])
VALUES
(2, 1, 1111111111111, 2, N'1900-01-01'),
(3, 1, 1111111111111, 3, N'1900-01-02')
SET IDENTITY_INSERT [Tables].[product_outflow_records] OFF;
SET IDENTITY_INSERT [Tables].[write_off_reasons] ON;
INSERT INTO [Tables].[write_off_reasons]([id], [name])
VALUES
(1, N'Âûøåë ñðîê ãîäíîñòè'),
(2, N'Áðàê ïðîäóêöèè');
SET IDENTITY_INSERT [Tables].[write_off_reasons] OFF;
SET IDENTITY_INSERT [Tables].[product_write_off_records] ON;
INSERT INTO [Tables].[product_write_off_records] ([id], [product_id], [barcode], [reason_id], [count], [datetime])
VALUES
(1, 1, 1111111111111, 2, 2, N'1900-01-03')
SET IDENTITY_INSERT [Tables].[product_write_off_records] OFF;
@@ -0,0 +1,8 @@
USE [Database]
-- Äîïîëíèòåëüíàÿ ïåðåìåííàÿ, íåîáõîäèìàÿ äëÿ îòñëåæèâàíèÿ
-- ïîñëäíåãî ñãåíåðèðîâàííîãî çíà÷åíèÿ áàðêîäà
SET IDENTITY_INSERT [Tables].[additional_variables] ON;
INSERT INTO [Tables].[additional_variables]([id], [name], [value])
VALUES(1, N'last_barcode', N'-1');
SET IDENTITY_INSERT [Tables].[additional_variables] OFF;
@@ -0,0 +1,12 @@
USE [Database]
-- Ñòàíäàðòíûå àòðèáóòû òîâàðîâ, êîòîðûå áóäóò èñïîëüçîâàòüñÿ
-- â òåñòèðîâàíèè íà îñíîâå èññëåäîâàíèé ïðåäìåòíîé îáëàñòè
SET IDENTITY_INSERT [Tables].[attributes] ON;
INSERT INTO [Tables].[attributes]([id], [name])
VALUES
(1, 'Article number'),
(2, 'Barcode'),
(3, 'Date of manufacture'),
(4, 'Expiration date');
SET IDENTITY_INSERT [Tables].[attributes] OFF;
@@ -0,0 +1,65 @@
USE [Database]
DECLARE
@article_number_attribute_id AS INT,
@barcode_attribute_id AS INT,
@date_of_manufacture_attribute_id AS INT,
@expiration_date_attribute_id AS INT;
SET @article_number_attribute_id =
(SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = N'Article number');
SET @barcode_attribute_id =
(SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = N'Barcode');
SET @date_of_manufacture_attribute_id =
(SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = N'Date of manufacture');
SET @expiration_date_attribute_id =
(SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = N'Expiration date');
DECLARE
@product_id_1 AS INT,
@product_id_2 AS INT,
@product_id_3 AS INT,
@product_id_4 AS INT;
EXECUTE [Procedures].[insert_product]
@name = N'Hobby World Êàðòî÷íàÿ èãðà Óæàñ Àðêõýìà',
@product_type_id = 9, -- Íàñòîëüíûå èãðû
@barcode_attribute_id = @barcode_attribute_id,
@inserted_product_id = @product_id_1 OUTPUT;
EXECUTE [Procedures].[insert_product]
@name = N'Æåâàòåëüíàÿ ðåçèíêà Orbit Mega Êëóáíèêà, áåç ñàõàðà, 16,4 ã',
@product_type_id = 23, -- Æåâàòåëüíàÿ ðåçèíêà
@barcode_attribute_id = @barcode_attribute_id,
@inserted_product_id = @product_id_2 OUTPUT;
EXECUTE [Procedures].[insert_product]
@name = N'Æåâàòåëüíàÿ ðåçèíêà Mentos Pure White âêóñ Êëóáíèêà, 15,5 ã',
@product_type_id = 23, -- Æåâàòåëüíàÿ ðåçèíêà
@barcode_attribute_id = @barcode_attribute_id,
@inserted_product_id = @product_id_3 OUTPUT;
EXECUTE [Procedures].[insert_product]
@name = N'Ïå÷åíüå Oreo, ñ êàêàî è íà÷èíêîé ñî âêóñîì êëóáíèêè, 228 ã',
@product_type_id = 22, -- Ïå÷åíüå
@barcode_attribute_id = @barcode_attribute_id,
@inserted_product_id = @product_id_4 OUTPUT;
INSERT INTO [Tables].[product_attributes]([product_id], [attribute_id])
VALUES
(@product_id_2, 3),
(@product_id_2, 4),
(@product_id_3, 3),
(@product_id_3, 4);
INSERT INTO [Tables].[product_values]([product_id], [attribute_id], [value])
VALUES
(2, 3, N'1900-01-01'), -- Äàòà èçãîòîâëåíèÿ
(2, 4, N'3650'), -- Ñðîê ãîäíîñòè
(3, 3, N'1900-01-01'),
(3, 4, N'30');
@@ -0,0 +1,16 @@
USE [Database]
IF OBJECT_ID('[Procedures].[products_join_product_types]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[products_join_product_types];
GO
CREATE PROCEDURE [Procedures].[products_join_product_types]
AS
SELECT [P].[id] AS [product_id], [P].[barcode],
[P].[product_type_id] AS [product_type_id],
[P].[name] AS [product_name],
[PT].[name] AS [product_type_name]
FROM [Tables].[products] AS [P]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [P].[product_type_id]
GO
@@ -0,0 +1,14 @@
USE [Database]
IF OBJECT_ID('[Procedures].[rename_product]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[rename_product];
GO
CREATE PROCEDURE [Procedures].[rename_product]
@product_id AS INT,
@name AS NVARCHAR(128)
AS
UPDATE [Tables].[products]
SET
[name] = @name
WHERE [id] = @product_id;
GO
@@ -0,0 +1,14 @@
USE [Database]
IF OBJECT_ID('[Procedures].[rename_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[rename_product_type];
GO
CREATE PROCEDURE [Procedures].[rename_product_type]
@product_type_id AS INT,
@name AS NVARCHAR(128)
AS
UPDATE [Tables].[product_types]
SET
[name] = @name
WHERE [id] = @product_type_id;
GO
@@ -0,0 +1,27 @@
USE [Database]
IF OBJECT_ID('[Procedures].[specify_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[specify_product_type];
GO
CREATE PROCEDURE [Procedures].[specify_product_type]
@parent_id INT = NULL,
@name NVARCHAR(128),
@inserted_product_type_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
INSERT INTO [Tables].[product_types]([parent_id], [name])
VALUES(DEFAULT, @name);
SET @inserted_product_type_id = SCOPE_IDENTITY();
UPDATE [Tables].[products]
SET [product_type_id] = @inserted_product_type_id
WHERE [product_type_id] = @parent_id;
UPDATE [Tables].[product_types]
SET [parent_id] = @parent_id
WHERE [id] = @inserted_product_type_id
COMMIT TRANSACTION;
GO
@@ -0,0 +1,18 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_barcode]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_barcode];
GO
CREATE PROCEDURE [Procedures].[update_barcode]
@product_id INT,
@barcode BIGINT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
UPDATE [Tables].[products]
SET [barcode] = @barcode
WHERE [id] = @product_id
COMMIT TRANSACTION;
GO
@@ -0,0 +1,22 @@
USE [Database]
IF OBJECT_ID('[Procedures].[get_next_barcode]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[get_next_barcode];
GO
CREATE PROCEDURE [Procedures].[get_next_barcode]
@next_barcode AS BIGINT OUTPUT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
SET @next_barcode = (
SELECT CAST([value] AS BIGINT) + 1
FROM [Tables].[additional_variables]
WHERE [id] = 1
);
UPDATE [Tables].[additional_variables]
SET [value] = CAST(@next_barcode AS NVARCHAR(13))
WHERE [id] = 1;
COMMIT TRANSACTION;
GO
@@ -0,0 +1,79 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_or_update_barcode]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_or_update_barcode];
GO
CREATE PROCEDURE [Procedures].[insert_or_update_barcode]
@product_id AS INT,
@barcode_attribute_id AS INT,
@barcode AS BIGINT = NULL,
@record_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
SET NOCOUNT ON;
IF @barcode IS NOT NULL AND EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND CAST([value] AS BIGINT) = @barcode
)
BEGIN;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
THROW 53000, 'The barcode is not unique', 1;
END;
CREATE TABLE [Tables].[#existing_hash_codes](
[hash_code] NVARCHAR(13)
);
INSERT INTO [#existing_hash_codes]([hash_code])
SELECT [value]
FROM [Tables].[product_attributes] AS [pa]
JOIN [Tables].[product_values] AS [pv]
ON [pv].[product_id] = [pa].[product_id]
AND [pv].[attribute_id] = [pa].[attribute_id]
WHERE [pa].[attribute_id] = @barcode_attribute_id
-- begin creating barcode
IF @barcode IS NULL
EXECUTE [Procedures].[get_next_barcode] @next_barcode = @barcode OUTPUT;
WHILE EXISTS(
SELECT *
FROM [#existing_hash_codes]
WHERE CAST([hash_code] AS BIGINT) = @barcode
)
OR EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND CAST([value] AS BIGINT) = @barcode
)
BEGIN
SET @barcode += 1;
END
-- end creating barcode
IF EXISTS(
SELECT *
FROM [Tables].[product_values]
WHERE [attribute_id] = @barcode_attribute_id
AND [product_id] = @product_id
AND [value] IS NOT NULL
)
BEGIN
UPDATE [Tables].[product_values]
SET @record_id = [id],
[value] = [Functions].[to_barcode_ean_13](@barcode)
WHERE [product_id] = @product_id
AND [attribute_id] = @barcode_attribute_id;
END
ELSE BEGIN
INSERT INTO [Tables].[product_values]([product_id], [attribute_id], [value])
VALUES(@product_id, @barcode_attribute_id, [Functions].[to_barcode_ean_13](@barcode));
SET @record_id = SCOPE_IDENTITY();
END
COMMIT TRANSACTION;
GO
@@ -0,0 +1,16 @@
USE [Database]
IF OBJECT_ID('[Procedures].[product-properties]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[product-properties];
GO
CREATE PROCEDURE [Procedures].[product-properties]
@product_id AS INT
AS
SELECT [A].[id], [A].[name], [PV].[value]
FROM [Tables].[product_attributes] AS [PA]
LEFT OUTER JOIN [Tables].[product_values] AS [PV]
ON [PV].[product_id] = [PA].[product_id]
AND [PV].[attribute_id] = [PA].[attribute_id]
JOIN [Tables].[attributes] AS [A]
ON [A].[id] = [PA].[attribute_id]
WHERE [PA].[product_id] = @product_id;
@@ -0,0 +1,31 @@
USE [Database]
IF OBJECT_ID('[Procedures].[select_expired_products]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[select_expired_products];
GO
CREATE PROCEDURE [Procedures].[select_expired_products]
@date_of_manufacture_attribute_id AS INT ,
@expiration_date_attribute_id AS INT ,
@actual_date AS DATE
AS
WITH [values] AS (
SELECT [p].[id], [pa].[attribute_id], [pv].[value]
FROM [Tables].[products] AS [p]
JOIN [Tables].[product_attributes] AS [pa]
ON [pa].[product_id] = [p].[id]
JOIN [Tables].[product_values] AS [pv]
ON [pv].[attribute_id] = [pa].[attribute_id]
AND [pv].[product_id] = [pa].[product_id]
WHERE [pa].[attribute_id] IN (@date_of_manufacture_attribute_id, @expiration_date_attribute_id)
)
SELECT [date_of_manufacture_attributes].[id]
FROM [values] AS date_of_manufacture_attributes
JOIN [values] AS expiration_date_attributes
ON [expiration_date_attributes].[id] = [date_of_manufacture_attributes].[id]
AND [expiration_date_attributes].[attribute_id] = @expiration_date_attribute_id
WHERE [date_of_manufacture_attributes].[attribute_id] = @date_of_manufacture_attribute_id
AND DATEADD(
DAY, CAST([expiration_date_attributes].[value] AS INT),
CAST([date_of_manufacture_attributes].[value] AS DATE)
) < @actual_date;
GO
@@ -0,0 +1,37 @@
USE [Database]
IF OBJECT_ID('[Procedures].[tree_positions]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[tree_positions];
GO
CREATE PROCEDURE [Procedures].[tree_positions]
@product_type_id INT
AS
IF OBJECT_ID('[tempdb].[dbo].[#product_types_row_number]') IS NOT NULL
DROP TABLE [dbo].[#product_types_row_number];
CREATE TABLE [#product_types_row_number]
(
[id] INT NOT NULL,
[parent_id] INT,
[row_number] INT NOT NULL
);
INSERT INTO [#product_types_row_number]
SELECT [id], [parent_id], ROW_NUMBER() OVER (PARTITION BY [parent_id] ORDER BY [parent_id], [id] ASC) - 1
FROM [Tables].[product_types];
WITH [result] AS (
SELECT
[id], [parent_id], [row_number]
FROM [#product_types_row_number]
WHERE [id] = @product_type_id
UNION ALL
SELECT [PT].[id], [PT].[parent_id], [PT].[row_number]
FROM [result] AS [R]
JOIN [#product_types_row_number] AS [PT]
ON [PT].[id] = [R].[parent_id]
)
SELECT [id], [parent_id], [row_number]
FROM [result]
ORDER BY [id];
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product];
GO
CREATE PROCEDURE [Procedures].[insert_product]
@name AS NVARCHAR(128),
@product_type_id AS INT,
@barcode AS BIGINT,
@inserted_product_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[products]([barcode], [name], [product_type_id])
VALUES(@barcode, @name, @product_type_id);
SET @inserted_product_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,14 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_attribute]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_attribute];
GO
CREATE PROCEDURE [Procedures].[insert_product_attribute]
@product_id AS INT,
@attribute_id AS INT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_attributes]([product_id], [attribute_id])
VALUES (@product_id, @attribute_id);
COMMIT TRANSACTION;
GO
@@ -0,0 +1,26 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_type];
GO
CREATE PROCEDURE [Procedures].[insert_product_type]
@parent_id AS INT = NULL,
@name AS NVARCHAR(128),
@inserted_product_type_id AS INT = NULL OUTPUT
AS
BEGIN TRY
INSERT INTO [Tables].[product_types]([parent_id], [name])
VALUES(@parent_id, @name);
SET @inserted_product_type_id = SCOPE_IDENTITY();
END TRY
BEGIN CATCH
DECLARE @message NVARCHAR(128);
SET @message =
'Cannot create [a] subgroup. Cause: the group '
+ '(name: "'+ (SELECT [name] FROM [Tables].[product_types] WHERE [id] = @parent_id) +'"; [id]:'+ LTRIM(STR(@parent_id)) +')'
+ ' is referenced by [a] product object.';
PRINT 'Actual error number: ' + CAST(ERROR_NUMBER() AS NVARCHAR(10));
PRINT 'Actual line number: ' + CAST(ERROR_LINE() AS NVARCHAR(10));
THROW 51000, @message, 1;
END CATCH
GO
@@ -0,0 +1,15 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_value]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_value];
GO
CREATE PROCEDURE [Procedures].[insert_product_value]
@product_id AS INT,
@attribute_id AS INT,
@value AS NVARCHAR(128)
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_values]([product_id], [attribute_id], [value])
VALUES (@product_id, @attribute_id, @value);
COMMIT TRANSACTION;
GO
@@ -0,0 +1,20 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product];
GO
CREATE PROCEDURE [Procedures].[insert_product]
@name AS NVARCHAR(128),
@product_type_id AS INT,
@barcode_attribute_id AS INT,
@inserted_product_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[products]([barcode], [name], [product_type_id])
VALUES(@name, @product_type_id);
SET @inserted_product_id = SCOPE_IDENTITY();
INSERT INTO [Tables].[product_attributes]([product_id], [attribute_id])
VALUES (@inserted_product_id, @barcode_attribute_id);
COMMIT TRANSACTION;
GO
@@ -0,0 +1,23 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_type];
GO
CREATE PROCEDURE [Procedures].[delete_product_type]
@product_type_id AS INT
AS
DECLARE @newParentId INT;
SET @newParentId = (
SELECT [parent_id]
FROM [Tables].[product_types]
WHERE [id] = @product_type_id
)
BEGIN TRANSACTION;
UPDATE [Tables].[product_types]
SET [parent_id] = @newParentId
WHERE [parent_id] = @product_type_id
DELETE FROM [Tables].[product_types]
WHERE [id] = @product_type_id;
COMMIT TRANSACTION;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product];
GO
CREATE PROCEDURE [Procedures].[delete_product]
@product_id AS INT
AS
BEGIN TRANSACTION;
DELETE FROM [Tables].[product_values]
WHERE [product_id] = @product_id;
DELETE FROM [Tables].[product_attributes]
WHERE [product_id] = @product_id;
DELETE FROM [Tables].[products]
WHERE [id] = @product_id;
COMMIT TRANSACTION;
GO
@@ -0,0 +1,15 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_attribute]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_attribute];
GO
CREATE PROCEDURE [Procedures].[insert_attribute]
@name AS NVARCHAR(128),
@inserted_attribute_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[attributes]([name])
VALUES (@name);
SET @inserted_attribute_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_price_history_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_price_history_record];
GO
CREATE PROCEDURE [Procedures].[insert_price_history_record]
@product_id AS INT,
@datetime AS DATETIME,
@price AS MONEY,
@inserted_price_history_record_id INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[price_history_records]([product_id], [datetime], [price])
VALUES (@product_id, @datetime, @price);
SET @inserted_price_history_record_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,20 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_inflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_inflow_record];
GO
CREATE PROCEDURE [Procedures].[insert_product_inflow_record]
@barcode_product_value_id AS INT,
@barcode_product_attribute_id AS INT,
@supplier_id AS INT,
@count AS INT,
@full_price AS INT,
@datetime AS DATETIME,
@inserted_product_inflow_record_id INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_inflow_records]([barcode_product_value_id], [supplier_id], [count], [full_price], [datetime])
VALUES (@barcode_product_value_id, @supplier_id, @count, @full_price, @datetime);
SET @inserted_product_inflow_record_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_outflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_outflow_record];
GO
CREATE PROCEDURE [Procedures].[insert_product_outflow_record]
@barcode_product_outflow_record_id AS INT,
@count AS INT,
@datetime AS DATETIME,
@inserted_product_outflow_records INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_outflow_records]([barcode_product_value_id], [count], [datetime])
VALUES (@barcode_product_outflow_record_id, @count, @datetime);
SET @inserted_product_outflow_records = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_value]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_value];
GO
CREATE PROCEDURE [Procedures].[insert_product_value]
@product_id AS INT,
@attribute_id AS INT,
@value AS NVARCHAR(128),
@inserted_product_value_id INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_values]([product_id], [attribute_id], [value])
VALUES (@product_id, @attribute_id, @value);
SET @inserted_product_value_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,18 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_product_write_off_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_product_write_off_record];
GO
CREATE PROCEDURE [Procedures].[insert_product_write_off_record]
@barcode_product_value_id AS INT,
@reason_id AS INT,
@count AS INT,
@datetime AS DATETIME,
@inserted_product_write_off_record_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[product_write_off_records]([barcode_product_value_id], [reason_id], [count], [datetime])
VALUES (@barcode_product_value_id, @reason_id, @count, @datetime);
SET @inserted_product_write_off_record_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_supplier]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_supplier];
GO
CREATE PROCEDURE [Procedures].[insert_supplier]
@name AS NVARCHAR(128),
@address AS NVARCHAR(128),
@telephone_number AS NVARCHAR(128),
@inserted_supplier_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[suppliers]([name], [address], [telephone_number])
VALUES (@name, @address, @telephone_number);
SET @inserted_supplier_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,15 @@
USE [Database]
IF OBJECT_ID('[Procedures].[insert_write_off_reason]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[insert_write_off_reason];
GO
CREATE PROCEDURE [Procedures].[insert_write_off_reason]
@name AS NVARCHAR(128),
@inserted_write_off_reason_id AS INT = NULL OUTPUT
AS
BEGIN TRANSACTION;
INSERT INTO [Tables].[write_off_reasons]([name])
VALUES (@name);
SET @inserted_write_off_reason_id = SCOPE_IDENTITY();
COMMIT TRANSACTION;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_inflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_inflow_record];
GO
CREATE PROCEDURE [Procedures].[delete_product_inflow_record]
@product_inflow_record_id AS INT
AS
DELETE FROM [Tables].[product_inflow_records]
WHERE [id] = @product_inflow_record_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_type];
GO
CREATE PROCEDURE [Procedures].[delete_product_type]
@product_type_id AS INT
AS
DELETE FROM [Tables].[product_types]
WHERE [id] = @product_type_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_write_off_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_write_off_record];
GO
CREATE PROCEDURE [Procedures].[delete_product_write_off_record]
@product_write_off_record_id AS INT
AS
DELETE FROM [Tables].[product_write_off_records]
WHERE [id] = @product_write_off_record_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_attribute]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_attribute];
GO
CREATE PROCEDURE [Procedures].[delete_attribute]
@attribute_id AS INT
AS
DELETE FROM [Tables].[attributes]
WHERE [id] = @attribute_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_price_history_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_price_history_record];
GO
CREATE PROCEDURE [Procedures].[delete_price_history_record]
@price_history_record_id AS INT
AS
DELETE FROM [Tables].[price_history_records]
WHERE [id] = @price_history_record_id;
GO
@@ -0,0 +1,13 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_attribute]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_attribute];
GO
CREATE PROCEDURE [Procedures].[delete_product_attribute]
@product_id AS INT,
@product_attribute_id AS INT
AS
DELETE FROM [Tables].[product_attributes]
WHERE [product_id] = @product_attribute_id
AND [attribute_id] = @product_attribute_id
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_outflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_outflow_record];
GO
CREATE PROCEDURE [Procedures].[delete_product_outflow_record]
@product_outflow_record_id AS INT
AS
DELETE FROM [Tables].[product_outflow_records]
WHERE [id] = @product_outflow_record_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_product_value]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_product_value];
GO
CREATE PROCEDURE [Procedures].[delete_product_value]
@product_value_id AS INT
AS
DELETE FROM [Tables].[product_values]
WHERE [id] = @product_value_id;
GO
@@ -0,0 +1,11 @@
USE [Database]
IF OBJECT_ID('[Procedures].[delete_supplier]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[delete_supplier];
GO
CREATE PROCEDURE [Procedures].[delete_supplier]
@supplier_id AS INT
AS
DELETE FROM [Tables].[suppliers]
WHERE [id] = @supplier_id;
GO
@@ -0,0 +1,13 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_attribute]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_attribute];
GO
CREATE PROCEDURE [Procedures].[update_attribute]
@attribute_id AS INT,
@name AS NVARCHAR(128)
AS
UPDATE [Tables].[attributes]
SET [name] = @name
WHERE [id] = @attribute_id;
GO
@@ -0,0 +1,18 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_price_history_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_price_history_record];
GO
CREATE PROCEDURE [Procedures].[update_price_history_record]
@price_history_record_id AS INT,
@product_id AS INT,
@datetime AS DATETIME,
@price AS MONEY
AS
UPDATE [Tables].[price_history_records]
SET
[product_id] = @product_id,
[datetime] = @datetime,
[price] = @price
WHERE [id] = @price_history_record_id;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product];
GO
CREATE PROCEDURE [Procedures].[update_product]
@product_id AS INT,
@name AS NVARCHAR(128),
@product_type_id AS INT,
@barcode_attribute_id AS INT
AS
UPDATE [Tables].[products]
SET
[name] = @name,
[product_type_id] = @product_type_id
WHERE [id] = @product_id;
GO
@@ -0,0 +1,22 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product_inflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product_inflow_record];
GO
CREATE PROCEDURE [Procedures].[update_product_inflow_record]
@product_inflow_record_id AS INT,
@barcode_product_value_id AS INT,
@supplier_id AS INT,
@count AS INT,
@full_price AS INT,
@datetime AS DATETIME
AS
UPDATE [Tables].[product_inflow_records]
SET
[barcode_product_value_id] = @barcode_product_value_id,
[supplier_id] = @supplier_id,
[count] = @count,
[full_price] = @full_price,
[datetime] = @datetime
WHERE [id] = @product_inflow_record_id;
GO
@@ -0,0 +1,19 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product_outflow_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product_outflow_record];
GO
CREATE PROCEDURE [Procedures].[update_product_outflow_record]
@product_outflow_record_id AS INT,
@barcode_product_value_record_id AS INT,
@count AS INT,
@datetime AS INT,
@inserted_product_outflow_records AS INT = NULL OUTPUT
AS
UPDATE [Tables].[product_outflow_records]
SET
[barcode_product_value_id] = @barcode_product_value_record_id,
[count] = @count,
[datetime] = @datetime
WHERE [id] = @product_outflow_record_id;
GO
@@ -0,0 +1,17 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product_type]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product_type];
GO
CREATE PROCEDURE [Procedures].[update_product_type]
@product_type_id AS INT,
@parent_id AS INT = NULL,
@name AS NVARCHAR(128)
AS
UPDATE [Tables].[product_types]
SET
[parent_id] = @parent_id,
[name] = @name
WHERE [id] = @product_type_id;
GO
@@ -0,0 +1,18 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product_value]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product_value];
GO
CREATE PROCEDURE [Procedures].[update_product_value]
@product_value_id AS INT,
@product_id AS INT,
@attribute_id AS INT,
@value AS NVARCHAR(128)
AS
UPDATE [Tables].[product_values]
SET
[product_id] = @product_id,
[attribute_id] = @attribute_id,
[value] = @value
WHERE [id] = @product_value_id;
GO
@@ -0,0 +1,20 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_product_write_off_record]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_product_write_off_record];
GO
CREATE PROCEDURE [Procedures].[update_product_write_off_record]
@product_write_off_record_id AS INT,
@barcode_product_value_id AS INT,
@reason_id AS INT,
@count AS INT,
@datetime AS DATETIME
AS
UPDATE [Tables].[product_write_off_records]
SET
[barcode_product_value_id] = @barcode_product_value_id,
[reason_id] = @reason_id,
[count] = @count,
[datetime] = @datetime
WHERE [id] = @product_write_off_record_id;
GO
@@ -0,0 +1,18 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_supplier]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_supplier];
GO
CREATE PROCEDURE [Procedures].[update_supplier]
@supplier_id AS INT,
@name AS NVARCHAR(128),
@address AS NVARCHAR(128),
@telephone_number AS NVARCHAR(128)
AS
UPDATE [Tables].[suppliers]
SET
[name] = @name,
[address] = @address,
[telephone_number] = @telephone_number
WHERE [id] = @supplier_id;
GO
@@ -0,0 +1,13 @@
USE [Database]
IF OBJECT_ID('[Procedures].[update_write_off_reason]', 'P') IS NOT NULL
DROP PROCEDURE [Procedures].[update_write_off_reason];
GO
CREATE PROCEDURE [Procedures].[update_write_off_reason]
@write_off_reason_id AS INT,
@name AS NVARCHAR(128)
AS
UPDATE [Tables].[write_off_reasons]
SET [name] = @name
WHERE [id] = @write_off_reason_id;
GO
+8
View File
@@ -0,0 +1,8 @@
@echo off
sqlcmd -S %SSMS_SERVER_NAME% -U %SSMS_USER_NAME% -P %SSMS_USER_PASSWORD% -i ^
.\tests\test-1-Database.sql ^
.\tests\test-2-Database.sql ^
.\tests\test-3-Database.sql ^
.\tests\test-4-Database.sql ^
-r1 1> NUL
+15
View File
@@ -0,0 +1,15 @@
USE [Database]
SELECT [E].[id], [E].[name], [T].[name], [A].[attribute_id], [AN].[name], [V].[value]
FROM [Tables].[products] AS [E]
JOIN [Tables].[product_types] AS [T]
ON [T].[id] = [E].[product_type_id]
JOIN [Tables].[product_attributes] AS [A]
ON [A].[product_id] = [E].[id]
JOIN [Tables].[attributes] AS [AN]
ON [AN].[id] = [A].attribute_id
LEFT JOIN [Tables].[product_values] AS [V]
ON [V].[product_id] = [A].[product_id]
AND [V].[attribute_id] = [A].[attribute_id]
EXECUTE [Procedures].[select_expired_products] @date_of_manufacture_attribute_id, @expiration_date_attribute_id, N'19000201'
+44
View File
@@ -0,0 +1,44 @@
USE [Database]
--<< 1
SELECT [p].[id], [p].[name], [pt].[id], [pt].[name]
FROM [Tables].[product_types] AS [pt]
LEFT JOIN [Tables].[products] AS [p]
ON [p].[product_type_id] = [pt].[id]
BEGIN TRY
EXECUTE [Procedures].[insert_product_type] 23, N'Orbit';
END TRY
BEGIN CATCH
PRINT N'The error occurred - the right case'
END CATCH
--<< 2
EXECUTE [Procedures].[specify_product_type] 23, N'Orbit';
SELECT [p].[id], [p].[name], [pt].[id], [pt].[name]
FROM [Tables].[product_types] AS [pt]
LEFT JOIN [Tables].[products] AS [p]
ON [p].[product_type_id] = [pt].[id]
--<< 3
EXECUTE [Procedures].[insert_product_type] 23, N'Mentos';
SELECT [p].[id], [p].[name], [pt].[id], [pt].[name]
FROM [Tables].[product_types] AS [pt]
LEFT JOIN [Tables].[products] AS [p]
ON [p].[product_type_id] = [pt].[id]
--<< 4
UPDATE [Tables].[products]
SET [product_type_id] = (
SELECT [id]
FROM [Tables].[product_types]
WHERE [name] = N'Mentos'
)
WHERE UPPER([name]) LIKE N'%MENTOS%';
SELECT [p].[id], [p].[name], [pt].[id], [pt].[name]
FROM [Tables].[product_types] AS [pt]
LEFT JOIN [Tables].[products] AS [p]
ON [p].[product_type_id] = [pt].[id]
+13
View File
@@ -0,0 +1,13 @@
USE [Database]
SELECT [p].[name] AS [product_name], [a].[name] AS [attribute_name],
ISNULL(TRY_CONVERT(NVARCHAR(128), TRY_PARSE([pv].[value] AS datetime), 103), [pv].[value])
FROM [Tables].[product_attributes] AS [pa]
JOIN [Tables].[attributes] AS [a]
ON [a].[id] = [pa].[attribute_id]
JOIN [Tables].[products] AS [p]
ON [p].[id] = [pa].[product_id]
LEFT OUTER JOIN [Tables].[product_values] AS [pv]
ON [pv].[product_id] = [pa].[product_id]
AND [pv].[attribute_id] = [pa].[attribute_id]
ORDER BY [p].[id];
+4
View File
@@ -0,0 +1,4 @@
USE [Database]
SELECT [Functions].[does_barcode_exist](1111111111111);
SELECT [Functions].[does_barcode_exist](4444444444444);
@@ -0,0 +1,153 @@
USE [Database]
GO
IF OBJECT_ID(N'[Functions].[product_information]') IS NOT NULL
DROP FUNCTION [Functions].[product_information];
GO
CREATE FUNCTION [Functions].[product_information] ()
RETURNS TABLE
AS
RETURN
SELECT [E].[id], [E].[name] AS [product_name], [T].[name] AS [type_name], [A].[attribute_id], [AN].[name], [V].[value]
FROM [Tables].[products] AS [E]
JOIN [Tables].[product_types] AS [T]
ON [T].[id] = [E].[product_type_id]
JOIN [Tables].[product_attributes] AS [A]
ON [A].[product_id] = [E].[id]
JOIN [Tables].[attributes] AS [AN]
ON [AN].[id] = [A].[attribute_id]
LEFT JOIN [Tables].[product_values] AS [V]
ON [V].[product_id] = [A].[product_id]
AND [V].[attribute_id] = [A].[attribute_id]
GO
IF OBJECT_ID(N'[Functions].[product_inflow_records_information]') IS NOT NULL
DROP FUNCTION [Functions].[product_inflow_records_information];
GO
CREATE FUNCTION [Functions].[product_inflow_records_information] ()
RETURNS TABLE
AS
RETURN
SELECT [pir].[id], [V].[value], [S].[name], [pir].[count], [pir].[full_price], [pir].[datetime]
FROM [Tables].[product_inflow_records] AS [pir]
JOIN [Tables].[product_values] AS [V]
ON [V].[id] = [pir].[barcode_product_value_id]
JOIN [Tables].[suppliers] AS [S]
ON [S].[id] = [pir].supplier_id
GO
--<< 0
SELECT *
FROM [Functions].[product_information]();
--<< 1
DECLARE @first_barcode_attribute_id AS INT,
@second_barcode_attribute_id AS INT;
DECLARE @barcode_attribute_id AS INT;
SET @barcode_attribute_id =
(SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = N'Barcode');
EXECUTE [Procedures].[insert_barcode]
@product_id = 1,
@barcode_attribute_id = @barcode_attribute_id,
@barcode = N'7145621037931',
@inserted_barcode_attribute_id = @first_barcode_attribute_id OUTPUT;
SELECT *
FROM [Functions].[product_information]();
--<< 2
BEGIN TRY
EXECUTE [Procedures].[insert_barcode] 1, @barcode_attribute_id, N'7145621037931';
END TRY
BEGIN CATCH -- The barcode is not unique
PRINT N'The error occurred - the right case'
END CATCH
BEGIN TRY
EXECUTE [Procedures].[insert_barcode] 1, @barcode_attribute_id, N'0289713447192';
END TRY
BEGIN CATCH -- The barcode is already exsist
PRINT N'The error occurred - the right case'
END CATCH
EXECUTE [Procedures].[insert_barcode] 2, @barcode_attribute_id, N'0000000000000', @inserted_barcode_attribute_id = @second_barcode_attribute_id OUTPUT;
EXECUTE [Procedures].[insert_barcode] 3, @barcode_attribute_id;
EXECUTE [Procedures].[insert_barcode] 4, @barcode_attribute_id;
SELECT *
FROM [Functions].[product_information]();
--<< 3
DECLARE
@buffer_date_1 AS DATE,
@buffer_date_2 AS DATE,
@buffer_date_3 AS DATE,
@buffer_date_4 AS DATE,
@buffer_date_5 AS DATE;
SET @buffer_date_1 = CAST(N'1900-01-01' AS DATE);
SET @buffer_date_2 = CAST(N'1900-01-02' AS DATE);
SET @buffer_date_3 = CAST(N'1900-01-03' AS DATE);
SET @buffer_date_4 = CAST(N'1900-01-04' AS DATE);
SET @buffer_date_5 = CAST(N'1900-01-05' AS DATE);
EXECUTE [Procedures].[insert_product_inflow_record]
@barcode_product_value_id = @first_barcode_attribute_id,
@barcode_product_attribute_id = @barcode_attribute_id,
@supplier_id = 1,
@count = 2,
@full_price = 3000,
@datetime = @buffer_date_1;
EXECUTE [Procedures].[insert_product_inflow_record]
@barcode_product_value_id = @second_barcode_attribute_id,
@barcode_product_attribute_id = @barcode_attribute_id,
@supplier_id = 3,
@count = 50,
@full_price = 1600,
@datetime = @buffer_date_2;
SELECT *
FROM [Functions].[product_inflow_records_information]();
--<< 4
BEGIN TRY
EXECUTE [Procedures].[insert_product_outflow_record] @second_barcode_attribute_id, 5, @buffer_date_2;
END TRY
BEGIN CATCH -- Price not specified
PRINT N'The error occurred - the right case'
END CATCH
EXECUTE [Procedures].[insert_price_history_record] 2, @buffer_date_1, 40 -- Æåâàòåëüíàÿ ðåçèíêà Orbit Mega Êëóáíèêà, áåç ñàõàðà, 16,4 ã
EXECUTE [Procedures].[insert_product_outflow_record] @second_barcode_attribute_id, 5, @buffer_date_2;
SELECT [Functions].[product_with_barcode_stock](@barcode_attribute_id, @second_barcode_attribute_id);
EXECUTE [Procedures].[insert_price_history_record] 2, @buffer_date_3, 45 -- Æåâàòåëüíàÿ ðåçèíêà Orbit Mega Êëóáíèêà, áåç ñàõàðà, 16,4 ã
EXECUTE [Procedures].[insert_product_outflow_record] @second_barcode_attribute_id, 10, @buffer_date_4;
SELECT [Functions].[product_with_barcode_stock](@barcode_attribute_id, @second_barcode_attribute_id);
EXECUTE [Procedures].[details_of_product_outflow_records];
--<< 5
EXECUTE [Procedures].[insert_product_write_off_record] @second_barcode_attribute_id, 1, 5, @buffer_date_5; -- Âûøåë ñðîê ãîäíîñòè
SELECT *
FROM [Tables].[product_write_off_records];
SELECT [Functions].[product_with_barcode_stock](@barcode_attribute_id, @second_barcode_attribute_id);
BEGIN TRY
INSERT INTO [Tables].[product_outflow_records]([barcode_product_value_id], [count], [datetime])
VALUES(@first_barcode_attribute_id, 3, @buffer_date_1);
END TRY
BEGIN CATCH
PRINT N'The error occurred - the right case'
END CATCH
DROP FUNCTION [Functions].[product_information];
DROP FUNCTION [Functions].[product_inflow_records_information];
@@ -0,0 +1,12 @@
USE [Database]
IF EXISTS(SELECT OBJECT_ID FROM [sys].[triggers] WHERE name = 'trg_drop_table')
DROP TRIGGER [trg_drop_table] ON DATABASE
GO
CREATE TRIGGER [trg_drop_table]
ON DATABASE FOR DROP_TABLE
AS
SET NOCOUNT ON;
ROLLBACK TRANSACTION;
GO
@@ -0,0 +1,20 @@
USE [Database]
IF OBJECT_ID('[Tables].[trg_insert_product_types]', 'TR') IS NOT NULL
DROP TRIGGER [Tables].[trg_insert_product_types];
GO
CREATE TRIGGER [Tables].[trg_insert_product_types] ON [Tables].[product_types] AFTER INSERT
AS
IF EXISTS(
SELECT *
FROM [inserted] AS [I]
JOIN [Tables].[product_types] AS [PT]
ON [PT].[id] = [I].[parent_id]
JOIN [Tables].[products] AS [p]
ON [p].[product_type_id] = [PT].[id]
)
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
GO
@@ -0,0 +1,34 @@
USE [Database]
IF OBJECT_ID('[Tables].[trg_insert_product_outflow_records]', 'TR') IS NOT NULL
DROP TRIGGER [Tables].[trg_insert_product_outflow_records];
GO
CREATE TRIGGER [Tables].[trg_insert_product_outflow_records] ON [Tables].[product_outflow_records] AFTER INSERT
AS
IF NOT EXISTS(
SELECT *
FROM [inserted] AS [i]
JOIN [Tables].[price_history_records] AS [phr]
ON [phr].[product_id] = [i].[product_id]
)
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
DECLARE @barcode_attribute_id AS INT;
SET @barcode_attribute_id = (
SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = 'Barcode'
);
DECLARE @stocks AS INT;
SET @stocks = (
SELECT [Functions].[product_with_barcode_stock]([barcode])
FROM [inserted]
);
IF @stocks < 0
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
GO
@@ -0,0 +1,22 @@
USE [Database]
IF OBJECT_ID('[Tables].[trg_insert_product_values]', 'TR') IS NOT NULL
DROP TRIGGER [Tables].[trg_insert_product_values];
GO
CREATE TRIGGER [Tables].[trg_insert_product_values] ON [Tables].[product_values] AFTER INSERT
AS
IF EXISTS(
SELECT *
FROM (
SELECT [i].[value], COUNT(*) AS [count]
FROM [Tables].[product_values] AS [PV]
JOIN [inserted] AS [i]
ON [i].[id] = [PV].[product_id]
) AS [values]
WHERE [count] > 1
)
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
GO
@@ -0,0 +1,36 @@
USE [Database]
IF OBJECT_ID('[Tables].[trg_insert_product_outflow_records]', 'TR') IS NOT NULL
DROP TRIGGER [Tables].[trg_insert_product_outflow_records];
GO
CREATE TRIGGER [Tables].[trg_insert_product_outflow_records] ON [Tables].[product_outflow_records] AFTER INSERT
AS
IF NOT EXISTS(
SELECT *
FROM [inserted] AS [i]
JOIN [Tables].[product_values] AS [pv]
ON [pv].[id] = [i].[barcode_product_value_id]
JOIN [Tables].[price_history_records] AS [phr]
ON [phr].[product_id] = [pv].[product_id]
)
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
DECLARE @barcode_attribute_id AS INT;
SET @barcode_attribute_id = (
SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = 'Barcode'
);
DECLARE @stocks AS INT;
SET @stocks = (
SELECT [Functions].[product_with_barcode_stock](@barcode_attribute_id, [barcode_product_value_id])
FROM [inserted]
);
IF @stocks < 0
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
GO
@@ -0,0 +1,25 @@
USE [Database]
IF OBJECT_ID('[Tables].[trg_insert_product_write_off_records]', 'TR') IS NOT NULL
DROP TRIGGER [Tables].[trg_insert_product_write_off_records];
GO
CREATE TRIGGER [Tables].[trg_insert_product_write_off_records] ON [Tables].[product_write_off_records] AFTER INSERT
AS
DECLARE @barcode_attribute_id AS INT;
SET @barcode_attribute_id = (
SELECT [id]
FROM [Tables].[attributes]
WHERE [name] = 'Barcode'
);
DECLARE @stocks AS INT;
SET @stocks = (
SELECT [Functions].[product_with_barcode_stock](@barcode_attribute_id, [barcode_product_value_id])
FROM [inserted]
);
IF @stocks < 0
BEGIN
ROLLBACK TRANSACTION;
RETURN;
END
GO
+40
View File
@@ -0,0 +1,40 @@
USE [Database]
GO
IF NOT EXISTS (
SELECT [name]
FROM [master].[sys].[server_principals]
WHERE [name] = 'user')
BEGIN
CREATE LOGIN [user]
WITH PASSWORD = 'user',
DEFAULT_DATABASE = [Database];
END
GO
IF NOT EXISTS (
SELECT *
FROM [dbo].[sysusers]
WHERE name = N'Casual user')
BEGIN
CREATE USER [Casual user]
FOR LOGIN [user]
WITH DEFAULT_SCHEMA = [Views]
END
GO
GRANT INSERT, SELECT, UPDATE, DELETE
ON SCHEMA :: [Tables]
TO [Casual user];
GRANT EXECUTE, SELECT
ON SCHEMA :: [Functions]
TO [Casual user];
GRANT EXECUTE, SELECT
ON SCHEMA :: [Procedures]
TO [Casual user];
GRANT SELECT, UPDATE
ON SCHEMA :: [Views]
TO [Casual user];