Oracle
  Home arrow Oracle arrow Page 3 - Database Interaction with PL/SQL, part...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Database Interaction with PL/SQL, part 1
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 33
    2005-05-24

    Table of Contents:
  • Database Interaction with PL/SQL, part 1
  • SELECT…INTO Statement
  • %TYPE attribute
  • %ROWTYPE attribute
  • Using Other DML Commands

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Database Interaction with PL/SQL, part 1 - %TYPE attribute


    (Page 3 of 5 )

    In the above program, we declared a variable "v_ename" as being of type "varchar(10)." What if we don't know the data type (or data length) of the field "ename"? In general, we will switch back to the SQL prompt and issue a DESCRIBE statement to show all of the columns and datatypes (and of course the lengths, too) of a particular table. What if DBA increases the width of "ename" column without informing you (which happens very often)? Your program may not work correctly.

    So, we need to have a special mechanism with which a variable should find the data type and width dynamically and automatically at runtime. This is where %TYPE comes in. Let us take a closer look at it, with a simple example.

    Declare
         v_ename emp.ename%Type;
         v_empno emp.empno%type := &empno;
    begin
         select ename into v_ename
         from emp
         where empno=v_empno;
         dbms_output.put_line('Name: '||v_ename);
    end;

    This is very similar to the first example, except for the declaration section. "v_ename" is declared as being of type "emp.ename%type." What does it mean? It means that the data type of "v_ename" would be the same as the data type of the "ename" column in the "emp" table. Similarly, the other variable, "v_empno," is also declared.

    We can declare multiple variables and fetch accordingly, as shown in the following example:

    Declare
         v_empno emp.empno%type := &empno;
         v_ename emp.ename%type;
         v_sal emp.sal%type;
         v_job emp.job%type;
    begin
         select ename,sal,job into v_ename,v_sal,v_job
         from emp
         Where empno=v_empno;
         dbms_output.put_line('Name: '|| v_ename);
         dbms_output.put_line('Salary: '|| v_sal);
         dbms_output.put_line('Job: '|| v_job);
    end;

    More Oracle Articles
    More By Jagadish Chatarji


     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway