Question

I am using Groovy Sql to fetch results. This is the output from my Linux box.

%isql -U abc -P abc -S support
1> sp_configure 'number of open partitions'
2> go
 Parameter Name                 Default     Memory Used Config Value    Run Value    Unit                 Type
 ------------------------------ ----------- ----------- ------------  ------------ -------------------- ----------
 number of open partitions              500        5201         5000          5000 number               dynamic

(1 row affected)
(return status = 0)
1>

I am using groovy code

def sql = Sql.newInstance("jdbc:abc:sybase://harley:6011;DatabaseName=support;",dbuname,dbpassword,Driver)
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
        println row.run_value
    }

but it gives me

Exception in thread "main" java.sql.SQLSyntaxErrorException: [abc][Sybase JDBC Driver]Invalid column name: run_value

So it says it cannot get the speciied columns, is there a way it can fetch the result and display?

Update

I used the below code

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
            println row
    }

and it gives me

[Parameter Name:number of open partitions     , Default:        500, Memory Used:       5201, Config Value:        5000, Run Value:        5000, Unit:number              , Type:dynamic   ]

How can I get Run Value? (since it has a space in it)

row.Run Value will not work for sure

Was it helpful?

Solution

Does this work? Querying the meta-data for the column name and getting the value by index:

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  (1..row.getMetaData().columnCount).each {
    println "${row.metaData().getColumnName( it )} = ${row[ it ]}"
  }
}

Or, to get the column directly

row.'Run Value'

OTHER TIPS

Another alternative is getAt(int). In your case, this will return the 'Run Value':

row.getAt(-3)

Additioannly to @tim_yates's answer I'd like to note that row has toRowResult() method. It allows to get a row simply as a Map. No need to call getMetadata() if we need only column names or table width.

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  row.toRowResult().each(){ colName, value ->
    println "${colName} = ${value}"
  }
}

This code doing absolutely the same but looks much more clear

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top