<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923</link>
    <title>MATLAB Central Newsreader - Using a variable in sql query: WHERE, IN</title>
    <description>Feed for thread: Using a variable in sql query: WHERE, IN</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 09 Nov 2008 18:41:02 -0500</pubDate>
      <title>Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#609866</link>
      <author>Vincent </author>
      <description>Hey all,&lt;br&gt;
&lt;br&gt;
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. :) &lt;br&gt;
&lt;br&gt;
I have a little issue pertaining to writing the sql for the exec function in the database toolbox. Hopefully someone can help me. &lt;br&gt;
&lt;br&gt;
Using the Visual Query Builder, this statement works:&lt;br&gt;
&lt;br&gt;
SELECT * FROM mytable WHERE id IN (680)&lt;br&gt;
&lt;br&gt;
It also works from a M-file:&lt;br&gt;
&lt;br&gt;
curs=exec(conn,'SELECT * FROM mytable WHERE id IN (680)');&lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
Does anyone here have a solution or perhaps a suggestion? &lt;br&gt;
&lt;br&gt;
Thank you for reading.</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 01:48:02 -0500</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#609900</link>
      <author>Nick Denman</author>
      <description>&quot;Vincent &quot; &amp;lt;vseah@hoodiny.com&amp;gt; wrote in message &amp;lt;gf7aru$aq5$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hey all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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. :) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have a little issue pertaining to writing the sql for the exec function in the database toolbox. Hopefully someone can help me. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Using the Visual Query Builder, this statement works:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; SELECT * FROM mytable WHERE id IN (680)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It also works from a M-file:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; curs=exec(conn,'SELECT * FROM mytable WHERE id IN (680)');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Does anyone here have a solution or perhaps a suggestion? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you for reading.&lt;br&gt;
&lt;br&gt;
Hi Vincent&lt;br&gt;
&lt;br&gt;
Try the following:&lt;br&gt;
&lt;br&gt;
G = '680';&lt;br&gt;
query =  ['SELECT * FROM mytable WHERE id IN (',G,')'];&lt;br&gt;
&lt;br&gt;
Hope that helps,&lt;br&gt;
Nick</description>
    </item>
    <item>
      <pubDate>Mon, 10 Nov 2008 16:44:02 -0500</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#610082</link>
      <author>Vincent </author>
      <description>Nick,&lt;br&gt;
&lt;br&gt;
Thank you for the suggestion!!  Much appreciated! :)&lt;br&gt;
&lt;br&gt;
Vince&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi Vincent&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Try the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; G = '680';&lt;br&gt;
&amp;gt; query =  ['SELECT * FROM mytable WHERE id IN (',G,')'];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope that helps,&lt;br&gt;
&amp;gt; Nick</description>
    </item>
    <item>
      <pubDate>Thu, 15 Jan 2009 10:48:12 -0500</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#621768</link>
      <author>pierca</author>
      <description>Hi all,&lt;br&gt;
It's my first post here and I don't speak english very well, hope you understand!&lt;br&gt;
I have Vincent's same problem but if I try the following &lt;br&gt;
&lt;br&gt;
&amp;gt; num = 'IT001E082';&lt;br&gt;
&amp;gt; e = exec(conn,'SELECT * FROM Mytable WHERE &quot;Pod No_&quot; IN(',num,')' );&lt;br&gt;
&lt;br&gt;
written in a M file Matlab says that there are Too many input arguments I think because it takes G as an argument of exec.&lt;br&gt;
Does anyone here have a solution or a suggestion?&lt;br&gt;
&lt;br&gt;
Thank you for reading.</description>
    </item>
    <item>
      <pubDate>Thu, 15 Jan 2009 14:36:29 -0500</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#621817</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;pierca&quot; &amp;lt;pierca85@gmail.com&amp;gt; wrote in message &lt;br&gt;
news:7302393.1232016523112.JavaMail.jakarta@nitrogen.mathforum.org...&lt;br&gt;
&amp;gt; Hi all,&lt;br&gt;
&amp;gt; It's my first post here and I don't speak english very well, hope you &lt;br&gt;
&amp;gt; understand!&lt;br&gt;
&amp;gt; I have Vincent's same problem but if I try the following&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; num = 'IT001E082';&lt;br&gt;
&amp;gt;&amp;gt; e = exec(conn,'SELECT * FROM Mytable WHERE &quot;Pod No_&quot; IN(',num,')' );&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; written in a M file Matlab says that there are Too many input arguments I &lt;br&gt;
&amp;gt; think because it takes G as an argument of exec.&lt;br&gt;
&amp;gt; Does anyone here have a solution or a suggestion?&lt;br&gt;
&lt;br&gt;
You need to concatenate the strings.&lt;br&gt;
&lt;br&gt;
x = 'world';&lt;br&gt;
disp(['Hello ', x])&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Thu, 15 Jan 2009 16:06:49 -0500</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#621849</link>
      <author>pierca</author>
      <description>Thank you very much your suggestion works!!!</description>
    </item>
    <item>
      <pubDate>Fri, 19 Jun 2009 20:23:02 -0400</pubDate>
      <title>Re: Using a variable in sql query: WHERE, IN</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238923#658887</link>
      <author>Rakesh </author>
      <description>Hello,&lt;br&gt;
&lt;br&gt;
I have a similar problem, but I do not fully understand the solution described below. I am trying to pass a variable from a for loop into the sql query as shown below. But MATLAB gives an error saying too many input arguments. Can you suggest any solutions ?&lt;br&gt;
&lt;br&gt;
for k = 1:40 &lt;br&gt;
&lt;br&gt;
exec(connA,'SELECT NAME FROM EMP_NAMES WHERE EMPID IN (',k,')')&lt;br&gt;
&lt;br&gt;
Thanks.&lt;br&gt;
&lt;br&gt;
Rakesh.&lt;br&gt;
&lt;br&gt;
&quot;Steven Lord&quot; &amp;lt;slord@mathworks.com&amp;gt; wrote in message &amp;lt;gknhld$isp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;pierca&quot; &amp;lt;pierca85@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:7302393.1232016523112.JavaMail.jakarta@nitrogen.mathforum.org...&lt;br&gt;
&amp;gt; &amp;gt; Hi all,&lt;br&gt;
&amp;gt; &amp;gt; It's my first post here and I don't speak english very well, hope you &lt;br&gt;
&amp;gt; &amp;gt; understand!&lt;br&gt;
&amp;gt; &amp;gt; I have Vincent's same problem but if I try the following&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; num = 'IT001E082';&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; e = exec(conn,'SELECT * FROM Mytable WHERE &quot;Pod No_&quot; IN(',num,')' );&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; written in a M file Matlab says that there are Too many input arguments I &lt;br&gt;
&amp;gt; &amp;gt; think because it takes G as an argument of exec.&lt;br&gt;
&amp;gt; &amp;gt; Does anyone here have a solution or a suggestion?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You need to concatenate the strings.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = 'world';&lt;br&gt;
&amp;gt; disp(['Hello ', x])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; Steve Lord&lt;br&gt;
&amp;gt; slord@mathworks.com &lt;br&gt;
&amp;gt; </description>
    </item>
  </channel>
</rss>

