0% found this document useful (0 votes)
8 views5 pages

Pl SQL Intro

This document contains PL/SQL learning notes that include basic output blocks, variable declaration, string concatenation, data manipulation examples, and table creation. It also covers conditional statements and loops, providing examples of IF statements, CASE statements, and various loop structures. The notes serve as a practical guide for understanding and implementing PL/SQL concepts and syntax.

Uploaded by

topbinge24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Pl SQL Intro

This document contains PL/SQL learning notes that include basic output blocks, variable declaration, string concatenation, data manipulation examples, and table creation. It also covers conditional statements and loops, providing examples of IF statements, CASE statements, and various loop structures. The notes serve as a practical guide for understanding and implementing PL/SQL concepts and syntax.

Uploaded by

topbinge24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PL/SQL Learning Notes

Compiled Practice Snippets and Concepts

1. Basic Output Block


set serveroutput on;
Begin
dbms_output.put_line('Welcome');
end;
/

2. Variable Declaration and Assignment


declare
num1 number(10);
num2 number(10):=5;
begin
num1:=10;
dbms_output.put_line('Addition of num1 and num2 is '||(num1+num2));
end;
/

3. Runtime Input Using &


declare
num1 number(10):=&num1;
num2 number(10):=&num2;
begin
dbms_output.put_line('Addition of num1 and num2 is '||(num1+num2));
end;
/

4. String Concatenation
select ('Welcome to '||'PLSQL') from dual;

5. SELECT INTO Statement


declare
var_ph_no varchar2(100);
var_jid varchar2(100);
begin
select phone_number, job_id into var_ph_no, var_jid
from employees where employee_id=102;
dbms_output.put_line('Phone number is '||var_ph_no||' and job id is '||var_jid);
end;
/

6. Data Manipulation Examples


-- Insert Hardcoded Values
begin
insert into table (col_1, col_2) values (val_1, val_2);
commit;
end;

-- Insert from Another Table with Condition


begin
insert into table (col_1, col_2)
select base_col_1, base_col_2 from base_table where id in(100);
commit;
end;

-- Update Example
begin
update table set col_1=val_1 where id=1;
commit;
end;

7. Table Creation and Data Insertion


create table customer_table (
cust_id number(8),
cust_name varchar2(40),
dob date,
mobile_no number(10),
city varchar2(40)
);

insert into customer_table (cust_id, cust_name, dob, mobile_no, city)


VALUES (1000, 'Arun', to_date('12/09/1985','mm/dd/yyyy'), 9090909090, 'Chennai');

commit;
8. Using %TYPE and %ROWTYPE
-- %TYPE for single column
declare
v_mob_no hr.customer_table.mobile_no%type;
begin
select mobile_no into v_mob_no from customer_table where cust_id=1000;
dbms_output.put_line(v_mob_no);
end;
/

-- %ROWTYPE for entire row


declare
v_row hr.customer_table%rowtype;
begin
select * into v_row from customer_table where cust_id=1000;
dbms_output.put_line(v_row.mobile_no);
end;
/

9. Changing Data Type of Column


-- Rename existing table
rename customer_table to customer_bkp;

-- Recreate with new datatype


create table customer_table (
cust_id number(8),
cust_name varchar2(40),
dob date,
mobile_no VARCHAR2(20),
city varchar2(40)
);

-- Insert with modified values


insert into customer_table
select cust_id, cust_name, dob, '+91-'||mobile_no, city from customer_bkp;
commit;
PL/SQL Conditional Statements and Loop
Examples

1. IF Statement (Simple)
DECLARE
salary NUMBER := 60000;
BEGIN
IF salary > 50000 THEN
dbms_output.put_line('High salary');
END IF;
END;
/

2. IF-ELSE Statement
DECLARE
total_marks NUMBER := 35;
BEGIN
IF total_marks >= 40 THEN
dbms_output.put_line('Pass');
ELSE
dbms_output.put_line('Fail');
END IF;
END;
/

3. IF-ELSIF-ELSE Ladder
DECLARE
grade CHAR(1) := 'B';
BEGIN
IF grade = 'A' THEN
dbms_output.put_line('Excellent');
ELSIF grade = 'B' THEN
dbms_output.put_line('Good');
ELSE
dbms_output.put_line('Needs Improvement');
END IF;
END;
/

4. CASE Statement (Simple)


DECLARE
department_id NUMBER := 20;
BEGIN
CASE department_id
WHEN 10 THEN dbms_output.put_line('HR');
WHEN 20 THEN dbms_output.put_line('Finance');
ELSE dbms_output.put_line('Other Dept');
END CASE;
END;
/

5. CASE Expression (Searched)


DECLARE
salary NUMBER := 75000;
BEGIN
CASE
WHEN salary > 100000 THEN dbms_output.put_line('Manager');
WHEN salary > 50000 THEN dbms_output.put_line('Lead');
ELSE dbms_output.put_line('Executive');
END CASE;
END;
/
6. CASE with SELECT Statement
SELECT job_id, job_title, max_salary,
CASE WHEN max_salary > 20000 THEN 'High Pay' ELSE 'Low Pay' END AS Pay_Status
FROM jobs;

7. Simple LOOP
DECLARE
num NUMBER(10) := 0;
BEGIN
LOOP
dbms_output.put_line(num);
num := num + 1;
EXIT WHEN num = 5;
END LOOP;
END;

8. WHILE LOOP
DECLARE
num NUMBER(10) := 0;
BEGIN
WHILE (num < 5)
LOOP
dbms_output.put_line(num);
num := num + 1;
END LOOP;
END;

9. FOR LOOP
DECLARE
num NUMBER(10);
BEGIN
FOR num IN 0..5
LOOP
dbms_output.put_line(num);
END LOOP;
END;

10. REVERSE FOR LOOP


DECLARE
num NUMBER(10);
BEGIN
FOR num IN REVERSE 0..5
LOOP
dbms_output.put_line(num);
END LOOP;
END;

You might also like