Windows Vista Forums

variable inside of a SQL query
  1. #1


    fixitchris Guest

    variable inside of a SQL query

    $host_ is my variable



    so how can i use $host_ in a LIKE query

    'this doesn't work
    $da = New-Object system.data.sqlclient.sqldataadapter("select * from
    isalogger where [desthost] like '*$('($host_)')*'", $cn2);

    ......eerrr

      My System SpecsSystem Spec

  2. #2


    Rick Guest

    RE: variable inside of a SQL query

    I think the syntax is
    %{$_["$host_"]} INstead of '*$('($host_)')*'", but I didn't try it.


    "fixitchris" wrote:

    > $host_ is my variable
    >
    > so how can i use $host_ in a LIKE query
    >
    > 'this doesn't work
    > $da = New-Object system.data.sqlclient.sqldataadapter("select * from
    > isalogger where [desthost] like '*$('($host_)')*'", $cn2);
    >
    > .....eerrr


      My System SpecsSystem Spec

  3. #3


    fixitchris Guest

    RE: variable inside of a SQL query

    oy.... so simple

    "select * from isalogger where [desthost] like '%$host_%'"

      My System SpecsSystem Spec

  4. #4


    Alex K. Angelopoulos [MVP] Guest

    Re: variable inside of a SQL query

    "fixitchris" <fixitchris@discussions.microsoft.com> wrote in message
    news:59E30274-C68A-4188-9BF1-12017E19F085@microsoft.com...
    > oy.... so simple
    >
    > "select * from isalogger where [desthost] like '%$host_%'"


    If I understand this correctly, you've actually found a way to do this -
    although it looks like what you show would turn into just
    select * from isalogger where [desthost] like '%%'

    If a string value seems to have problems, I just stuff it into a variable
    and then echo it to see how it is actually rendered by PS. In your case, it
    would work out like this assuming that $host_ contains the value
    192.168.1.250:

    PS> $query = "select * from isalogger where [desthost] like '*$host_*'"
    PS> $query
    select * from isalogger where [desthost] like '*192.168.1.250*'

    ====

    Here's where your original statement went wrong, in case you didn't figure
    out the details. You were using this query:
    "select * from isalogger where [desthost] like '*$('($host_)')*'"

    Since the string starts with a doublequote, everything inside it is treated
    as if double-quoted until a closing double-quote is encountered. So
    PowerShell treats everything as literal values up to the first $. When it
    sees a $, it tries to find the nested variable/expression and evaluate it.
    At this point, we've already treated this first bit as a literal string:
    select * from isalogger where [desthost] like '*
    and PowerShell sees the following as a parenthesized term to evaluate:
    $('($host_)')
    In other words, it is trying to evaluate this:
    '($host_)'
    Since that's a single-quoted string, it's just itself, and PowerShell
    returns this as the literal value:
    ($host_)
    You tack on the terminal *' and get:
    select * from isalogger where [desthost] like '*($host_)*'
    as your expression.



      My System SpecsSystem Spec

variable inside of a SQL query problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
manipulating text inside a variable ober PowerShell 15 14 Mar 2010
null variable inside if statement Arlo Armstrong-Kooy PowerShell 3 11 Oct 2009
Novice question: How I can query for a variable scope? OK PowerShell 0 05 Nov 2006
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 22 Sep 2006
Variable expansion inside double quoted string =?Utf-8?B?ZHJlZXNjaGtpbmQ=?= PowerShell 12 06 Sep 2006