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.
Form? is this your table name ?Form?Formbecause I can do basic operations on it. And I have also removed @Table declaration because I could not set @Table to my Form table in database.PRINT @SQL, copy the result and paste into a new query window and execute. Any error ?