Home » SQL & PL/SQL » SQL & PL/SQL » Difference between variables declared under Begin and Declare (Oracle 10g)
Difference between variables declared under Begin and Declare [message #578140] Sun, 24 February 2013 10:38 Go to next message
amireee
Messages: 2
Registered: February 2013
Junior Member
Hi all

i just what to understand the differnce between the variable declared under the Begin and variable declared under Declared. to understand this i tried a sample plsql procedure as show below
create or replace
PACKAGE BODY TEST_DEV AS

 PROCEDURE TESTING(I_NAME VARCHAR2,
                   I_AGE NUMBER) AS
  
  INTEGE binary_integer := 100;
 
  L_NUM NATURALN :=100;
 --vc varchar := '134444444444';
  
    BEGIN
     [b] L_nums1 NUMBER := 100/0;[/b]
     
      DBMS_OUTPUT.PUT_LINE(' THIS IS TEST exception ' || L_nums1);
      
   EXCEPTION 
    WHEN OTHERS THEN     
     DBMS_OUTPUT.PUT_LINE(' THIS IS TEST exception '|| SQLCODE);
  END;



the Line L_nums1 NUMBER := 100/0; is throwing error(PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:)


if i remove the line L_nums1 NUMBER := 100/0; and just declare the variable out side the begin , and using inside the same variable inside the begin works fine

Why question is why we are not able to decalre, initialize and use the same Number variable insde the begin



[Edit MC: fix code tags]

[Updated on: Sun, 24 February 2013 12:03] by Moderator

Report message to a moderator

Re: Difference between variables declared under Begin and Declare [message #578141 is a reply to message #578140] Sun, 24 February 2013 12:05 Go to previous messageGo to next message
Michel Cadot
Messages: 58521
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
All variables are declared in "declare" sections (but global package variables).
Your code is invalid.

Quote:
   EXCEPTION 
    WHEN OTHERS THEN     
     DBMS_OUTPUT.PUT_LINE(' THIS IS TEST exception '|| SQLCODE);


This is a bug, NEVER ever use it.
Read WHEN OTHERS.

Regards
Michel
Re: Difference between variables declared under Begin and Declare [message #578148 is a reply to message #578141] Sun, 24 February 2013 21:41 Go to previous messageGo to next message
amireee
Messages: 2
Registered: February 2013
Junior Member
Hi Michel,

Thanks for the reply,


So we should not declare any variable inside the Begin, And the variable declared in the declare block are global variable ,this is my understanding , Please correct me if i am worng

Also my questions is iam not able to create only the NUMBER variable inside the begin, But able to create varchar2 and other types, why iam not able to create a variable of type Number inside the Begin, this what iam trying to figure out..

Thanks
Amir
Re: Difference between variables declared under Begin and Declare [message #578149 is a reply to message #578148] Sun, 24 February 2013 22:09 Go to previous messageGo to next message
BlackSwan
Messages: 22489
Registered: January 2009
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/

>Also my questions is iam not able to create only the NUMBER variable inside the begin,
>But able to create varchar2 and other types, why iam not able to create a variable of type Number inside the Begin,

please do #7 from URL above
Re: Difference between variables declared under Begin and Declare [message #578158 is a reply to message #578148] Mon, 25 February 2013 01:01 Go to previous message
Michel Cadot
Messages: 58521
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
Also my questions is iam not able to create only the NUMBER variable inside the begin, But able to create varchar2 and other types, why iam not able to create a variable of type Number inside the Begin, this what iam trying to figure out..


This is wrong, read again my post.

PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Regards
Michel
Previous Topic: sql (merged)
Next Topic: DBMS_SQL usages
Goto Forum:
  


Current Time: Wed Jul 23 21:19:47 CDT 2014

Total time taken to generate the page: 0.10550 seconds