0% found this document useful (0 votes)
28 views

PLSQL Notes

The document discusses the %TYPE attribute in PL/SQL, which allows variables to be declared based on the data type of a database column or other variable. It provides an example of using %TYPE to declare variables that match the data types of columns in the employees table. The document also covers other PL/SQL variable types like scalars, references, LOBs, composites, and bind variables.

Uploaded by

47Rutuja Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

PLSQL Notes

The document discusses the %TYPE attribute in PL/SQL, which allows variables to be declared based on the data type of a database column or other variable. It provides an example of using %TYPE to declare variables that match the data types of columns in the employees table. The document also covers other PL/SQL variable types like scalars, references, LOBs, composites, and bind variables.

Uploaded by

47Rutuja Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

[13:21] Academy

---------- 05/08/2023 ------------------------------

-- %TYPE Attribute -----------

--- it is used to declare a variable according to:

-- A databse column definition

-- Another declared variable

-- is prefixed with:

-- The database table and column name

-- declared variable name

-- Syntax : identifier table.column%type; / declared_variable_name%type;

-- Advantages of %TYPE :

-- you can avoid errors caused by data type mismatch and wrong precision

-- you can hardcoding of data type of a variable

SET SERVEROUTPUT ON;

Declare

v_name employees.first_name%TYPE;

v_sal employees.salary%TYPE;

V_date_of_joining employees.hire_Date%TYPE;

V_balance number(10,2);

v_min_sal v_sal%TYPE;

V_min_balance V_balance%TYPE default 1000;

v_max_balance V_balance%TYPE;
BEGIN

select first_name,salary,hire_date

INTO v_name,v_sal,V_date_of_joining

from employees

where employee_id=101;

DBMS_OUTPUT.put_line(v_name);

DBMS_OUTPUT.put_line(v_sal);

DBMS_OUTPUT.put_line(V_date_of_joining);

END;

-- write a plsql prog to display employee_id,first_name,dept_id,dept_name using


%type attribute.

set serveroutput on;

DECLARE

v_emp_id employees.employee_id%TYPE;

v_ename employees.first_name%TYPE;

v_dep_id employees.department_id%TYPE;

v_dep_name departments.department_name%TYPE;

BEGIN

SELECT e.employee_id,e.first_name,e.department_id,d.department_name

into v_emp_id,v_ename,v_dep_id,v_dep_name

FROM employees e , departments d

WHERE e.department_id = d.department_id

and e.employee_id = 101;

DBMS_OUTPUT.put_line('The Employee id is: '|| v_emp_id);


DBMS_OUTPUT.put_line('The Employee Name is: '|| v_ename);

DBMS_OUTPUT.put_line('The Employee Departmant id is: '|| v_dep_id);

DBMS_OUTPUT.put_line('The Employee Department Name is: '|| v_dep_name);

end;

--- Types of Variables:

--PLSQL Variables

-- Scalar -- will hold single value at a time.the value depends on datatype of


variable

-- Reference -- will hold values called pointers which will point to the
storage location.

-- Large Object(LOB) --

-- CLOB - character LOB large character values like paragraphs

-- BLOB - Binary LOB -- large binary values like Image

-- BFILE - Binary File -- Audio ,video files

-- Composite

-- to stoe multiple values at a time in variable

-- Record

-- Collection

-- Non PLSQL Variables

-- Bind Variables/host variables

-- SCALAR DATA TYPE

-- CHAR

-- varchar

-- varchar2

-- Date

-- number
-- binary_number

-- int_number

-- binary_float

-- binary_double

-- declaring boolean variables

-- ONLY TRUE, FALSE and NULL values can be stored in boolean variable

-- it will used conditional expressions like using AND,OR ans NOT

-- arithmetic,character and date expression can return a boolean value

DECLARE

v_num1 NUMBER(4) := 30;

v_num2 NUMBER(4) := 20;

v_flag BOOLEAN;

BEGIN

-- v_flag := v_num1 < v_num2;

-- IF v_flag THEN

IF v_num1 < v_num2 THEN

dbms_output.put_line('Num1 < Num2');

ELSE

dbms_output.put_line('Num1 > Num2');

END IF;

END;

-- Bind Variables

-- called host variables as well


-- declared in the client environment or outside the plsql block

-- created using the VARIABLE keyword

-- used in sql statements and plsql blocks

-- accessed even after program exection is completed

-- referenced with preceding colon

-- values can be output using PRINT command.

-- display salary of employee using bind variable

variable v_sal number;

variable v_name varchar2(20);

begin

select salary,first_name into :v_sal,:v_name

from employees where employee_id=101;

end;

print v_sal;

print v_name;

variable v_sal number;

variable v_name varchar2(20);

variable v_email varchar2(20);

set serveroutput on;

set autoprint on;


declare

v_job employees.job_id%type;

begin

select salary,first_name,job_id,email into :v_sal,:v_name,v_job,:v_email

from employees where employee_id=101;

dbms_output.put_line(v_job);

end;

print v_sal;

print v_name;

variable v_sal number;

begin

select salary into :v_sal

from employees where employee_id=100;

end;

select first_name,salary,job_id from employees where salary=:v_sal;

declare

v_sal number(6);

begin

select salary into v_sal

from employees where employee_id=101;

dbms_output.put_line('salary is: '||v_sal);


end;

select first_name,salary,job_id from employees where salary=v_sal;

You might also like