sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('get_emp_name', 'FN') IS NOT NULL
DROP FUNCTION get_emp_name;
GO
CREATE FUNCTION get_emp_name(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT ename
FROM emp
WHERE empno = emp_id
);
END;
/
DECLARE
@emp_name VARCHAR(20);
BEGIN
SET @emp_name = dbo.get_emp_name(7839);
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END
END;
/
|
CREATE OR REPLACE FUNCTION get_emp_name(emp_id IN NUMBER) RETURN VARCHAR2
AS
BEGIN
RETURN (
SELECT ename
FROM emp
WHERE empno = emp_id
);
END;
/
DECLARE
emp_name VARCHAR2(20);
BEGIN
emp_name := get_emp_name(7839);
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END;
/
|
CREATE PROCEDURE emp_salary_raise (
@emp_id IN AS NUMBER,
amount IN NUMBER
)
AS
BEGIN
*** ry of the employee with the specified ID by the specified amount
UPDATE employees SET salary = salary + amount WHERE employee_id = emp_id;
*** n if the update fails
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Employee not found or salary update failed', 16, 1);
END
END;
/
DECLARE
*** to store the employee ID and the amount to raise the salary by
@emp_id FLOAT := 100;
amount SET @NUMBER = 1000;
BEGIN
*** rocedure to raise the salary of the employee
dbo.emp_salary_raise(@emp_id, amount);
*** s to the database
COMMIT;
END;
/
|
CREATE PROCEDURE emp_salary_raise (
emp_id IN NUMBER,
amount IN NUMBER
)
AS
BEGIN
-- Increase the salary of the employee with the specified ID by the specified amount
UPDATE employees SET salary = salary + amount WHERE employee_id = emp_id;
-- Raise an exception if the update fails
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found or salary update failed');
END IF;
END;
/
DECLARE
-- Declare variables to store the employee ID and the amount to raise the salary by
emp_id NUMBER := 100;
amount NUMBER := 1000;
BEGIN
-- Call the stored procedure to raise the salary of the employee
emp_salary_raise(emp_id, amount);
-- Commit the changes to the database
COMMIT;
END;
/
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
SELECT @v_ename = ename FROM emp WHERE empno = 7369;
IF v_ename IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.put_line('Employee Name: ' + isnull(v_ename, ''));
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
SELECT ename INTO v_ename FROM emp WHERE empno = 7369;
IF v_ename IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.put_line('Employee Name: ' || v_ename);
END;
|
DECLARE
@cursor my_cursor is
select name, age, role
from employees;
employee_record %rowtype;
BEGIN
OPEN my_cursor;
WHILE 1=1 BEGIN
FETCH my_cursor INTO @employee_record;
IF @@FETCH_STATUS <> 0 BREAK;
*** record here
END;
CLOSE my_cursor;
END;
|
DECLARE
cursor my_cursor is
select name, age, role
from employees;
employee_record %rowtype;
BEGIN
OPEN my_cursor;
LOOP
FETCH my_cursor INTO employee_record;
EXIT WHEN my_cursor%notfound;
-- Process employee record here
END LOOP;
CLOSE my_cursor;
END;
|
CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER)
IS
emp_name dbo.VARCHAR2(50);
emp_salary NUMBER;
BEGIN
SELECT @emp_name = name, @emp_salary = salary FROM employees WHERE emp_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(emp_id, ''));
END
END;
|
CREATE PROCEDURE get_employee_info(emp_id IN NUMBER)
IS
emp_name VARCHAR2(50);
emp_salary NUMBER;
BEGIN
SELECT name, salary INTO emp_name, emp_salary FROM employees WHERE emp_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || emp_id);
END;
|
CREATE PROCEDURE loop_through_emp(@emp_no IN AS NUMBER) IS
BEGIN
DECLARE i CURSOR FOR SELECT department_id, last_name
FROM employees
WHERE employee_id = emp_no;
OPEN i;
FETCH i INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE('Department ID: ' + isnull(@i_department_id, '') + ', Last Name: ' + isnull(@i_last_name, ''));
FETCH i INTO;
END;
CLOSE i;
DEALLOCATE i;
END loop_through_emp;
|
CREATE PROCEDURE loop_through_emp(emp_no IN NUMBER) IS
BEGIN
FOR i IN (SELECT department_id, last_name
FROM employees
WHERE employee_id = emp_no)
LOOP
DBMS_OUTPUT.PUT_LINE('Department ID: ' || i.department_id || ', Last Name: ' || i.last_name);
END LOOP;
END loop_through_emp;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE last_name = 'King';
IF v_salary > 10000 BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low to be increased.', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found.', 16, 1);
END
WHEN OTHERS THEN
RAISE;
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 = 'King';
IF v_salary > 10000 THEN
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to be increased.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found.');
WHEN OTHERS THEN
RAISE;
END;
|
CREATE PROCEDURE GetCustomerOrders
(@p_customer_id IN AS NUMBER)
IS
*** to hold result set and error message
orders_rs SYS_REFCURSOR;
error_msg dbo.VARCHAR2(2000);
BEGIN
*** fetch orders for the specified customer
SELECT ORDER_ID, ORDER_DATE, TOTAL_AMOUNT
FROM ORDERS
WHERE CUSTOMER_ID = p_customer_id;
*** r occurred
IF @@ERROR <> 0 BEGIN
set @error_msg = 'Error occurred while fetching orders: ' + ISNULL(SQLERRM, '');
RAISERROR(-20001, error_msg, 16, 1);
END
*** reference to the caller
RETURN orders_rs;
END GetCustomerOrders;
|
CREATE PROCEDURE GetCustomerOrders
(p_customer_id IN NUMBER)
IS
-- Declare variables to hold result set and error message
orders_rs SYS_REFCURSOR;
error_msg VARCHAR2(2000);
BEGIN
-- Open a cursor to fetch orders for the specified customer
OPEN orders_rs FOR
SELECT ORDER_ID, ORDER_DATE, TOTAL_AMOUNT
FROM ORDERS
WHERE CUSTOMER_ID = p_customer_id;
-- Check if any error occurred
IF SQLCODE <> 0 THEN
error_msg := 'Error occurred while fetching orders: ' || SQLERRM;
RAISE_APPLICATION_ERROR(-20001, error_msg);
END IF;
-- Return the cursor reference to the caller
RETURN orders_rs;
END GetCustomerOrders;
|
DECLARE
@v_emp_id INTEGER;
BEGIN
SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'King';
IF @v_emp_id IS NULL BEGIN
-- Em... ***
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 INTEGER;
BEGIN
SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'King';
IF v_emp_id IS NULL THEN
-- Employee not found
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_deptno FLOAT;
v_dname dbo.VARCHAR2(20);
v_loc dbo.VARCHAR2(20);
CURSOR c_dept IS
SELECT deptno, dname, loc
FROM dept;
BEGIN
OPEN c_dept;
FETCH c_dept INTO @v_deptno, @v_dname, @v_loc;
DBMS_OUTPUT.PUT_LINE('Retrieved deptno: ' + isnull(@v_deptno, ''));
DBMS_OUTPUT.PUT_LINE('Retrieved dname: ' + isnull(v_dname, ''));
DBMS_OUTPUT.PUT_LINE('Retrieved loc: ' + isnull(v_loc, ''));
CLOSE c_dept;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_deptno NUMBER;
v_dname VARCHAR2(20);
v_loc VARCHAR2(20);
CURSOR c_dept IS
SELECT deptno, dname, loc
FROM dept;
BEGIN
OPEN c_dept;
FETCH c_dept INTO v_deptno, v_dname, v_loc;
DBMS_OUTPUT.PUT_LINE('Retrieved deptno: ' || v_deptno);
DBMS_OUTPUT.PUT_LINE('Retrieved dname: ' || v_dname);
DBMS_OUTPUT.PUT_LINE('Retrieved loc: ' || v_loc);
CLOSE c_dept;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
BEGIN
DECLARE r CURSOR FOR SELECT * FROM employees; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
IF @r_salary > 10000 BEGIN
UPDATE employees SET bonus = salary * 0.1
WHERE employee_id = @r_employee_id;
END
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Bonuses updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
|
BEGIN
FOR r IN (SELECT * FROM employees) LOOP
IF r.salary > 10000 THEN
UPDATE employees SET bonus = salary * 0.1
WHERE employee_id = r.employee_id;
END IF;
END LOOP;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Bonuses updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
ROLLBACK;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
v_emp_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT TOP 1 @v_emp_id = emp_id,
@v_emp_name = emp_name,
@v_emp_salary = emp_salary
FROM emp
;
IF @v_emp_id IS NOT NULL
BEGIN
SET @v_bonus = v_emp_salary * 0.2;
UPDATE emp
SET emp_salary = v_emp_salary + v_bonus
WHERE emp_id = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary for ' + isnull(v_emp_name, '') + ' to ' + ISNULL((v_emp_salary + v_bonus), ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary for ' + isnull(v_emp_name, ''));
END;
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_emp_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT emp_id,
emp_name,
emp_salary
INTO v_emp_id, v_emp_name, v_emp_salary
FROM emp
WHERE rownum = 1;
IF v_emp_id IS NOT NULL THEN
BEGIN
v_bonus := v_emp_salary * 0.2;
UPDATE emp
SET emp_salary = v_emp_salary + v_bonus
WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary for ' || v_emp_name || ' to ' || (v_emp_salary + v_bonus));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary for ' || v_emp_name);
END;
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IF;
END;
|
IF OBJECT_ID('get_customer_name', 'FN') IS NOT NULL
DROP FUNCTION get_customer_name;
GO
CREATE FUNCTION get_customer_name(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
customer_name dbo.VARCHAR2(50);
BEGIN
SELECT @customer_name = customer_name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
IF OBJECT_ID('update_customer_address', 'P') IS NOT NULL
DROP PROCEDURE update_customer_address;
GO
CREATE PROCEDURE update_customer_address(@customer_id IN AS NUMBER, new_address IN VARCHAR2)
AS
old_address dbo.VARCHAR2(100);
BEGIN
SELECT @old_address = address FROM customers WHERE customer_id = customer_id;
IF old_address IS NOT NULL BEGIN
UPDATE customers SET address = new_address WHERE customer_id = customer_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Customer with ID ' + isnull(customer_id, '') + ' not found', 16, 1);
END
END;
CREATE OR REPLACE TRIGGER log_customer_changes ON customers
AFTER INSERT OR UPDATE OR DELETE
AS
DECLARE @operation VARCHAR(10);
DECLARE deleted_cur CURSOR FOR SELECT FROM deleted;
OPEN deleted_cur;
WHILE 1=0 BEGIN
FETCH deleted_cur INTO ;
IF @@FETCH_STATUS <> 0 BREAK;
BEGIN
IF INSERTING BEGIN
SET @operation = 'INSERT';
END
ELSE IF UPDATING BEGIN
SET @operation = 'UPDATE';
END
ELSE IF DELETING BEGIN
SET @operation = 'DELETE';
END
INSERT INTO customer_log (operation, customer_id, customer_name, address, change_date)
VALUES (@operation, :NEW.customer_id, :NEW.customer_name, :NEW.address, GETDATE());
END
CLOSE DELETED_CUR;
DEALLOCATE DELETED_CUR;END;
|
CREATE OR REPLACE FUNCTION get_customer_name(customer_id IN NUMBER) RETURN VARCHAR2
AS
customer_name VARCHAR2(50);
BEGIN
SELECT customer_name INTO customer_name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
CREATE OR REPLACE PROCEDURE update_customer_address(customer_id IN NUMBER, new_address IN VARCHAR2)
AS
old_address VARCHAR2(100);
BEGIN
SELECT address INTO old_address FROM customers WHERE customer_id = customer_id;
IF old_address IS NOT NULL THEN
UPDATE customers SET address = new_address WHERE customer_id = customer_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Customer with ID ' || customer_id || ' not found');
END IF;
END;
CREATE OR REPLACE TRIGGER log_customer_changes
AFTER INSERT OR UPDATE OR DELETE ON customers
FOR EACH ROW
AS
DECLARE
operation VARCHAR2(10);
BEGIN
IF INSERTING THEN
operation := 'INSERT';
ELSIF UPDATING THEN
operation := 'UPDATE';
ELSIF DELETING THEN
operation := 'DELETE';
END IF;
INSERT INTO customer_log (operation, customer_id, customer_name, address, change_date)
VALUES (operation, :NEW.customer_id, :NEW.customer_name, :NEW.address, SYSDATE);
END;
|
DECLARE
@v_emp_id INT;
v_salary ROW_NUMBER(10,2);
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'King';
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = v_salary + (v_salary * 0.1)
WHERE employee_id = @v_emp_id;
END
ELSE BEGIN
RAISERROR(
'-20000 Salary is less than 10000, no raise given.'
, 16, 1);
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER(6);
v_salary NUMBER(10,2);
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'King';
IF v_salary > 10000 THEN
UPDATE employees
SET salary = v_salary + (v_salary * 0.1)
WHERE employee_id = v_emp_id;
ELSE
RAISE_APPLICATION_ERROR(
-20000,
'Salary is less than 10000, no raise given.'
);
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE TABLE my_table (id FLOAT PRIMARY KEY, name VARCHAR(10), age FLOAT);
INSERT INTO my_table VALUES (1, 'John', 30);
INSERT INTO my_table VALUES (2, 'Mary', 25);
INSERT INTO my_table VALUES (3, 'Bob', 40);
CREATE PROCEDURE get_employee_by_id(@p_id IN AS NUMBER) IS
v_name dbo.VARCHAR2(10);
v_age NUMBER;
BEGIN
SELECT @v_name = name, @v_age = age FROM my_table WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(v_name, ''));
DBMS_OUTPUT.PUT_LINE('Age: ' + isnull(v_age, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END
END;
BEGIN
dbo.get_employee_by_id(1);
dbo.get_employee_by_id(4);
END;
|
CREATE TABLE my_table (id NUMBER PRIMARY KEY, name VARCHAR2(10), age NUMBER);
INSERT INTO my_table VALUES (1, 'John', 30);
INSERT INTO my_table VALUES (2, 'Mary', 25);
INSERT INTO my_table VALUES (3, 'Bob', 40);
PROCEDURE get_employee_by_id(p_id IN NUMBER) IS
v_name VARCHAR2(10);
v_age NUMBER;
BEGIN
SELECT name, age INTO v_name, v_age FROM my_table WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Age: ' || v_age);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
BEGIN
get_employee_by_id(1);
get_employee_by_id(4);
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT TOP 9 @v_empno = empno, @v_ename = ename, @v_sal = sal
FROM emp
;
IF v_sal > 3000 BEGIN
UPDATE emp
SET sal = sal + 1000
WHERE empno = @v_empno;
END
ELSE BEGIN
SET @v_sal = v_sal + 500;
END
IF @@fetch_status <> 0 BEGIN
DBMS_OUTPUT.put_line('No employee found');
END
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 rownum < 10;
IF v_sal > 3000 THEN
UPDATE emp
SET sal = sal + 1000
WHERE empno = v_empno;
ELSE
v_sal := v_sal + 500;
END IF;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.put_line('No employee found');
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@p_empno IN AS NUMBER,
p_salary IN NUMBER,
p_deptno IN NUMBER)
AS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE empno = p_empno;
COMMIT;
UPDATE departments
SET budget = budget + (p_salary - (
SELECT salary
FROM employees
WHERE empno = p_empno
))
WHERE deptno = p_deptno;
COMMIT;
END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(
@p_empno IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE empno = p_empno
);
END;
BEGIN
DECLARE
@empno FLOAT := 7369;
salary SET @NUMBER = 10000;
deptno SET @NUMBER = 20;
BEGIN
dbo.update_employee(p_empno, p_salary, p_deptno);
SET @salary = dbo.get_employee_salary(p_empno);
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
p_empno IN NUMBER,
p_salary IN NUMBER,
p_deptno IN NUMBER)
AS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE empno = p_empno;
COMMIT;
UPDATE departments
SET budget = budget + (p_salary - (
SELECT salary
FROM employees
WHERE empno = p_empno
))
WHERE deptno = p_deptno;
COMMIT;
END;
CREATE OR REPLACE FUNCTION get_employee_salary(
p_empno IN NUMBER)
RETURN NUMBER
AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE empno = p_empno
);
END;
BEGIN
DECLARE
empno NUMBER := 7369;
salary NUMBER := 10000;
deptno NUMBER := 20;
BEGIN
update_employee(p_empno, p_salary, p_deptno);
salary := get_employee_salary(p_empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
END;
|
CREATE TABLE t1 (id FLOAT PRIMARY KEY, name VARCHAR(100), age FLOAT);
-- In... ***
INSERT INTO t1 (id, name, age) VALUES (1, 'John', 30);
INSERT INTO t1 (id, name, age) VALUES (2, 'Mary', 25);
*** ocedure
CALL get_avg_age();
*** e function
IF OBJECT_ID('get_avg_age', 'FN') IS NOT NULL
DROP FUNCTION get_avg_age;
GO
CREATE FUNCTION get_avg_age() RETURNS FLOAT AS
BEGIN
DECLARE @avg_age FLOAT;
SELECT @avg_age = AVG(age) FROM t1;
RETURN @avg_age;
END;
*** control flow construct
DECLARE
@avg_age FLOAT;
BEGIN
SELECT @avg_age = AVG(age) FROM t1 WHERE age >= 25;
IF @avg_age IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('No records found');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Average age: ' + isnull(@avg_age, ''));
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE t1 (id NUMBER PRIMARY KEY, name VARCHAR2(100), age NUMBER);
-- Insert data
INSERT INTO t1 (id, name, age) VALUES (1, 'John', 30);
INSERT INTO t1 (id, name, age) VALUES (2, 'Mary', 25);
-- Execute stored procedure
CALL get_avg_age();
-- Define and execute function
CREATE OR REPLACE FUNCTION get_avg_age RETURN NUMBER IS
avg_age NUMBER;
BEGIN
SELECT AVG(age) INTO avg_age FROM t1;
RETURN avg_age;
END;
-- Select data using control flow construct
DECLARE
avg_age NUMBER;
BEGIN
SELECT AVG(age) INTO avg_age FROM t1 WHERE age >= 25;
IF avg_age IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No records found');
ELSE
DBMS_OUTPUT.PUT_LINE('Average age: ' || avg_age);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
sql
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
IF i MOD 2 = 0 BEGIN
dbms_output.put_line('Even: ' + isnull(@i_to_char, ''));
END
ELSE BEGIN
dbms_output.put_line('Odd: ' + isnull(@i_to_char, ''));
END
END;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error: ' + ISNULL(@@ERROR, '') + ' - ' + ISNULL(SQLERRM, ''));
END;
| sql
BEGIN
FOR i IN 1 .. 10 LOOP
IF i MOD 2 = 0 THEN
dbms_output.put_line('Even: ' || i.to_char);
ELSE
dbms_output.put_line('Odd: ' || i.to_char);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error: ' || SQLCODE || ' - ' || SQLERRM);
END;
|
CREATE PROCEDURE employee_salary_update(@emp_id IN AS NUMBER, salary_amount IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary_amount WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
@emp_id FLOAT;
salary_amount NUMBER;
BEGIN
SELECT @emp_id = employee_id, @salary_amount = salary FROM employees WHERE last_name = 'Smith';
dbo.employee_salary_update(@emp_id, salary_amount * 1.10);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee with last name Smith not found', 16, 1);
END
END;
CREATE TABLE orders ( order_id FLOAT, customer_id FLOAT, order_date DATE, order_total FLOAT );
INSERT INTO orders ( order_id, customer_id, order_date, order_total ) VALUES ( 1, 100, '2023-02-01', 100.00 );
INSERT INTO orders ( order_id, customer_id, order_date, order_total ) VALUES ( 2, 200, '2023-02-02', 200.00 );
SELECT * FROM orders WHERE order_total > 150;
BEGIN
RAISERROR('-20100 Custom error message', 16, 1);
END;
|
CREATE PROCEDURE employee_salary_update(emp_id IN NUMBER, salary_amount IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary_amount WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
emp_id NUMBER;
salary_amount NUMBER;
BEGIN
SELECT employee_id, salary INTO emp_id, salary_amount FROM employees WHERE last_name = 'Smith';
employee_salary_update(emp_id, salary_amount * 1.10);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with last name Smith not found');
END;
CREATE TABLE orders ( order_id NUMBER, customer_id NUMBER, order_date DATE, order_total NUMBER );
INSERT INTO orders ( order_id, customer_id, order_date, order_total ) VALUES ( 1, 100, '2023-02-01', 100.00 );
INSERT INTO orders ( order_id, customer_id, order_date, order_total ) VALUES ( 2, 200, '2023-02-02', 200.00 );
SELECT * FROM orders WHERE order_total > 150;
BEGIN
RAISE_APPLICATION_ERROR(-20100, 'Custom error message');
END;
|
IF OBJECT_ID('is_leap_year', 'FN') IS NOT NULL
DROP FUNCTION is_leap_year;
GO
CREATE FUNCTION is_leap_year(@year IN NUMBERDECLARE )
RETURN
BEGIN BOOLEAN
RETURN NULL;
END;
AS
BEGIN
IF year MOD 400 = 0
BEGIN
RETURN TRUE;
END
ELSE IF year MOD 100 = 0
BEGIN
RETURN FALSE;
END
ELSE IF year MOD 4 = 0
BEGIN
RETURN TRUE;
END
ELSE BEGIN
RETURN FALSE;
END
END;
/
DECLARE
@leap_year BIT;
BEGIN
SET @leap_year = dbo.is_leap_year(2020);
IF @leap_year = TRUE
BEGIN
DBMS_OUTPUT.PUT_LINE('2020 is a leap year.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('2020 is not a leap year.');
END
END;
/
|
CREATE OR REPLACE FUNCTION is_leap_year(year IN NUMBER)
RETURN BOOLEAN
AS
BEGIN
IF year MOD 400 = 0
THEN
RETURN TRUE;
ELSIF year MOD 100 = 0
THEN
RETURN FALSE;
ELSIF year MOD 4 = 0
THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;
/
DECLARE
leap_year BOOLEAN;
BEGIN
leap_year := is_leap_year(2020);
IF leap_year = TRUE
THEN
DBMS_OUTPUT.PUT_LINE('2020 is a leap year.');
ELSE
DBMS_OUTPUT.PUT_LINE('2020 is not a leap year.');
END IF;
END;
/
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@p_employee_id IN AS NUMBER,
p_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_employee_id;
IF @@ERROR <> 0 BEGIN
RAISERROR('-20001 Error updating employee', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
p_employee_id IN NUMBER,
p_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_employee_id;
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Error updating employee');
END IF;
END;
|
DECLARE
@v_emp_id FLOAT := 10;
v_emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = @v_emp_id;
dbms_output.put_line('Employee Name: ' + isnull(v_emp_name, ''));
IF @@fetch_status <> 0 BEGIN
dbms_output.put_line('Employee not found!');
END
END;
/
|
DECLARE
v_emp_id NUMBER := 10;
v_emp_name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name INTO v_emp_name FROM employees WHERE employee_id = v_emp_id;
dbms_output.put_line('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Employee not found!');
END;
/
|
CREATE PROCEDURE emp_bonus
AS
total_salary NUMBER;
BEGIN
SELECT @total_salary = SUM(salary) FROM employees;
UPDATE employees SET bonus = CASE WHEN salary > total_salary / 2 THEN 0.1 * salary ELSE 0.05 * salary END;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 Error occurred during bonus calculation', 16, 1);
END;
|
CREATE PROCEDURE emp_bonus
AS
total_salary NUMBER;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees;
UPDATE employees SET bonus = CASE WHEN salary > total_salary / 2 THEN 0.1 * salary ELSE 0.05 * salary END;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Error occurred during bonus calculation');
END;
|
BEGIN
BEGIN
INSERT INTO orders (order_id, order_date) VALUES (1, DATE '2023-03-10');
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Duplicate order ID.', 16, 1);
END;
DECLARE
@salary DECIMAL(10,2) := 0;
BEGIN
SELECT @salary = @salary FROM employees WHERE employee_id = 1;
UPDATE employees SET salary = @salary + 100 WHERE employee_id = 1;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found.', 16, 1);
END
WHEN TOO_MANY_ROWS THEN
RAISERROR('-20003 Multiple employees with the same ID.', 16, 1);
END;
END;
/ | BEGIN
BEGIN
INSERT INTO orders (order_id, order_date) VALUES (1, DATE '2023-03-10');
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Duplicate order ID.');
END;
DECLARE
salary NUMBER(10,2) := 0;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = 1;
UPDATE employees SET salary = salary + 100 WHERE employee_id = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found.');
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20003, 'Multiple employees with the same ID.');
END;
END;
/ |
DECLARE
@v_emp_id DECIMAL(19,4) /* Use -meta option EMPLOYEE.employee_id%TYPE */;
v_emp_name EMPLOYEE.name%TYPE;
BEGIN
SELECT @v_emp_id = employee_id, @v_emp_name = name
FROM EMPLOYEE
WHERE employee_id = 10;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE EMPLOYEE
SET name = 'John Doe'
WHERE employee_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id EMPLOYEE.employee_id%TYPE;
v_emp_name EMPLOYEE.name%TYPE;
BEGIN
SELECT employee_id, name
INTO v_emp_id, v_emp_name
FROM EMPLOYEE
WHERE employee_id = 10;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE EMPLOYEE
SET name = 'John Doe'
WHERE employee_id = v_emp_id;
COMMIT;
END;
|
*** rocedure to insert a new row in a table
CREATE PROCEDURE insert_data(
@p_name IN AS VARCHAR2,
p_age IN NUMBER,
p_salary IN NUMBER,
p_dept_id IN NUMBER
) AS
v_err_msg dbo.VARCHAR2(200);
BEGIN
*** into the table
INSERT INTO employees (
name,
age,
salary,
dept_id
)
VALUES (
p_name,
p_age,
p_salary,
p_dept_id
);
*** ccurs
EXCEPTION
WHEN OTHERS THEN
set @v_err_msg = SUBSTRING(SQLERRM, 1, 100);
RAISERROR(-20000, v_err_msg, 16, 1);
END;
*** to calculate the total salary of a department
CREATE FUNCTION get_total_salary(
@p_dept_id IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
v_total_salary NUMBER;
BEGIN
SELECT @v_total_salary = SUM(salary)
FROM employees
WHERE dept_id = p_dept_id;
RETURN v_total_salary;
END;
*** ement to retrieve all employees from a department
SELECT *
FROM employees
WHERE dept_id = 10;
*** flow construct to loop through a cursor
DECLARE
@CURSOR c_employees IS
SELECT *
FROM employees;
BEGIN
DECLARE rec CURSOR FOR c_employees LOOP
*** the record
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
*** andling scenario to handle a specific error
DECLARE
@v_err_msg VARCHAR(200);
BEGIN
*** se an error
EXCEPTION
WHEN ORA-01400 THEN
SET @v_err_msg = SUBSTRING(SQLERRM, 1, 100);
RAISERROR(-20001, @v_err_msg, 16, 1);
END;
|
-- Create a stored procedure to insert a new row in a table
CREATE PROCEDURE insert_data(
p_name IN VARCHAR2,
p_age IN NUMBER,
p_salary IN NUMBER,
p_dept_id IN NUMBER
) AS
v_err_msg VARCHAR2(200);
BEGIN
-- Insert a new row into the table
INSERT INTO employees (
name,
age,
salary,
dept_id
)
VALUES (
p_name,
p_age,
p_salary,
p_dept_id
);
-- Handle error if occurs
EXCEPTION
WHEN OTHERS THEN
v_err_msg := SUBSTR(SQLERRM, 1, 100);
RAISE_APPLICATION_ERROR(-20000, v_err_msg);
END;
-- Create a function to calculate the total salary of a department
CREATE FUNCTION get_total_salary(
p_dept_id IN NUMBER
) RETURN NUMBER AS
v_total_salary NUMBER;
BEGIN
SELECT SUM(salary)
INTO v_total_salary
FROM employees
WHERE dept_id = p_dept_id;
RETURN v_total_salary;
END;
-- Create a SQL statement to retrieve all employees from a department
SELECT *
FROM employees
WHERE dept_id = 10;
-- Create a control flow construct to loop through a cursor
DECLARE
CURSOR c_employees IS
SELECT *
FROM employees;
BEGIN
FOR rec IN c_employees LOOP
-- Do something with the record
END LOOP;
END;
-- Create an error handling scenario to handle a specific error
DECLARE
v_err_msg VARCHAR2(200);
BEGIN
-- Code that may raise an error
EXCEPTION
WHEN ORA-01400 THEN
v_err_msg := SUBSTR(SQLERRM, 1, 100);
RAISE_APPLICATION_ERROR(-20001, v_err_msg);
END;
|
BEGIN
-- De... ***
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
*** umber and name
SELECT @v_empno = empno, @v_ename = ename
FROM emp
WHERE empno = 7839;
-- Ha... ***
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
*** ame
UPDATE emp
SET ename = 'NEW NAME'
WHERE empno = @v_empno;
-- Co... ***
COMMIT;
END; | BEGIN
-- Declare variables
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
-- Select employee number and name
SELECT empno, ename
INTO v_empno, v_ename
FROM emp
WHERE empno = 7839;
EXCEPTION
-- Handle errors
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
-- Update employee name
UPDATE emp
SET ename = 'NEW NAME'
WHERE empno = v_empno;
-- Commit changes
COMMIT;
END; |
IF OBJECT_ID('emp_salary', 'P') IS NOT NULL
DROP PROCEDURE emp_salary;
GO
CREATE PROCEDURE emp_salary(@empid FLOAT, @salary_hike FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = salary + (salary * @salary_hike) WHERE employee_id = @empid;
COMMIT;
END;
-- Ca... ***
BEGIN
dbo.emp_salary(100, 0.10);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
*** to calculate bonus
IF OBJECT_ID('bonus', 'FN') IS NOT NULL
DROP FUNCTION bonus;
GO
CREATE FUNCTION bonus(@salary FLOAT)
RETURNS FLOAT
AS
BEGIN
RETURN CASE
WHEN @salary < 10000 THEN @salary * 0.05
WHEN @salary >= 10000 AND @salary < 20000 THEN @salary * 0.10
ELSE @salary * 0.15
END;
END;
|
CREATE OR REPLACE PROCEDURE emp_salary(empid NUMBER, salary_hike NUMBER)
AS
BEGIN
UPDATE employees SET salary = salary + (salary * salary_hike) WHERE employee_id = empid;
COMMIT;
END;
-- Call the procedure
BEGIN
emp_salary(100, 0.10);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
-- Define a function to calculate bonus
CREATE OR REPLACE FUNCTION bonus(salary NUMBER)
RETURN NUMBER
AS
BEGIN
RETURN CASE
WHEN salary < 10000 THEN salary * 0.05
WHEN salary >= 10000 AND salary < 20000 THEN salary * 0.10
ELSE salary * 0.15
END;
END;
|
BEGIN
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT @v_salary = salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 BEGIN
SET @v_bonus = v_salary * 0.1;
END
ELSE BEGIN
SET @v_bonus = v_salary * 0.05;
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
/
|
BEGIN
DECLARE
v_empno NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT salary INTO v_salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 THEN
v_bonus := v_salary * 0.1;
ELSE
v_bonus := v_salary * 0.05;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
/
|
BEGIN
-- De... ***
DECLARE
@v_deptno SMALLINT;
v_dname dbo.VARCHAR2(14);
v_loc dbo.VARCHAR2(13);
BEGIN
*** formation
SELECT @v_deptno = deptno, @v_dname = dname, @v_loc = loc FROM dept WHERE deptno = 20;
*** information
DBMS_OUTPUT.PUT_LINE('Department Information:');
DBMS_OUTPUT.PUT_LINE(' Department Number: ' + isnull(@v_deptno, ''));
DBMS_OUTPUT.PUT_LINE(' Department Name: ' + isnull(v_dname, ''));
DBMS_OUTPUT.PUT_LINE(' Location: ' + isnull(v_loc, ''));
-- Ha... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Department not found.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END; | BEGIN
-- Declare variables
DECLARE
v_deptno NUMBER(2);
v_dname VARCHAR2(14);
v_loc VARCHAR2(13);
BEGIN
-- Get department information
SELECT deptno, dname, loc INTO v_deptno, v_dname, v_loc FROM dept WHERE deptno = 20;
-- Print department information
DBMS_OUTPUT.PUT_LINE('Department Information:');
DBMS_OUTPUT.PUT_LINE(' Department Number: ' || v_deptno);
DBMS_OUTPUT.PUT_LINE(' Department Name: ' || v_dname);
DBMS_OUTPUT.PUT_LINE(' Location: ' || v_loc);
EXCEPTION
-- Handle errors
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Department not found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END; |
DECLARE
@v_sal FLOAT;
BEGIN
SELECT @v_sal = salary FROM emp WHERE empno = 7788;
IF @v_sal > 10000 BEGIN
UPDATE emp SET salary = @v_sal * 1.1 WHERE empno = 7788;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_sal NUMBER;
BEGIN
SELECT salary INTO v_sal FROM emp WHERE empno = 7788;
IF v_sal > 10000 THEN
UPDATE emp SET salary = v_sal * 1.1 WHERE empno = 7788;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_ename dbo.VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT @v_ename = ename, @v_salary = salary FROM emp WHERE empno = @v_empno;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary exceeds limit', 16, 1);
END
*** base operations here
UPDATE emp SET salary = v_salary + 1000 WHERE empno = @v_empno;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER := 10;
v_ename VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT ename, salary INTO v_ename, v_salary FROM emp WHERE empno = v_empno;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary exceeds limit');
END IF;
-- Execute some database operations here
UPDATE emp SET salary = v_salary + 1000 WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
|
CREATE TABLE employees (
id FLOAT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
salary FLOAT
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane Smith', 20000);
CREATE PROCEDURE get_employee (
@id IN AS NUMBER,
OUT employee_name VARCHAR(255),
OUT employee_salary NUMBER
)
AS
BEGIN
SELECT @employee_name = name, @employee_salary = salary
FROM employees
WHERE id = id;
END;
DECLARE
@emp_name VARCHAR(255);
emp_salary NUMBER;
BEGIN
dbo.get_employee(1, @emp_name, emp_salary);
DBMS_OUTPUT.put_line('Employee name: ' + isnull(@emp_name, ''));
DBMS_OUTPUT.put_line('Employee salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.put_line('No employee found with that ID');
END
END;
|
CREATE TABLE employees (
id NUMBER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
salary NUMBER
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane Smith', 20000);
CREATE PROCEDURE get_employee (
id IN NUMBER,
OUT employee_name VARCHAR(255),
OUT employee_salary NUMBER
)
AS
BEGIN
SELECT name, salary
INTO employee_name, employee_salary
FROM employees
WHERE id = id;
END;
DECLARE
emp_name VARCHAR(255);
emp_salary NUMBER;
BEGIN
get_employee(1, emp_name, emp_salary);
DBMS_OUTPUT.put_line('Employee name: ' || emp_name);
DBMS_OUTPUT.put_line('Employee salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line('No employee found with that ID');
END;
|
CREATE PROCEDURE UpdateCustomer(
@IN p_cust_id AS NUMBER,
IN p_new_name VARCHAR2,
OUT p_status VARCHAR2
)
IS
v_old_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_old_name = name
FROM customers
WHERE cust_id = p_cust_id;
UPDATE customers
SET name = p_new_name
WHERE cust_id = p_cust_id;
set @p_status = 'Success';
EXCEPTION
WHEN OTHERS THEN
set @p_status = 'Error: ' + ISNULL(SQLERRM, '');
END;
|
CREATE PROCEDURE UpdateCustomer(
IN p_cust_id NUMBER,
IN p_new_name VARCHAR2,
OUT p_status VARCHAR2
)
IS
v_old_name VARCHAR2(100);
BEGIN
SELECT name
INTO v_old_name
FROM customers
WHERE cust_id = p_cust_id;
UPDATE customers
SET name = p_new_name
WHERE cust_id = p_cust_id;
p_status := 'Success';
EXCEPTION
WHEN OTHERS THEN
p_status := 'Error: ' || SQLERRM;
END;
|
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_id = id, @v_name = name FROM table_name WHERE id = :id;
DBMS_OUTPUT.PUT_LINE('ID: ' + isnull(@v_id, '') + ', Name: ' + isnull(v_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the given ID.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_id NUMBER;
v_name VARCHAR2(50);
BEGIN
SELECT id, name INTO v_id, v_name FROM table_name WHERE id = :id;
DBMS_OUTPUT.PUT_LINE('ID: ' || v_id || ', Name: ' || v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given ID.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE emp_salary_update (
@emp_id IN AS NUMBER,
salary_raise IN NUMBER
)
IS
BEGIN
UPDATE employees SET salary = salary + salary_raise
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
@emp_id FLOAT := 100;
salary_raise SET @NUMBER = 500;
BEGIN
dbo.emp_salary_update(@emp_id, salary_raise);
END;
|
CREATE PROCEDURE emp_salary_update (
emp_id IN NUMBER,
salary_raise IN NUMBER
)
IS
BEGIN
UPDATE employees SET salary = salary + salary_raise
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
emp_id NUMBER := 100;
salary_raise NUMBER := 500;
BEGIN
emp_salary_update(emp_id, salary_raise);
END;
|
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
*** e of type NUMBER
v_num SET @NUMBER = 10;
*** e of type VARCHAR2
v_str dbo.VARCHAR2(20) := 'Hello, world!';
-- De... ***
CURSOR c_emp IS
SELECT *
FROM emp;
-- Op... ***
OPEN c_emp;
*** ow from the cursor
FETCH c_emp INTO @v_num, @v_str;
-- Cl... ***
CLOSE c_emp;
*** procedure
CREATE PROCEDURE p_emp(@p_id IN AS NUMBER, p_name IN VARCHAR2) IS
BEGIN
*** into the emp table
INSERT INTO emp (id, name)
VALUES (p_id, p_name);
END;
*** rocedure
CALL p_emp(10, 'John Doe');
-- De... ***
CREATE FUNCTION f_emp(@p_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
BEGIN
*** f the employee with the specified ID
RETURN (SELECT name FROM emp WHERE id = p_id);
END;
-- Ca... ***
set @v_str = dbo.f_emp(10);
*** ion handler
EXCEPTION
WHEN OTHERS THEN
*** ion
DBMS_OUTPUT.PUT_LINE('An error occurred!');
END; | BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
-- Declare a variable of type NUMBER
v_num NUMBER := 10;
-- Declare a variable of type VARCHAR2
v_str VARCHAR2(20) := 'Hello, world!';
-- Declare a cursor
CURSOR c_emp IS
SELECT *
FROM emp;
-- Open the cursor
OPEN c_emp;
-- Fetch the first row from the cursor
FETCH c_emp INTO v_num, v_str;
-- Close the cursor
CLOSE c_emp;
-- Declare a stored procedure
PROCEDURE p_emp(p_id IN NUMBER, p_name IN VARCHAR2) IS
BEGIN
-- Insert a new row into the emp table
INSERT INTO emp (id, name)
VALUES (p_id, p_name);
END;
-- Call the stored procedure
CALL p_emp(10, 'John Doe');
-- Declare a function
FUNCTION f_emp(p_id IN NUMBER) RETURN VARCHAR2 IS
BEGIN
-- Return the name of the employee with the specified ID
RETURN (SELECT name FROM emp WHERE id = p_id);
END;
-- Call the function
v_str := f_emp(10);
-- Declare an exception handler
EXCEPTION
WHEN OTHERS THEN
-- Handle the exception
DBMS_OUTPUT.PUT_LINE('An error occurred!');
END; |
DECLARE
@v_empno FLOAT;
v_sal NUMBER;
BEGIN
SELECT @v_empno = ename, @v_sal = sal
FROM emp
WHERE empno = 7900;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_empno, '') + ', Salary: ' + isnull(v_sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END; | DECLARE
v_empno NUMBER;
v_sal NUMBER;
BEGIN
SELECT ename, sal
INTO v_empno, v_sal
FROM emp
WHERE empno = 7900;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_empno || ', Salary: ' || v_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END; |
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_salary OUT NUMBER) IS
BEGIN
SELECT @out_salary = salary
FROM employees
WHERE employee_id = p_id;
END;
/
DECLARE
@v_salary FLOAT;
BEGIN
dbo.get_employee_salary(100, @v_salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END
END;
/
|
CREATE OR REPLACE PROCEDURE get_employee_salary(p_id IN NUMBER, out_salary OUT NUMBER) IS
BEGIN
SELECT salary INTO out_salary
FROM employees
WHERE employee_id = p_id;
END;
/
DECLARE
v_salary NUMBER;
BEGIN
get_employee_salary(100, v_salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END;
/
|
CREATE PROCEDURE usp_Example(
-- De... ***
@p_num IN AS INTEGER,
p_str IN VARCHAR2,
p_date IN DATE
) AS
BEGIN
-- Bo... ***
IF p_num IS NULL BEGIN
RAISERROR(
'-20001 Parameter p_num must not be null'
, 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Values from the procedure:');
DBMS_OUTPUT.PUT_LINE(p_num);
DBMS_OUTPUT.PUT_LINE(p_str);
DBMS_OUTPUT.PUT_LINE(p_date);
END usp_Example;
|
PROCEDURE usp_Example(
-- Declarations:
p_num IN INTEGER,
p_str IN VARCHAR2,
p_date IN DATE
) AS
BEGIN
-- Body:
IF p_num IS NULL THEN
RAISE_APPLICATION_ERROR(
-20001,
'Parameter p_num must not be null'
);
END IF;
DBMS_OUTPUT.PUT_LINE('Values from the procedure:');
DBMS_OUTPUT.PUT_LINE(p_num);
DBMS_OUTPUT.PUT_LINE(p_str);
DBMS_OUTPUT.PUT_LINE(p_date);
END usp_Example;
|
CREATE PROCEDURE customer_orders AS
BEGIN
SET NOCOUNT ON;
DECLARE order_id CURSOR FOR SELECT order_id FROM orders; OPEN order_id;
FETCH order_id INTO;
WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO customer_orders (customer_id, order_id) VALUES (SELECT customer_id FROM customers WHERE name = 'John Doe', order_id);
FETCH order_id INTO;
END;
CLOSE order_id;
DEALLOCATE order_id;
COMMIT;
END; | CREATE PROCEDURE customer_orders AS
BEGIN
FOR order_id IN (SELECT order_id FROM orders) LOOP
INSERT INTO customer_orders (customer_id, order_id) VALUES (SELECT customer_id FROM customers WHERE name = 'John Doe', order_id);
END LOOP;
COMMIT;
END; |
DECLARE
@v_empno INTEGER;
v_sal ROW_NUMBER(10,2);
BEGIN
SELECT @v_empno = empno, @v_sal = sal FROM emp WHERE empno = 10;
IF v_sal > 10000 BEGIN
UPDATE emp SET sal = v_sal + 1000 WHERE empno = @v_empno;
END
ELSE BEGIN
UPDATE emp SET sal = v_sal - 1000 WHERE empno = @v_empno;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_empno INTEGER;
v_sal NUMBER(10,2);
BEGIN
SELECT empno, sal INTO v_empno, v_sal FROM emp WHERE empno = 10;
IF v_sal > 10000 THEN
UPDATE emp SET sal = v_sal + 1000 WHERE empno = v_empno;
ELSE
UPDATE emp SET sal = v_sal - 1000 WHERE empno = v_empno;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary
FROM employees
WHERE emp_id = 1;
IF v_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 10000');
END
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = @v_emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
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_id = 1;
IF v_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 10000');
END IF;
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END; |
DECLARE
@procedure_count FLOAT;
l_variable dbo.VARCHAR2(50);
BEGIN
SELECT @procedure_count = COUNT(*) FROM user_procedures;
IF @procedure_count > 0 BEGIN
SELECT @l_variable = procedure_name FROM user_procedures WHERE procedure_name = 'my_procedure';
IF l_variable IS NOT NULL BEGIN
EXECUTE sp_executesql @'DROP PROCEDURE ' || l_variable;
END
END
END;
/
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure(@p_input IN AS VARCHAR2) IS
BEGIN
*** the input parameter.
END;
/
DECLARE
@cursor_variable DBMS_SQL.REFCURSOR;
BEGIN
SELECT * FROM my_table;
FETCH cursor_variable @INTO ...;
CLOSE cursor_variable;
END;
/
|
DECLARE
procedure_count NUMBER;
l_variable VARCHAR2(50);
BEGIN
SELECT COUNT(*) INTO procedure_count FROM user_procedures;
IF procedure_count > 0 THEN
SELECT procedure_name INTO l_variable FROM user_procedures WHERE procedure_name = 'my_procedure';
IF l_variable IS NOT NULL THEN
EXECUTE IMMEDIATE 'DROP PROCEDURE ' || l_variable;
END IF;
END IF;
END;
/
CREATE OR REPLACE PROCEDURE my_procedure(p_input IN VARCHAR2) IS
BEGIN
-- Do something with the input parameter.
END;
/
DECLARE
cursor_variable DBMS_SQL.REFCURSOR;
BEGIN
OPEN cursor_variable FOR
SELECT * FROM my_table;
FETCH cursor_variable INTO ...;
CLOSE cursor_variable;
END;
/
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure (
@x IN AS NUMBER,
y IN OUT NUMBER
) AS
BEGIN
set @y = x * x;
EXCEPTION
WHEN OTHERS THEN
set @y = NULL;
RAISE;
END;
DECLARE
@x FLOAT := 10;
y NUMBER;
BEGIN
dbo.my_procedure(@x, y);
END;
|
CREATE OR REPLACE PROCEDURE my_procedure (
x IN NUMBER,
y IN OUT NUMBER
) AS
BEGIN
y := x * x;
EXCEPTION
WHEN OTHERS THEN
y := NULL;
RAISE;
END;
DECLARE
x NUMBER := 10;
y NUMBER;
BEGIN
my_procedure(x, y);
END;
|
DECLARE
@CURSOR CURSOR1 IS
SELECT *
FROM EMP
WHERE DEPTNO = 10;
V_EMPNO EMP.EMPNO%TYPE;
V_ENAME EMP.ENAME%TYPE;
BEGIN
OPEN CURSOR1;
WHILE 1=1 BEGIN
FETCH CURSOR1 INTO @V_EMPNO, @V_ENAME;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Empno: ' + ISNULL(V_EMPNO, '') + ' Ename: ' + ISNULL(V_ENAME, ''));
END;
CLOSE CURSOR1;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END; | DECLARE
CURSOR CURSOR1 IS
SELECT *
FROM EMP
WHERE DEPTNO = 10;
V_EMPNO EMP.EMPNO%TYPE;
V_ENAME EMP.ENAME%TYPE;
BEGIN
OPEN CURSOR1;
LOOP
FETCH CURSOR1 INTO V_EMPNO, V_ENAME;
EXIT WHEN CURSOR1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Empno: ' || V_EMPNO || ' Ename: ' || V_ENAME);
END LOOP;
CLOSE CURSOR1;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END; |
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@param1 IN AS VARCHAR2, param2 OUT NUMBER) IS
BEGIN
SELECT @param2 = COUNT(*) FROM my_table WHERE col1 = param1;
IF param2 = 0 BEGIN
RAISERROR('-20000 No rows found for ' + isnull(param1, ''), 16, 1);
END
END;
IF OBJECT_ID('my_func', 'FN') IS NOT NULL
DROP FUNCTION my_func;
GO
CREATE FUNCTION my_func(@param1 IN VARCHAR2DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
RETURN (
SELECT COUNT(*)
FROM my_table
WHERE col1 = param1
);
END;
BEGIN
*** ple
DECLARE
@param1 VARCHAR(20) := 'value1';
param2 NUMBER;
BEGIN
dbo.my_proc(@param1, param2);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
*** ample
BEGIN
dbo.my_func(NULL);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE PROCEDURE my_proc(param1 IN VARCHAR2, param2 OUT NUMBER) IS
BEGIN
SELECT COUNT(*) INTO param2 FROM my_table WHERE col1 = param1;
IF param2 = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No rows found for ' || param1);
END IF;
END;
CREATE OR REPLACE FUNCTION my_func(param1 IN VARCHAR2) RETURN NUMBER IS
BEGIN
RETURN (
SELECT COUNT(*)
FROM my_table
WHERE col1 = param1
);
END;
BEGIN
-- Control flow example
DECLARE
param1 VARCHAR2(20) := 'value1';
param2 NUMBER;
BEGIN
my_proc(param1, param2);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
-- Error handling example
BEGIN
my_func(NULL);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
|
BEGIN
DECLARE @v_emp_id INTEGER;
DECLARE @v_salary FLOAT;
DECLARE @v_bonus FLOAT;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE emp_name = 'John Doe';
IF @v_salary > 5000 BEGIN
SET @v_bonus = @v_salary * 0.1;
END
ELSE BEGIN
SET @v_bonus = 0;
END
UPDATE employees SET bonus = @v_bonus WHERE emp_id = @v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END; | BEGIN
DECLARE v_emp_id INTEGER;
DECLARE v_salary NUMBER;
DECLARE v_bonus NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE emp_name = 'John Doe';
IF v_salary > 5000 THEN
v_bonus := v_salary * 0.1;
ELSE
v_bonus := 0;
END IF;
UPDATE employees SET bonus = v_bonus WHERE emp_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
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
emp_salary NUMBER;
BEGIN
SELECT @emp_salary = salary FROM employees WHERE employee_id = emp_id;
IF emp_salary IS NULL BEGIN
RAISERROR('-20001 Employee with ID ' + isnull(emp_id, '') + ' not found', 16, 1);
END
RETURN emp_salary;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
emp_salary NUMBER;
BEGIN
SELECT salary INTO emp_salary FROM employees WHERE employee_id = emp_id;
IF emp_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found');
END IF;
RETURN emp_salary;
END;
|
DECLARE
@v_num1 FLOAT := 10;
v_num2 SET @NUMBER = 20;
BEGIN
*** alize variables
SET @v_num1 = @v_num1 + 10;
SET @v_num2 = v_num2 - 5;
*** ment
IF @v_num1 > v_num2 BEGIN
*** n if condition is true
RAISERROR('-20001 v_num1 is greater than v_num2', 16, 1);
END
*** nge of numbers
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
*** able
UPDATE my_table SET col1 = i WHERE id = i;
END;
*** cedure
dbo.my_proc(@v_num1, v_num2);
-- Ha... ***
BEGIN
*** me code
EXCEPTION
WHEN OTHERS THEN
*** that occur
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
DECLARE
v_num1 NUMBER := 10;
v_num2 NUMBER := 20;
BEGIN
-- Declare and initialize variables
v_num1 := v_num1 + 10;
v_num2 := v_num2 - 5;
-- Conditional statement
IF v_num1 > v_num2 THEN
-- Raise an exception if condition is true
RAISE_APPLICATION_ERROR(-20001, 'v_num1 is greater than v_num2');
END IF;
-- Loop through a range of numbers
FOR i IN 1..10 LOOP
-- Update database table
UPDATE my_table SET col1 = i WHERE id = i;
END LOOP;
-- Call a stored procedure
my_proc(v_num1, v_num2);
-- Handle errors
BEGIN
-- Try to execute some code
EXCEPTION
WHEN OTHERS THEN
-- Handle any errors that occur
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|
/* PACKAGE BODY emp_pkg */
*** to get employee details
IF OBJECT_ID('emp_pkg_get_emp_details', 'P') IS NOT NULL
DROP PROCEDURE emp_pkg_get_emp_details;
GO
CREATE PROCEDURE emp_pkg_get_emp_details(
@emp_id IN AS emp.emp_id%TYPEDECLARE ,
OUT DECLARE @emp_name OUT DECLARE @VARCHAR2,
DECLARE @OUT salary DECLARE @OUT FLOAT
DECLARE ) IS
DECLARE c1 CURSOR LOCAL FOR
SELECT @emp_name, salary
FROM emp
WHERE emp_id = emp_id;
BEGIN
OPEN c1;
FETCH c1 INTO @emp_name, @salary;
CLOSE c1;
IF @@FETCH_STATUS <> 0 BEGIN
SET @emp_name = NULL;
SET @salary = NULL;
END
*** late bonus
CREATE FUNCTION calc_bonus(@salary IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
RETURN salary * 0.1;
END;
END emp_pkg;
/
*** rocedure
DECLARE
@emp_id FLOAT := 10;
emp_name dbo.VARCHAR2(20);
salary NUMBER;
BEGIN
emp_pkg.get_emp_details(@emp_id, emp_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(emp_pkg.calc_bonus(salary), ''));
END;
/
|
CREATE OR REPLACE PACKAGE BODY emp_pkg AS
-- Stored Procedure to get employee details
PROCEDURE get_emp_details(
emp_id IN emp.emp_id%TYPE,
OUT emp_name OUT VARCHAR2,
OUT salary OUT NUMBER
) IS
CURSOR c1 IS
SELECT emp_name, salary
FROM emp
WHERE emp_id = emp_id;
BEGIN
OPEN c1;
FETCH c1 INTO emp_name, salary;
CLOSE c1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
emp_name := NULL;
salary := NULL;
END;
-- Function to calculate bonus
FUNCTION calc_bonus(salary IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN salary * 0.1;
END;
END emp_pkg;
/
-- Call the stored procedure
DECLARE
emp_id NUMBER := 10;
emp_name VARCHAR2(20);
salary NUMBER;
BEGIN
emp_pkg.get_emp_details(emp_id, emp_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
DBMS_OUTPUT.PUT_LINE('Bonus: ' || emp_pkg.calc_bonus(salary));
END;
/
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000
BEGIN
UPDATE emp
SET salary = salary * 1.1
WHERE empno = @v_empno;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary
INTO v_empno, v_salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000
THEN
UPDATE emp
SET salary = salary * 1.1
WHERE empno = v_empno;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE employee_id = 1;
IF @v_salary < 10000 BEGIN
UPDATE employees
SET salary = @v_salary * 1.1
WHERE employee_id = 1;
END
COMMIT;
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM employees
WHERE employee_id = 1;
IF v_salary < 10000 THEN
UPDATE employees
SET salary = v_salary * 1.1
WHERE employee_id = 1;
END IF;
COMMIT;
END;
|
DECLARE
@total_salary FLOAT;
BEGIN
SELECT @total_salary = SUM(salary) FROM employees;
IF @total_salary > 100000 BEGIN
RAISERROR('-1001 Total salary too high', 16, 1);
END
COMMIT;
END; | DECLARE
total_salary NUMBER;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees;
IF total_salary > 100000 THEN
RAISE_APPLICATION_ERROR(-1001, 'Total salary too high');
END IF;
COMMIT;
END; |
DECLARE
@salary FLOAT := 200000;
bonus SET @NUMBER = CASE
WHEN @salary > 100000 THEN 0.1 * @salary
WHEN @salary > 50000 THEN 0.05 * @salary
ELSE 0
END;
BEGIN
IF @salary <= 0 BEGIN
RAISERROR('-20001 Invalid salary', 16, 1);
END
INSERT INTO employees (name, salary, bonus)
VALUES ('John Doe', @salary, bonus);
END;
|
DECLARE
salary NUMBER := 200000;
bonus NUMBER := CASE
WHEN salary > 100000 THEN 0.1 * salary
WHEN salary > 50000 THEN 0.05 * salary
ELSE 0
END;
BEGIN
IF salary <= 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid salary');
END IF;
INSERT INTO employees (name, salary, bonus)
VALUES ('John Doe', salary, bonus);
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_salary = salary
FROM emp
WHERE empno = 7839;
IF v_salary > 5000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END
UPDATE emp
SET salary = salary + 1000
WHERE empno = @v_empno;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for empno ' + isnull(@v_empno, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT empno, ename, salary
INTO v_empno, v_ename, v_salary
FROM emp
WHERE empno = 7839;
IF v_salary > 5000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END IF;
UPDATE emp
SET salary = salary + 1000
WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for empno ' || v_empno);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc
(
@p_input IN AS VARCHAR2
)
AS
v_output dbo.VARCHAR2(20);
BEGIN
SELECT @v_output = isnull(p_input, '') + ' processed';
DBMS_OUTPUT.PUT_LINE(v_output);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for input: ' + isnull(p_input, ''));
END
END;
BEGIN
dbo.my_proc('Hello World');
END;
|
CREATE OR REPLACE PROCEDURE my_proc
(
p_input IN VARCHAR2
)
AS
v_output VARCHAR2(20);
BEGIN
SELECT p_input || ' processed' INTO v_output FROM dual;
DBMS_OUTPUT.PUT_LINE(v_output);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for input: ' || p_input);
END;
BEGIN
my_proc('Hello World');
END;
|
IF OBJECT_ID('calculate_bonus', 'P') IS NOT NULL
DROP PROCEDURE calculate_bonus;
GO
CREATE PROCEDURE calculate_bonus (
@employee_id IN AS NUMBER,
bonus_amount OUT NUMBER
) AS
BEGIN
SELECT @bonus_amount = salary * 0.1
FROM employees
WHERE employee_id = employee_id;
IF bonus_amount > 500 BEGIN
set @bonus_amount = 500;
END
END;
|
CREATE OR REPLACE PROCEDURE calculate_bonus (
employee_id IN NUMBER,
bonus_amount OUT NUMBER
) AS
BEGIN
SELECT salary * 0.1 INTO bonus_amount
FROM employees
WHERE employee_id = employee_id;
IF bonus_amount > 500 THEN
bonus_amount := 500;
END IF;
END;
|
CREATE PROCEDURE create_employee(
@p_first_name IN AS VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_phone_number IN VARCHAR2,
p_hire_date IN DATE,
p_job_id IN VARCHAR2
)
IS
v_emp_id NUMBER;
BEGIN
SELECT @v_emp_id = isnull(max(employee_id), 0) + 1 FROM employees;
INSERT INTO employees (
employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id
)
VALUES (
v_emp_id,
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id
);
COMMIT;
END create_employee;
|
PROCEDURE create_employee(
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_phone_number IN VARCHAR2,
p_hire_date IN DATE,
p_job_id IN VARCHAR2
)
IS
v_emp_id NUMBER;
BEGIN
SELECT nvl(max(employee_id), 0) + 1 INTO v_emp_id FROM employees;
INSERT INTO employees (
employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id
)
VALUES (
v_emp_id,
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id
);
COMMIT;
END create_employee;
|
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
employee_name dbo.VARCHAR2(50);
BEGIN
SELECT @employee_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = employee_id;
RETURN employee_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE
@employee_id FLOAT := 100;
employee_name dbo.VARCHAR2(50);
BEGIN
SET @employee_name = dbo.get_employee_name(@employee_id);
IF employee_name IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(employee_name, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(employee_id IN NUMBER) RETURN VARCHAR2 IS
employee_name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name
INTO employee_name
FROM employees
WHERE employee_id = employee_id;
RETURN employee_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE
employee_id NUMBER := 100;
employee_name VARCHAR2(50);
BEGIN
employee_name := get_employee_name(employee_id);
IF employee_name IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_name);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END;
|
IF OBJECT_ID('update_emp_salary', 'P') IS NOT NULL
DROP PROCEDURE update_emp_salary;
GO
CREATE PROCEDURE update_emp_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
*** of the employee with the specified employee ID.
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
*** s to the database.
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_emp_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
-- Update the salary of the employee with the specified employee ID.
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
-- Commit the changes to the database.
COMMIT;
END;
|
CREATE TABLE employees(
employee_id BIGINT PRIMARY KEY,
employee_name VARCHAR(50) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (1, 'John Doe', 'Sales', 50000);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (2, 'Jane Smith', 'Marketing', 40000);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (3, 'Tom Jones', 'Engineering', 60000);
CREATE PROCEDURE get_employee_details(@employee_id IN AS NUMBER)
AS
v_employee_name dbo.VARCHAR2(50);
v_department dbo.VARCHAR2(50);
v_salary ROW_NUMBER(10,2);
BEGIN
SELECT @v_employee_name = employee_name, @v_department = department, @v_salary = salary
FROM employees
WHERE employee_id = employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_employee_name, ''));
DBMS_OUTPUT.PUT_LINE('Department: ' + isnull(v_department, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
END;
BEGIN
dbo.get_employee_details(1);
END;
|
CREATE TABLE employees(
employee_id NUMBER(10) PRIMARY KEY,
employee_name VARCHAR2(50) NOT NULL,
department VARCHAR2(50),
salary NUMBER(10,2)
);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (1, 'John Doe', 'Sales', 50000);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (2, 'Jane Smith', 'Marketing', 40000);
INSERT INTO employees(employee_id, employee_name, department, salary)
VALUES (3, 'Tom Jones', 'Engineering', 60000);
CREATE PROCEDURE get_employee_details(employee_id IN NUMBER)
AS
v_employee_name VARCHAR2(50);
v_department VARCHAR2(50);
v_salary NUMBER(10,2);
BEGIN
SELECT employee_name, department, salary
INTO v_employee_name, v_department, v_salary
FROM employees
WHERE employee_id = employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
DBMS_OUTPUT.PUT_LINE('Department: ' || v_department);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END;
BEGIN
get_employee_details(1);
END;
|
DECLARE
@sal FLOAT := 1000;
BEGIN
IF @sal > 2000 BEGIN
SET @sal = @sal * 1.1;
END
ELSE BEGIN
SET @sal = @sal * 1.05;
END
UPDATE emp SET sal = @sal WHERE empno = 1;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 An error occurred.', 16, 1);
END;
|
DECLARE
sal NUMBER := 1000;
BEGIN
IF sal > 2000 THEN
sal := sal * 1.1;
ELSE
sal := sal * 1.05;
END IF;
UPDATE emp SET sal = sal WHERE empno = 1;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'An error occurred.');
END;
|
DECLARE
@num_rows FLOAT;
BEGIN
SELECT @num_rows = COUNT(*) FROM emp;
IF @num_rows > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('There are ' + isnull(@num_rows, '') + ' rows in the EMP table.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('The EMP table is empty.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
num_rows NUMBER;
BEGIN
SELECT COUNT(*) INTO num_rows FROM emp;
IF num_rows > 0 THEN
DBMS_OUTPUT.PUT_LINE('There are ' || num_rows || ' rows in the EMP table.');
ELSE
DBMS_OUTPUT.PUT_LINE('The EMP table is empty.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE UpdateEmployeeSalary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee with ID ' + isnull(emp_id, '') + ' not found', 16, 1);
END
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20002 Duplicate salary value not allowed', 16, 1);
WHEN OTHERS THEN
RAISE;
END;
DECLARE
@emp_id FLOAT := 100;
new_salary SET @NUMBER = 25000;
BEGIN
dbo.UpdateEmployeeSalary(@emp_id, new_salary);
END;
|
CREATE PROCEDURE UpdateEmployeeSalary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found');
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20002, 'Duplicate salary value not allowed');
WHEN OTHERS THEN
RAISE;
END;
DECLARE
emp_id NUMBER := 100;
new_salary NUMBER := 25000;
BEGIN
UpdateEmployeeSalary(emp_id, new_salary);
END;
|
declare
@l_emp_id int;
l_emp_name dbo.varchar2(20);
begin
select @l_emp_id = emp_id, @l_emp_name = emp_name from emp where emp_id = 10;
dbms_output.put_line('Employee ID: ' + isnull(@l_emp_id, ''));
dbms_output.put_line('Employee Name: ' + isnull(l_emp_name, ''));
if @@fetch_status <> 0 begin
dbms_output.put_line('No data found for employee ID 10');
end
end;
|
declare
l_emp_id number(6);
l_emp_name varchar2(20);
begin
select emp_id, emp_name into l_emp_id, l_emp_name from emp where emp_id = 10;
dbms_output.put_line('Employee ID: ' || l_emp_id);
dbms_output.put_line('Employee Name: ' || l_emp_name);
exception
when no_data_found then
dbms_output.put_line('No data found for employee ID 10');
end;
|
DECLARE
@v_emp_id FLOAT := 100;
v_emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_emp_name = ename FROM emp WHERE empno = @v_emp_id;
IF v_emp_name IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END
ELSE IF v_emp_name LIKE '%KING%' BEGIN DBMS_OUTPUT.PUT_LINE('Employee is a manager'); END
ELSE DBMS_OUTPUT.PUT_LINE('Employee is not a manager'); END
END;
|
DECLARE
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(50);
BEGIN
SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id;
CASE
WHEN v_emp_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN v_emp_name LIKE '%KING%' THEN DBMS_OUTPUT.PUT_LINE('Employee is a manager');
ELSE DBMS_OUTPUT.PUT_LINE('Employee is not a manager');
END CASE;
END;
|
DECLARE
@v_count FLOAT;
BEGIN
SELECT @v_count = COUNT(*) FROM employees;
IF @v_count > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employees found.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Error: No data found.');
END
END;
|
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM employees;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Employees found.');
ELSE
DBMS_OUTPUT.PUT_LINE('No employees found.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: No data found.');
END;
|
IF OBJECT_ID('prc_demo', 'P') IS NOT NULL
DROP PROCEDURE prc_demo;
GO
CREATE PROCEDURE prc_demo(@a IN AS NUMBER, b OUT NUMBER)
AS
BEGIN
set @b = a * 2;
IF b > 10 BEGIN
RAISERROR('-20001 Value is greater than 10.', 16, 1);
END
END prc_demo;
DECLARE
@l_var FLOAT;
BEGIN
dbo.prc_demo(5, @l_var);
DBMS_OUTPUT.PUT_LINE('Result: ' + isnull(@l_var, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE prc_demo(a IN NUMBER, b OUT NUMBER)
AS
BEGIN
b := a * 2;
IF b > 10 THEN
RAISE_APPLICATION_ERROR(-20001, 'Value is greater than 10.');
END IF;
END prc_demo;
DECLARE
l_var NUMBER;
BEGIN
prc_demo(5, l_var);
DBMS_OUTPUT.PUT_LINE('Result: ' || l_var);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
CREATE FUNCTION get_emp_details(@empno IN NUMBERDECLARE )
RETURN
BEGIN employee%ROWTYPE
RETURN NULL;
END;
IS
emp employee%ROWTYPE;
BEGIN
SELECT @emp = * FROM employee WHERE empno = empno;
RETURN emp;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
CREATE PROCEDURE update_emp_salary(@empno IN AS NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employee SET salary = new_salary WHERE empno = empno;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee Not Found', 16, 1);
END
END;
BEGIN
DECLARE @empno FLOAT := 10;
DECLARE
BEGIN
SET @emp = dbo.get_emp_details(@empno);
IF @emp IS NOT NULL BEGIN
SET @emp.salary = @emp_salary * 1.10;
dbo.update_emp_salary(@empno, @emp_salary);
COMMIT;
END
END;
END;
|
CREATE FUNCTION get_emp_details(empno IN NUMBER)
RETURN employee%ROWTYPE
IS
emp employee%ROWTYPE;
BEGIN
SELECT * INTO emp FROM employee WHERE empno = empno;
RETURN emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE PROCEDURE update_emp_salary(empno IN NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employee SET salary = new_salary WHERE empno = empno;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee Not Found');
END;
BEGIN
DECLARE empno NUMBER := 10;
DECLARE emp employee%ROWTYPE;
BEGIN
emp := get_emp_details(empno);
IF emp IS NOT NULL THEN
emp.salary := emp.salary * 1.10;
update_emp_salary(empno, emp.salary);
COMMIT;
END IF;
END;
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 -1;
END
END;
DECLARE
@bonus FLOAT;
BEGIN
SET @bonus = dbo.get_employee_salary(100);
IF @bonus IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('No bonus for employee 100');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Bonus for employee 100: ' + isnull(@bonus, ''));
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 -1;
END;
DECLARE
bonus NUMBER;
BEGIN
bonus := get_employee_salary(100);
IF bonus IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No bonus for employee 100');
ELSE
DBMS_OUTPUT.PUT_LINE('Bonus for employee 100: ' || bonus);
END IF;
END;
|
DECLARE
@v_empno FLOAT := 7788;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary
FROM emp
WHERE empno = @v_empno;
UPDATE emp
SET salary = v_salary * 1.10
WHERE empno = @v_empno;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
v_empno NUMBER := 7788;
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM emp
WHERE empno = v_empno;
UPDATE emp
SET salary = v_salary * 1.10
WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN OTHERS THEN
RAISE;
END;
|
BEGIN
DECLARE @v_employee_id FLOAT := 100;
DECLARE @v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = @v_employee_id;
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = @v_salary + 1000 WHERE employee_id = @v_employee_id;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low to increase', 16, 1);
END
END;
END; | BEGIN
DECLARE v_employee_id NUMBER := 100;
DECLARE v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = v_employee_id;
IF v_salary > 10000 THEN
UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = v_employee_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to increase');
END IF;
END;
END; |
CREATE PROCEDURE create_dept(@dept_id IN AS NUMBER, dept_name IN VARCHAR2, loc_id IN NUMBER) IS
BEGIN
INSERT INTO departments (dept_id, dept_name, loc_id) VALUES (dept_id, dept_name, loc_id);
COMMIT;
END;
CREATE FUNCTION get_emp_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
SELECT @emp_salary = salary FROM employees WHERE emp_id = emp_id;
RETURN emp_salary;
END;
BEGIN
DECLARE
@emp_name VARCHAR(50);
BEGIN
SELECT @emp_name = @emp_name FROM employees WHERE emp_id = 1;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found!');
END
END;
END;
|
PROCEDURE create_dept(dept_id IN NUMBER, dept_name IN VARCHAR2, loc_id IN NUMBER) IS
BEGIN
INSERT INTO departments (dept_id, dept_name, loc_id) VALUES (dept_id, dept_name, loc_id);
COMMIT;
END;
FUNCTION get_emp_salary(emp_id IN NUMBER) RETURN NUMBER IS
BEGIN
SELECT salary INTO emp_salary FROM employees WHERE emp_id = emp_id;
RETURN emp_salary;
END;
BEGIN
DECLARE
emp_name VARCHAR2(50);
BEGIN
SELECT emp_name INTO emp_name FROM employees WHERE emp_id = 1;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found!');
END;
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SET @v_empno = 7839;
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
IF @@ERROR <> 0 BEGIN
RAISERROR('-20001 Error updating employee salary', 16, 1);
END
COMMIT;
END;
|
DECLARE
v_empno NUMBER;
BEGIN
v_empno := 7839;
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Error updating employee salary');
END IF;
COMMIT;
END;
|
CREATE PROCEDURE sp_InsertEmp(@p_id IN AS NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER)
AS
BEGIN
INSERT INTO emp(id, name, salary) VALUES (p_id, p_name, p_salary);
END;
CREATE FUNCTION fn_GetEmpSalary(@p_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
SELECT @result = salary FROM emp WHERE id = p_id;
RETURN result;
END;
BEGIN
DECLARE @v_empId FLOAT;
DECLARE @v_empName VARCHAR(50);
DECLARE @v_empSalary FLOAT;
BEGIN
SELECT @v_empId = empid, @v_empName = empname, @v_empSalary = salary FROM emp WHERE empid = 10;
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('ID: ' + ISNULL(@v_empId, ''));
DBMS_OUTPUT.PUT_LINE('Name: ' + ISNULL(@v_empName, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(@v_empSalary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 10');
END
END;
END;
|
CREATE PROCEDURE sp_InsertEmp(p_id IN NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER)
AS
BEGIN
INSERT INTO emp(id, name, salary) VALUES (p_id, p_name, p_salary);
END;
CREATE FUNCTION fn_GetEmpSalary(p_id IN NUMBER) RETURN NUMBER
AS
BEGIN
SELECT salary INTO result FROM emp WHERE id = p_id;
RETURN result;
END;
BEGIN
DECLARE v_empId NUMBER;
DECLARE v_empName VARCHAR2(50);
DECLARE v_empSalary NUMBER;
BEGIN
SELECT empid, empname, salary INTO v_empId, v_empName, v_empSalary FROM emp WHERE empid = 10;
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('ID: ' || v_empId);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_empName);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_empSalary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 10');
END;
END;
|
DECLARE
*** and cursors
@v_emp_id FLOAT := 100;
c_emp_detail CURSOR (p_emp_id NUMBER)
IS
SELECT ename, job, sal, deptno FROM emp WHERE empno = p_emp_id;
BEGIN
-- Op... ***
SET = @v_emp_id;
OPEN c_emp_detail;
*** employee data
FETCH c_emp_detail INTO @v_ename, @v_job, @v_sal, @v_deptno;
WHILE SQL%FOUND BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
DBMS_OUTPUT.PUT_LINE('Job Title: ' + isnull(v_job, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_sal, ''));
DBMS_OUTPUT.PUT_LINE('Department No: ' + isnull(v_deptno, ''));
FETCH c_emp_detail INTO @v_ename, @v_job, @v_sal, @v_deptno;
END;
-- Cl... ***
CLOSE c_emp_detail;
-- Er... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee ID ' + isnull(@v_emp_id, ''));
END
END; | DECLARE
-- Declare variables and cursors
v_emp_id NUMBER := 100;
c_emp_detail CURSOR (p_emp_id NUMBER)
IS
SELECT ename, job, sal, deptno FROM emp WHERE empno = p_emp_id;
BEGIN
-- Open the cursor
OPEN c_emp_detail(v_emp_id);
-- Fetch and display employee data
FETCH c_emp_detail INTO v_ename, v_job, v_sal, v_deptno;
WHILE SQL%FOUND LOOP
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Job Title: ' || v_job);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_sal);
DBMS_OUTPUT.PUT_LINE('Department No: ' || v_deptno);
FETCH c_emp_detail INTO v_ename, v_job, v_sal, v_deptno;
END LOOP;
-- Close the cursor
CLOSE c_emp_detail;
-- Error handling
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee ID ' || v_emp_id);
END; |
CREATE PROCEDURE calculate_total_sales AS
BEGIN
SET NOCOUNT ON;
SELECT @total_sales = SUM(sales) FROM sales_table;
END;
|
CREATE PROCEDURE calculate_total_sales AS
BEGIN
SELECT SUM(sales) INTO @total_sales FROM sales_table;
END;
|
/* package body p_example */
if object_id('p_example_f_example', 'fn') is not null
drop function p_example_f_example;
go
create function p_example_f_example(
@p_value in number
DECLARE ) return
begin varchar2
return null;
end; declare @is
begin
declare @return 'Example value: ' declare || declare @p_value;
create procedure p_insert(
@p_id in as number,
p_value in varchar2
) is
begin
insert into t_example (id, value) values (p_id, p_value);
end;
create procedure p_update(
@p_id in as number,
p_value in varchar2
) is
begin
update t_example set value = p_value where id = p_id;
end;
create procedure p_delete(
@p_id in as number
) is
begin
delete from t_example where id = p_id;
end;
end;
/
|
create or replace package body p_example as
function f_example(
p_value in number
) return varchar2 is
begin
return 'Example value: ' || p_value;
end;
procedure p_insert(
p_id in number,
p_value in varchar2
) is
begin
insert into t_example (id, value) values (p_id, p_value);
end;
procedure p_update(
p_id in number,
p_value in varchar2
) is
begin
update t_example set value = p_value where id = p_id;
end;
procedure p_delete(
p_id in number
) is
begin
delete from t_example where id = p_id;
end;
end;
/
|
CREATE PROCEDURE calculate_total_sales(@customer_id IN AS NUMBER) IS
total_sales NUMBER;
BEGIN
SELECT @total_sales = SUM(amount)
FROM sales
WHERE customer_id = customer_id;
IF total_sales IS NULL BEGIN
Raiserror('-20000 No sales found for customer', 16, 1);
END
RETURN total_sales;
END;
|
CREATE PROCEDURE calculate_total_sales(customer_id IN NUMBER) IS
total_sales NUMBER;
BEGIN
SELECT SUM(amount)
INTO total_sales
FROM sales
WHERE customer_id = customer_id;
IF total_sales IS NULL THEN
Raise_application_error(-20000, 'No sales found for customer');
END IF;
RETURN total_sales;
END;
|
CREATE TABLE employees (
id FLOAT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane', 12000);
INSERT INTO employees (id, name, salary) VALUES (3, 'Jim', 15000);
IF OBJECT_ID('get_employees', 'P') IS NOT NULL
DROP PROCEDURE get_employees;
GO
CREATE PROCEDURE get_employees (
@p_id IN AS NUMBER,
OUT p_name OUT VARCHAR2,
OUT p_salary OUT NUMBER
) IS
BEGIN
SELECT @p_name = name, @p_salary = salary FROM employees WHERE id = p_id;
END;
IF OBJECT_ID('get_employee_count', 'FN') IS NOT NULL
DROP FUNCTION get_employee_count;
GO
CREATE FUNCTION get_employee_count () RETURNS FLOAT AS
BEGIN
RETURN (SELECT COUNT(*) FROM employees);
END;
BEGIN
DECLARE
@emp_id FLOAT := 1;
emp_name dbo.VARCHAR2(50);
emp_salary ROW_NUMBER(10,2);
BEGIN
dbo.get_employees(@emp_id, emp_name, emp_salary);
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(@emp_id, ''));
END
END;
END;
SELECT id, name, salary FROM employees WHERE salary > (
SELECT AVG(salary) FROM employees
); | CREATE TABLE employees (
id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
salary NUMBER(10,2) NOT NULL
);
INSERT INTO employees (id, name, salary) VALUES (1, 'John', 10000);
INSERT INTO employees (id, name, salary) VALUES (2, 'Jane', 12000);
INSERT INTO employees (id, name, salary) VALUES (3, 'Jim', 15000);
CREATE OR REPLACE PROCEDURE get_employees (
p_id IN NUMBER,
OUT p_name OUT VARCHAR2,
OUT p_salary OUT NUMBER
) IS
BEGIN
SELECT name, salary INTO p_name, p_salary FROM employees WHERE id = p_id;
END;
CREATE OR REPLACE FUNCTION get_employee_count () RETURN NUMBER IS
BEGIN
RETURN (SELECT COUNT(*) FROM employees);
END;
BEGIN
DECLARE
emp_id NUMBER := 1;
emp_name VARCHAR2(50);
emp_salary NUMBER(10,2);
BEGIN
get_employees(emp_id, emp_name, emp_salary);
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || emp_id);
END;
END;
SELECT id, name, salary FROM employees WHERE salary > (
SELECT AVG(salary) FROM employees
); |
CREATE FUNCTION GetEmployeeName(@emp_id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
RETURN (SELECT name FROM employees WHERE id = @emp_id);
END;
DECLARE
@emp_name VARCHAR(20);
BEGIN
SELECT @emp_name = dbo.GetEmployeeName(12345);
DBMS_OUTPUT.PUT_LINE(@emp_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE FUNCTION GetEmployeeName(emp_id NUMBER) RETURN VARCHAR2 AS
BEGIN
RETURN (SELECT name FROM employees WHERE id = emp_id);
END;
DECLARE
emp_name VARCHAR2(20);
BEGIN
SELECT GetEmployeeName(12345) INTO emp_name;
DBMS_OUTPUT.PUT_LINE(emp_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
DECLARE
@v_emp_name VARCHAR(30);
v_emp_salary NUMBER;
BEGIN
SELECT @v_emp_name = ename, @v_emp_salary = sal
FROM emp
WHERE empno = 7934;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_emp_salary, ''));
IF v_emp_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee record not found.');
END
END;
|
DECLARE
v_emp_name VARCHAR2(30);
v_emp_salary NUMBER;
BEGIN
SELECT ename, sal
INTO v_emp_name, v_emp_salary
FROM emp
WHERE empno = 7934;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_emp_salary);
IF v_emp_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee record not found.');
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE dept_id = 10;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 No employee found with department ID 10', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
BEGIN
SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE dept_id = 10;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with department ID 10');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
DECLARE @name VARCHAR(20);
DECLARE @age INT;
BEGIN
SET @name = 'John Doe';
SET @age = 30;
INSERT INTO People (Name, Age) VALUES (@name, @age);
END;
BEGIN
DECLARE @result VARCHAR(20);
SELECT @result = Name FROM People WHERE Age = 30;
IF (@result IS NULL) BEGIN
RAISE ERROR 'No person with age 30 was found';
END
END; | DECLARE @name VARCHAR(20);
DECLARE @age INT;
BEGIN
SET @name = 'John Doe';
SET @age = 30;
INSERT INTO People (Name, Age) VALUES (@name, @age);
END;
BEGIN
DECLARE @result VARCHAR(20);
SET @result = (SELECT Name FROM People WHERE Age = 30);
IF (@result IS NULL) THEN
RAISE ERROR 'No person with age 30 was found';
END IF;
END; |
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal, @v_comm = comm
FROM emp
WHERE empno = 7839;
IF v_comm IS NULL BEGIN
UPDATE emp
SET comm = 0
WHERE empno = 7839;
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
BEGIN
SELECT empno, ename, sal, comm
INTO v_empno, v_ename, v_sal, v_comm
FROM emp
WHERE empno = 7839;
IF v_comm IS NULL THEN
UPDATE emp
SET comm = 0
WHERE empno = 7839;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END;
|
BEGIN
IF state = 'VALID' BEGIN
UPDATE customer SET status = 'ACTIVE' WHERE name = 'John Doe';
END
ELSE BEGIN
RAISERROR('-20001 Customer not valid', 16, 1);
END
IF dbo.row_count() > 0 BEGIN
COMMIT;
RETURN TRUE;
END
ELSE BEGIN
ROLLBACK;
RETURN FALSE;
END
END; | BEGIN
IF state = 'VALID' THEN
UPDATE customer SET status = 'ACTIVE' WHERE name = 'John Doe';
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Customer not valid');
END IF;
IF row_count() > 0 THEN
COMMIT;
RETURN TRUE;
ELSE
ROLLBACK;
RETURN FALSE;
END IF;
END; |
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
UPDATE employees SET salary = @v_salary * 1.10 WHERE department_id = 20;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
raiserror('-20001 No employee with that ID found', 16, 1);
END
WHEN OTHERS THEN
raise;
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
UPDATE employees SET salary = v_salary * 1.10 WHERE department_id = 20;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
raise_application_error(-20001, 'No employee with that ID found');
WHEN OTHERS THEN
raise;
END;
|
BEGIN
DECLARE rec CURSOR FOR SELECT * FROM employees; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO employee_history VALUES (@rec_employee_id, @rec_first_name, @rec_last_name, GETDATE());
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
COMMIT;
END;
|
BEGIN
FOR rec IN (SELECT * FROM employees) LOOP
INSERT INTO employee_history VALUES (rec.employee_id, rec.first_name, rec.last_name, SYSDATE);
END LOOP;
COMMIT;
END;
|
BEGIN
-- De... ***
v_emp_id NUMBER;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
*** rmation
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 100;
*** nformation
UPDATE employees
SET salary = v_salary * 1.10
WHERE emp_id = v_emp_id;
-- Co... ***
COMMIT;
*** d salary
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' now has a salary of ' + isnull(v_salary * 1.10, ''));
EXCEPTION
WHEN OTHERS THEN
*** ges and output the error
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END; | BEGIN
-- Declare variables
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
-- Get employee information
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 100;
-- Update employee information
UPDATE employees
SET salary = v_salary * 1.10
WHERE emp_id = v_emp_id;
-- Commit the changes
COMMIT;
-- Output the updated salary
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' now has a salary of ' || v_salary * 1.10);
EXCEPTION
WHEN OTHERS THEN
-- Rollback any changes and output the error
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END; |
CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER) AS
BEGIN
SELECT @salary = salary FROM employees WHERE id = emp_id;
IF @salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
RETURN @salary;
END;
/
|
CREATE PROCEDURE get_employee_salary(emp_id IN NUMBER) AS
BEGIN
SELECT salary INTO @salary FROM employees WHERE id = emp_id;
IF @salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
RETURN @salary;
END;
/
|
IF OBJECT_ID('get_customer_details', 'FN') IS NOT NULL
DROP FUNCTION get_customer_details;
GO
CREATE FUNCTION get_customer_details(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
c SYS_REFCURSOR;
BEGIN
SELECT * FROM customers WHERE customer_id = customer_id;
RETURN c;
END;
DECLARE
@customer_id FLOAT;
customer_details SYS_REFCURSOR;
customer_name dbo.VARCHAR2(50);
BEGIN
SET @customer_id = 100;
SET @customer_details = dbo.get_customer_details(@customer_id);
FETCH customer_details INTO @customer_name;
DBMS_OUTPUT.PUT_LINE(customer_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE FUNCTION get_customer_details(customer_id IN NUMBER) RETURN SYS_REFCURSOR IS
c SYS_REFCURSOR;
BEGIN
OPEN c FOR
SELECT * FROM customers WHERE customer_id = customer_id;
RETURN c;
END;
DECLARE
customer_id NUMBER;
customer_details SYS_REFCURSOR;
customer_name VARCHAR2(50);
BEGIN
customer_id := 100;
customer_details := get_customer_details(customer_id);
FETCH customer_details INTO customer_name;
DBMS_OUTPUT.PUT_LINE(customer_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM emp WHERE emp_name = 'John Doe';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE emp 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 emp WHERE emp_name = 'John Doe';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE emp SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
END;
/ |
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name
FROM emp
WHERE emp_id = 10;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(20);
BEGIN
SELECT emp_id, emp_name
INTO v_emp_id, v_emp_name
FROM emp
WHERE emp_id = 10;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
|
CREATE PROCEDURE get_employee_info(@ename IN AS VARCHAR2, sal OUT NUMBER) AS
BEGIN
SELECT @sal = salary FROM employees WHERE name = ename;
IF @@fetch_status <> 0 BEGIN
set @sal = -1;
END
END;
/
CALL get_employee_info('Alice', :salary);
IF :salary < 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found!');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(:salary, ''));
END
|
CREATE PROCEDURE get_employee_info(ename IN VARCHAR2, sal OUT NUMBER) AS
BEGIN
SELECT salary INTO sal FROM employees WHERE name = ename;
EXCEPTION
WHEN no_data_found THEN
sal := -1;
END;
/
CALL get_employee_info('Alice', :salary);
IF :salary < 0 THEN
DBMS_OUTPUT.PUT_LINE('Employee not found!');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary: ' || :salary);
END IF;
|
DECLARE
@v_deptno SMALLINT;
BEGIN
SELECT @v_deptno = deptno FROM dept WHERE dname = 'Sales';
BEGIN
IF @v_deptno IS NULL BEGIN
RAISERROR('-20001 Department not found', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No data found', 16, 1);
END
END;
DECLARE r CURSOR FOR SELECT empno, ename FROM emp WHERE deptno = @v_deptno; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@r_empno, '') + ' ' + isnull(@r_ename, ''));
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
END; | DECLARE
v_deptno NUMBER(2);
BEGIN
SELECT deptno INTO v_deptno FROM dept WHERE dname = 'Sales';
BEGIN
IF v_deptno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Department not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No data found');
END;
FOR r IN (SELECT empno, ename FROM emp WHERE deptno = v_deptno) LOOP
DBMS_OUTPUT.PUT_LINE(r.empno || ' ' || r.ename);
END LOOP;
END; |
declare
*** ions
@v_emp_id float;
v_emp_name dbo.varchar2(100);
v_cursor sys_refcursor;
begin
*** r Cursor Opening
begin
select emp_id, emp_name from employees where emp_id > 100;
if @@fetch_status <> 0 begin
raiserror('-20101 No employees found with ID greater than 100', 16, 1);
end
end;
*** he Cursor
while 1=1 begin
fetch v_cursor into @v_emp_id, @v_emp_name;
if @@fetch_status <> 0 break;
*** Displaying Employee Data
if @v_emp_id mod 2 = 0 begin
dbms_output.put_line('Even ID: ' + isnull(@v_emp_id, '') + ', ' + isnull(v_emp_name, ''));
end
else begin
dbms_output.put_line('Odd ID: ' + isnull(@v_emp_id, '') + ', ' + isnull(v_emp_name, ''));
end
end;
-- Cl... ***
close v_cursor;
*** Call
emp_pkg.incr_salary(101, 1000);
-- Fu... ***
update employees set salary = salary * dbo.func_get_bonus(5) where emp_id = 102;
commit;
exception
when others then
rollback;
raise;
end;
|
declare
-- Variable Declarations
v_emp_id number;
v_emp_name varchar2(100);
v_cursor sys_refcursor;
begin
-- Error Handling for Cursor Opening
begin
open v_cursor for
select emp_id, emp_name from employees where emp_id > 100;
exception
when no_data_found then
raise_application_error(-20101, 'No employees found with ID greater than 100');
end;
-- Looping Through the Cursor
loop
fetch v_cursor into v_emp_id, v_emp_name;
exit when v_cursor%notfound;
-- Control Flow for Displaying Employee Data
if v_emp_id mod 2 = 0 then
dbms_output.put_line('Even ID: ' || v_emp_id || ', ' || v_emp_name);
else
dbms_output.put_line('Odd ID: ' || v_emp_id || ', ' || v_emp_name);
end if;
end loop;
-- Closing the Cursor
close v_cursor;
-- Stored Procedure Call
emp_pkg.incr_salary(101, 1000);
-- Function Call
update employees set salary = salary * func_get_bonus(5) where emp_id = 102;
commit;
exception
when others then
rollback;
raise;
end;
|
DECLARE
@v_emp_id FLOAT := 10;
BEGIN
IF @v_emp_id IS NOT NULL BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_emp_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20002 Invalid employee ID', 16, 1);
END
END;
/
|
DECLARE
v_emp_id NUMBER := 10;
BEGIN
IF v_emp_id IS NOT NULL THEN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_emp_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20002, 'Invalid employee ID');
END IF;
END;
/
|
CREATE PROCEDURE GetCustomerInfo(@cust_id IN AS NUMBER)
AS
name dbo.VARCHAR2(50);
balance NUMBER;
BEGIN
SELECT @name = name, @balance = balance FROM customers WHERE customer_id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(name, ''));
DBMS_OUTPUT.PUT_LINE('Customer Balance: ' + isnull(balance, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No customer found with ID ' + isnull(cust_id, ''));
END
END;
/
BEGIN
DECLARE
@i FLOAT;
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE(@i);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
CREATE PROCEDURE GetCustomerInfo(cust_id IN NUMBER)
AS
name VARCHAR2(50);
balance NUMBER;
BEGIN
SELECT name, balance INTO name, balance FROM customers WHERE customer_id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || name);
DBMS_OUTPUT.PUT_LINE('Customer Balance: ' || balance);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No customer found with ID ' || cust_id);
END;
/
BEGIN
DECLARE
i NUMBER;
BEGIN
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
/
|