Initial commit
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user