Skip to Main Content
  • Questions
  • DBMS_SQL: Why is define_column needed?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Peter.

Asked: February 12, 2016 - 4:55 pm UTC

Last updated: February 14, 2016 - 4:11 am UTC

Version: 11.2.0.4.0

Viewed 1000+ times

You Asked

If I open a cursor and parse a "select * from <view>" statement, I can use the describe_columns procedure to see everything about the columns I will be fetching before I execute it. So why is there a need to use the define_column procedure before using the fetch_rows procedure?

and Connor said...

define_column is all about the *output*, ie, where the data you fetch is going to go.

It could *change* the result.

eg select [datecol] from my_table

- if you define_column with a date variable target, then it comes back as a date.

- if you define_column with a string variable target, then it comes back as different data (ie, a string based on nls settings).

- if you define_column with a number variable target, then it will probably bomb out on fetch.

So you get *different* behaviour depending on the output datatypes, so we need to know that.

(I take your point though -it would be nice to have a 'default' position where all datatypes are expected to align, and just have things crash if they did not).

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks for that last thought

Peter Nosko, February 13, 2016 - 5:34 pm UTC

Connor, thanks for the reply. There may be other reasons unknown to me why using define_column is useful, but it seems unnecessary to make it required. Fortunately, a simple loop lets me define the output from the input.

Using it to change datatypes seems as sloppy to me as writing SQL that relies on implicit datatype conversions. I go out of my way to avoid implicit conversions, especially in WHERE and JOIN clause conditions. Helping the optimizer with thoughtfully-written SQL instead of using hints has worked well for me.

My particular use is a context manager to populate our application context from a view. We're in development mode and the view is growing a bit here and there and this context manager is very generic in handling the columns. Or abstract, like the OO folks like to say. It sure would be nice if context variable could be typed.

On an unrelated note, I'd like to give you a positive shout out for your great KISS series on YouTube. Is this forum a good place to discuss those? The YouTube comments section is a little inadequate.
Connor McDonald
February 14, 2016 - 4:11 am UTC

I tend to agree. I'd love to have a setting where you choose to have Oracle return an error whenever implicit datatype conversions took place, although I guess a whole lot of code, including mine :-) would suddenly break.

As is often the case, convenience leads to laziness.

Thanks for the feedback on the KISS series. If you have questions, probably the best way would be to ask AskTom question with a reference to the video in question. That way the broader community can participate and learn from it.


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library