首页 > 解决方案 > 将表传递给存储过程会出现无效列错误

问题描述

我编写了一个存储过程(usp_Roll_UP),它将被其他过程调用。我收到以下错误消息。我将该数据复制到#temp 表并通过表中的一列进行查询。我使用#Temp 的原因。我需要删除在过程中处理的数据。

我收到以下错误消息:

消息 207,级别 16,状态 1,过程 usp_Roll_UP,第 38 行 [批处理开始行 235]
列名“T_Week”无效。

CREATE TYPE [dbo].[Usr_defined_Table ] AS TABLE(
    [T_Year] [int] NULL,
    [T_Week] [int] NULL,
    [Measure] [varchar](100) NULL,
    [Amount] [numeric](16, 2) NULL
)

Create Procedure usp_Roll_UP
   @AllData Usr_defined_Table READONLY
AS
BEGIN
    SELECT * INTO #Temp FROM @AllData 
    SELECT * FROM @AllData  WHERE [T_Week] =5  -- Works give the records
    SELECT * FROM #Temp WHERE [T_Week] =5  -- give me an error with 

--- More business logic here. as the records are processed, i need to delete ---the data from this #Temp
END

标签: sql-serverstored-procedures

解决方案


我认为你不能保持相同的名字。

SELECT * INTO #AllData FROM @AllData 我从上面的行更改为下面的修复了问题

从@AllData 中选择 * 到 #Temp


推荐阅读