Trincula
Trincula

Reputation: 59

I get an error "Invalid Column Name" even when all the columns are present in the table

I have created a #table inside a stored procedure using a select * into statement. The table is shown fine, but when I start using an if statement, it says "Invalid Column Name". Why is this happening, the columns are all available in the table I created.

Can anyone tell me whats the issue ?

I have tried using select * from #table, all the columns and its values are present.

I have also tried update #Table and change several values. It works fine till here.

Only when I use the if statement, its showing the error,

UPDATE #SMSB_temp
SET Status = '09'
WHERE RenewStatus = '03'

IF NOT EXISTS (SELECT * FROM #SMSB_temp)
BEGIN
    IF Status = '09' AND AvailableBalance > @SMSBRenewalCharges
    BEGIN

    END
END

Upvotes: 0

Views: 515

Answers (1)

Venkataraman R
Venkataraman R

Reputation: 12959

You have to write IF statement like below. Your code is currently not referring to any table in the IF statement. I hope you want to check if there is a row with Status = '09' and AvailableBalance > @SMSBRenewalCharges.

DECLARE @Check bit

SET @check = (SELECT TOP 1 1
              FROM #SMSB_temp 
              WHERE Status = '09' AND AvailableBalance > @SMSBRenewalCharges)

IF @check = 1
BEGIN
   ...
END 

As @squirrel suggested in the comments, you can simply write as below:

IF EXISTS ( SELECT TOP 1 1
              FROM #SMSB_temp 
              WHERE Status = '09' AND AvailableBalance > @SMSBRenewalCharges)
BEGIN
 ...
END

Upvotes: 1

Related Questions