dynamic column name in where clause

  • i,

    i would like to change the value of column names in the where cluse.

    for ex.

    where (case when @Store ='Country' then 'Country'

    when @Store ='State' then 'State'

    when @Store ='City' then 'City'

    when @Store ='Region' then 'Region'

    when @Store ='Store Name' then 'StoreName'

    end)

    = @StoreAttribute

    i want to change the columnnames on runtime

    when i compile this i'm not getting any errors but i'm getting null values.

    how to solve this any idea.

    Thanks,

    Regards

    Viji

  • Try it like this:

    where (case when @Store ='Country' then [Country]

    when @Store ='State' then [State]

    when @Store ='City' then [City]

    when @Store ='Region' then [Region]

    when @Store ='Store Name' then [StoreName]

    end)

    = @StoreAttribute

    In order for this to work, all of the columns must be of compatible data types.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 2 posts - 1 through 1 (of 1 total)

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