0

I am trying to pull back all the data from my sql statement but I am only able to pull back the first row data in the first put statement. So what I am trying to do is do a for loop through the $data variable. The for loop won't run because it says that the variable is an array.

orasql crsr $sql
orafetch $crsr -datarray data -indexbyname
puts $data(customer)
foreach value $data {
    puts $data(customer)
}

1 Answer 1

1

The problem is when you do foreach value $data; since the data variable is an array, you can't read it as a simple value. To print the keys and values in the array, do this:

foreach {key value} [array get data] {
    puts "$key => $value"
}

The array get command takes the name of an array variable and gets a list of keys and values from it, which can be iterated over with a two-variable foreach. (The parray command does a somewhat more sophisticated version of this; you could use parray data in place of that loop above, but then you'd have no real other route into processing the row.)

You usually ought to know the names of the columns you're getting back from an SQL query so that you don't need to loop over it like that. Also, the order of columns doesn't really matter, but neither does the order of entries in the array. (The order of rows in the result set of the query matters, but orafetch only gets one at a time.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.