Home » SQL & PL/SQL » SQL & PL/SQL » Try this code
Try this code [message #210597] Thu, 21 December 2006 08:21 Go to next message
Splinter2010
Messages: 20
Registered: July 2006
Location: EGYPT
Junior Member

this code make the pl/sql hang ....
i think in programming logic is allright .. but i missunderstand something please help me on this .. here is the code

declare
v_counter NUMBER := 0;
v_total NUMBER := 0;
BEGIN
WHILE v_counter >= 0 LOOP
v_counter := &count;
v_total := v_total + v_counter ;
END LOOP;
DBMS_OUTPUT.PUT_LINE (v_total);
END;
/
Re: Try this code [message #210602 is a reply to message #210597] Thu, 21 December 2006 08:39 Go to previous messageGo to next message
ebrian
Messages: 2794
Registered: April 2006
Senior Member
If you enter a non-negative number for &count, then the loop condition is always going to be true.
Re: Try this code [message #210629 is a reply to message #210602] Thu, 21 December 2006 10:23 Go to previous messageGo to next message
Frank
Messages: 7880
Registered: March 2000
Senior Member
In addition to what ebrian said:
sqlplus is only going to ask you ONCE what &count should be replaced with. It replaces it, parses the code and then executes it.
You cannot have user interaction in pl/sql.
Re: Try this code [message #210637 is a reply to message #210597] Thu, 21 December 2006 11:14 Go to previous messageGo to next message
apparao1503
Messages: 2
Registered: December 2006
Location: Columbus, OH,USA
Junior Member

As the other members pointed out the condition
v_counter >= 0 will always be ture and the program will go into infinte loops.
Re: Try this code [message #210947 is a reply to message #210597] Sun, 24 December 2006 00:14 Go to previous messageGo to next message
ab_trivedi
Messages: 460
Registered: August 2006
Location: Pune, India
Senior Member
Apart from the other suggested you I am sending you the code which you can try ...

Quote:
declare
v_counter NUMBER := 0;
v_total NUMBER := 0;
v_excep exception;
BEGIN
WHILE v_counter >= 0
LOOP
v_counter := &count;
if v_counter>0 then
DBMS_OUTPUT.PUT_LINE ('V_counter shud be less than 0');
DBMS_OUTPUT.PUT_LINE ('It would be in endless loop');
raise v_excep;
end if;
v_total := v_total + v_counter ;
END LOOP;
DBMS_OUTPUT.PUT_LINE (v_total);
exception
when v_excep then
DBMS_OUTPUT.PUT_LINE ('V_counter shud be less than 0');
DBMS_OUTPUT.PUT_LINE ('It would be in endless loop');
END;
/


This is an example of exception handling .

Bye
Ashu
Re: Try this code [message #210951 is a reply to message #210947] Sun, 24 December 2006 02:07 Go to previous message
Frank
Messages: 7880
Registered: March 2000
Senior Member
It looks like you did not understand what went wrong. It had nothing to do with exception handling or anything, the OP either wanted to get another value from the user for each iteration, or he forgot to change count inside the loop.
Previous Topic: New to Oracle!
Next Topic: to get table's column from user_objects
Goto Forum:
  


Current Time: Wed Dec 07 08:54:51 CST 2016

Total time taken to generate the page: 0.09939 seconds