PLSQL
PLSQL
Factorial
-- Factorial
DECLARE
n NUMBER;
fact NUMBER := 1;
i NUMBER ;
BEGIN
n := &n;
DBMS_OUTPUT.PUT_LINE('Factorial is '||fact);
END;
/
2. Prime number
-- Prime
DECLARE
n NUMBER;
i NUMBER:=2;
flag NUMBER:=0;
BEGIN
n := &n;
IF MOD(n,i) = 0 THEN
flag :=1;
EXIT;
END IF;
i:=i+1;
END LOOP;
IF flag = 0 THEN
DBMS_OUTPUT.PUT_LINE('Prime');
ELSE
DBMS_OUTPUT.PUT_LINE('Non Prime');
END IF;
END;
/
DECLARE
sum NUMBER :=0;
i NUMBER :=2;
BEGIN
LOOP
sum := sum + i;
i := i+2;
END LOOP;
END;
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'DD-MON-YYYY')||'
'||TO_CHAR(SYSTIMESTAMP,'DD-MM-YY HH24:MM:SS:FF'));
END;
/
5. Largest
DECLARE
largest NUMBER := -1 * POWER(10, 25); -- Initialize largest to a very
small number
choice NUMBER := 1; -- Variable to control the loop
(1 to continue, 0 to stop)
n NUMBER; -- Variable to store input number
BEGIN
END LOOP;
END;
/
5. Reversing a String
DECLARE
str VARCHAR2(50);
rev VARCHAR2(50);
length NUMBER;
i NUMBER;
BEGIN
str := '&str';
length := 5;
DBMS_OUTPUT.PUT_LINE(rev);
end;
/
IS
pi NUMBER:= 3.14159;
BEGIN
return pi*radius*radius;
END calculate_area;
/
BEGIN
DBMS_OUTPUT.PUT_LINE(calculate_area(100));
END;
/
BEGIN
DBMS_OUTPUT.PUT_LINE(x+y);
DBMS_OUTPUT.PUT_LINE(x*y);
END calculate_sum;
/
DECLARE
x NUMBER;
y NUMBER;
BEGIN
x:=&x;
y:=&y;
calculate_sum(x,y);
END;
-- -- Now you can use this table and data for practicing cursors
DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees;
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
UPDATE employees
SET salary = emp_record.salary*1.1
where
emp_record.employee_id=employee_id;
END LOOP;
CLOSE emp_cursor;
DBMS_OUTPUT.PUT_LINE('Salaries updated successfully.');
COMMIT;
END;
/
DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees;
emp_record emp_cursor%ROWTYPE;
avg_salary NUMBER;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
SELECT
AVG(SALARY) INTO avg_salary
FROM
employees
INNER JOIN
departments
ON
emp_record.department_id = departments.department_id
GROUP BY
departments.department_id;
IF emp_record.salary>avg_salary THEN
DBMS_OUTPUT.PUT_LINE(emp_record.first_name || ' ' ||
emp_record.salary || ' ' || avg_salary);
END IF;
END LOOP;
CLOSE emp_cursor;
END;
/
BEGIN
UPDATE departments
SET
department_name='abc';
IF SQL%FOUND then
COMMIT;
DBMS_OUTPUT.PUT_LINE('COMMITTED');
END IF;
END;
/
1. ALTER Questions:
Question 1: Add a column hire_date to the employees table with
DATE type.
2. UPDATE Questions:
Question 1: Increase the salary by 10% for all employees in the
'Engineering' department.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';
Explanation: This updates the salary of all employees in the 'Engineering' department by
increasing it by 10%.
UPDATE employees
SET status = 'Resigned'
WHERE employee_id = 103;
UPDATE employees
SET department = 'Sales'
WHERE employee_id = 101;
Question 4: Set the status to 'Active' for all employees with a
salary greater than 50000.
UPDATE employees
SET status = 'Active'
WHERE salary > 50000;
3. DELETE Questions:
Question 1: Delete the record of the employee with employee_id
104.
Note:
Be careful when using DELETE without a WHERE clause, as it will remove all data from the
table. Always verify your WHERE conditions to avoid unintentional data loss.