PL Lab Manual-1
PL Lab Manual-1
(AUTONOMOUS)
DATABASE PROGRAMMING WITH PL/SQL LAB (Lab - II )
Professional Elective - I
• Course Objectives:
• Course Outcomes:
ListofPrograms
Week 1 : Write a pl/sql program using FOR LOOP to insert ten rows into a database table.
Week 3.IILUSTRATE how you can embed PL/SQL in a high level HOST language SUCH AS
C/JAVA AND DEMONSTRATE BANKING DEBIT CARD TRANSACTION might be done.
Week 4.GIVEN AN INTEGER i,write a PL/SQL procedure to insert the tuple (i,'XXX')into a given
relation.
OUTPUT:
11
1
2
2.Give the table EMPLOYEE (EmpNo,Name,Salary,Designation ,DeptID),write
a cursor to select the three highest paid employees from the table.
createtable employee(
emp_no integerprimary key
,lastname varchar2(20) notnull
,salary number(3)
);
insertinto employee(emp_no,lastname,salary)
values(1,'Tomy',2);
insertinto employee(emp_no,lastname,salary)
values(2,'Jacky',3);
insertinto employee(emp_no,lastname,salary)
values(3,'Joey',4);
insertinto employee(emp_no,lastname,salary)
values(4,'Janey',5);
OUTPUT :
LASTNAME SALARY
------------------- ----------------------
Janey 5
Joey 4
Jacky 3
3.IILUSTRATE how you can embed PL/SQL in a high level HOST language SUCH AS
C/JAVA AND DEMONSTRATE BANKING DEBIT CARD TRANSACTION might be
done.
main()
{
extern double atof();
strcpy (uid.arr,"scott");
uid.len=strlen(uid.arr);
strcpy (pwd.arr,"tiger");
pwd.len=strlen(pwd.arr);
/* ---------------------------------- */
/* ----- Begin the PL/SQL block ----- */
/* ---------------------------------- */
EXEC SQL EXECUTE
DECLARE
insufficient_funds EXCEPTION;
old_bal NUMBER;
min_bal CONSTANT NUMBER := 500;
BEGIN
SELECT bal INTO old_bal FROM accounts
WHERE account_id = :acct;
-- If the account doesn't exist, the NO_DATA_FOUND
-- exception will be automatically raised.
:new_bal := old_bal - :debit;
IF :new_bal >= min_bal THEN
UPDATE accounts SET bal = :new_bal
WHERE account_id = :acct;
INSERT INTO journal
VALUES (:acct, 'Debit', :debit, SYSDATE);
:status := 'Transaction completed.';
ELSE
RAISE insufficient_funds;
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
:status := 'Account not found.';
:new_bal := -1;
WHEN insufficient_funds THEN
:status := 'Insufficient funds.';
:new_bal := old_bal;
WHEN OTHERS THEN
ROLLBACK;
:status := 'Error: ' || SQLERRM(SQLCODE);
:new_bal := -1;
END;
END-EXEC;
/* -------------------------------- */
/* ----- End the PL/SQL block ----- */
/* -------------------------------- */
errprint:
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n\n>>>>> Error during execution:\n");
printf("%s\n",sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK RELEASE;
exit(1)}
Interactive Session
Embedded PL/SQL Debit Transaction Demo
ACCOUNT_ID BAL
---------- ------
1 700
2 1400
3 1500
4 6500
5 500
DECLARE
a NUMBER;
b NUMBER;
BEGIN
SELECT e,f INTO a,b FROM T1 WHERE e>1;
INSERT INTO T1 VALUES(b,a);
END;
.
OUTPUT:
Fortuitously, there is only one tuple of T1 that has first component greater than 1, namely
(2,4). The insert-statement thus inserts (4,2) into T1.
5.Write PL/SQL Program to demonstrate Exceptions.
DECLARE
salary_too_high EXCEPTION;
erroneous_salary NUMBER;
BEGIN
BEGIN
END IF;
EXCEPTION
erroneous_salary := current_salary;
END;
EXCEPTION
current_salary := max_salary;
DBMS_OUTPUT.PUT_LINE (
END;
OUTPUT :
1. DECLARE
2. total_rows number(2);
3. BEGIN
4. UPDATE customers
5. SET salary = salary + 5000;
6. IF sql%notfound THEN
7. dbms_output.put_line('no customers updated');
8. ELSIF sql%found THEN
9. total_rows := sql%rowcount;
10. dbms_output.put_line( total_rows || ' customers updated ');
11. END IF;
12. END;
13. /
Output:
6 customers updated.
PL/SQL procedure successfully completed.
7.Write PL/SQL Program to demonstrate Functions.
DECLARE
x INTEGER;
FUNCTION f (n INTEGER)
RETURN INTEGER
IS
BEGIN
RETURN (n*n);
END;
BEGIN
DBMS_OUTPUT.PUT_LINE (
);
x := f(2);
END;
OUTPUT :
END;
aa aa_pkg.aa_type
) AUTHID DEFINER IS
i VARCHAR2(15);
BEGIN
i := aa.FIRST;
i := aa.NEXT(i);
END LOOP;
END;
DECLARE
aa_var aa_pkg.aa_type;
BEGIN
aa_var('zero') := 0;
aa_var('one') := 1;
aa_var('two') := 2;
print_aa(aa_var);
END;
/
OUTPUT :
1 one
2 two
0 zero
first_name employees.first_name%TYPE;
last_name employees.last_name%TYPE;
email employees.email%TYPE;
name1 VARCHAR2,
name2 VARCHAR2,
company VARCHAR2
IS
DBMS_OUTPUT.PUT_LINE(error_message);
END create_email;
BEGIN
first_name := 'John';
last_name := 'Doe';
first_name := 'Elizabeth';
last_name := 'MacDonald';
END;
OUTPUT:
OUTPUT:
Trigger created.