I am using dynamic SQL on SQL Server 2008 to select specified columns from a row but I keep getting the following error:
Invalid object name 'Form'
My code is as follows:
DECLARE @SQL varchar(MAX)
SET @SQL = 'select
[City],[Place]'
+
'
from Form where [Age:] = 20'
EXEC (@SQL)
I also tried using + QUOTENAME(@Table)
and declared@Table
as nvarchar(MAX)
but could not define that @Table
is basically the Form
table in my database.
As I checked the previous examples, people were able to select columns from tables the same way without getting errors, so what could be the reason for the error I get? Should I use @QUOTENAME
function at all?
Help will be appreciated.