This query is working but i need to define table name and column name. My requirement is the query should automatically detects the table and column name.
Your requirement is still unclear, but I like to point out that an SQL query always refers to a fixed of tables and columns. An SQL query as such is never dynamic.
But you can build queries dynamically by means of string manipulation, and possibly by reading metadata tables. String manipulation can be done in any language, including T-SQL, but there are language that are equipped for the task.