selecting column names from where clause

  • Hi all

    Is there any way that I can pass the column names in where clause and select them.

    like

    select * from employee

    where column_name in (age,salary)

    this shows me only Age and salary from the Employee table.

    any help on this.

    [font="Comic Sans MS"]
    ---------------------------------------------------

    Thanks [/font]

  • Learner1 (10/19/2010)


    Hi all

    Is there any way that I can pass the column names in where clause and select them.

    like

    select * from employee

    where column_name in (age,salary)

    this shows me only Age and salary from the Employee table.

    any help on this.

    Don't you just want

    select age,salary from employee

    ?

    Or if the list of columns to show must be dynamic (from a variable/parameter), you'll need to use dynamic sql.

  • yes, it is dynamically passed ,the list of column is passed through a parameter

    it is dynamic sql, Can be any column in the table like Age ,Salary, Name, Address, Phone, City.........

    [font="Comic Sans MS"]
    ---------------------------------------------------

    Thanks [/font]

  • Learner1 (10/19/2010)


    yes, it is dynamically passed ,the list of column is passed through a parameter

    it is dynamic sql, Can be any column in the table like Age ,Salary, Name, Address, Phone, City.........

    Ok, the way to do that then is to build your query as a string variable and use either EXEC or sp_executesql (which will let you parameterize it) to run the query in your string. If you search "dynamic sql" on the forums, you'll find some examples that will give you the idea.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply