创建后Sql表变量不存在?

时间:2010-12-06 18:41:47

标签: sql tsql sql-server-2000

我正在运行以下查询,但是收到的错误是我的表变量在最后一个查询中不存在。任何人都可以看到为什么会发生这种情况以及如何纠正?

DECLARE @customers Table(custName varchar(20), custCode varchar(10) )

INSERT INTO @customers
select distinct  JM.CustLongName, JM.CustomerCode
    From Jobmaster JM
    WHERE StartDate > '20091031' 

DECLARE @Oct Table(custCode varchar(10), PromiseAvg decimal(10,4), ShipAvg decimal(10,4) )

--The following statement is run 12 times with the different months of the year
INSERT INTO @Oct
SELECT  JM.CustomerCode
  , isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(PromiseDate, 0)) As Decimal)),0) As PromiseAvg
  , isnull(AVG(Cast(DATEDIFF(dd, ISNULL(startDate, 0), ISNULL(BOL.ShipDate, 0)) As Decimal)),0) As ShipAvg
from jobitems JI
LEFT JOIN jobmaster JM ON JI.Job_Number = JM.JobNumber
LEFT JOIN dbo.NCL_BOLDetails BD ON JM.JobNumber = BD.JobNumber AND JI.Item_Code = BD.ItemNumber
INNER JOIN dbo.NCL_BOLs BOL ON BD.BOLID = BOL.BOLID
WHERE StartDate BETWEEN '20101001' AND '20101031'    
Group By JM.CustomerCode, JM.CustLongName
Order By JM.CustomerCode

/*....copies removed for brevity.....*/

Select [@customers].custCode, [@customers].custName
  , [@Nov].PromiseAvg, [@Nov].ShipDate
  , [@Dec].PromiseAvg, [@Dec].ShipDate
  , [@Jan].PromiseAvg, [@Jan].ShipDate
  , [@Feb].PromiseAvg, [@Feb].ShipDate
  , [@Mar].PromiseAvg, [@Mar].ShipDate
  , [@Apr].PromiseAvg, [@Apr].ShipDate
  , [@May].PromiseAvg, [@May].ShipDate
  , [@Jun].PromiseAvg, [@Jun].ShipDate
  , [@Jul].PromiseAvg, [@Jul].ShipDate
  , [@Aug].PromiseAvg, [@Aug].ShipDate
  , [@Sep].PromiseAvg, [@Sep].ShipDate
  , [@Oct].PromiseAvg, [@Oct].ShipDate
FROM @customers
LEFT JOIN [@Nov] ON  [@customers].custCode = [@Nov].custCode
LEFT JOIN [@Dec] ON  [@customers].custCode = [@Dec].custCode
LEFT JOIN [@Jan] ON  [@customers].custCode = [@Jan].custCode
LEFT JOIN [@Feb] ON  [@customers].custCode = [@Feb].custCode
LEFT JOIN [@Mar] ON  [@customers].custCode = [@Mar].custCode
LEFT JOIN [@Apr] ON  [@customers].custCode = [@Apr].custCode
LEFT JOIN [@May] ON  [@customers].custCode = [@May].custCode
LEFT JOIN [@Jun] ON  [@customers].custCode = [@Jun].custCode
LEFT JOIN [@Jul] ON  [@customers].custCode = [@Jul].custCode
LEFT JOIN [@Aug] ON  [@customers].custCode = [@Aug].custCode
LEFT JOIN [@Sep] ON  [@customers].custCode = [@Sep].custCode
LEFT JOIN [@Oct] ON  [@customers].custCode = [@Oct].custCode

4 个答案:

答案 0 :(得分:2)

尝试这样的事情:

Select c.custCode, c.custName
  , n.PromiseAvg, n.ShipDate
  , d.PromiseAvg, d.ShipDate
  , j.PromiseAvg, j.ShipDate
  , f.PromiseAvg, f.ShipDate
  , m.PromiseAvg, m.ShipDate
  , a.PromiseAvg, a.ShipDate
  , mm.PromiseAvg, mm.ShipDate
  , jj.PromiseAvg, jj.ShipDate
  , jl.PromiseAvg, jl.ShipDate
  , ag.PromiseAvg, ag.ShipDate
  , s.PromiseAvg, s.ShipDate
  , o.PromiseAvg, o.ShipDate
FROM @customers c
LEFT JOIN @Nov n ON  c.custCode = n.custCode
LEFT JOIN @Dec d ON  c.custCode = d.custCode
LEFT JOIN @Jan j ON  c.custCode = j.custCode
LEFT JOIN @Feb f ON  c.custCode = f.custCode
LEFT JOIN @Mar m ON  c.custCode = m.custCode
LEFT JOIN @Apr a ON  c.custCode = a.custCode
LEFT JOIN @May mm ON  c.custCode = mm.custCode
LEFT JOIN @Jun jj ON  c.custCode = jj.custCode
LEFT JOIN @Jul jl ON  c.custCode = jl.custCode
LEFT JOIN @Aug ag ON  c.custCode = ag.custCode
LEFT JOIN @Sep s ON  c.custCode = s.custCode
LEFT JOIN @Oct o ON  c.custCode = o.custCode

答案 1 :(得分:1)

你错过了别名

FROM @customers

应该是

FROM @customers [@customers]

您可能还需要将LEFT JOIN [@Nov]更改为LEFT JOIN @Nov [@Nov]等,除非这些实际上是永久性表格。所以你的查询需要看起来像

...
FROM @customers [@customers]
LEFT JOIN @Nov [@Nov] ON  [@customers].custCode = [@Nov].custCode
LEFT JOIN @Dec [@Dec] ON  [@customers].custCode = [@Dec].custCode
LEFT JOIN @Jan [@Jan] ON  [@customers].custCode = [@Jan].custCode
...

答案 2 :(得分:0)

如果我不得不猜测,我会说你在某处我们看不到GO语句。如果是这样,这将导致之前发生的所有声明变量丢失范围,并且在GO之后不可用。分号可能会产生相同的效果,但我对此并不自信。

答案 3 :(得分:0)

我没有找到定义的Shipdate列。解决方案看起来也可能类似

...
Select c.custCode, c.custName
  , nov.PromiseAvg, nov.ShipDate
  , dec.PromiseAvg, dec.ShipDate
FROM @customers c
LEFT JOIN @Nov nov ON  c.custCode = nov.custCode
LEFT JOIN @Dec dec ON  c.custCode = dec.custCode