Please help me out!! [message #249328] |
Wed, 04 July 2007 01:57 |
fastfreeeasy
Messages: 25 Registered: June 2007
|
Junior Member |
|
|
Oracle scans a table twice for a simple SQL statement like "SELECT emp_code INTO l_emp_code FROM emp" to check if there is a duplicate record and raise TOW_MANY_ROWS exception. What happens when there is an aggregate function in an SQL?
|
|
|
|
|
Re: Please help me out!! [message #249345 is a reply to message #249333] |
Wed, 04 July 2007 02:44 |
fastfreeeasy
Messages: 25 Registered: June 2007
|
Junior Member |
|
|
I am changing my SQL statement like this "SELECT emp_code INTO l_emp_code FROM emp WHERE emp_code = 1234". If this statement is used in a PL/SQL block then will Oracle scans emp table twice? If not then how Oracle raise TWO_MANY_ROWS exception. Believe me I read this fact in Oracle book itself.
|
|
|
|
|
|
|
Re: Please help me out!! [message #249364 is a reply to message #249355] |
Wed, 04 July 2007 03:28 |
fastfreeeasy
Messages: 25 Registered: June 2007
|
Junior Member |
|
|
DECLARE
l_emp_code emp.emp_code%TYPE ;
BEGIN
SELECT emp_code
INTO l_emp_code
FROM emp
WHERE emp_code = 1234 ;
END;
-- Now what will happen if more than 1 record exists in emp table --
-- for emp_code 1234 . Oracle will raise TOW_MANY_ROWS exception --
-- My question is how Oracle raise TOW_MANY_ROWS exception without--
-- scanning 2 twice. Is it possible in single scan? If so what is --
-- mechanism?
|
|
|
|
|
|
|
Re: Please help me out!! [message #249470 is a reply to message #249328] |
Wed, 04 July 2007 10:56 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
ab_trivedi wrote on Wed, 04 July 2007 15:08 | I appreciate the doubt which fastfreeeasy posted.
|
fastfreeeasy wrote on Wed, 04 July 2007 09:44 | Believe me I read this fact in Oracle book itself.
|
I would appreaciate if fastfreeeasy would specify this 'Oracle book' (name, author or at least ISBN).
Maybe this was the behaviour in some historic release (although I do not believe it).
|
|
|
Re: Please help me out!! [message #249638 is a reply to message #249470] |
Thu, 05 July 2007 07:30 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
As I remember, back in the days of 7.3.4, it used to be the case that if you did a SELECT INTO, then Oracle would access the table to get the row specified, and then access it again to see if there were more rows, whereas if you opened an explicit cursor and did a fetch, it would only access the table once.
I believe this changed at release 8.0
|
|
|
Re: Please help me out!! [message #249878 is a reply to message #249638] |
Fri, 06 July 2007 06:56 |
fastfreeeasy
Messages: 25 Registered: June 2007
|
Junior Member |
|
|
Yes, I agree with your comments.
"In very simplistic terms (very simplistic) think of it this way - Oracle does the select statement (select emp_code from emp where emp_code = 1234; - a single 'scan' as so clearly demonstrated) and then it tries to store that in your variable l_emp_code. However there is only one variable, so only room for one value. If the select returns more than one row they can't both fit in the single variable hence the TOO_MANY_ROWS error. Oracle doesn't get one record and then go and see if there are any more, the TOO_MANY_ROWS exception is returned on the SELECT INTO syntax." As per Scorpio Biker. Is this correct concept?
|
|
|
|
|