Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Filling a variable (SELECT INTO vs FOR LOOP)

422557Jun 11 2004 — edited Jun 12 2004
Howdy all. I am a somewhat beginner to Oracle, and I used the OMWB to migrate a MSSQL2K database to Oracle. I have a question about how to fill a variable with the results of a SELECT statement.

In T-SQL, the following is valid ...

----------------------------------

SELECT @ID = p.ID
FROM Persons p
WHERE Name = 'wally'

----------------------------------

This is valid and will not cause a runtime error for any number of rows returned (0 or more).


Now, for the Oracle looking equivalent ...

----------------------------------

SELECT ID
INTO ID
FROM PERSONS p
WHERE NAME = 'wally';

----------------------------------

I understand that this will throw a NO_DATA_FOUND exception if there are no rows in the result set or a TOO_MANY_ROWS exception if there are more than one rows in the result set.


However, the OMWB did the folliwing for all cases where the T-SQL looks like the above ...

----------------------------------

FOR rec IN (SELECT p.ID
FROM PERSONS p
WHERE p.NAME = 'wally')
LOOP
ID := rec.ID;
END LOOP;

----------------------------------


To my understanding, another way to write the same query would be ...

----------------------------------

SELECT ID
INTO ID
FROM PERSONS p
WHERE NAME = 'wally'
AND ROWNUM = 1;

----------------------------------


Ergo, my question. Is the LOOP method of filling the variable the same as the SELECT INTO method with the ROWNUM limiter? I realize that the SELECT INTO method must still handle the NO_DATA_FOUND exception. But, assuming there is at least one row in the result set without the ROWNUM limiter, will it that statement ever throw a NO_DATA_FOUND exception because of the ROWNUM limiter?

Any explanation would be appreciated.
wally
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2004
Added on Jun 11 2004
10 comments
1,294 views