1

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.

6
  • 1
    You describe @table? Where is this in your query? Commented Mar 15, 2018 at 6:39
  • 1
    what is Form ? is this your table name ? Commented Mar 15, 2018 at 6:41
  • 1
    Did you check if you have a table by name Form? Commented Mar 15, 2018 at 6:41
  • First of all, I am sure I do have a table that is called Form because 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. Commented Mar 15, 2018 at 6:47
  • 1
    do a PRINT @SQL, copy the result and paste into a new query window and execute. Any error ? Commented Mar 15, 2018 at 7:01

2 Answers 2

3

Try below query and if you still get error check basic things first.

DECLARE @SQL varchar(MAX)
SET @SQL = 'select [City],[Place] from [Form] where [Age] =  20'
EXEC (@SQL)
  1. Is table [Form] present in database ?
  2. Are you running in correct DB?
  3. I see ":" after AGE is that in col name ?
  4. If Age is varchar try to add "'" before and after 20.
Sign up to request clarification or add additional context in comments.

1 Comment

It is solved sir, thank you. I should have checked the syntax better since there is no correction system in Dynamic Programming because we write queries in quotation marks. The database I have imported was called 'Form', not Form (as also @Nisarg has warned me) . The rest of query was written true.
1

You describe @table. Table is a reserved keyword, so it's a good practice not to use that word. Lets assume @mytable is a variable containing the table name for the query. In that case, you concatenate like you do with the rest of the string. You also need to be connected to the database you are trying to query from.

For example:

DECLARE @SQL varchar(MAX)
DECLARE @mytable varchar(1000) = 'Form' --put the table name here

SET @SQL = 'select [City], [Place] '
         + 'from ' + @mytable + ' where [Age:] =  20'

EXEC (@SQL)

2 Comments

even though table is a reserved word, there is nothing wrong using variable @table at all
True. But if you declare a variable in this manner it will highlight @table with the reserved keyword color in stackoverflow. Also, you can use reserved keywords with brackets... example CREATE [TABLE] TABLE (...), which is valid- but any DBA is going to tell you not to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.