Is there any way to use a field value as part of a SELECT statement?

  • Is there any way to use a field value as part of a SELECT statement?

    For example, if I had Table1

    ID MyColor1 MyColor2 MyModifier

    1 Blue Red OR

    2 Yellow Yellow AND

    3 Red Blue AND

    Is there any way I could use the value of the MyModifier field such that something like

    SELECT * FROM Table1 t

    Where t.MyColor1 & t.MyModifier.Value & MyColor2 = 'Blue'

    becomes for each row...

    ID 1

    SELECT * FROM Table1 t

    Where t.MyColor1 OR MyColor2 = 'Blue'

    ID 2 & 3

    SELECT * FROM Table1 t

    Where t.MyColor1 AND MyColor2 = 'Blue'

    Thanks

  • No.

    You can only do that by composing dynamic sql. :crazy:

    Please have a look at: The curse and blessings of dynamic SQL

    www.sommarskog.se/dynamic_sql.html

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • OK, thanks

  • You can do something like this:

    select *

    from Table1

    where

    (MyModifier = 'or' -- OR statement

    and

    MyColor1 = 'Blue'

    or

    MyColor2 = 'Blue')

    or

    (MyModifier = 'and' -- AND statement

    and

    MyColor1 = 'Blue'

    and

    MyColor2 = 'Blue')

    But it's not going to run as well as a dynamic SQL query. It is, however, much more secure.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

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

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