Path: news.mathworks.com!not-for-mail
From: "Nick Denman" <ngdenmanNOSPAM@gmail.REMOVETHIS.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Using a variable in sql query: WHERE, IN
Date: Mon, 10 Nov 2008 01:48:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 32
Message-ID: <gf83si$4oq$1@fred.mathworks.com>
References: <gf7aru$aq5$1@fred.mathworks.com>
Reply-To: "Nick Denman" <ngdenmanNOSPAM@gmail.REMOVETHIS.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226281682 4890 172.30.248.37 (10 Nov 2008 01:48:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 10 Nov 2008 01:48:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 745609
Xref: news.mathworks.com comp.soft-sys.matlab:499824


"Vincent " <vseah@hoodiny.com> wrote in message <gf7aru$aq5$1@fred.mathworks.com>...
> Hey all,
> 
> My first post here. How is everyone doing? Just want to say that this is a splendid resource for finding solutions to questions. I can't be more grateful. :) 
> 
> I have a little issue pertaining to writing the sql for the exec function in the database toolbox. Hopefully someone can help me. 
> 
> Using the Visual Query Builder, this statement works:
> 
> SELECT * FROM mytable WHERE id IN (680)
> 
> It also works from a M-file:
> 
> curs=exec(conn,'SELECT * FROM mytable WHERE id IN (680)');
> 
> Now my question is I want 680 to be a variable, eg, G, instead of a constant. Under the Database manual, 7-37 to 7-38, there is a short explanation on how to do that with the WHERE clause using square brackets and 2 pairs of quotes. 
> 
> I tried doing that with the IN operator but the M-file produces a Unexpected Matlab Expression. I figure that the pair of round brackets that guard the variable is the culprit. But not sure how so. 
> 
> Does anyone here have a solution or perhaps a suggestion? 
> 
> Thank you for reading.

Hi Vincent

Try the following:

G = '680';
query =  ['SELECT * FROM mytable WHERE id IN (',G,')'];

Hope that helps,
Nick