abi1964 abi1964 - 1 year ago 167
SQL Question

Groovy Sql to fetch all columns

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)

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?


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

Answer Source

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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download