Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Declaring temp table produces Msg 1087 "Must declare the table variable"

$
0
0

Hello,

I have acquired a .sql script that creates a temporary table, populates it, and then uses it to populate a second table. It supposedly worked correctly before I received it. But now when I run it, I received an error message.

The script is as follows:

DECLARE @MaxThinning TABLE
  (
  CID int NOT NULL,
  LegConditionId int NOT NULL,
  InspectionID int NOT NULL,
  ThinningAmount numeric(5,2),
  CorrosionPresent bit NOT NULL
  );

  
--Select data into temp max thinning table    
INSERT INTO @MaxThinning
  (
  CID,
  LegConditionId,
  InspectionID,
  ThinningAmount,
  CorrosionPresent
  )
SELECT  dbo.OUS_LEGCOMPONENT.CID,
  dbo.OUS_LEGCOMPONENT.LegConditionId,
  dbo.OUS_LEGCONDITION.InspectionID,
  MAX(dbo.OUS_LEGCOMPONENT.OriginalThickness - dbo.OUS_LEGCOMPONENT.EffectiveThickness) as ThinningAmount,
  MAX(CAST(dbo.OUS_LEGCOMPONENT.CorrosionPresent as int))
FROM  dbo.OUS_LEGCOMPONENT INNER JOIN
  dbo.OUS_LEGCONDITION ON dbo.OUS_LEGCOMPONENT.CID = dbo.OUS_LEGCONDITION.CID
  AND
  dbo.OUS_LEGCOMPONENT.LegConditionID = dbo.OUS_LEGCONDITION.LegConditionID
GROUP BY dbo.OUS_LEGCOMPONENT.CID,
  dbo.OUS_LEGCOMPONENT.LegConditionId,
  dbo.OUS_LEGCONDITION.InspectionID
  ;
GO


IF OBJECT_ID('dbo.RawData_SteelTowers', 'U') IS NOT NULL
    DROP TABLE dbo.RawData_SteelTowers;
GO

--Create the final dataset; finish by joining to temp table to get max values
SELECT  (long list of columns),
  g.CorrosionPresent,
  g.ThinningAmount
INTO  dbo.RawData_SteelTowers
FROM  (list of tables) INNER JOIN,
  @MaxThinning g ON b.LegConditionID = g.LegConditionId
  AND
  b.CID = g.CID
GROUP BY (long list of columns),
   CorrosionPresent,
   ThinningAmount
;
GO

The message I get back is:

(54043 row(s) affected)

Msg 1087, Level 15, State 2, Line 34

Must declare the table variable "@MaxThinning".

My thought is that I AM declaring the table variable "@MaxThinning". But apparently it's not working. What am I missing?

Thanks.


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>