Thread Subject: Using a variable in sql query: WHERE, IN

Subject: Using a variable in sql query: WHERE, IN

From: Vincent

Date: 9 Nov, 2008 18:41:02

Message: 1 of 7

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.

Subject: Using a variable in sql query: WHERE, IN

From: Nick Denman

Date: 10 Nov, 2008 01:48:02

Message: 2 of 7

"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

Subject: Using a variable in sql query: WHERE, IN

From: Vincent

Date: 10 Nov, 2008 16:44:02

Message: 3 of 7

Nick,

Thank you for the suggestion!! Much appreciated! :)

Vince

>
> Hi Vincent
>
> Try the following:
>
> G = '680';
> query = ['SELECT * FROM mytable WHERE id IN (',G,')'];
>
> Hope that helps,
> Nick

Subject: Using a variable in sql query: WHERE, IN

From: pierca

Date: 15 Jan, 2009 10:48:12

Message: 4 of 7

Hi all,
It's my first post here and I don't speak english very well, hope you understand!
I have Vincent's same problem but if I try the following

> num = 'IT001E082';
> e = exec(conn,'SELECT * FROM Mytable WHERE "Pod No_" IN(',num,')' );

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.
Does anyone here have a solution or a suggestion?

Thank you for reading.

Subject: Using a variable in sql query: WHERE, IN

From: Steven Lord

Date: 15 Jan, 2009 14:36:29

Message: 5 of 7


"pierca" <pierca85@gmail.com> wrote in message
news:7302393.1232016523112.JavaMail.jakarta@nitrogen.mathforum.org...
> Hi all,
> It's my first post here and I don't speak english very well, hope you
> understand!
> I have Vincent's same problem but if I try the following
>
>> num = 'IT001E082';
>> e = exec(conn,'SELECT * FROM Mytable WHERE "Pod No_" IN(',num,')' );
>
> 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.
> Does anyone here have a solution or a suggestion?

You need to concatenate the strings.

x = 'world';
disp(['Hello ', x])

--
Steve Lord
slord@mathworks.com

Subject: Using a variable in sql query: WHERE, IN

From: pierca

Date: 15 Jan, 2009 16:06:49

Message: 6 of 7

Thank you very much your suggestion works!!!

Subject: Using a variable in sql query: WHERE, IN

From: Rakesh

Date: 19 Jun, 2009 20:23:02

Message: 7 of 7

Hello,

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 ?

for k = 1:40

exec(connA,'SELECT NAME FROM EMP_NAMES WHERE EMPID IN (',k,')')

Thanks.

Rakesh.

"Steven Lord" <slord@mathworks.com> wrote in message <gknhld$isp$1@fred.mathworks.com>...
>
> "pierca" <pierca85@gmail.com> wrote in message
> news:7302393.1232016523112.JavaMail.jakarta@nitrogen.mathforum.org...
> > Hi all,
> > It's my first post here and I don't speak english very well, hope you
> > understand!
> > I have Vincent's same problem but if I try the following
> >
> >> num = 'IT001E082';
> >> e = exec(conn,'SELECT * FROM Mytable WHERE "Pod No_" IN(',num,')' );
> >
> > 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.
> > Does anyone here have a solution or a suggestion?
>
> You need to concatenate the strings.
>
> x = 'world';
> disp(['Hello ', x])
>
> --
> Steve Lord
> slord@mathworks.com
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
variable Rakesh 19 Jun, 2009 16:24:05
sql Rakesh 19 Jun, 2009 16:24:05
string Nick Denman 9 Nov, 2008 20:50:04
sql Nick Denman 9 Nov, 2008 20:50:04
using variable ... Vincent 9 Nov, 2008 13:45:05
in operator Vincent 9 Nov, 2008 13:45:05
where clause Vincent 9 Nov, 2008 13:45:05
database Vincent 9 Nov, 2008 13:45:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com