Sure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Sure!

Here are 20 Oracle SQL commands and examples demonstrating the use of the `COMMIT` statement:

1. Basic COMMIT
INSERT INTO employees (employee_id, first_name, last_name)
VALUES (1, 'John', 'Doe');
COMMIT;

2. COMMIT After Multiple Inserts


INSERT INTO employees (employee_id, first_name, last_name) VALUES (2, 'Jane', 'Smith');
INSERT INTO employees (employee_id, first_name, last_name) VALUES (3, 'Alice', 'Johnson');
COMMIT;

3. COMMIT After Update


UPDATE employees SET last_name = 'Williams' WHERE employee_id = 1;
COMMIT;

4. COMMIT in PL/SQL Block


BEGIN
INSERT INTO departments (department_id, department_name) VALUES (10, 'Sales');
COMMIT;
END;

5. Rollback Example Before COMMIT


BEGIN
INSERT INTO products (product_id, product_name) VALUES (100, 'Gadget');
ROLLBACK; -- Undo the insert
END;

6. COMMIT After Conditional Check

BEGIN
INSERT INTO orders (order_id, order_date) VALUES (1, SYSDATE);

IF (SELECT COUNT(*) FROM orders) > 0 THEN


COMMIT; -- Commit if there are orders
ELSE
ROLLBACK; -- Otherwise, undo
END IF;
END;

7. Using COMMIT in a Loop

DECLARE
CURSOR emp_cursor IS SELECT employee_id, first_name FROM employees;
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;

-- Insert processed employee


INSERT INTO processed_employees VALUES (emp_record.employee_id, emp_record.first_name);
END LOOP;
COMMIT; -- Commit after loop
CLOSE emp_cursor;
END;

8. COMMIT in a Distributed Transaction


BEGIN
INSERT INTO local_table (col1) VALUES ('Local Data');
INSERT INTO remote_table@remote_db (col1) VALUES ('Remote Data');
COMMIT; -- Commit both operations
END;

9. COMMIT with Exception Handling

BEGIN
INSERT INTO employees (employee_id, first_name, last_name) VALUES (4, 'Charlie', 'Brown');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK; -- Undo on error
END;

10. Batch Insert with COMMIT

BEGIN
FOR i IN 1..100 LOOP
INSERT INTO employees (employee_id, first_name, last_name) VALUES (i, 'Employee' || i, 'Last' || i);
END LOOP;

COMMIT; -- Commit after batch insert


END;

11. COMMIT with DDL Statement

CREATE TABLE new_table (id NUMBER, name VARCHAR2(100));


COMMIT; -- DDL commands automatically commit

12. Using COMMIT After a Series of Updates

BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 30;
UPDATE employees SET salary = salary * 1.05 WHERE department_id = 20;
COMMIT; -- Commit all updates
END;

13. COMMIT in a Function

CREATE OR REPLACE FUNCTION add_employee(emp_id NUMBER, fname VARCHAR2, lname VARCHAR2) RETURN NUMBER IS
BEGIN
INSERT INTO employees (employee_id, first_name, last_name) VALUES (emp_id, fname, lname);
COMMIT; -- Commit within function
RETURN emp_id;
END;

14. COMMIT with a Conditional Insert

BEGIN
INSERT INTO inventory (item_id, quantity) VALUES (1, 50);

IF (SELECT quantity FROM inventory WHERE item_id = 1) > 0 THEN


COMMIT; -- Commit if quantity is greater than zero
ELSE
ROLLBACK; -- Undo if no quantity
END IF;
END;
15. COMMIT After Multiple Transactions

BEGIN
INSERT INTO customers (customer_id, customer_name) VALUES (1, 'Customer A');
INSERT INTO customers (customer_id, customer_name) VALUES (2, 'Customer B');
COMMIT; -- Commit both inserts
END;

16. COMMIT in an Autonomous Transaction

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION; -- Declare autonomous transaction
BEGIN
INSERT INTO audit_log (log_id, message) VALUES (1, 'Transaction started');
COMMIT; -- Commit independently
END;

17. COMMIT with Merge Operation

MERGE INTO employees e


USING (SELECT 1 AS employee_id, 'Updated Name' AS first_name FROM dual) src
ON (e.employee_id = src.employee_id)
WHEN MATCHED THEN
UPDATE SET e.first_name = src.first_name;

COMMIT; -- Commit the merge operation

18. COMMIT after Creating Multiple Tables

BEGIN
CREATE TABLE department_log (log_id NUMBER, log_message VARCHAR2(100));
CREATE TABLE employee_log (log_id NUMBER, log_message VARCHAR2(100));
COMMIT; -- Commit after DDL statements
END;

19. COMMIT After Inserting with Error Handling

BEGIN
INSERT INTO employees (employee_id, first_name, last_name) VALUES (5, 'David', 'Clark');
COMMIT; -- Commit if no errors
EXCEPTION
WHEN OTHERS THEN
ROLLBACK; -- Undo if there is an error
END;

20. COMMIT after Trigger Execution

CREATE OR REPLACE TRIGGER before_insert_employee


BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
-- some validation logic
IF :NEW.first_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'First name cannot be null');
END IF;
END;

-- When you insert, if validation passes:


INSERT INTO employees (employee_id, first_name, last_name) VALUES (6, 'Eve', 'Taylor');
COMMIT; -- Commit if insert is successful
These examples illustrate various scenarios for using the `COMMIT` statement in Oracle SQL, including simple inserts, PL/SQL blocks, error handling, and
more complex operations.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy