views:

52

answers:

1

I'm trying to make an in statement with values coming from DB and then use them in another query. DB is Oracle

Example:

I've been beating my head around this for quite some time now. I need some help:

Groovy Code:

def myList = []
def myQuery = "select USER_ID from USER_TABLE where rownum < 3"
println myQuery
sql_dw.eachRow(myQuery) {
    myList.add("'"+it.USER_ID+"'")
}
println myList
 myQuery = "select * from USER_TABLE where USER_ID in (${myList.join(",")})"
 println myQuery
def myRow =     sql_dw.firstRow(myQuery);
if (myRow == null)
    println "OMG its null!!"

Output is:

select USER_ID from eiv.USER_TABLE where rownum < 3
['5xsubmit', 'A10165']
select * from USER_TABLE where USER_ID in ('5xsubmit','A10165')
OMG its null!!

I don't know what could be causing this!!

Everything works fine if I populate myList with hardcoded values like

def myList = ["'5xsubmit'", "'A10165'"]

Also, copy pasting the query returns the result in the DB!!

Update

After googling, found this link So turns out we have to use prepared statements. Will investigate how to change code accordingly.

A: 

Does it work if you change the line to:

  myQuery = "select * from USER_TABLE where USER_ID in (${myList.join(",")})" as String

This should stop the groovy SQL being clever and trying to convert your GString to a PreparedStatement (which as you point out, doesn't work)

tim_yates