Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
I have checked various forums , everyone agrees that implcit is better and less prone for error, but this developer insists that explicit is better according to Joakim Treugut ( http://www.linkedin.com/pub/2/2A0/826) and does not want to change - he always use explicit cursor.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
The question should not be which is faster but which is better. The answer depends on what you are doing. Unless you only want one row or you are doing a bulk collect into a collection don't use a select into. If you want every row that a query returns then by all means use an implicit cursor. There is less stuff to type and the cursor will be opened and closed for you. However, if you might exit the for loop early, then you might want an explicit cursor.
I worked on a metadata modeling tool years ago and I replaced dozens of cases of select count(*) into...if count(*) > 1... code with functions that opened a cursor read a single line, closed the cursor of course, then returned either true or false depending on whether or not there was data. I suppose I could have also done a select into with rownum=1. Either way I would have to handle the error if no data was found, which meant that the row did not exist.
Unless you only want one row or you are doing a bulk collect into a collection don't use a select into.
Gandolf, I'm confused by this statement.
If I read right, you are saying for:
SINGLE rows, use select into (implicit cursor)
MULTIPLE rows use select bulk collect into (again, implicit cursor)
What else is there (aside from a cursor that returns no rows)?
Originally Posted by gandolf989
I suppose I could have also done a select into with rownum=1. Either way I would have to handle the error if no data was found, which meant that the row did not exist.
I'm not sure the function based approach is the best here. If I have a table with no rows and issue:
SQL> TRUNCATE TABLE j1;
Table truncated.
SQL> SELECT COUNT(*)
2 FROM j1;
COUNT(*)
----------
0
There is no error handling required. I'm completely open to new ideas, were you simply trying to improve readability, or is there another reason?
Thanks,
J
Bookmarks