简体   繁体   中英

Invalid Column name for a Temporary table column in a stored procedure

I have a stored procedure inside of which a temporary table is created:

I get an error that says:

Invalid column name 'ValFromUser'.

Why is that? Why do I get this error only for ValFromuser but no other column?How I may get rid of this?

CREATE Procedure [dbo].[OutputProcedure]

--declarations here 
AS
BEGIN
SET NOCOUNT ON 

IF OBJECT_ID('tempdb.dbo.##Temp2') Is null
Begin
create table ##Temp2
(
Rownumber int not null,
ValFromUser nvarchar(30),
Percentage decimal(18, 4) not null
);

select * 
from dbo.ResultsStored
join (
    select 
        Rownumber,
        (SUM(Percentage) / @cnt) as Perc 
    from ##Temp2 
    GROUP BY 
        Rownumber, ValFromUser
) t -- Invalid column Name ValFromUser error here
    on dbo.ResultsStored.RowId = t.Rownumber
    and dbo.ResultsStored.HashedKey = HASHBYTES('MD5', @StringConcat)
end

End



Insert into dbo.ResultsStored( searchSerial,FinalSearchSeral, StringSearched, RowId,PercentMatch, HashedKey) 
select @searchNumber, @searchNumber, dbo.encrypt(@StringConcat), RowNumber, (SUM(Percentage)/@cnt) as Percentage , HASHBYTES('MD5', @StringConcat)
FROM ##Temp2 GROUP BY RowNumber, ValFromUser --Here

end

1: Try a selecting ##Temp2 table and check whether you are getting the ValFromUser column displayed. If not drop the ##Temp table and execute your above statements again.

2: Remove the Comma (,) after the last column from " Percentage decimal(18, 4) not null, " The

create table ##Temp2
(
Rownumber int not null,
ValFromUser nvarchar(30),
--ColumnName nvarchar(30),
--ValFromFunc decimal(18, 4),
--FuncWeight decimal(18, 4),
Percentage decimal(18, 4) not null
);

3: Include ValFromUser Column in the select statement

Select Rownumber, ValFromUser, (SUM(Percentage) / @cnt) as Perc 
FROM ##Temp2 
GROUP BY Rownumber, ValFromUser

Try the above steps and let me know whether it solves your problem or not ?

At the last line of create table statement. Remove comma(,) and reply your result again.

create table ##Temp2
(
Rownumber int not null,
ValFromUser nvarchar(30),
--ColumnName nvarchar(30),
--ValFromFunc decimal(18, 4),
--FuncWeight decimal(18, 4),
Percentage decimal(18, 4) not null
);

I also encountered this. I had a temp table in my sp that was getting the error:

Invalid column name <column name> 

I was only running the stored procedure when I am encountering this. However, the stored procedure is run on a query window where I was also testing the code (for another bug) where I declared the temp table but without the the erroring fields specified (for brevity).

When I renamed the sp's temp table, the problem disappeared. So, I am guessing that the sp is calling this temp table instead of the one that I specified in the sp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM