在存储过程中使用临时表

时间:2013-05-21 17:34:41

标签: sql sql-server-2008 devexpress temp-tables

我正在尝试创建然后调用存储过程中的临时表。我想要完成的是将年度数据与季度数据相结合。例如:我有一个包含2013年数据的字段。要获取按年份分组的年度数据,以获取季度数据,我按季度分组。我通常会使用年度视图,并在季度存储过程中调用该视图。我试图这样做的原因是因为我们不能按开发人员使用视图。因此,为了解决这个问题,我使用年度数据创建了一个临时表。(参见下面的代码)当我执行它时,它工作正常,但是当我尝试将存储过程添加到Devexpress时,它不包含任何字段。这是我第一次尝试临时表,所以如果有任何错误或更好的方法来写这个,请告诉我。谢谢你的帮助。

CREATE TABLE #StdEcoYearlyData1(
  CaseCaseId int , 
  EcoYear int, 
  NetInvY  float, 
  NDCashY  float, 
  DiscCashY  float, 
  GrossGasY  float, 
  GrossOilY  float, 
  NetProdTaxY  float, 
  NetOpCostY  float,
  WellCostY  float, 
  NetOilY  float, 
  CoNetRevOilY  float, 
  CoNetRevGasY  float, 
  NetGasY  float, 
  NetLocalTaxY  float, 
  NetFixedCostY  float, 
  NetOtherCostY  float, 
  NetProfitY  float, 
  NetTransCostY  float
)

INSERT INTO #StdEcoYearlyData1 (
  CaseCaseId   , 
  EcoYear  , 
  NetInvY  , 
  NDCashY  , 
  DiscCashY  , 
  GrossGasY  , 
  GrossOilY  , 
  NetProdTaxY  , 
  NetOpCostY  ,
  WellCostY  , 
  NetOilY  , 
  CoNetRevOilY  , 
  CoNetRevGasY  , 
  NetGasY  , 
  NetLocalTaxY  , 
  NetFixedCostY  , 
  NetOtherCostY  , 
  NetProfitY  , 
  NetTransCostY  )
SELECT  rcl.CaseCaseId AS CaseId,  YEAR(rce.ecodate) as EYear,
  SUM(rce.NetInv) AS NetInvY, SUM(rce.NDCash) AS NDCashY, 
  SUM(rce.DiscCash) AS DiscCashY, SUM(rce.GrossGas) AS GrossGasY, 
  SUM(rce.GrossOil) AS GrossOilY, SUM(rce.NetProdTax) AS NetProdTaxY, 
  SUM(rce.NetOpCost) AS NetOpCostY, SUM(rce.WellCost) AS WellCostY, 
  SUM(rce.NetOil) AS NetOilY, SUM(rce.CoNetRevOil) AS CoNetRevOilY, 
  SUM(rce.CoNetRevGas) AS CoNetRevGasY, SUM(rce.NetGas) AS NetGasY, 
  Sum(rce.NetLocalTax) as NetLocalTaxY, Sum(rce.NetFixedCost) as NetFixedCostY, 
  Sum(rce.NetOtherCost) as NetOtherCostY, Sum(rce.NetProfit) as NetProfitY, 
  Sum(rce.NetTransCost) as NetTransCostY
from PhdRpt.RptCaseEco rce
   INNER JOIN PhdRpt.ReportCaseList rcl ON 
     rce.ReportRunCaseId = rcl.ReportRunCaseId
 group by rce.reportruncaseid,rcl.CaseCaseId,year(rce.EcoDate)


 select 
  rcl.CaseCaseId AS CaseId, 
  EcoYear  , 
  DATEPART(qq,rce.EcoDate) as Quarter,
  NetInvY  , 
  NDCashY  , 
  DiscCashY  , 
  GrossGasY  , 
  GrossOilY  , 
  NetProdTaxY  , 
  NetOpCostY  ,
  WellCostY  , 
  NetOilY  , 
  CoNetRevOilY  , 
  CoNetRevGasY  , 
  NetGasY  , 
  NetLocalTaxY  , 
  NetFixedCostY  , 
  NetOtherCostY  , 
  NetProfitY  , 
  NetTransCostY,  YEAR(rce.EcoDate) AS EcoYear, 
  SUM(rce.NetInv) AS NetInvSum, SUM(rce.NDCash) AS NDCashSum, 
  SUM(rce.DiscCash) AS DiscCashSum, SUM(rce.GrossGas) AS GrossGasSum, 
  SUM(rce.GrossOil) AS GrossOilSum, SUM(rce.NetProdTax) AS NetProdTaxSum, 
  SUM(rce.NetOpCost) AS NetOpCostSum, SUM(rce.WellCost) AS WellCostSum, 
  SUM(rce.NetOil) AS NetOilSum, SUM(rce.CoNetRevOil) AS CoNetRevOilSum, 
  SUM(rce.CoNetRevGas) AS CoNetRevGasSum, SUM(rce.NetGas) AS NetGasSum, 
  Sum(rce.NetLocalTax) as NetLocalTaxSum, Sum(rce.NetFixedCost) as NetFixedCostSum, 
  Sum(rce.NetOtherCost) as NetOtherCostSum, Sum(rce.NetProfit) as NetProfitSum, 
  Sum(rce.NetTransCost) as NetTransCostSum
 from PhdRpt.RptCaseEco rce
   INNER JOIN PhdRpt.ReportCaseList rcl ON 
     rce.ReportRunCaseId = rcl.ReportRunCaseId 
   INNER JOIN #StdEcoYearlyData1 sey on
    rce.ReportRunCaseId =  sey.CaseCaseId and 
    year(rce.ecodate) = sey.EcoYear
  group by 
  EcoYear  , 
  NetInvY  , 
  NDCashY  , 
  DiscCashY  , 
  GrossGasY  , 
  GrossOilY  , 
  NetProdTaxY  , 
  NetOpCostY  ,
  WellCostY  , 
  NetOilY  , 
  CoNetRevOilY  , 
  CoNetRevGasY  , 
  NetGasY  , 
  NetLocalTaxY  , 
  NetFixedCostY  , 
  NetOtherCostY  , 
  NetProfitY,
  NetTransCostY, 
  rcl.CaseCaseId,
  YEAR(rce.EcoDate),
  DATEPART(qq,rce.EcoDate)

  order by rcl.CaseCaseId


drop table #StdEcoYearlyData1

This is the result of the code

0 个答案:

没有答案