I am trying to execute following SQL statement :
USE[Northwind] declare @MainQuery varchar(255), @WhereClause varchar(255) Select @WhereClause = 'where CustomerId =ALFKI' Select @MainQuery ='Select * from Customers '[email protected] execute(@MainQuery)
But getting the error ,
Msg 207, Level 16, State 1, Line 1 Invalid column name 'ALFKI'.
What is wrong ?
ALFKI is being treated as if it is a column name. Your generated sql will look like this:
select * from Customers where CustomerID = ALFKI
What you want is your code to look like this:
select * from Customers where CustomerID = 'ALFKI'
To achieve this, change your where clause generator to this:
Select @WhereClause = 'where CustomerId = ''ALFKI'' '
Select @WhereClause = 'where CustomerId =''ALFKI'''