Friday, March 9, 2012

Invalid Column Name

Hi the following SP that causes an error.

CREATE PROCEDURE GetInfo
(
@.MinPriceint=0,
@.MaxPriceint=9999999999,
@.TypeHomenvarchar(50)=NULL,
@.Locationnvarchar(100)=NULL

)
AS

Declare @.strSql nvarchar(255)
Set @.strSql="Select * from table WHERE "
Set @.strSql=@.strSql + 'Price BETWEEN ' + CONVERT(nvarchar(20),@.MinPrice) + ' and ' + CONVERT(nvarchar(20),@.MaxPrice )

If @.TypeHome != "No Preference"
Set @.strSql=@.strSql + ' and Type = ''' + @.TypeHome+ ''''

If @.Location != "No Preference"
Set @.strSql=@.strSql + ' and City = ''' + @.Location+ ''''

Set @.strSql=@.strSql + ' and IDX = ''Y'' ORDER BY Price'
Exec(@.strSql)
GO

The Error I get is:
"Error 207: Invalide Column Name 'Select * from table WHERE'
Invalid Column Name 'No Preference'
Invalid Column Name 'No Preference'

I have checked the table and the columns do exist, spelled correctly and caps are all the same. Also, this same SP in another table works just fine.

What is causing this error?

Thanks in advance!change your double quotes to single quotes when you assign a string to a variable.
sample : you should say set @.var = 'value' and not set @.var= "value "

hth

No comments:

Post a Comment