Good morning. More XML questions, of course. I'm using the following method to perform Oracle DB queries and assign query results to a variable name. The variable name, DB attribute, DB table, and an amplifying "where" string are contained in a list that the method takes as an argument. Most of the queries are simple, but 2 require use of the "where" string to restrict results, and I'm getting DB errors when using the "where". Maybe this is a syntax issue? I'm not sure when to use Sql.expand and when to just use $variable when feeding variables to a method that constructs a SQL query... I've learned to always use Sql.expand for tables and attributes, but for variable values, I get errors either way. The following code gives me "ORA-00920: invalid relational operator" errors, and using Sql.expand for the survey and where variables results in "ORA-00904: invalid identifier". Thanks!
Method:
def query(list)
//input: sql_queries list [[variable_name, table, attribute, "where" string], etc.]
//output: none.
//function: conducts sql query, saves result in variable to update dynamic xml values
{
list.each{
variable = it[0]
table = it[1]
attribute = it[2]
if ( it.size == 4 )
{
where = it[3]
try{
db.eachRow("select count(${Sql.expand attribute}) from hydro.${Sql.expand table} where survey = $survey and $where")
{
if (it[0] == 0 )
{
binding."$variable" = "Unknown"
}else{
db.eachRow("select ${Sql.expand attribute} from hydro.${Sql.expand table} where survey = $survey and $where")
{
binding."$variable" = it[0] ?: "Unknown"
}
}
}
}catch(e){
println "--SQL query failed: select $attribute from hydro.$table where survey = $survey and $where"
log.append("SQL query failed: select $attribute from hydro.$table where survey = $survey and $where\n")
log.append("$e\n")
err_bit = 1
}
}else{
try{
db.eachRow("select count(${Sql.expand attribute}) from hydro.${Sql.expand table} where survey = $survey")
{
if (it[0] == 0 )
{
binding."$variable" = "Unknown"
}else{
db.eachRow("select ${Sql.expand attribute} from hydro.${Sql.expand table} where survey = $survey")
{
binding."$variable" = it[0] ?: "Unknown"
}
}
}
}catch(e){
println "--SQL query failed: select $attribute from hydro.$table where survey = $survey"
log.append("SQL query failed: select $attribute from hydro.$table where survey = $survey\n")
log.append("$e\n")
err_bit = 1
}
}
}
}