+2  A: 

I'm not to familiar with MS-SQL, but I don't think that parameter markers like this

SELECT @pthis FROM @tables

work.

Parameters are normally be used instead of literal values, not instead of identifiers.

inflagranti
That's correct.
Locksfree
"literal values, not instead of identifiers." okay, will you please describe what you mean by literal and identifiers, its a querry, where does this term come in. donot understand
@user287745 He means you can't put a table name in a variable and run the query. SQL won't let you.
Meff
yes i understand that, thanks
+6  A: 

SQL Server will definately not allow the use of a variable as a table name. As for your column name variable, it will return the number of results that the table has, but it will output TABLE_SCHEMA.

Ardman
well i need a wa to get around it and also to use the parameters method to aviod injection, so please guide. and why would it display number of results!, i am asking it to select table_schema!
You are creating a new column when you use a variable, this is why it returns all of the results from the table. Try it yourself:DECLARE @table AS VARCHAR(20);SET @table = 'TABLE_SCHEMA';SELECT * FROM INFORMATION_SCHEMA.TABLES;
Ardman
o, so the above is not possible, what do you answerers... suggest i should do, i dont want to cancatenate string and variable as that will not allow me to check/enforce the data type. i have to provide the user the option with which table do you want to select and also save coding
Not a great solution, but you could do it by building up the SQL string as you were before.
Ardman
okay, please suggest a good solution. thanks
One way could be to have a const set of all the variations of the SELECT command that can be done, then use a SWITCH statement in your code to choose the correct statement.
Ardman