SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetStock]
-- Add the parameters for the stored procedure
@CatID varchar(2),
@SubCatID varchar(2),
@LocExpr varchar(800)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @FormulaDozen varchar(max)
DECLARE @FormulaLacking varchar(max)
DECLARE @Qry varchar(max)
SET @FormulaDozen = (SELECT TOP 1 FormulaDozen FROM tbSubCategory WHERE SubCatID = @SubCatID AND CatID = @CatID)
SET @FormulaLacking = (SELECT TOP 1 FormulaLacking FROM tbSubCategory WHERE SubCatID = @SubCatID AND CatID = @CatID)
SET @Qry = '
SELECT M.MatName,
SUM(S.Unit1) Unit1,U1.UnitName Unit1_Name,
SUM(S.Unit3) Unit3,U3.UnitName Unit3_Name,
SUM('+@FormulaDozen+') Dozen,
M.MatID,L.LocID,
SUM(
CAST(
Unit3 / nullif(M.QtyOfUsing, 0)
AS DECIMAL(7,2)
)
) Warn,
M.Warn Warn_Month
FROM tbStock S
INNER JOIN tbImportOrder O ON S.TagID = O.TagID
INNER JOIN tbMat M ON O.MatID = M.MatID
INNER JOIN tbSubCategory SC ON M.SubCatID = SC.SubCatID AND M.CatID = SC.CatID
INNER JOIN tbUnit U1 ON U1.UnitID = S.Unit1_ID
INNER JOIN tbUnit U3 ON U3.UnitID = SC.Unit3_ID
INNER JOIN tbLocation L ON S.LocID = L.LocID
WHERE S.STAT = 1 AND M.CatID='''+@CatID+''' AND M.SubCatID = '''+@SubCatID+'''
GROUP BY M.MatName,U1.UnitName,U3.UnitName,M.matID,L.LocID,M.Warn
ORDER BY M.MatName
'
IF OBJECT_ID('tempdb..#MainStock') IS NOT NULL DROP TABLE #MainStock
CREATE TABLE #MainStock (
MatName varchar(20),Unit1 float,Unit1_Name varchar(20),
Unit3 float,Unit3_Name varchar(20),Dozen integer,
MatID varchar(10),LocID varchar(10),Warn float,Warn_Month float
)
INSERT #MainStock
EXECUTE(@Qry)
--NewFunc
IF OBJECT_ID('tempdb..#Qry_OtherStock') IS NOT NULL DROP TABLE #Qry_OtherStock
BEGIN
--QryOtherStock
DECLARE @New_LocExpr varchar(max) = REPLACE(@LocExpr,'LocID','L.LocID')
DECLARE @Qry_OtherStock varchar(max)
SET @Qry_OtherStock ='
SELECT L.LocShort, M.MatID, SUM(S.Unit1) Unit1,U1.UnitName Unit1_Name, Sum(S.Unit3) Unit3,U3.UnitName Unit3_Name
FROM tbStock S
INNER JOIN tbMat M ON S.MatID = M.MatID
INNER JOIN tbLocation L ON S.LocID = L.LocID
INNER JOIN tbSubCategory SC ON M.SubCatID = SC.SubCatID AND M.CatID = SC.CatID
INNER JOIN tbUnit U1 ON U1.UnitID = S.Unit1_ID
INNER JOIN tbUnit U3 ON U3.UnitID = SC.Unit3_ID
'+@New_LocExpr+'
AND S.Stat <> 0
GROUP BY M.MatID,U1.UnitName,U3.UnitName,L.LocShort
'
CREATE TABLE #Qry_OtherStock (
LocShort varchar(20),MatID varchar(20)
,Unit1 float,Unit1_Name varchar(50)
,Unit3 float,Unit3_Name varchar(50)
)
INSERT #Qry_OtherStock
EXECUTE(@Qry_OtherStock)
--Create Vertical Table
DECLARE @LocCol varchar(100) = (
SELECT TOP 1 LocShort =
STUFF((SELECT DISTINCT', ' + LocShort
FROM tbLocation b
--WHERE b.LocID = a.LocID
FOR XML PATH('')), 1, 2, '')
FROM tbLocation a
GROUP BY LocShort
)
IF OBJECT_ID('tempdb..#Pivot_OtherStock') IS NOT NULL DROP TABLE #Pivot_OtherStock
EXECUTE ('SELECT * INTO #Pivot_OtherStock FROM (SELECT MatID,Unit1,LocShort FROM #Qry_OtherStock) AS SRC
PIVOT(Sum(Unit1) for LocShort in ('+@locCol+')) AS PV
SELECT MS.MatName,
SUM(Unit1) Unit1,MS.Unit1_Name,
SUM(Unit3) Unit3,MS.Unit3_Name,
SUM(Dozen) Dozen,
MS.MatID,SUM(Warn) Warn,''เดือน'' AS Warn_Name,
MS.Warn_Month
,PV.JL
,PV.KIWI
,PV.JLK,MS.Unit1_Name AS '' ''
FROM #MainStock MS
INNER JOIN #Pivot_OtherStock PV ON MS.MatID = PV.MatID
'+@Locexpr+'
GROUP By MS.MatName,MS.Unit1_Name,MS.Unit3_Name,MS.MatID,MS.Warn_Month,PV.JL,PV.KIWI,PV.JLK
')
END --end-NewFunc
END