PLSQL
PLSQL
as plsql code is combination of SQL statement and PLSQL statement so plsql engine
willsend SQL statemnt
to SQL statement executor and retrive the results from it. and PLSQL statements
will be executed
by PLSQL statement executor in PLSQL engine.
--- variables
-- constants
-- data types
-- conditional statements
-- looping
-- collections
-- cursor
-- exception handling
-- anonymous block
--- named blocks -- procedure,function,package,trigger
PLSQL Advantages:
Begin (mandatory)
-- SQL statements
-- PLSQL statements
Exception (optional)
- Actions to perform when any exceptions occur
-- Anonymous Block
-- it will not have name and it won't get stored in database so we can't reuse
it.
-- Named Block -- it will have name and it will get stored in database so we can
reuse/recall it.
-- procudure
-- function
*/
Declare
v_emp_name varchar2(10):='Ashlesha'; -- variable initialization
v_sal number(10); -- variable Declaration
--v_job varchar2(10);
Begin
DBMS_OUTPUT.put_line('The Employee Name is: '||v_emp_name);
DBMS_OUTPUT.put_line('The Employee Salary is: '||v_sal);
SELECT first_name,salary
INTO v_emp_name,v_sal
FROM employees
where employee_id=100; -- where condition tomretrive exactly one row
DBMS_OUTPUT.put_line('The Employee Name is: '||v_emp_name);
end;
DECLARE
v_emp_name VARCHAR2(30);
v_job_id VARCHAR2(20);
v_hire_date DATE;
v_dept_id VARCHAR2(20);
BEGIN
SELECT
first_name,
job_id,
hire_date,
department_id
INTO
v_emp_name,
v_job_id,
v_hire_date,
v_dept_id
FROM
employees
WHERE
employee_id = 102;
--Syntax: identifier [CONSTANT] data type [NOT NULL] [:= | default expr]
begin
DBMS_OUTPUT.PUT_LINE('Commission is: '|| v_Comm);
select commission_pct
INTO v_Comm
from employees
where employee_id=148;
DBMS_OUTPUT.PUT_LINE('Commission is: '|| v_Comm);
end;
declare
begin
DBMS_OUTPUT.PUT_LINE('Interest rate is: '|| V_int_rate);
-- V_int_rate:=11.5;--expression 'V_INT_RATE' cannot be used as an assignment
target
select commission_pct
INTO V_int_rate
from employees
where employee_id=148;
end;
declare
v_Comm:= v_Comm+3;
DBMS_OUTPUT.PUT_LINE('Commission is: '|| v_Comm);
select commission_pct
INTO v_Comm
from employees
where employee_id=198;
DBMS_OUTPUT.PUT_LINE('Commission is: '|| v_Comm);
end;
declare
v_event varchar2(40);
begin
v_event:='FRiendship day';
dbms_output.put_line(v_event);
v_event:='Mother''s day';
dbms_output.put_line(v_event);
v_event:=q'[Father's Day]';
dbms_output.put_line(v_event);
end;