0

저장 프로 시저에 도움이 필요합니다. 데이터베이스와 관련하여 전적으로 멍청한 사람입니다. 나는 클라이언트가 고정시킬 필요가있는 프로그램을 가지고있다. 그것은 다른 개발자가 클라이언트가 연락을 취하지 못하게 만든 프로그램이다.SQL Server 2000에서 실행되는 저장 프로 시저의 리버스 엔지니어링

이 고객은이 프로그램을 소유하고 있으며 내가 좋아하는 모든 것을 할 수있는 완전한 권리를 부여 받았습니다. 문제는이 프로그램이 Crystal Reports를 사용하고 있으며 문제를 일으키는 특정 보고서가 StockAndSales입니다. 이 보고서는 저장 프로 시저에서 채워집니다. 그러나 문제는 데이터베이스의 모든 테이블을 비운 후에도 보고서에 여전히 존재하지 않아야하는 일부 필드가 표시된다는 것입니다.

프로그램은 .net 기반 응용 프로그램입니다.

그리고이 멍청한 게시물에 대해 유감스럽게 생각하지만 여기서는 새로운 것입니다.

P.S이 절차는 저에 의해 작성되지 않았습니다!

절차는 다음과 같습니다

CREATE PROCEDURE GetStockAndSales 
    @StartDate Varchar(10), 
    @EndDate Varchar(10) 
As 
Begin 

SELECT  T1.Item, dbo.Items.Name, T2. NetPPrice, SUM(T1.O) AS O, SUM(T1.P1) AS P1,  SUM(T1.P2) AS P2, SUM(T1.P3) AS P3, SUM(T1.S1) AS S1, SUM(T1.S2) AS S2, SUM(T1.S3) AS S3,SUM(T1.SR1) AS SR1, SUM(T1.SR2) AS SR2, SUM(T1.SR3) AS SR3, SUM(T1.D) AS D 
FROM   (SELECT  Item, SUM(Q) AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   (SELECT  PD.Item, SUM(PD.Quantity) AS Q 
      FROM   Purchase P INNER JOIN 
            PurchaseDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] < @StartDate) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, - (SUM(PD.Quantity)) AS Q 
      FROM   Sales P INNER JOIN 
           SalesDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] < @StartDate) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, SUM(PD.Quantity) AS Q 
      FROM   Sales P INNER JOIN 
           SalesReturns PD ON P.Invoice = PD.Invoice 
      WHERE  (PD.[Date] < @StartDate) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  Item, - (SUM(Quantity)) AS Q 
      FROM   Damage D 
      WHERE  ([Date] < @StartDate) 
      GROUP BY Item) T 
      GROUP BY Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, SUM(PD.Quantity) AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Purchase P INNER JOIN 
           dbo.PurchaseDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 1) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, 0 AS P1, SUM(PD.Quantity) AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Purchase P INNER JOIN 
           dbo.PurchaseDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 2) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, 0 AS P1, 0 AS P2, SUM(PD.Quantity) AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Purchase P INNER JOIN 
           dbo.PurchaseDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 3) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, SUM(PD.Quantity) AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Sales P INNER JOIN 
           dbo.SalesDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 1) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, SUM(PD.Quantity) AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Sales P INNER JOIN 
           dbo.SalesDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 2) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  PD.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, SUM(PD.Quantity) AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.Sales P INNER JOIN 
           dbo.SalesDetails PD ON P.Invoice = PD.Invoice 
      WHERE  (P.[Date] BETWEEN @StartDate And @EndDate) AND (P.Company = 3) 
      GROUP BY PD.Item 
      UNION ALL 
      SELECT  dbo.SalesReturns.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, SUM(dbo.SalesReturns.Quantity) AS SR1, 0 AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.SalesReturns INNER JOIN 
           dbo.Sales ON dbo.SalesReturns.Invoice = dbo.Sales.Invoice 
      WHERE  (dbo.SalesReturns.[Date] BETWEEN @StartDate And @EndDate) AND (dbo.Sales.Company = 1) 
      GROUP BY dbo.SalesReturns.Item 
      UNION ALL 
      SELECT  dbo.SalesReturns.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, SUM(dbo.SalesReturns.Quantity) AS SR2, 0 AS SR3, 0 AS D 
      FROM   dbo.SalesReturns INNER JOIN 
           dbo.Sales ON dbo.SalesReturns.Invoice = dbo.Sales.Invoice 
      WHERE  (dbo.SalesReturns.[Date] BETWEEN @StartDate And @EndDate) AND (dbo.Sales.Company = 2) 
      GROUP BY dbo.SalesReturns.Item 
      UNION ALL 
      SELECT  dbo.SalesReturns.Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, SUM(dbo.SalesReturns.Quantity) AS SR3, 0 AS D 
      FROM   dbo.SalesReturns INNER JOIN 
           dbo.Sales ON dbo.SalesReturns.Invoice = dbo.Sales.Invoice 
      WHERE  (dbo.SalesReturns.[Date] BETWEEN @StartDate And @EndDate) AND (dbo.Sales.Company = 3) 
      GROUP BY dbo.SalesReturns.Item 
      UNION ALL 
      SELECT  Item, 0 AS O, 0 AS P1, 0 AS P2, 0 AS P3, 0 AS S1, 0 AS S2, 0 AS S3, 0 AS SR1, 0 AS SR2, 0 AS SR3, SUM(Quantity) AS D 
      FROM   dbo.Damage 
      WHERE  ([Date] BETWEEN @StartDate And @EndDate) 
      GROUP BY Item) T1 INNER JOIN 
      dbo.Items ON T1.Item = dbo.Items.Code INNER JOIN (SELECT  Code, Name, SUM(CASE WHEN NETPPRICE <> 0 THEN NETPPRICE ELSE 0 END) AS NetPPrice, 
      SUM(CASE WHEN NETSPRICE <> 0 THEN NETSPRICE ELSE 0 END) AS NetSPrice, SUM(CASE WHEN Diff <> 0 THEN Diff ELSE 0 END) 
      AS Difference 
      FROM   (SELECT  TOP 100 PERCENT dbo.Items.Code, dbo.Items.Name, V1.NetPPrice, V2.NetSPrice, ROUND(V2.NetSPrice - V1.NetPPrice, 2) AS Diff 
      FROM   dbo.Items LEFT OUTER JOIN 
      (SELECT  Item, ROUND(NetPPrice, 2) AS NetPPrice 
      FROM   (SELECT  Item, AVG([Value]/Units) AS NetPPrice 
      FROM   (SELECT  dbo.PurchaseDetails.Item, SUM(dbo.PurchaseDetails.Quantity * dbo.PurchaseDetails.Price) AS Value, 
     SUM(dbo.PurchaseDetails.Quantity) AS Units 
      FROM   dbo.Purchase INNER JOIN 
     dbo.PurchaseDetails ON dbo.Purchase.Invoice = dbo.PurchaseDetails.Invoice 
      GROUP BY dbo.Purchase.[Date], dbo.PurchaseDetails.Item 
      HAVING  (dbo.Purchase.[Date] <= GETDATE()) AND SUM(dbo.PurchaseDetails.Quantity)<>0) T 
      GROUP BY Item) T1) V1 ON dbo.Items.Code = V1.Item LEFT OUTER JOIN 
      (SELECT  Item, ROUND(NetSPrice, 2) AS NetSPrice 
      FROM   (SELECT  Item, AVG([Value]/Units) AS NetSPrice 
      FROM   (SELECT  dbo.SalesDetails.Item, 
     SUM(dbo.SalesDetails.Quantity * (dbo.SalesDetails.Price - dbo.SalesDetails.Discount)) AS Value, 
     SUM(dbo.SalesDetails.Quantity) AS Units 
      FROM   dbo.Sales INNER JOIN 
     dbo.SalesDetails ON dbo.Sales.Invoice = dbo.SalesDetails.Invoice 
      GROUP BY dbo.Sales.[Date], dbo.SalesDetails.Item 
            HAVING  (dbo.Sales.[Date] <= GETDATE())) T 
      GROUP BY Item) T1) V2 ON dbo.Items.Code = V2.Item) G 
      GROUP BY Code, Name 
     ) T2 ON T1.Item = T2.Code 
      GROUP BY T1.Item, dbo.Items.Name, T2. NetPPrice 
      ORDER BY dbo.Items.Name 
End 
GO 
+0

덕분에 많은 appretiated 및 지적 편집 @marc_s. – MoniXx

답변

0

당신은 필드 저장 프로 시저에서 반환되는 계산했다. 첫 번째 SELECT 문에서 모든 SUM() 필드

SUM(T1.O) AS O, SUM(T1.P1) AS P1,  SUM(T1.P2) AS P2, SUM(T1.P3) AS P3,....... 
+0

이 'T1'및 'P2'는 무엇입니까? – MoniXx

+0

내부 하위 쿼리의 별칭 이름입니다. –