PLSQL Programming
PLSQL Programming
Scenario 1:
Answer:
BEGIN
FROM customers
LOOP
UPDATE customers
END LOOP;
COMMIT;
END;
Explanation:
a) The FOR loop selects all customers where age > 60.
Question:
Write a PL/SQL block that iterates through all customers and sets a flag Is VIP to TRUE for
those with a balance over $10,000.
Answer:
BEGIN
FOR cust IN (SELECT customer_id
FROM customers
LOOP
UPDATE customers
COMMIT;
END;
Explanation:
Scenario 3:
Send Reminders for Loans Due in the Next 30 Days
Question:
Write a PL/SQL block that fetches all loans due in the next 30 days and prints a reminder
message for each customer.
Answer:
BEGIN
FROM loans
WHERE due_date BETWEEN SYSDATE AND SYSDATE + 30)
LOOP
END LOOP;
END;
Explanation:
1. This block selects loans with a due_date within 30 days from today.
2. It uses DBMS_OUTPUT.PUT_LINE to display a reminder message.
Exercise 3:
Stored Procedures
Question:
Write a stored procedure ProcessMonthlyInterest that calculates and updates the balance of
all savings accounts by applying an interest rate of 1% to the current balance.
Answer:
BEGIN
FROM accounts
WHERE account_type = 'SAVINGS')
LOOP
UPDATE accounts
COMMIT;
END;
Explanation:
Question:
Write a stored procedure UpdateEmployeeBonus that updates the salary of employees in a
given department by adding a bonus percentage passed as a parameter.
Answer:
dept_id IN NUMBER,
bonus_pct IN NUMBER
) AS
BEGIN
UPDATE employees
COMMIT;
END;
Explanation:
Scenario 3:
Question:
Write a stored procedure TransferFunds that transfers a specified amount from one account to
another, ensuring the source account has enough balance.
Answer:
source_acc_id IN NUMBER,
target_acc_id IN NUMBER,
amount IN NUMBER
) AS
source_balance NUMBER;
BEGIN
FROM accounts
UPDATE accounts
UPDATE accounts
SET balance = balance + amount
COMMIT;
ELSE
END IF;
END;
Explanation: