user1718890
user1718890

Reputation: 39

MSSQL/SQL Query condition error

I have this 2 following SQL Queries on my grails controller:

def query1 = sql.rows("select abc from table_one where cond = 1")

The return result from query1 gives me [{abc=5}]

def query2 = sql.rows("select req from table_two where abc = " +query1)

I am having an error trying to run query2 and I have no idea why.

the column name abc exist in both table_one and table_two, I am trying to fetch the value of abc from table_one, whereby cond = 1, and assign it to variable query1.

and with query2, I am trying to get the values of req from table_two, whereby the value of abc in table_two is equals to the value of query1.

any help on this?

Error message from query2:

Message:Unclosed quotation mark after the character string '[abc:5]'.

Upvotes: 0

Views: 190

Answers (2)

paxdiablo
paxdiablo

Reputation: 881403

Why not use something like the following query:

select  table_two.req
  from  table_one,
        table_two
 where  table_one.cond = 1
   and  table_one.abc  = table_two.abc

Joining the tables like that allows you to do it in one query, which is usually both more efficient and more correct (ie, the database table won't change between the two queries).

Upvotes: 1

John Woo
John Woo

Reputation: 263723

You join the table using INNER JOIN

select  b.req
from    table_one a
        INNER JOIN table_two b
            ON a.abc = b.abc
where   a.cond = 1

the following syntax is ANSI SQL-92.

Upvotes: 0

Related Questions