sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
CREATE PROCEDURE get_employee_details (
@emp_id IN AS NUMBER
) AS
emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @emp_name = ename FROM employees WHERE empno = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(emp_name, ''));
END;
BEGIN
dbo.get_employee_details(100);
BEGIN
dbo.get_employee_details(200);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE PROCEDURE get_employee_details (
emp_id IN NUMBER
) AS
emp_name VARCHAR2(50);
BEGIN
SELECT ename INTO emp_name FROM employees WHERE empno = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name);
END;
BEGIN
get_employee_details(100);
BEGIN
get_employee_details(200);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure
(
@in_param IN AS VARCHAR2,
out_param OUT VARCHAR2
)
IS
BEGIN
SELECT @out_param = name
FROM employees
WHERE id = in_param;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No data found for employee with ID ' + isnull(in_param, ''), 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE my_procedure
(
in_param IN VARCHAR2,
out_param OUT VARCHAR2
)
IS
BEGIN
SELECT name INTO out_param
FROM employees
WHERE id = in_param;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No data found for employee with ID ' || in_param);
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'SMITH';
IF @v_empno IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
END; | DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'SMITH';
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
END; |
BEGIN
-- De... ***
v_emp_id NUMBER;
v_emp_name dbo.VARCHAR2(50);
v_salary NUMBER;
*** ils
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 100;
*** exists
IF v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
*** alary
UPDATE employees
SET salary = v_salary + 100
WHERE emp_id = v_emp_id;
-- Co... ***
COMMIT;
*** essage
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
-- Ro... ***
ROLLBACK;
*** sage
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
BEGIN
-- Declare variables
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_salary NUMBER;
-- Get employee details
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 100;
-- Check if employee exists
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Update employee salary
UPDATE employees
SET salary = v_salary + 100
WHERE emp_id = v_emp_id;
-- Commit changes
COMMIT;
-- Display success message
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
-- Rollback changes
ROLLBACK;
-- Display error message
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@salary FLOAT;
BEGIN
SET @salary = dbo.get_employee_salary(10);
IF @salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@salary, ''));
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
salary NUMBER;
BEGIN
salary := get_employee_salary(10);
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || salary);
END IF;
END;
|
CREATE PROCEDURE process_orders(@order_id IN AS NUMBER) AS
BEGIN
DECLARE
@order_total FLOAT;
order_status dbo.VARCHAR2(10);
BEGIN
SELECT @order_total = @order_total, @order_status = order_status
FROM orders
WHERE order_id = order_id;
IF order_status = 'NEW' BEGIN
*** us to 'PROCESSING'
UPDATE orders SET order_status = 'PROCESSING'
WHERE order_id = order_id;
END
ELSE IF order_status = 'PROCESSING' BEGIN
*** otal
SET @order_total = dbo.calculate_order_total(order_id);
*** l and status to 'PROCESSED'
UPDATE orders SET order_total = @order_total,
order_status = 'PROCESSED'
WHERE order_id = order_id;
END
ELSE BEGIN
*** invalid order status
RAISERROR('-20001 Invalid order status', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
*** lback transaction
dbo.log_error(@@ERROR, SQLERRM);
ROLLBACK;
END;
END;
CREATE FUNCTION calculate_order_total(@order_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
DECLARE
@order_details_total FLOAT;
BEGIN
SELECT @order_details_total = SUM(order_detail_total)
FROM order_details
WHERE order_id = order_id;
RETURN @order_details_total;
EXCEPTION
WHEN OTHERS THEN
*** return 0
RETURN 0;
END;
END;
|
CREATE PROCEDURE process_orders(order_id IN NUMBER) AS
BEGIN
DECLARE
order_total NUMBER;
order_status VARCHAR2(10);
BEGIN
SELECT order_total, order_status INTO order_total, order_status
FROM orders
WHERE order_id = order_id;
IF order_status = 'NEW' THEN
-- Update order status to 'PROCESSING'
UPDATE orders SET order_status = 'PROCESSING'
WHERE order_id = order_id;
ELSIF order_status = 'PROCESSING' THEN
-- Calculate order total
order_total := calculate_order_total(order_id);
-- Update order total and status to 'PROCESSED'
UPDATE orders SET order_total = order_total,
order_status = 'PROCESSED'
WHERE order_id = order_id;
ELSE
-- Handle error for invalid order status
RAISE_APPLICATION_ERROR(-20001, 'Invalid order status');
END IF;
EXCEPTION
WHEN OTHERS THEN
-- Log error and rollback transaction
log_error(SQLCODE, SQLERRM);
ROLLBACK;
END;
END;
CREATE FUNCTION calculate_order_total(order_id IN NUMBER) RETURN NUMBER AS
BEGIN
DECLARE
order_details_total NUMBER;
BEGIN
SELECT SUM(order_detail_total) INTO order_details_total
FROM order_details
WHERE order_id = order_id;
RETURN order_details_total;
EXCEPTION
WHEN OTHERS THEN
-- Handle error and return 0
RETURN 0;
END;
END;
|
BEGIN
*** e to store the employee ID
emp_id row_number(10);
*** to fetch employee data
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = :dept_id;
-- Op... ***
OPEN emp_cursor;
*** result set
WHILE 1=1 BEGIN
FETCH emp_cursor INTO @emp_id, @first_name, @last_name;
IF @@FETCH_STATUS <> 0 BREAK;
*** yee information
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, ''));
END;
-- Cl... ***
CLOSE emp_cursor;
END;
|
BEGIN
-- Declare a variable to store the employee ID
emp_id number(10);
-- Declare a cursor to fetch employee data
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = :dept_id;
-- Open the cursor
OPEN emp_cursor;
-- Loop through the result set
LOOP
FETCH emp_cursor INTO emp_id, first_name, last_name;
EXIT WHEN emp_cursor%NOTFOUND;
-- Display the employee information
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name);
END LOOP;
-- Close the cursor
CLOSE emp_cursor;
END;
|
CREATE PROCEDURE get_customer_orders(@p_customer_id IN AS NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, order_status
FROM orders
WHERE customer_id = p_customer_id;
rec_orders c_orders%ROWTYPE;
BEGIN
OPEN c_orders;
FETCH c_orders INTO @rec_orders;
WHILE c_orders%FOUND BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(rec_orders.order_id, '') + ' ' + isnull(rec_orders.order_date, '') + ' ' + isnull(rec_orders.order_status, ''));
FETCH c_orders INTO @rec_orders;
END;
CLOSE c_orders;
END;
|
CREATE PROCEDURE get_customer_orders(p_customer_id IN NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, order_status
FROM orders
WHERE customer_id = p_customer_id;
rec_orders c_orders%ROWTYPE;
BEGIN
OPEN c_orders;
FETCH c_orders INTO rec_orders;
WHILE c_orders%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(rec_orders.order_id || ' ' || rec_orders.order_date || ' ' || rec_orders.order_status);
FETCH c_orders INTO rec_orders;
END LOOP;
CLOSE c_orders;
END;
|
IF OBJECT_ID('UpdateCustomerEmail', 'P') IS NOT NULL
DROP PROCEDURE UpdateCustomerEmail;
GO
CREATE PROCEDURE UpdateCustomerEmail(@IN cust_id AS NUMBER, IN new_email VARCHAR2) IS
BEGIN
UPDATE customers SET email = new_email WHERE customer_id = cust_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Customer ' + isnull(cust_id, '') + ' has been updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while updating the customer email.');
ROLLBACK;
END;
/
|
CREATE OR REPLACE PROCEDURE UpdateCustomerEmail(IN cust_id NUMBER, IN new_email VARCHAR2) IS
BEGIN
UPDATE customers SET email = new_email WHERE customer_id = cust_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Customer ' || cust_id || ' has been updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while updating the customer email.');
ROLLBACK;
END;
/
|
CREATE PROCEDURE GetProductCountByCategory(
@IN categoryID AS NUMBER,
OUT productCount NUMBER
)
AS
BEGIN
SELECT @productCount = COUNT(*)
FROM products
WHERE categoryID = categoryID;
END;
CREATE FUNCTION GetProductPrice(
@IN productID NUMBER
DECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
DECLARE @productPrice FLOAT;
SELECT @productPrice = price
FROM products
WHERE productID = productID;
IF @productPrice IS NULL BEGIN
RETURN 0;
END
ELSE BEGIN
RETURN @productPrice;
END
END;
BEGIN
DECLARE @productCount FLOAT;
DECLARE @productID FLOAT;
DECLARE @productPrice FLOAT;
SET @productCount = 0;
CALL GetProductCountByCategory(1, @productCount);
IF @productCount > 0 BEGIN
SELECT TOP 1 @productID = @productID, @productPrice = price
FROM products
WHERE categoryID = 1
;
DBMS_OUTPUT.PUT_LINE('Product ID: ' + ISNULL(@productID, ''));
DBMS_OUTPUT.PUT_LINE('Product Price: ' + ISNULL(@productPrice, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No products found in the specified category.');
END
END;
|
CREATE PROCEDURE GetProductCountByCategory(
IN categoryID NUMBER,
OUT productCount NUMBER
)
AS
BEGIN
SELECT COUNT(*) INTO productCount
FROM products
WHERE categoryID = categoryID;
END;
CREATE FUNCTION GetProductPrice(
IN productID NUMBER
)
RETURN NUMBER
AS
BEGIN
DECLARE productPrice NUMBER;
SELECT price INTO productPrice
FROM products
WHERE productID = productID;
IF productPrice IS NULL THEN
RETURN 0;
ELSE
RETURN productPrice;
END IF;
END;
BEGIN
DECLARE productCount NUMBER;
DECLARE productID NUMBER;
DECLARE productPrice NUMBER;
productCount := 0;
CALL GetProductCountByCategory(1, productCount);
IF productCount > 0 THEN
SELECT productID, price
INTO productID, productPrice
FROM products
WHERE categoryID = 1
AND rownum = 1;
DBMS_OUTPUT.PUT_LINE('Product ID: ' || productID);
DBMS_OUTPUT.PUT_LINE('Product Price: ' || productPrice);
ELSE
DBMS_OUTPUT.PUT_LINE('No products found in the specified category.');
END IF;
END;
|
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@emp_id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @emp_name VARCHAR(255);
SELECT @emp_name = ename FROM emp WHERE empno = @emp_id;
RETURN @emp_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE @emp_id FLOAT := 10;
DECLARE @emp_name VARCHAR(255);
BEGIN
SET @emp_name = dbo.get_employee_name(@emp_id);
IF @emp_name IS NULL BEGIN
RAISE NO_DATA_FOUND;
END
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, ''));
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(emp_id NUMBER) RETURN VARCHAR2 IS
emp_name VARCHAR2(255);
BEGIN
SELECT ename INTO emp_name FROM emp WHERE empno = emp_id;
RETURN emp_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE emp_id NUMBER := 10;
DECLARE emp_name VARCHAR2(255);
BEGIN
emp_name := get_employee_name(emp_id);
IF emp_name IS NULL THEN
RAISE NO_DATA_FOUND;
END IF;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
CREATE FUNCTION get_product_name(@id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @product_name VARCHAR(100);
SELECT @product_name = name FROM products WHERE @id = @id;
RETURN @product_name;
END;
BEGIN
DECLARE
@product_name VARCHAR(100);
BEGIN
SET @product_name = dbo.get_product_name(1234);
DBMS_OUTPUT.PUT_LINE('Product name: ' + isnull(@product_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Product not found');
END
END;
END;
|
CREATE FUNCTION get_product_name(id NUMBER) RETURN VARCHAR2 IS
product_name VARCHAR2(100);
BEGIN
SELECT name INTO product_name FROM products WHERE id = id;
RETURN product_name;
END;
BEGIN
DECLARE
product_name VARCHAR2(100);
BEGIN
product_name := get_product_name(1234);
DBMS_OUTPUT.PUT_LINE('Product name: ' || product_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Product not found');
END;
END;
|
CREATE FUNCTION get_employee_salary (@employee_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL BEGIN
RETURN -1; -- Em... ***
END
RETURN salary;
END;
/
DECLARE
@employee_salary FLOAT;
BEGIN
SET @employee_salary = dbo.get_employee_salary(10);
IF @employee_salary = -1 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('The salary of employee 10 is ' + isnull(@employee_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE FUNCTION get_employee_salary (employee_id IN NUMBER)
RETURN NUMBER
IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL THEN
RETURN -1; -- Employee not found
END IF;
RETURN salary;
END;
/
DECLARE
employee_salary NUMBER;
BEGIN
employee_salary := get_employee_salary(10);
IF employee_salary = -1 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('The salary of employee 10 is ' || employee_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
CREATE PROCEDURE get_employee_details(@emp_id IN AS NUMBER, OUT emp_name VARCHAR2, OUT emp_salary NUMBER) AS
BEGIN
SELECT @emp_name = ename, @emp_salary = sal FROM emp WHERE empno = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @emp_name = NULL;
set @emp_salary = NULL;
END
END;
DECLARE
@emp_id FLOAT := 10;
emp_name dbo.VARCHAR2(50);
emp_salary NUMBER;
BEGIN
dbo.get_employee_details(@emp_id, emp_name, emp_salary);
IF emp_name IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(emp_salary, ''));
END
END;
|
CREATE PROCEDURE get_employee_details(emp_id IN NUMBER, OUT emp_name VARCHAR2, OUT emp_salary NUMBER) AS
BEGIN
SELECT ename, sal INTO emp_name, emp_salary FROM emp WHERE empno = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
emp_name := NULL;
emp_salary := NULL;
END;
DECLARE
emp_id NUMBER := 10;
emp_name VARCHAR2(50);
emp_salary NUMBER;
BEGIN
get_employee_details(emp_id, emp_name, emp_salary);
IF emp_name IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary > 10000 BEGIN
-- Ra... ***
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = @v_emp_id;
END
-- Co... ***
COMMIT;
*** errors
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee with the last name ''Smith'' was found.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
END;
/
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary > 10000 THEN
-- Raise salary by 10%
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = v_emp_id;
END IF;
-- Commit changes
COMMIT;
-- Handle potential errors
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with the last name ''Smith'' was found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
END;
/
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee
(
@emp_id IN AS NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = isnull(salary, '') + '0' WHERE employee_id = emp_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee
(
emp_id IN NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = salary || '0' WHERE employee_id = emp_id;
COMMIT;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id;
IF @salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
RETURN @salary;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURNS NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
RETURN salary;
END;
|
CREATE FUNCTION get_max_salary(@dept_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @max_salary FLOAT;
SELECT @max_salary = MAX(salary) FROM employees WHERE department_id = @dept_id;
RETURN @max_salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN 0;
END
END;
CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20000 Duplicate value on unique index', 16, 1);
END;
BEGIN
DECLARE
@max_salary FLOAT;
BEGIN
SET @max_salary = dbo.get_max_salary(10);
IF @max_salary > 10000 BEGIN
dbo.update_employee_salary(1, @max_salary);
END
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
END;
|
CREATE FUNCTION get_max_salary(dept_id NUMBER) RETURN NUMBER IS
max_salary NUMBER;
BEGIN
SELECT MAX(salary) INTO max_salary FROM employees WHERE department_id = dept_id;
RETURN max_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
CREATE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20000, 'Duplicate value on unique index');
END;
BEGIN
DECLARE
max_salary NUMBER;
BEGIN
max_salary := get_max_salary(10);
IF max_salary > 10000 THEN
update_employee_salary(1, max_salary);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@p_param IN AS NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Parameter value: ' + isnull(p_param, ''));
IF p_param > 0 BEGIN
INSERT INTO my_table (id, value) VALUES (p_param, p_param * 2);
END
ELSE BEGIN
RAISERROR('-20000 Invalid parameter value', 16, 1);
END
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE my_proc (p_param IN NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Parameter value: ' || p_param);
IF p_param > 0 THEN
INSERT INTO my_table (id, value) VALUES (p_param, p_param * 2);
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Invalid parameter value');
END IF;
COMMIT;
END;
|
IF OBJECT_ID('UpdateCustomerAddress', 'P') IS NOT NULL
DROP PROCEDURE UpdateCustomerAddress;
GO
CREATE PROCEDURE UpdateCustomerAddress(
@IN old_address AS @IN VARCHAR2,
IN new_address IN VARCHAR2,
OUT status OUT VARCHAR2
)
IS
BEGIN
UPDATE customers SET address = new_address WHERE address = old_address;
IF @@ROWCOUNT > 0 BEGIN
set @status = 'Address updated successfully';
END
ELSE BEGIN
set @status = 'No customer found with address: ' + isnull(old_address, '');
END
EXCEPTION
WHEN OTHERS THEN
set @status = 'Error updating customer address: ' + ISNULL(SQLERRM, '');
END;
/
BEGIN
DECLARE
@old_address VARCHAR(255) := '123 Main Street';
new_address dbo.VARCHAR2(255) := '456 Elm Street';
status dbo.VARCHAR2(255);
BEGIN
dbo.UpdateCustomerAddress(@old_address, new_address, status);
DBMS_OUTPUT.PUT_LINE(status);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
CREATE OR REPLACE PROCEDURE UpdateCustomerAddress(
IN old_address IN VARCHAR2,
IN new_address IN VARCHAR2,
OUT status OUT VARCHAR2
)
IS
BEGIN
UPDATE customers SET address = new_address WHERE address = old_address;
IF SQL%ROWCOUNT > 0 THEN
status := 'Address updated successfully';
ELSE
status := 'No customer found with address: ' || old_address;
END IF;
EXCEPTION
WHEN OTHERS THEN
status := 'Error updating customer address: ' || SQLERRM;
END;
/
BEGIN
DECLARE
old_address VARCHAR2(255) := '123 Main Street';
new_address VARCHAR2(255) := '456 Elm Street';
status VARCHAR2(255);
BEGIN
UpdateCustomerAddress(old_address, new_address, status);
DBMS_OUTPUT.PUT_LINE(status);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
/
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'King';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'King';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
END;
|
DECLARE
@v_cursor SYS_REFCURSOR;
v_empno NUMBER;
v_ename dbo.VARCHAR2(30);
BEGIN
SELECT empno, ename FROM emp;
WHILE 1=1 BEGIN
FETCH v_cursor INTO @v_empno, @v_ename;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.put_line('Empno: ' + ISNULL(CONVERT(VARCHAR, v_empno), '') + ' Ename: ' + isnull(v_ename, ''));
END;
CLOSE v_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.put_line('No data found');
END
END; | DECLARE
v_cursor SYS_REFCURSOR;
v_empno NUMBER;
v_ename VARCHAR2(30);
BEGIN
OPEN v_cursor FOR SELECT empno, ename FROM emp;
LOOP
FETCH v_cursor INTO v_empno, v_ename;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.put_line('Empno: ' || TO_CHAR(v_empno) || ' Ename: ' || v_ename);
END LOOP;
CLOSE v_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line('No data found');
END; |
CREATE PROCEDURE myStoredProc(@IN param1 AS INTEGER, IN param2 VARCHAR(255))
AS
BEGIN
*** c here
UPDATE table SET col1 = param1, col2 = param2 WHERE id = 1;
*** ndling scenario
IF (param1 < 0) BEGIN
RAISERROR('-20001 Param1 cannot be negative', 16, 1);
END
END;
SELECT * FROM table WHERE col1 > 5;
WHILE 1=1 BEGIN
*** gic here
UPDATE table SET col1 = col1 + 1 WHERE col2 > 10;
EXIT WHEN col1 > 100;
END;
CREATE FUNCTION myFunction(@IN param1 INTEGERDECLARE ) RETURNS
BEGIN INTEGER
RETURN NULL;
END;
AS
DECLARE
@result INTEGER;
BEGIN
*** ulations here
SET @result = param1 * param1 + 5;
RETURN @result;
END; | CREATE PROCEDURE myStoredProc(IN param1 INTEGER, IN param2 VARCHAR(255))
AS
BEGIN
-- Some complex logic here
UPDATE table SET col1 = param1, col2 = param2 WHERE id = 1;
-- Possible error handling scenario
IF (param1 < 0) THEN
RAISE_APPLICATION_ERROR(-20001, 'Param1 cannot be negative');
END IF;
END;
SELECT * FROM table WHERE col1 > 5;
LOOP
-- Some iterative logic here
UPDATE table SET col1 = col1 + 1 WHERE col2 > 10;
EXIT WHEN col1 > 100;
END LOOP;
CREATE FUNCTION myFunction(IN param1 INTEGER) RETURNS INTEGER
AS
DECLARE
result INTEGER;
BEGIN
-- Some complex calculations here
result := param1 * param1 + 5;
RETURN result;
END; |
CREATE PROCEDURE get_employee_info(
@last_name IN AS VARCHAR2,
first_name IN VARCHAR2,
OUT r_employee_id NUMBER,
OUT r_salary NUMBER
) AS
BEGIN
SELECT @r_employee_id = employee_id, @r_salary = salary
FROM employees
WHERE last_name = last_name
AND first_name = first_name;
END;
|
CREATE PROCEDURE get_employee_info(
last_name IN VARCHAR2,
first_name IN VARCHAR2,
OUT r_employee_id NUMBER,
OUT r_salary NUMBER
) AS
BEGIN
SELECT employee_id, salary
INTO r_employee_id, r_salary
FROM employees
WHERE last_name = last_name
AND first_name = first_name;
END;
|
CREATE PROCEDURE get_customer_orders(@customer_id IN AS NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
BEGIN
*** cursor rows
DECLARE rec CURSOR FOR c_orders LOOP
*** he cursor perform operations
-- In... ***
INSERT INTO order_history
(order_id, order_date, total_amount)
VALUES (@rec_order_id, @rec_order_date, @rec_total_amount);
*** g example
EXCEPTION
WHEN OTHERS THEN
-- Lo... ***
INSERT INTO error_log (error_message)
VALUES ('Error encountered while processing customer orders: ' + ISNULL(SQLERRM, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
|
CREATE PROCEDURE get_customer_orders(customer_id IN NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
BEGIN
-- Iterate over the cursor rows
FOR rec IN c_orders LOOP
-- FOR each row in the cursor perform operations
-- Insert example
INSERT INTO order_history
(order_id, order_date, total_amount)
VALUES (rec.order_id, rec.order_date, rec.total_amount);
-- Exception handling example
EXCEPTION
WHEN OTHERS THEN
-- Log the error
INSERT INTO error_log (error_message)
VALUES ('Error encountered while processing customer orders: ' || SQLERRM);
END LOOP;
END;
|
DECLARE
@v_id FLOAT;
BEGIN
*** oyee
INSERT INTO employees (name, salary) VALUES ('John Doe', 2000);
*** s ID
SELECT @v_id = employee_id
FROM employees
WHERE name = 'John Doe';
-- Pr... ***
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_id, ''));
*** ee's salary
UPDATE employees SET salary = 3000
WHERE employee_id = @v_id;
-- De... ***
DELETE
FROM employees
WHERE employee_id = @v_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Error: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_id NUMBER;
BEGIN
-- Insert a new employee
INSERT INTO employees (name, salary) VALUES ('John Doe', 2000);
-- Get the employee's ID
SELECT employee_id
INTO v_id
FROM employees
WHERE name = 'John Doe';
-- Print the ID
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_id);
-- Update the employee's salary
UPDATE employees SET salary = 3000
WHERE employee_id = v_id;
-- Delete the employee
DELETE
FROM employees
WHERE employee_id = v_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Error: ' || SQLERRM);
END;
|
CREATE PROCEDURE my_proc (
@p1 IN AS VARCHAR2,
p2 OUT NUMBER
) AS
BEGIN
SELECT @p2 = COUNT(*) FROM my_table WHERE col1 = p1;
IF p2 = 0
BEGIN
RAISERROR('-20001 No rows found for value = ' + isnull(p1, ''), 16, 1);
END
COMMIT;
END;
|
CREATE PROCEDURE my_proc (
p1 IN VARCHAR2,
p2 OUT NUMBER
) AS
BEGIN
SELECT COUNT(*) INTO p2 FROM my_table WHERE col1 = p1;
IF p2 = 0
THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found for value = ' || p1);
END IF;
COMMIT;
END;
|
IF OBJECT_ID('sp_demo', 'P') IS NOT NULL
DROP PROCEDURE sp_demo;
GO
CREATE PROCEDURE sp_demo(@p_id IN AS NUMBER, p_name IN VARCHAR2, OUT p_emp_count OUT NUMBER) IS
l_sql dbo.VARCHAR2(4000);
l_emp_count NUMBER;
BEGIN
set @l_sql = 'SELECT COUNT(*) FROM employees WHERE manager_id = :p_id';
EXECUTE sp_executesql @l_sql, N'@l_emp_count output, @p_id', @l_emp_count output @p_id;
set @p_emp_count = l_emp_count;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_emp_count = 0;
END
END sp_demo;
|
CREATE OR REPLACE PROCEDURE sp_demo(p_id IN NUMBER, p_name IN VARCHAR2, OUT p_emp_count OUT NUMBER) IS
l_sql VARCHAR2(4000);
l_emp_count NUMBER;
BEGIN
l_sql := 'SELECT COUNT(*) FROM employees WHERE manager_id = :p_id';
EXECUTE IMMEDIATE l_sql INTO l_emp_count USING p_id;
p_emp_count := l_emp_count;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_emp_count := 0;
END sp_demo;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF @v_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = 1;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
IF v_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = 1;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER)
IS
emp_salary NUMBER;
BEGIN
*** mployee's salary
SELECT @emp_salary = salary FROM employees WHERE employee_id = emp_id;
*** new salary is greater than the current salary
IF new_salary <= emp_salary BEGIN
RAISERROR('-20000 New salary must be greater than current salary', 16, 1);
END
*** ee's salary
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
-- Co... ***
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER)
IS
emp_salary NUMBER;
BEGIN
-- Get the current employee's salary
SELECT salary INTO emp_salary FROM employees WHERE employee_id = emp_id;
-- Validate that the new salary is greater than the current salary
IF new_salary <= emp_salary THEN
RAISE_APPLICATION_ERROR(-20000, 'New salary must be greater than current salary');
END IF;
-- Update the employee's salary
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
-- Commit the changes
COMMIT;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM emp WHERE ename = 'SMITH';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20101 Employee not found', 16, 1);
END
ELSE BEGIN
UPDATE emp SET salary = ROUND(v_salary * 1.10, 2) WHERE emp_id = @v_emp_id;
COMMIT;
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20102 No employee found', 16, 1);
END
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK;
RAISERROR('-20103 Duplicate employee id', 16, 1);
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM emp WHERE ename = 'SMITH';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20101, 'Employee not found');
ELSE
UPDATE emp SET salary = ROUND(v_salary * 1.10, 2) WHERE emp_id = v_emp_id;
COMMIT;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20102, 'No employee found');
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20103, 'Duplicate employee id');
END;
|
IF OBJECT_ID('calculate_salary', 'P') IS NOT NULL
DROP PROCEDURE calculate_salary;
GO
CREATE PROCEDURE calculate_salary(@employee_id IN AS NUMBER)
AS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL BEGIN
raiserror('-20001 Employee not found', 16, 1);
END
set @salary = salary * 1.1;
UPDATE employees SET salary = salary WHERE employee_id = employee_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE calculate_salary(employee_id IN NUMBER)
AS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL THEN
raise_application_error(-20001, 'Employee not found');
END IF;
salary := salary * 1.1;
UPDATE employees SET salary = salary WHERE employee_id = employee_id;
COMMIT;
END;
|
CREATE PROCEDURE employee_bonus(@employee_id IN AS NUMBER) IS
bonus_amount NUMBER;
BEGIN
SELECT @bonus_amount = bonus FROM employee_bonuses WHERE employee_id = employee_id;
IF bonus_amount IS NULL BEGIN
RAISERROR('-20001 No bonus defined for employee ' + isnull(employee_id, ''), 16, 1);
END
UPDATE employees SET salary = salary * (1 + bonus_amount) WHERE employee_id = employee_id;
END;
|
PROCEDURE employee_bonus(employee_id IN NUMBER) IS
bonus_amount NUMBER;
BEGIN
SELECT bonus INTO bonus_amount FROM employee_bonuses WHERE employee_id = employee_id;
IF bonus_amount IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No bonus defined for employee ' || employee_id);
END IF;
UPDATE employees SET salary = salary * (1 + bonus_amount) WHERE employee_id = employee_id;
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee (
@emp_id IN AS NUMBER,
emp_name IN VARCHAR2,
salary IN NUMBER
)
IS
l_emp_name dbo.VARCHAR2(20);
l_salary NUMBER;
BEGIN
SELECT @l_emp_name = emp_name, @l_salary = salary
FROM employees
WHERE emp_id = emp_id;
IF l_emp_name IS NULL BEGIN
RAISERROR('-20000 Employee not found.', 16, 1);
END
UPDATE employees
SET emp_name = emp_name,
salary = salary
WHERE emp_id = emp_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee (
emp_id IN NUMBER,
emp_name IN VARCHAR2,
salary IN NUMBER
)
IS
l_emp_name VARCHAR2(20);
l_salary NUMBER;
BEGIN
SELECT emp_name, salary INTO l_emp_name, l_salary
FROM employees
WHERE emp_id = emp_id;
IF l_emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found.');
END IF;
UPDATE employees
SET emp_name = emp_name,
salary = salary
WHERE emp_id = emp_id;
COMMIT;
END;
|
*** with Parameterized Input
CREATE PROCEDURE get_employee_by_id(@emp_id IN AS NUMBER) AS
emp_row employee%ROWTYPE;
BEGIN
SELECT @emp_row = * FROM employees WHERE employee_id = emp_id;
IF emp_row IS NULL BEGIN
RAISERROR('-20001 Employee with ID ' + isnull(emp_id, '') + ' not found', 16, 1);
END
dbms_output.put_line('Employee Name: ' + isnull(emp_row.first_name, '') + ' ' + isnull(emp_row.last_name, ''));
END;
/
*** h Conditional Clause
SELECT department_name
FROM departments
WHERE department_id IN (
SELECT department_id
FROM employees
WHERE salary > 50000
);
*** Control Flow Constructs
DECLARE
@emp_count FLOAT;
BEGIN
SELECT @emp_count = COUNT(*) FROM employees;
IF @emp_count > 100 BEGIN
DBMS_OUTPUT.PUT_LINE('Company has a large number of employees (' + isnull(@emp_count, '') + ').');
END
ELSE IF @emp_count BETWEEN 50 AND 100 BEGIN
DBMS_OUTPUT.PUT_LINE('Company has a medium number of employees (' + isnull(@emp_count, '') + ').');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Company has a small number of employees (' + isnull(@emp_count, '') + ').');
END
END;
/
*** or Handling
CREATE FUNCTION validate_email(@email IN VARCHAR2DECLARE ) RETURN
BEGIN BOOLEAN
RETURN NULL;
END; AS
BEGIN
IF email LIKE '%@%' BEGIN
RETURN TRUE;
END
ELSE BEGIN
RAISERROR('-20002 Invalid email format', 16, 1);
END
END;
/
|
-- Stored Procedure with Parameterized Input
CREATE PROCEDURE get_employee_by_id(emp_id IN NUMBER) AS
emp_row employee%ROWTYPE;
BEGIN
SELECT * INTO emp_row FROM employees WHERE employee_id = emp_id;
IF emp_row IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found');
END IF;
dbms_output.put_line('Employee Name: ' || emp_row.first_name || ' ' || emp_row.last_name);
END;
/
-- SQL Statement with Conditional Clause
SELECT department_name
FROM departments
WHERE department_id IN (
SELECT department_id
FROM employees
WHERE salary > 50000
);
-- PL/SQL Block with Control Flow Constructs
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees;
IF emp_count > 100 THEN
DBMS_OUTPUT.PUT_LINE('Company has a large number of employees (' || emp_count || ').');
ELSIF emp_count BETWEEN 50 AND 100 THEN
DBMS_OUTPUT.PUT_LINE('Company has a medium number of employees (' || emp_count || ').');
ELSE
DBMS_OUTPUT.PUT_LINE('Company has a small number of employees (' || emp_count || ').');
END IF;
END;
/
-- Function with Error Handling
CREATE FUNCTION validate_email(email IN VARCHAR2) RETURN BOOLEAN AS
BEGIN
IF email LIKE '%@%' THEN
RETURN TRUE;
ELSE
RAISE_APPLICATION_ERROR(-20002, 'Invalid email format');
END IF;
END;
/
|
BEGIN
-- De... ***
v_empno NUMBER;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
-- Op... ***
SELECT empno, ename, sal, comm
FROM emp;
-- Fe... ***
FETCH cur_emp INTO @v_empno, @v_ename, @v_sal, @v_comm;
-- Pr... ***
IF v_sal > 10000 BEGIN
-- Up... ***
UPDATE emp SET sal = v_sal + 1000
WHERE empno = v_empno;
-- Co... ***
COMMIT;
END
-- Cl... ***
CLOSE cur_emp;
EXCEPTION
-- Ha... ***
WHEN OTHERS THEN
-- Ro... ***
ROLLBACK;
*** message
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END; | BEGIN
-- Declare variables
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
-- Open a cursor
OPEN cur_emp FOR
SELECT empno, ename, sal, comm
FROM emp;
-- Fetch the first row
FETCH cur_emp INTO v_empno, v_ename, v_sal, v_comm;
-- Process the row
IF v_sal > 10000 THEN
-- Update the row
UPDATE emp SET sal = v_sal + 1000
WHERE empno = v_empno;
-- Commit the change
COMMIT;
END IF;
-- Close the cursor
CLOSE cur_emp;
EXCEPTION
-- Handle errors
WHEN OTHERS THEN
-- Rollback the change
ROLLBACK;
-- Display the error message
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END; |
CREATE PROCEDURE emp_bonus(@p_empid IN AS NUMBER, p_bonus IN NUMBER)
IS
BEGIN
UPDATE employees
SET salary = salary + p_bonus
WHERE employee_id = p_empid;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20000 Error processing bonus', 16, 1);
END emp_bonus;
|
CREATE PROCEDURE emp_bonus(p_empid IN NUMBER, p_bonus IN NUMBER)
IS
BEGIN
UPDATE employees
SET salary = salary + p_bonus
WHERE employee_id = p_empid;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20000, 'Error processing bonus');
END emp_bonus;
|
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL
DROP PROCEDURE sample_proc;
GO
CREATE PROCEDURE sample_proc(@x IN AS NUMBER, y OUT NUMBER)
IS
BEGIN
set @y = x + 1;
IF x < 0 BEGIN
RAISERROR('-20001 Invalid input: ' + ISNULL(CONVERT(VARCHAR, x), ''), 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
set @y = NULL;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE sample_proc(x IN NUMBER, y OUT NUMBER)
IS
BEGIN
y := x + 1;
IF x < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid input: ' || TO_CHAR(x));
END IF;
EXCEPTION
WHEN OTHERS THEN
y := NULL;
RAISE;
END;
|
BEGIN
BEGIN
UPDATE Employees SET salary = salary * 1.10 WHERE department_id = 10;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
@average_salary DECIMAL(10, 2);
BEGIN
SELECT @average_salary = AVG(salary) FROM Employees;
DBMS_OUTPUT.PUT_LINE('Average salary: ' + ISNULL(CONVERT(VARCHAR, @average_salary), ''));
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
DECLARE emp CURSOR FOR SELECT * FROM Employees; OPEN emp;
FETCH emp INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@emp_empno, '') + ' ' + isnull(@emp_ename, ''));
FETCH emp INTO;
END;
CLOSE emp;
DEALLOCATE emp;
IF @average_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('High average salary'); END
ELSE
DBMS_OUTPUT.PUT_LINE('Low average salary'); END
IF @average_salary > 8000 BEGIN
DBMS_OUTPUT.PUT_LINE('8000 exceeded');
ELIF @average_salary > 6000 THEN
DBMS_OUTPUT.PUT_LINE('6000 exceeded');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('6000 not exceeded');
END
END;
/
|
BEGIN
BEGIN
UPDATE Employees SET salary = salary * 1.10 WHERE department_id = 10;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
average_salary NUMBER(10, 2);
BEGIN
SELECT AVG(salary) INTO average_salary FROM Employees;
DBMS_OUTPUT.PUT_LINE('Average salary: ' || TO_CHAR(average_salary));
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
FOR emp IN (SELECT * FROM Employees) LOOP
DBMS_OUTPUT.PUT_LINE(emp.empno || ' ' || emp.ename);
END LOOP;
CASE
WHEN average_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('High average salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low average salary');
END CASE;
IF average_salary > 8000 THEN
DBMS_OUTPUT.PUT_LINE('8000 exceeded');
ELIF average_salary > 6000 THEN
DBMS_OUTPUT.PUT_LINE('6000 exceeded');
ELSE
DBMS_OUTPUT.PUT_LINE('6000 not exceeded');
END IF;
END;
/
|
CREATE PROCEDURE example_procedure(@a IN AS NUMBER, b OUT NUMBER)
AS
BEGIN
SELECT @b = a * 2;
END;
BEGIN
DECLARE @x FLOAT;
BEGIN
dbo.example_procedure(10, @x);
DBMS_OUTPUT.PUT_LINE('x: ' + isnull(@x, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
END;
|
CREATE PROCEDURE example_procedure(a IN NUMBER, b OUT NUMBER)
AS
BEGIN
SELECT a * 2 INTO b FROM dual;
END;
BEGIN
DECLARE x NUMBER;
BEGIN
example_procedure(10, x);
DBMS_OUTPUT.PUT_LINE('x: ' || x);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary_out NUMBER;
BEGIN
SELECT @salary_out = salary
FROM emp
WHERE empno = emp_id;
RETURN salary_out;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN -1;
END
END get_employee_salary;
BEGIN
DECLARE emp_rec CURSOR FOR SELECT empno, ename, dbo.get_employee_salary(empno) AS salary
FROM emp;
OPEN emp_rec;
FETCH emp_rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@emp_rec_ename, '') + ' earns ' + isnull(@emp_rec_salary, ''));
FETCH emp_rec INTO;
END;
CLOSE emp_rec;
DEALLOCATE emp_rec;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
salary_out NUMBER;
BEGIN
SELECT salary INTO salary_out
FROM emp
WHERE empno = emp_id;
RETURN salary_out;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
END get_employee_salary;
BEGIN
FOR emp_rec IN (SELECT empno, ename, get_employee_salary(empno) AS salary
FROM emp)
LOOP
DBMS_OUTPUT.PUT_LINE(emp_rec.ename || ' earns ' || emp_rec.salary);
END LOOP;
END;
|
CREATE TABLE employee (emp_id FLOAT, emp_name VARCHAR(50), emp_dept VARCHAR(20));
INSERT INTO employee (emp_id, emp_name, emp_dept) VALUES (101, 'John Doe', 'Sales');
INSERT INTO employee (emp_id, emp_name, emp_dept) VALUES (102, 'Jane Smith', 'Marketing');
CREATE PROCEDURE get_employee (@emp_id IN AS NUMBER, OUT emp_name VARCHAR2, OUT emp_dept VARCHAR2) AS
BEGIN
SELECT @emp_name = emp_name, @emp_dept = emp_dept
FROM employee
WHERE emp_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR ('-20001 Employee not found', 16, 1);
END
END;
BEGIN
dbo.get_employee(101, :emp_name, :emp_dept);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Dept: ' + isnull(emp_dept, ''));
END;
|
CREATE TABLE employee (emp_id NUMBER, emp_name VARCHAR2(50), emp_dept VARCHAR2(20));
INSERT INTO employee (emp_id, emp_name, emp_dept) VALUES (101, 'John Doe', 'Sales');
INSERT INTO employee (emp_id, emp_name, emp_dept) VALUES (102, 'Jane Smith', 'Marketing');
CREATE PROCEDURE get_employee (emp_id IN NUMBER, OUT emp_name VARCHAR2, OUT emp_dept VARCHAR2) AS
BEGIN
SELECT emp_name, emp_dept
INTO emp_name, emp_dept
FROM employee
WHERE emp_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20001, 'Employee not found');
END;
BEGIN
get_employee(101, :emp_name, :emp_dept);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Dept: ' || emp_dept);
END;
|
IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL
DROP PROCEDURE get_employee_info;
GO
CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER, OUT fname OUT VARCHAR2, OUT lname OUT VARCHAR2, OUT salary OUT NUMBER)
IS
BEGIN
SELECT @fname = first_name, @lname = last_name, @salary = salary
FROM employees
WHERE employee_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @fname = NULL;
set @lname = NULL;
set @salary = NULL;
END
END;
|
CREATE OR REPLACE PROCEDURE get_employee_info(emp_id IN NUMBER, OUT fname OUT VARCHAR2, OUT lname OUT VARCHAR2, OUT salary OUT NUMBER)
IS
BEGIN
SELECT first_name, last_name, salary
INTO fname, lname, salary
FROM employees
WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
fname := NULL;
lname := NULL;
salary := NULL;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@param_in IN AS VARCHAR2)
AS
BEGIN
UPDATE my_table SET my_column = UPPER(param_in)
WHERE id = 1;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 No rows updated', 16, 1);
END
END my_proc;
/
|
CREATE OR REPLACE PROCEDURE my_proc(param_in IN VARCHAR2)
AS
BEGIN
UPDATE my_table SET my_column = UPPER(param_in)
WHERE id = 1;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows updated');
END IF;
END my_proc;
/
|
BEGIN
DECLARE @name VARCHAR(50);
BEGIN
SELECT @name = @name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with id 1');
END
END;
DECLARE
@COUNT FLOAT;
BEGIN
SELECT @COUNT = COUNT(*) FROM employees;
DBMS_OUTPUT.PUT_LINE('Total employees: ' + ISNULL(@COUNT, ''));
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many employees to count');
END;
|
BEGIN
DECLARE name VARCHAR(50);
BEGIN
SELECT name INTO name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with id 1');
END;
DECLARE
COUNT NUMBER;
BEGIN
SELECT COUNT(*) INTO COUNT FROM employees;
DBMS_OUTPUT.PUT_LINE('Total employees: ' || COUNT);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many employees to count');
END;
|
CREATE TABLE employees (eno FLOAT, ename VARCHAR(50), deptno FLOAT, salary FLOAT);
INSERT INTO employees VALUES (10, 'John Doe', 10, 5000);
INSERT INTO employees VALUES (20, 'Jane Smith', 20, 6000);
INSERT INTO employees VALUES (30, 'Bill Jones', 30, 7000);
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 3 BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE eno = i;
END;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
DECLARE
@v_eno VARCHAR(4000) /* Use -meta option employees.eno%TYPE */ := 10;
v_salary employees.salary%TYPE;
BEGIN
SELECT @v_salary = salary FROM employees WHERE eno = @v_eno;
DBMS_OUTPUT.PUT_LINE('Salary of employee ' + isnull(@v_eno, '') + ': ' + isnull(v_salary, ''));
END;
/
|
CREATE TABLE employees (eno NUMBER, ename VARCHAR2(50), deptno NUMBER, salary NUMBER);
INSERT INTO employees VALUES (10, 'John Doe', 10, 5000);
INSERT INTO employees VALUES (20, 'Jane Smith', 20, 6000);
INSERT INTO employees VALUES (30, 'Bill Jones', 30, 7000);
BEGIN
FOR i IN 1..3 LOOP
UPDATE employees SET salary = salary * 1.1 WHERE eno = i;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
DECLARE
v_eno employees.eno%TYPE := 10;
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE eno = v_eno;
DBMS_OUTPUT.PUT_LINE('Salary of employee ' || v_eno || ': ' || v_salary);
END;
/
|
IF OBJECT_ID('get_employee_count', 'FN') IS NOT NULL
DROP FUNCTION get_employee_count;
GO
CREATE FUNCTION get_employee_count(@department_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
emp_count NUMBER;
BEGIN
SELECT @emp_count = COUNT(*) FROM employees WHERE department_id = department_id;
RETURN emp_count;
EXCEPTION
WHEN OTHERS THEN
RETURN -1;
END;
DECLARE
@v_emp_count FLOAT;
BEGIN
SET @v_emp_count = dbo.get_employee_count(10);
IF @v_emp_count < 10 BEGIN
RAISERROR('-20001 Employee count is less than 10', 16, 1);
END
COMMIT;
END;
|
CREATE OR REPLACE FUNCTION get_employee_count(department_id IN NUMBER) RETURN NUMBER IS
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees WHERE department_id = department_id;
RETURN emp_count;
EXCEPTION
WHEN OTHERS THEN
RETURN -1;
END;
DECLARE
v_emp_count NUMBER;
BEGIN
v_emp_count := get_employee_count(10);
IF v_emp_count < 10 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee count is less than 10');
END IF;
COMMIT;
END;
|
DECLARE
@v_salary FLOAT := 35000;
BEGIN
IF @v_salary < 40000 BEGIN
RAISERROR('-20001 Salary should be greater than 40000', 16, 1);
END
END;
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details(
@p_employee_id IN AS NUMBER,
OUT emp_name VARCHAR2,
OUT job_title VARCHAR2,
OUT salary NUMBER
) AS
BEGIN
SELECT @emp_name = name, @job_title = job_title, @salary = salary
FROM employees
WHERE employee_id = p_employee_id;
END;
BEGIN
DECLARE
@e_name VARCHAR(20);
e_job dbo.VARCHAR2(20);
e_salary NUMBER;
BEGIN
dbo.get_employee_details(101, @e_name, e_job, e_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@e_name, ''));
DBMS_OUTPUT.PUT_LINE('Job Title: ' + isnull(e_job, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(e_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
|
DECLARE
v_salary NUMBER := 35000;
BEGIN
IF v_salary < 40000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary should be greater than 40000');
END IF;
END;
CREATE OR REPLACE PROCEDURE get_employee_details(
p_employee_id IN NUMBER,
OUT emp_name VARCHAR2,
OUT job_title VARCHAR2,
OUT salary NUMBER
) AS
BEGIN
SELECT name, job_title, salary
INTO emp_name, job_title, salary
FROM employees
WHERE employee_id = p_employee_id;
END;
BEGIN
DECLARE
e_name VARCHAR2(20);
e_job VARCHAR2(20);
e_salary NUMBER;
BEGIN
get_employee_details(101, e_name, e_job, e_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || e_name);
DBMS_OUTPUT.PUT_LINE('Job Title: ' || e_job);
DBMS_OUTPUT.PUT_LINE('Salary: ' || e_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
|
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
*** the table
INSERT INTO myTable (id, value) VALUES (i, i * i);
END;
-- Co... ***
COMMIT;
EXCEPTION
*** that occur
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | BEGIN
FOR i IN 1..10 LOOP
-- Insert a row into the table
INSERT INTO myTable (id, value) VALUES (i, i * i);
END LOOP;
-- Commit the changes
COMMIT;
EXCEPTION
-- Handle any errors that occur
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
CREATE TABLE orders (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL,
order_date DATE NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL
);
CREATE TABLE customers (
customer_id BIGINT PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL
);
CREATE PROCEDURE get_order_total (
@p_order_id IN AS NUMBER,
p_total_amount OUT NUMBER
) AS
BEGIN
SELECT @p_total_amount = total_amount
FROM orders
WHERE order_id = p_order_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_total_amount = 0;
END
END;
BEGIN
DBMS_OUTPUT.PUT_LINE('Customer Orders:');
DECLARE rec CURSOR FOR
SELECT c.customer_name, o.order_date, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_customer_name, '') + ' - ' + isnull(@rec_order_date, '') + ' - ' + ISNULL(FORMAT(@rec_total_amount, 'N2'), ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
DBMS_OUTPUT.PUT_LINE('End of Customer Orders');
END;
|
CREATE TABLE orders (
order_id NUMBER(10) PRIMARY KEY,
customer_id NUMBER(10) NOT NULL,
order_date DATE NOT NULL,
total_amount NUMBER(10, 2) NOT NULL
);
CREATE TABLE customers (
customer_id NUMBER(10) PRIMARY KEY,
customer_name VARCHAR2(50) NOT NULL
);
CREATE PROCEDURE get_order_total (
p_order_id IN NUMBER,
p_total_amount OUT NUMBER
) AS
BEGIN
SELECT total_amount
INTO p_total_amount
FROM orders
WHERE order_id = p_order_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_total_amount := 0;
END;
BEGIN
DBMS_OUTPUT.PUT_LINE('Customer Orders:');
FOR rec IN (
SELECT c.customer_name, o.order_date, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
) LOOP
DBMS_OUTPUT.PUT_LINE(rec.customer_name || ' - ' || rec.order_date || ' - ' || TO_CHAR(rec.total_amount, '999,999.99'));
END LOOP;
DBMS_OUTPUT.PUT_LINE('End of Customer Orders');
END;
|
PL/SQL
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL
DROP PROCEDURE emp_bonus;
GO
CREATE PROCEDURE emp_bonus(@emp_id IN AS NUMBER, bonus_percent IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary * (1 + bonus_percent / 100)
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20100 Error updating employee salary', 16, 1);
END emp_bonus;
| PL/SQL
CREATE OR REPLACE PROCEDURE emp_bonus(emp_id IN NUMBER, bonus_percent IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary * (1 + bonus_percent / 100)
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20100, 'Error updating employee salary');
END emp_bonus;
|
DECLARE
@emp_no FLOAT;
BEGIN
SELECT @emp_no = @emp_no FROM employees WHERE first_name = 'John';
IF @emp_no IS NOT NULL BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE @emp_no = @emp_no;
END
ELSE BEGIN
RAISERROR('-20101 Employee ' + 'John' + ' not found', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END *** und errors
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
emp_no NUMBER;
BEGIN
SELECT emp_no INTO emp_no FROM employees WHERE first_name = 'John';
IF emp_no IS NOT NULL THEN
UPDATE employees SET salary = salary * 1.10 WHERE emp_no = emp_no;
ELSE
RAISE_APPLICATION_ERROR(-20101, 'Employee ' || 'John' || ' not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL; -- Ignore no data found errors
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary FROM emp WHERE empno = @v_empno;
IF v_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('High salary');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Low salary');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_empno NUMBER := 10;
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM emp WHERE empno = v_empno;
IF v_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('High salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low salary');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
CREATE PROCEDURE get_employee_details (
@emp_id IN AS NUMBER
) AS
emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
END;
/
DECLARE
@emp_id FLOAT;
BEGIN
SET @emp_id = 100;
dbo.get_employee_details(@emp_id);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with employee ID ' + isnull(@emp_id, ''));
END
END;
/
|
CREATE PROCEDURE get_employee_details (
emp_id IN NUMBER
) AS
emp_name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name INTO emp_name
FROM employees
WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
END;
/
DECLARE
emp_id NUMBER;
BEGIN
emp_id := 100;
get_employee_details(emp_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with employee ID ' || emp_id);
END;
/
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal
FROM emp
WHERE empno = 10;
IF v_sal < 30000 BEGIN
SET @v_sal = v_sal * 1.1;
END
ELSE BEGIN
SET @v_sal = v_sal * 1.05;
END
UPDATE emp SET sal = v_sal WHERE empno = 10;
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT empno, ename, sal
INTO v_empno, v_ename, v_sal
FROM emp
WHERE empno = 10;
IF v_sal < 30000 THEN
v_sal := v_sal * 1.1;
ELSE
v_sal := v_sal * 1.05;
END IF;
UPDATE emp SET sal = v_sal WHERE empno = 10;
END;
|
CREATE PROCEDURE get_employee_info (@p_employee_id IN AS NUMBER) AS
BEGIN
*** iables
v_first_name dbo.VARCHAR2(50);
v_last_name dbo.VARCHAR2(50);
v_email dbo.VARCHAR2(100);
*** e to retrieve employee information
SELECT @v_first_name = first_name, @v_last_name = last_name, @v_email = email
FROM employees
WHERE employee_id = p_employee_id;
*** ee information
DBMS_OUTPUT.put_line('Employee ID: ' + isnull(p_employee_id, ''));
DBMS_OUTPUT.put_line('First Name: ' + isnull(v_first_name, ''));
DBMS_OUTPUT.put_line('Last Name: ' + isnull(v_last_name, ''));
DBMS_OUTPUT.put_line('Email: ' + isnull(v_email, ''));
END get_employee_info;
/
*** rocedure to retrieve employee information
BEGIN
dbo.get_employee_info(100);
END;
/
|
CREATE PROCEDURE get_employee_info (p_employee_id IN NUMBER) AS
BEGIN
-- Declare local variables
v_first_name VARCHAR2(50);
v_last_name VARCHAR2(50);
v_email VARCHAR2(100);
-- Query the database to retrieve employee information
SELECT first_name, last_name, email
INTO v_first_name, v_last_name, v_email
FROM employees
WHERE employee_id = p_employee_id;
-- Output the employee information
DBMS_OUTPUT.put_line('Employee ID: ' || p_employee_id);
DBMS_OUTPUT.put_line('First Name: ' || v_first_name);
DBMS_OUTPUT.put_line('Last Name: ' || v_last_name);
DBMS_OUTPUT.put_line('Email: ' || v_email);
END get_employee_info;
/
-- Call the stored procedure to retrieve employee information
BEGIN
get_employee_info(100);
END;
/
|
DECLARE
@salary FLOAT := 12000;
BEGIN
DECLARE
@bonus FLOAT;
BEGIN
IF @salary > 10000 BEGIN
SET @bonus = @salary * 0.1;
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.');
END;
SET @salary = @salary + @bonus;
END;
|
DECLARE
salary NUMBER := 12000;
BEGIN
DECLARE
bonus NUMBER;
BEGIN
IF salary > 10000 THEN
bonus := salary * 0.1;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.');
END;
salary := salary + bonus;
END;
|
DECLARE
@v_num_rows INTEGER;
BEGIN
SELECT @v_num_rows = COUNT(*) FROM employees;
IF @v_num_rows > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('There are ' + isnull(@v_num_rows, '') + ' rows in the employees table.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('The employees table is empty.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Error: There is no data in the employees table.');
END
END;
|
DECLARE
v_num_rows PLS_INTEGER;
BEGIN
SELECT COUNT(*) INTO v_num_rows FROM employees;
IF v_num_rows > 0 THEN
DBMS_OUTPUT.PUT_LINE('There are ' || v_num_rows || ' rows in the employees table.');
ELSE
DBMS_OUTPUT.PUT_LINE('The employees table is empty.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: There is no data in the employees table.');
END;
|
DECLARE
@l_var VARCHAR(10);
l_result BOOLEAN;
BEGIN
SELECT @l_var = name FROM sys.all_users;
SET @l_result = NOT FOUND;
DECLARE rec CURSOR FOR SELECT username, email FROM users WHERE username = @l_var; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
SET @l_result = TRUE;
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF NOT l_result BEGIN
RAISERROR('-20001 User not found', 16, 1);
END
END;
|
DECLARE
l_var VARCHAR2(10);
l_result BOOLEAN;
BEGIN
SELECT name INTO l_var FROM sys.all_users;
l_result := NOT FOUND;
FOR rec IN (SELECT username, email FROM users WHERE username = l_var) LOOP
l_result := TRUE;
END LOOP;
IF NOT l_result THEN
RAISE_APPLICATION_ERROR(-20001, 'User not found');
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary, @v_dept_id = department_id
FROM employees
WHERE last_name = 'Scott';
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = v_salary * 1.10
WHERE employee_id = @v_emp_id;
END
ELSE IF v_salary <= 10000 BEGIN
UPDATE employees
SET salary = v_salary * 1.05
WHERE employee_id = @v_emp_id;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
SELECT employee_id, salary, department_id
INTO v_emp_id, v_salary, v_dept_id
FROM employees
WHERE last_name = 'Scott';
IF v_salary > 10000 THEN
UPDATE employees
SET salary = v_salary * 1.10
WHERE employee_id = v_emp_id;
ELSIF v_salary <= 10000 THEN
UPDATE employees
SET salary = v_salary * 1.05
WHERE employee_id = v_emp_id;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 Error updating employee salary', 16, 1);
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
emp_salary NUMBER;
BEGIN
SET @emp_salary = dbo.get_employee_salary(@emp_id);
IF emp_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
dbo.update_employee_salary(@emp_id, emp_salary * 1.10);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'Error updating employee salary');
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(emp_id);
IF emp_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
update_employee_salary(emp_id, emp_salary * 1.10);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@id IN AS NUMBER)
IS
v_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_name = name FROM employees WHERE id = id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_name, ''));
-- Er... ***
IF v_name IS NULL BEGIN
RAISERROR('-20001 No employee found with ID ' + isnull(id, ''), 16, 1);
END
END my_proc;
BEGIN
dbo.my_proc(100);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_proc(id IN NUMBER)
IS
v_name VARCHAR2(20);
BEGIN
SELECT name INTO v_name FROM employees WHERE id = id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
-- Error handling
IF v_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with ID ' || id);
END IF;
END my_proc;
BEGIN
my_proc(100);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_emp_id FLOAT := 10;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_emp_name = emp_name FROM employees WHERE emp_id = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(@v_emp_id, ''));
END
END;
CREATE PROCEDURE update_salary(@p_emp_id IN AS NUMBER, p_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = p_salary WHERE emp_id = p_emp_id;
COMMIT;
END;
CREATE FUNCTION get_total_salary() RETURNS FLOAT
AS
BEGIN
DECLARE @v_total_salary FLOAT;
SELECT @v_total_salary = SUM(salary) FROM employees;
RETURN @v_total_salary;
END;
|
DECLARE
v_emp_id NUMBER := 10;
v_emp_name VARCHAR2(20);
BEGIN
SELECT emp_name INTO v_emp_name FROM employees WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_emp_id);
END;
PROCEDURE update_salary(p_emp_id IN NUMBER, p_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = p_salary WHERE emp_id = p_emp_id;
COMMIT;
END;
FUNCTION get_total_salary() RETURN NUMBER
IS
v_total_salary NUMBER;
BEGIN
SELECT SUM(salary) INTO v_total_salary FROM employees;
RETURN v_total_salary;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
emp_id, salary
employees
emp_name ;
v_salary 50000 BEGIN
employees
SET bonus 0.1 salary
emp_id ;
END
ELSE v_salary 30000 BEGIN
employees
SET bonus 0.05 salary
emp_id ;
END
@ 0 BEGIN
RAERR, 16, 1;
END
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE emp_name = 'John Doe';
IF v_salary > 50000 THEN
UPDATE employees
SET bonus = 0.1 * salary
WHERE emp_id = v_emp_id;
ELSIF v_salary > 30000 THEN
UPDATE employees
SET bonus = 0.05 * salary
WHERE emp_id = v_emp_id;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
|
CREATE PROCEDURE insert_employee(
@p_employee_id FLOAT,
@p_first_name VARCHAR(4000),
@p_last_name VARCHAR(4000),
@p_email VARCHAR(4000),
@p_phone_number VARCHAR(4000),
@p_hire_date DATE,
@p_job_id VARCHAR(4000),
@p_salary FLOAT,
@p_commission_pct FLOAT,
@p_manager_id FLOAT
) AS
*** iables
v_insert_count NUMBER;
BEGIN
*** oyee into the EMPLOYEES table
INSERT INTO EMPLOYEES (
employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id
) VALUES (
p_employee_id,
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_commission_pct,
p_manager_id
);
*** rows inserted
SELECT @v_insert_count = COUNT(*) FROM EMPLOYEES WHERE employee_id = p_employee_id;
*** rt was successful
IF v_insert_count = 0 BEGIN
*** the insert failed
RAISERROR('-20000 Error inserting employee', 16, 1);
END
END;
/
CALL insert_employee(100, 'John', 'Doe', 'john.doe@example.com', '555-555-5555', CONVERT(DATETIME, '2023-02-01'), 'IT_PROG', 50000, 0.1, 10);
|
CREATE PROCEDURE insert_employee(
p_employee_id NUMBER,
p_first_name VARCHAR2,
p_last_name VARCHAR2,
p_email VARCHAR2,
p_phone_number VARCHAR2,
p_hire_date DATE,
p_job_id VARCHAR2,
p_salary NUMBER,
p_commission_pct NUMBER,
p_manager_id NUMBER
) IS
-- Declare local variables
v_insert_count NUMBER;
BEGIN
-- Insert a new employee into the EMPLOYEES table
INSERT INTO EMPLOYEES (
employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id
) VALUES (
p_employee_id,
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_commission_pct,
p_manager_id
);
-- Get the number of rows inserted
SELECT COUNT(*) INTO v_insert_count FROM EMPLOYEES WHERE employee_id = p_employee_id;
-- Check if the insert was successful
IF v_insert_count = 0 THEN
-- Raise an error if the insert failed
RAISE_APPLICATION_ERROR(-20000, 'Error inserting employee');
END IF;
END;
/
CALL insert_employee(100, 'John', 'Doe', 'john.doe@example.com', '555-555-5555', TO_DATE('2023-02-01', 'YYYY-MM-DD'), 'IT_PROG', 50000, 0.1, 10);
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'EMP1';
IF @v_empno IS NOT NULL BEGIN
UPDATE emp SET sal = sal + 100 WHERE empno = @v_empno;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20100 Employee not found.', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that name.');
END
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred. Contact your system administrator.');
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'EMP1';
IF v_empno IS NOT NULL THEN
UPDATE emp SET sal = sal + 100 WHERE empno = v_empno;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20100, 'Employee not found.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that name.');
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred. Contact your system administrator.');
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
-- Ge... ***
SELECT @v_emp_id = emp_id
FROM employees
WHERE last_name = 'Scott';
*** lary
SELECT @v_salary = salary
FROM salaries
WHERE emp_id = @v_emp_id;
*** by 10%
UPDATE salaries
SET salary = salary * 1.10
WHERE emp_id = @v_emp_id;
*** the salary is too high
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary is too high!', 16, 1);
END
-- Co... ***
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
-- Get employee ID
SELECT emp_id
INTO v_emp_id
FROM employees
WHERE last_name = 'Scott';
-- Get employee's salary
SELECT salary
INTO v_salary
FROM salaries
WHERE emp_id = v_emp_id;
-- Raise the salary by 10%
UPDATE salaries
SET salary = salary * 1.10
WHERE emp_id = v_emp_id;
-- Raise an error if the salary is too high
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary is too high!');
END IF;
-- Commit the changes
COMMIT;
END;
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = employee_id
FROM employees
WHERE last_name = 'King';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 No employee found with last name King', 16, 1);
END
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT employee_id
INTO v_emp_id
FROM employees
WHERE last_name = 'King';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with last name King');
END IF;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_emp_id;
COMMIT;
END;
|
CREATE PROCEDURE update_employee(@p_empid IN AS NUMBER, p_salary IN NUMBER)
AS
l_oldsalary NUMBER;
BEGIN
SELECT @l_oldsalary = salary FROM employees WHERE empid = p_empid;
IF p_salary > l_oldsalary BEGIN
UPDATE employees SET salary = p_salary WHERE empid = p_empid;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('New salary must be greater than old salary');
END
END;
|
PROCEDURE update_employee(p_empid IN NUMBER, p_salary IN NUMBER)
AS
l_oldsalary NUMBER;
BEGIN
SELECT salary INTO l_oldsalary FROM employees WHERE empid = p_empid;
IF p_salary > l_oldsalary THEN
UPDATE employees SET salary = p_salary WHERE empid = p_empid;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
ELSE
DBMS_OUTPUT.PUT_LINE('New salary must be greater than old salary');
END IF;
END;
|
CREATE PROCEDURE get_employee_info(
@employee_id IN AS NUMBER,
OUT OUT p_first_name VARCHAR(25),
OUT OUT p_last_name VARCHAR(25)
)
AS
BEGIN
SELECT @p_first_name = first_name, @p_last_name = last_name
FROM employees
WHERE employee_id = employee_id;
END;
BEGIN
DECLARE
@v_first_name VARCHAR(25);
v_last_name VARCHAR(25);
BEGIN
dbo.get_employee_info(1, @v_first_name, v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee info: ' + isnull(@v_first_name, '') + ' ' + isnull(v_last_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE PROCEDURE get_employee_info(
employee_id IN NUMBER,
OUT OUT p_first_name VARCHAR(25),
OUT OUT p_last_name VARCHAR(25)
)
AS
BEGIN
SELECT first_name, last_name
INTO p_first_name, p_last_name
FROM employees
WHERE employee_id = employee_id;
END;
BEGIN
DECLARE
v_first_name VARCHAR(25);
v_last_name VARCHAR(25);
BEGIN
get_employee_info(1, v_first_name, v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee info: ' || v_first_name || ' ' || v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
|
CREATE PROCEDURE get_customer_orders (@p_cust_id IN AS NUMBER) AS
BEGIN
SELECT * FROM orders WHERE cust_id = p_cust_id;
END;
|
CREATE PROCEDURE get_customer_orders (p_cust_id IN NUMBER) AS
BEGIN
SELECT * FROM orders WHERE cust_id = p_cust_id;
END;
|
DECLARE
@v_empid FLOAT := 100;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary
FROM emp
WHERE empid = @v_empid;
IF v_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No data found', 16, 1);
END
END;
/
|
DECLARE
v_empid NUMBER := 100;
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM emp
WHERE empid = v_empid;
IF v_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No data found');
END;
/
|
DECLARE
@cursor_name SYS_REFCURSOR;
v_department_id NUMBER;
BEGIN
SELECT department_id FROM hr.departments;
WHILE 1=1 BEGIN
FETCH cursor_name INTO @v_department_id;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(v_department_id);
END;
CLOSE cursor_name;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
|
DECLARE
cursor_name SYS_REFCURSOR;
v_department_id NUMBER;
BEGIN
OPEN cursor_name FOR SELECT department_id FROM hr.departments;
LOOP
FETCH cursor_name INTO v_department_id;
EXIT WHEN cursor_name%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_department_id);
END LOOP;
CLOSE cursor_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
|
PL/SQL
DECLARE
@var1 FLOAT := 1;
@var2 SET @NUMBER = 2;
BEGIN
IF @var1 > @var2 BEGIN
RAISERROR('-20001 Var1 is greater than Var2', 16, 1);
END
END;
| PL/SQL
DECLARE
@var1 NUMBER := 1;
@var2 NUMBER := 2;
BEGIN
IF @var1 > @var2 THEN
RAISE_APPLICATION_ERROR(-20001, 'Var1 is greater than Var2');
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_sal NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_sal = salary FROM emp WHERE emp_id = 10;
IF v_sal > 10000 BEGIN
UPDATE emp SET bonus = v_sal * 0.1 WHERE emp_id = @v_emp_id;
END
ELSE IF v_sal > 5000 BEGIN
UPDATE emp SET bonus = v_sal * 0.05 WHERE emp_id = @v_emp_id;
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No data found for employee with id: ' + isnull(@v_emp_id, ''), 16, 1);
END
WHEN OTHERS THEN
*** re-raise it
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_sal NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_sal FROM emp WHERE emp_id = 10;
IF v_sal > 10000 THEN
UPDATE emp SET bonus = v_sal * 0.1 WHERE emp_id = v_emp_id;
ELSIF v_sal > 5000 THEN
UPDATE emp SET bonus = v_sal * 0.05 WHERE emp_id = v_emp_id;
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No data found for employee with id: ' || v_emp_id);
WHEN OTHERS THEN
-- Log the error and re-raise it
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
RAISE;
END;
|
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL
DROP PROCEDURE sp_update_employee;
GO
CREATE PROCEDURE sp_update_employee(
@p_employee_id IN AS NUMBER,
p_salary IN NUMBER
)
IS
l_old_salary NUMBER;
BEGIN
SELECT @l_old_salary = salary
FROM employees
WHERE employee_id = p_employee_id;
IF l_old_salary > p_salary BEGIN
RAISERROR('-20001 Cannot decrease salary.', 16, 1);
END
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE sp_update_employee(
p_employee_id IN NUMBER,
p_salary IN NUMBER
)
IS
l_old_salary NUMBER;
BEGIN
SELECT salary INTO l_old_salary
FROM employees
WHERE employee_id = p_employee_id;
IF l_old_salary > p_salary THEN
RAISE_APPLICATION_ERROR(-20001, 'Cannot decrease salary.');
END IF;
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'KING';
IF @v_empno IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
ELSE BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
END
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'KING';
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
ELSE
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
END IF;
END;
|
CREATE PROCEDURE update_employee(@emp_id IN AS NUMBER, fname IN VARCHAR2, lname IN VARCHAR2, salary IN NUMBER)
IS
BEGIN
UPDATE employees SET fname = fname, lname = lname, salary = salary
WHERE emp_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
CREATE FUNCTION get_employee_name(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @emp_name = ename FROM employees WHERE empid = emp_id;
RETURN emp_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@salary FLOAT;
emp_name dbo.VARCHAR2(50);
BEGIN
SET @salary = CAST(dbo.GET_PARAMETER('SALARY') AS FLOAT);
SET @emp_name = dbo.GET_EMPLOYEE_NAME(1001);
IF @salary > 5000 BEGIN
dbo.UPDATE_EMPLOYEE(1001, emp_name, 'Doe', @salary + 1000);
END
ELSE BEGIN
RAISERROR('-20001 Salary too low for raise', 16, 1);
END
END;
|
PROCEDURE update_employee(emp_id IN NUMBER, fname IN VARCHAR2, lname IN VARCHAR2, salary IN NUMBER)
IS
BEGIN
UPDATE employees SET fname = fname, lname = lname, salary = salary
WHERE emp_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END;
FUNCTION get_employee_name(emp_id IN NUMBER) RETURN VARCHAR2
IS
emp_name VARCHAR2(50);
BEGIN
SELECT ename INTO emp_name FROM employees WHERE empid = emp_id;
RETURN emp_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
salary NUMBER;
emp_name VARCHAR2(50);
BEGIN
salary := TO_NUMBER(GET_PARAMETER('SALARY'));
emp_name := GET_EMPLOYEE_NAME(1001);
IF salary > 5000 THEN
UPDATE_EMPLOYEE(1001, emp_name, 'Doe', salary + 1000);
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary too low for raise');
END IF;
END;
|
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
salary DECIMAL(10,2) DEFAULT 0
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane Smith', 12000);
INSERT INTO employees (id, name, salary) VALUES (3, 'Bill Jones', 15000);
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary (@p_id IN AS NUMBER, OUT p_salary OUT NUMBER)
IS
BEGIN
SELECT @p_salary = salary
FROM employees
WHERE id = p_id;
END;
DECLARE
@v_salary FLOAT;
BEGIN
dbo.get_employee_salary(2, @v_salary);
DBMS_OUTPUT.PUT_LINE('Salary for employee 2: ' + isnull(@v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
-- Dr... ***
DROP PROCEDURE get_employee_salary;
DROP TABLE employees;
|
CREATE TABLE employees (
id NUMBER(6) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
salary NUMBER(10,2) DEFAULT 0
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane Smith', 12000);
INSERT INTO employees (id, name, salary) VALUES (3, 'Bill Jones', 15000);
CREATE OR REPLACE PROCEDURE get_employee_salary (p_id IN NUMBER, OUT p_salary OUT NUMBER)
IS
BEGIN
SELECT salary INTO p_salary
FROM employees
WHERE id = p_id;
END;
DECLARE
v_salary NUMBER;
BEGIN
get_employee_salary(2, v_salary);
DBMS_OUTPUT.PUT_LINE('Salary for employee 2: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
-- Drop objects
DROP PROCEDURE get_employee_salary;
DROP TABLE employees;
|
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details(
@p_employee_id IN AS NUMBER,
p_employee_name OUT VARCHAR2,
p_salary OUT NUMBER,
p_dept_id OUT NUMBER) IS
BEGIN
SELECT @p_employee_name = first_name,
@p_salary = salary,
@p_dept_id = department_id
FROM employees
WHERE employee_id = p_employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_employee_name = NULL;
set @p_salary = NULL;
set @p_dept_id = NULL;
END
END;
/
|
CREATE OR REPLACE PROCEDURE get_employee_details(
p_employee_id IN NUMBER,
p_employee_name OUT VARCHAR2,
p_salary OUT NUMBER,
p_dept_id OUT NUMBER) IS
BEGIN
SELECT first_name,
salary,
department_id
INTO p_employee_name,
p_salary,
p_dept_id
FROM employees
WHERE employee_id = p_employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_employee_name := NULL;
p_salary := NULL;
p_dept_id := NULL;
END;
/
|
if object_id('update_employees', 'P') is not null
drop procedure update_employees;
go
create procedure update_employees(
@salary in as number
)
is
emp_count pls_integer;
begin
update employees set salary = salary * :salary
where department_id = 10;
select @emp_count = count(*) from employees where salary > 50000;
if emp_count > 0 begin
raiserror('-20000 Too many employees with high salary', 16, 1);
end
commit;
end;
|
create or replace procedure update_employees(
salary in number
)
is
emp_count pls_integer;
begin
update employees set salary = salary * :salary
where department_id = 10;
select count(*) into emp_count from employees where salary > 50000;
if emp_count > 0 then
raise_application_error(-20000, 'Too many employees with high salary');
end if;
commit;
end;
|
IF OBJECT_ID('GET_CUST_INFO', 'P') IS NOT NULL
DROP PROCEDURE GET_CUST_INFO;
GO
CREATE PROCEDURE GET_CUST_INFO
(
@cust_id IN AS NUMBER,
OUT cust_name OUT VARCHAR2,
OUT cust_address OUT VARCHAR2,
OUT cust_city OUT VARCHAR2,
OUT cust_state OUT VARCHAR2,
OUT cust_zip OUT VARCHAR2
)
IS
BEGIN
SELECT @cust_name = * cust_address, cust_city, cust_state, cust_zip
FROM customers
WHERE customer_id = cust_id;
END;
BEGIN
DECLARE
@v_name VARCHAR(50);
v_address dbo.VARCHAR2(100);
v_city dbo.VARCHAR2(50);
v_state dbo.VARCHAR2(2);
v_zip dbo.VARCHAR2(10);
v_cust_id SET @NUMBER = 12345;
BEGIN
dbo.GET_CUST_INFO(v_cust_id, @v_name, v_address, v_city, v_state, v_zip);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Customer not found', 16, 1);
END
END;
END;
|
CREATE OR REPLACE PROCEDURE GET_CUST_INFO
(
cust_id IN NUMBER,
OUT cust_name OUT VARCHAR2,
OUT cust_address OUT VARCHAR2,
OUT cust_city OUT VARCHAR2,
OUT cust_state OUT VARCHAR2,
OUT cust_zip OUT VARCHAR2
)
IS
BEGIN
SELECT * INTO cust_name, cust_address, cust_city, cust_state, cust_zip
FROM customers
WHERE customer_id = cust_id;
END;
BEGIN
DECLARE
v_name VARCHAR2(50);
v_address VARCHAR2(100);
v_city VARCHAR2(50);
v_state VARCHAR2(2);
v_zip VARCHAR2(10);
v_cust_id NUMBER := 12345;
BEGIN
GET_CUST_INFO(v_cust_id, v_name, v_address, v_city, v_state, v_zip);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer not found');
END;
END;
|
sql
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@p1 IN AS NUMBER, p2 OUT VARCHAR2) AS
BEGIN
SELECT @p2 = name FROM users WHERE id = p1;
IF @@FETCH_STATUS <> 0 BEGIN
set @p2 = 'User not found';
END
WHEN OTHERS THEN
RAISE;
END;
| sql
CREATE OR REPLACE PROCEDURE my_proc(p1 IN NUMBER, p2 OUT VARCHAR2) AS
BEGIN
SELECT name INTO p2 FROM users WHERE id = p1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p2 := 'User not found';
WHEN OTHERS THEN
RAISE;
END;
|
IF OBJECT_ID('calculate_total_sales', 'P') IS NOT NULL
DROP PROCEDURE calculate_total_sales;
GO
CREATE PROCEDURE calculate_total_sales(
@p_start_date IN AS DATE,
p_end_date IN DATE,
OUT total_sales OUT NUMBER)
AS
BEGIN
SELECT @total_sales = SUM(sales)
FROM sales
WHERE sale_date BETWEEN p_start_date AND p_end_date;
IF @@FETCH_STATUS <> 0 BEGIN
set @total_sales = 0;
END
END;
DECLARE
@sales_total FLOAT;
BEGIN
dbo.calculate_total_sales(CONVERT(DATETIME, '2023-01-01'), CONVERT(DATETIME, '2023-03-31'), @sales_total);
DBMS_OUTPUT.PUT_LINE('Total sales for Q1 2023: ' + isnull(@sales_total, ''));
END;
|
CREATE OR REPLACE PROCEDURE calculate_total_sales(
p_start_date IN DATE,
p_end_date IN DATE,
OUT total_sales OUT NUMBER)
AS
BEGIN
SELECT SUM(sales) INTO total_sales
FROM sales
WHERE sale_date BETWEEN p_start_date AND p_end_date;
EXCEPTION
WHEN NO_DATA_FOUND THEN
total_sales := 0;
END;
DECLARE
sales_total NUMBER;
BEGIN
calculate_total_sales(TO_DATE('2023-01-01', 'YYYY-MM-DD'), TO_DATE('2023-03-31', 'YYYY-MM-DD'), sales_total);
DBMS_OUTPUT.PUT_LINE('Total sales for Q1 2023: ' || sales_total);
END;
|
CREATE FUNCTION GetEmployeeName(@emp_id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @emp_name VARCHAR(100);
SELECT @emp_name = ename FROM emp WHERE empno = @emp_id;
RETURN @emp_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
|
CREATE FUNCTION GetEmployeeName(emp_id NUMBER) RETURN VARCHAR2 IS
emp_name VARCHAR2(100);
BEGIN
SELECT ename INTO emp_name FROM emp WHERE empno = emp_id;
RETURN emp_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
|
DECLARE
@v_empno FLOAT;
v_job_id dbo.VARCHAR2(50);
v_salary NUMBER;
v_deptno NUMBER;
BEGIN
SELECT @v_empno = empno,
@v_job_id = job_id,
@v_salary = salary,
@v_deptno = deptno
FROM emp
WHERE empno = 7369;
IF v_job_id = 'CLERK' BEGIN
UPDATE emp
SET salary = v_salary * 1.10
WHERE empno = @v_empno;
END
ELSE IF v_job_id = 'SALESMAN' BEGIN
UPDATE emp
SET salary = v_salary * 1.20
WHERE empno = @v_empno;
END
END;
|
DECLARE
v_empno NUMBER;
v_job_id VARCHAR2(50);
v_salary NUMBER;
v_deptno NUMBER;
BEGIN
SELECT empno,
job_id,
salary,
deptno
INTO v_empno,
v_job_id,
v_salary,
v_deptno
FROM emp
WHERE empno = 7369;
IF v_job_id = 'CLERK' THEN
UPDATE emp
SET salary = v_salary * 1.10
WHERE empno = v_empno;
ELSIF v_job_id = 'SALESMAN' THEN
UPDATE emp
SET salary = v_salary * 1.20
WHERE empno = v_empno;
END IF;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
|
CREATE PROCEDURE emp_salary_update(
@emp_id IN AS NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = salary WHERE employee_id = emp_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20100 Employee not found.', 16, 1);
END
END;
DECLARE
@emp_salary FLOAT;
BEGIN
SELECT @emp_salary = salary FROM employees WHERE employee_id = 10;
dbo.emp_salary_update(10, @emp_salary + 1000);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END; | CREATE PROCEDURE emp_salary_update(
emp_id IN NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = salary WHERE employee_id = emp_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20100, 'Employee not found.');
END IF;
END;
DECLARE
emp_salary NUMBER;
BEGIN
SELECT salary INTO emp_salary FROM employees WHERE employee_id = 10;
emp_salary_update(10, emp_salary + 1000);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
ROLLBACK;
END; |
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL
DROP FUNCTION get_employee_details;
GO
CREATE FUNCTION get_employee_details(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN sys_refcursor
return null;
end; IS
emp_cursor SYS_REFCURSOR;
BEGIN
SELECT * FROM employees WHERE employee_id = employee_id;
RETURN emp_cursor;
END;
/
DECLARE
emp_ref CURSOR;
emp_id NUMBER;
emp_name dbo.VARCHAR2(255);
BEGIN
SET @emp_id = 100;
SET @emp_ref = dbo.get_employee_details(emp_id);
WHILE 1=1 BEGIN
FETCH emp_ref INTO @emp_id, @emp_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, '') + ', Employee Name: ' + isnull(emp_name, ''));
END;
CLOSE emp_ref;
END;
/
|
CREATE OR REPLACE FUNCTION get_employee_details(employee_id IN NUMBER) RETURN sys_refcursor IS
emp_cursor SYS_REFCURSOR;
BEGIN
OPEN emp_cursor FOR
SELECT * FROM employees WHERE employee_id = employee_id;
RETURN emp_cursor;
END;
/
DECLARE
emp_ref CURSOR;
emp_id NUMBER;
emp_name VARCHAR2(255);
BEGIN
emp_id := 100;
emp_ref := get_employee_details(emp_id);
LOOP
FETCH emp_ref INTO emp_id, emp_name;
EXIT WHEN emp_ref%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id || ', Employee Name: ' || emp_name);
END LOOP;
CLOSE emp_ref;
END;
/
|
CREATE PROCEDURE Emp_Raise(@p_empno IN AS NUMBER, p_amount IN NUMBER) AS
BEGIN
*** salary
UPDATE employees
SET salary = salary + p_amount
WHERE employee_id = p_empno;
*** employee found
IF SQL%NOTFOUND BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
-- Co... ***
COMMIT;
END;
/
*** rocedure
BEGIN
dbo.Emp_Raise(100, 1000);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
/
|
CREATE PROCEDURE Emp_Raise(p_empno IN NUMBER, p_amount IN NUMBER) AS
BEGIN
-- Update employee's salary
UPDATE employees
SET salary = salary + p_amount
WHERE employee_id = p_empno;
-- Raise error if no employee found
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Commit changes
COMMIT;
END;
/
-- Call the stored procedure
BEGIN
Emp_Raise(100, 1000);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
/
|
DECLARE
@TYPE Employee_Type IS RECORD (
Employee_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Salary NUMBER,
Hire_Date DATE
);
Emp_Rec Employee_Type;
BEGIN
SELECT @Emp_Rec = * FROM Employees WHERE Employee_ID = 1;
IF Emp_Rec.Hire_Date > GETDATE() - 365 BEGIN
RAISERROR('-20001 Employee has not been employed for a year', 16, 1);
END
*** changes
UPDATE Employees SET Salary = Emp_Rec.Salary * 1.1 WHERE Employee_ID = 1;
COMMIT;
*** lary
SELECT @Emp_Rec.Salary = Salary FROM Employees WHERE Employee_ID = 1;
*** ed data
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + ISNULL(Emp_Rec.Employee_ID, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + ISNULL(Emp_Rec.First_Name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + ISNULL(Emp_Rec.Last_Name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(Emp_Rec.Salary, ''));
DBMS_OUTPUT.PUT_LINE('Hire Date: ' + ISNULL(Emp_Rec.Hire_Date, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
|
DECLARE
TYPE Employee_Type IS RECORD (
Employee_ID INTEGER,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Salary NUMBER,
Hire_Date DATE
);
Emp_Rec Employee_Type;
BEGIN
SELECT * INTO Emp_Rec FROM Employees WHERE Employee_ID = 1;
IF Emp_Rec.Hire_Date > SYSDATE - 365 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee has not been employed for a year');
END IF;
-- Update and commit changes
UPDATE Employees SET Salary = Emp_Rec.Salary * 1.1 WHERE Employee_ID = 1;
COMMIT;
-- Select updated salary
SELECT Salary INTO Emp_Rec.Salary FROM Employees WHERE Employee_ID = 1;
-- Display the updated data
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || Emp_Rec.Employee_ID);
DBMS_OUTPUT.PUT_LINE('First Name: ' || Emp_Rec.First_Name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || Emp_Rec.Last_Name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || Emp_Rec.Salary);
DBMS_OUTPUT.PUT_LINE('Hire Date: ' || Emp_Rec.Hire_Date);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
ROLLBACK;
END;
|
DECLARE
@v_cursor SYS_REFCURSOR;
BEGIN
SELECT *
FROM employees;
WHILE 1=1 BEGIN
FETCH v_cursor INTO @v_emp_id, @v_last_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(v_emp_id, '') + ' ' + isnull(v_last_name, ''));
END;
CLOSE v_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found.');
END
END;
|
DECLARE
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR
SELECT *
FROM employees;
LOOP
FETCH v_cursor INTO v_emp_id, v_last_name;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_id || ' ' || v_last_name);
END LOOP;
CLOSE v_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found.');
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'SMITH';
IF @v_empno IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for employee ' + isnull(@v_empno, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'SMITH';
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for employee ' || v_empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
@v_empno 員工編號;
v_sal 員工薪水;
v_deptno 部門編號;
BEGIN
SELECT @v_empno = empno, @v_sal = sal, @v_deptno = deptno
FROM employees
WHERE empno = 100;
IF v_sal > 10000 BEGIN
UPDATE employees
SET sal = v_sal + 1000
WHERE empno = 100;
END
ELSE BEGIN
RAISERROR('-20001 薪水低於 10000', 16, 1);
END
END;
|
DECLARE
v_empno 員工編號;
v_sal 員工薪水;
v_deptno 部門編號;
BEGIN
SELECT empno, sal, deptno
INTO v_empno, v_sal, v_deptno
FROM employees
WHERE empno = 100;
IF v_sal > 10000 THEN
UPDATE employees
SET sal = v_sal + 1000
WHERE empno = 100;
ELSE
RAISE_APPLICATION_ERROR(-20001, '薪水低於 10000');
END IF;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
CURSOR c_emp IS
SELECT empno, ename
FROM emp
WHERE deptno = 10;
BEGIN
OPEN c_emp;
WHILE 1=1 BEGIN
FETCH c_emp INTO @v_empno, @v_ename;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Empno: ' + isnull(@v_empno, '') + ', Ename: ' + isnull(v_ename, ''));
END;
CLOSE c_emp;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found in department 10.');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
CURSOR c_emp IS
SELECT empno, ename
FROM emp
WHERE deptno = 10;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_empno, v_ename;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Empno: ' || v_empno || ', Ename: ' || v_ename);
END LOOP;
CLOSE c_emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found in department 10.');
END;
|
IF OBJECT_ID('valid_age', 'FN') IS NOT NULL
DROP FUNCTION valid_age;
GO
CREATE FUNCTION valid_age(@age FLOAT)
RETURNS BIT
AS
BEGIN
RETURN @age >= 18 AND @age <= 100;
END;
IF OBJECT_ID('update_user', 'P') IS NOT NULL
DROP PROCEDURE update_user;
GO
CREATE PROCEDURE update_user(@user_id FLOAT, @new_age FLOAT)
AS
v_valid_age BOOLEAN;
BEGIN
set @v_valid_age = dbo.valid_age(new_age);
IF v_valid_age BEGIN
UPDATE users
SET age = new_age
WHERE user_id = user_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Invalid age: ' + isnull(new_age, ''), 16, 1);
END
END update_user;
|
CREATE OR REPLACE FUNCTION valid_age(age NUMBER)
RETURN BOOLEAN
IS
BEGIN
RETURN age >= 18 AND age <= 100;
END valid_age;
CREATE OR REPLACE PROCEDURE update_user(user_id NUMBER, new_age NUMBER)
IS
v_valid_age BOOLEAN;
BEGIN
v_valid_age := valid_age(new_age);
IF v_valid_age THEN
UPDATE users
SET age = new_age
WHERE user_id = user_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Invalid age: ' || new_age);
END IF;
END update_user;
|
BEGIN
-- De... ***
v_my_var INTEGER;
v_my_string dbo.VARCHAR2(255);
-- So... ***
SELECT @v_my_var = COUNT(*) FROM user_tables;
-- So... ***
IF v_my_var > 0 BEGIN
set @v_my_string = 'Table exists.';
END
ELSE BEGIN
set @v_my_string = 'Table does not exist.';
END
-- So... ***
BEGIN
*** me code
UPDATE user_tables SET table_name = 'new_name' WHERE table_name = 'old_name';
EXCEPTION
-- Ca... ***
WHEN OTHERS THEN
set @v_my_string = 'Error occurred.';
END;
-- Su... ***
COMMIT;
END;
|
BEGIN
-- Declare variables
v_my_var INTEGER;
v_my_string VARCHAR2(255);
-- Some SQL statement
SELECT COUNT(*) INTO v_my_var FROM user_tables;
-- Some IF statement
IF v_my_var > 0 THEN
v_my_string := 'Table exists.';
ELSE
v_my_string := 'Table does not exist.';
END IF;
-- Some error handling
BEGIN
-- Try to execute some code
UPDATE user_tables SET table_name = 'new_name' WHERE table_name = 'old_name';
EXCEPTION
-- Catch any errors
WHEN OTHERS THEN
v_my_string := 'Error occurred.';
END;
-- Submit changes
COMMIT;
END;
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure(@p_id IN AS NUMBER, p_name IN OUT VARCHAR2) IS
BEGIN
SELECT @p_name = name
FROM employees
WHERE id = p_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISE;
END
END;
|
CREATE OR REPLACE PROCEDURE my_procedure(p_id IN NUMBER, p_name IN OUT VARCHAR2) IS
BEGIN
SELECT name INTO p_name
FROM employees
WHERE id = p_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE;
END;
|
BEGIN
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE emp_name = 'John Doe';
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_id = @v_emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found.', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
END; | BEGIN
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE emp_name = 'John Doe';
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_id = v_emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found.');
WHEN OTHERS THEN
RAISE;
END;
END; |
CREATE PROCEDURE usp_get_customer_info (@p_customer_id IN AS NUMBER) AS
BEGIN
SELECT @v_customer = * FROM customers WHERE customer_id = p_customer_id;
IF v_customer.is_active = 'N' BEGIN
RAISERROR('-20000 Customer is inactive', 16, 1);
END
RETURN v_customer;
END;
BEGIN
TRY
DECLARE
BEGIN
SET @v_customer = dbo.usp_get_customer_info(1234);
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(@v_customer_customer_name, ''));
END;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE PROCEDURE usp_get_customer_info (p_customer_id IN NUMBER) AS
BEGIN
SELECT * INTO v_customer FROM customers WHERE customer_id = p_customer_id;
IF v_customer.is_active = 'N' THEN
RAISE_APPLICATION_ERROR(-20000, 'Customer is inactive');
END IF;
RETURN v_customer;
END;
BEGIN
TRY
DECLARE
v_customer customers%ROWTYPE;
BEGIN
v_customer := usp_get_customer_info(1234);
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer.customer_name);
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|