Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion how to construct "select in (...)" statements?

Received: by 10.220.201.194 with SMTP id fb2mr2492153vcb.22.1289937195260;
        Tue, 16 Nov 2010 11:53:15 -0800 (PST)
X-BeenThere: pymssql@googlegroups.com
Received: by 10.220.160.2 with SMTP id l2ls762528vcx.4.p; Tue, 16 Nov 2010
 11:53:14 -0800 (PST)
MIME-Version: 1.0
Received: by 10.220.198.131 with SMTP id eo3mr423471vcb.49.1289937194440; Tue,
 16 Nov 2010 11:53:14 -0800 (PST)
Received: by j9g2000vbr.googlegroups.com with HTTP; Tue, 16 Nov 2010 11:53:14
 -0800 (PST)
Date: Tue, 16 Nov 2010 11:53:14 -0800 (PST)
In-Reply-To: <1f48c779-b690-4c64-aabd-26c10f4bb70e@a37g2000yqi.googlegroups.com>
X-IP: 93.145.245.156
References: <1f48c779-b690-4c64-aabd-26c10f4bb70e@a37g2000yqi.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12)
 Gecko/20101027 Firefox/3.6.12,gzip(gfe)
Message-ID: <cba3c8e8-1ceb-4857-9485-c2ba8ca9e5a6@j9g2000vbr.googlegroups.com>
Subject: Re: how to construct "select in (...)" statements?
From: xpaolo <pa...@paolodestefani.it>
To: pymssql <pymssql@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1

If i understand the problem i think you need to construct the tuple of
id and then format an sql statement with python string methods as:
>>> id = (1, 2, 3, 4)
>>> "select * from table where id in {0}".format(str(a))
'select * from table where id in (1, 2, 3, 4)'



On 16 Nov, 11:35, Sven Hergenhahn <sven.hergenh...@googlemail.com>
wrote:
> Hi,
>
> I understand that the usual way to construct sql-Statements within the
> pymssql-Module is to use Python's builtin string interpolation
> operator.
>
> While tzhis is perfectly fine for queries like "select ... where id=
> %d" or "select ... from name=%s", it fails completely if you need a
> query with say IDs like in "select ... where id in (1, 2, 3 )".
>
> Is this the point where I need to do something like
>
> cur.execute( ''.join(['select. ... where ID in (',
> ','.join(list_of_ids)])
>
> ???
>
> Or is there a prefered way of doing it?
>
> Thanks in advance,