MSSQL LIKE and IN statements in ADO problem

Roger Upole rupole at hotmail.com
Wed Jan 18 14:01:26 EST 2006


<ram0812 at hotmail.com> wrote in message news:1137598018.349898.213860 at g43g2000cwa.googlegroups.com...
> Helo guys,
>    I am trying to query the MSSQL DB using ADO.
> I am not able to make the LIKE statement fetch the correct results.
> Can anyone tell me what I need to do to get this working?
> Below is the code snippet:
>
>        import win32com.client
>        const = win32com.client.constants
>
>        #conn = establish SQL connection
>        cmd = win32com.client.Dispatch("ADODB.Command")
>        cmd.ActiveConnection = conn
>
>        name = '@fname'
>        value = "'raj"
>        p=cmd.CreateParameter(name, const.adVarchar, Value=value)
>        cmd.Parameters.Append(p)
>        cmd.CommandText = \
>                "SELECT * FROM tb_name WHERE firstname LIKE @fname"
>        cmd.CommandType = const.adCmdText
>        (rs, dummy) = cmd.Execute()
>        while not rs.EOF:
>            print rs.Fields('firstname').Value
>            rs.MoveNext()
>        rs.Close()
>
> I originally was using the '%?%' symbol but that did not work and now,
> i changed it to @fname but this returns a traceback telling that I need
> to declare @fname.
>
> Also, I have another probelm with using the "IN" SQL statement.
>
> I appreciate your help in advance,
>
> Thank you,
> Raja Raman

The wildcards are part of your input string, so you'd need
value = "%raj%" (looks like there was an extraneous single
quote in the original) and your sql would be
"SELECT * FROM tb_name WHERE firstname LIKE ?"
Named parameters are usually used for calling
stored procedures.  As far as I know, you can't use them
with plain Sql.

        hth
             Roger




----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---



More information about the Python-list mailing list