sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('p_create_student', 'P') IS NOT NULL
DROP PROCEDURE p_create_student;
GO
CREATE PROCEDURE p_create_student(@student_id IN AS NUMBER, student_name IN VARCHAR2, dob IN DATE)
IS
BEGIN
INSERT INTO students (student_id, student_name, dob)
VALUES (student_id, student_name, dob);
END;
DECLARE
@v_student_id FLOAT := 10;
v_student_name dbo.VARCHAR2(20) := 'John Doe';
v_dob SET @DATE = CONVERT(DATETIME, '2000-01-01');
BEGIN
dbo.p_create_student(@v_student_id, v_student_name, v_dob);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END; | CREATE OR REPLACE PROCEDURE p_create_student(student_id IN NUMBER, student_name IN VARCHAR2, dob IN DATE)
IS
BEGIN
INSERT INTO students (student_id, student_name, dob)
VALUES (student_id, student_name, dob);
END;
DECLARE
v_student_id NUMBER := 10;
v_student_name VARCHAR2(20) := 'John Doe';
v_dob DATE := TO_DATE('2000-01-01', 'YYYY-MM-DD');
BEGIN
p_create_student(v_student_id, v_student_name, v_dob);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END; |
DECLARE
@v_sal FLOAT;
v_deptno NUMBER;
BEGIN
*** partment number for employee with employee ID 7369
SELECT @v_sal = salary, @v_deptno = department_id
FROM employees
WHERE employee_id = 7369;
IF @v_sal > 10000 BEGIN
*** than 10,000
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10,000');
END
ELSE BEGIN
*** an or equal to 10,000
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 10,000');
END
*** is in department 20
IF v_deptno = 20 BEGIN
*** partment 20
DBMS_OUTPUT.PUT_LINE('Employee is in department 20');
END
ELSE BEGIN
*** n department 20
DBMS_OUTPUT.PUT_LINE('Employee is not in department 20');
END
IF @@FETCH_STATUS <> 0 BEGIN
*** employee with employee ID 7369
DBMS_OUTPUT.PUT_LINE('No data found for employee with employee ID 7369');
END
END;
|
DECLARE
v_sal NUMBER;
v_deptno NUMBER;
BEGIN
-- Get salary and department number for employee with employee ID 7369
SELECT salary, department_id
INTO v_sal, v_deptno
FROM employees
WHERE employee_id = 7369;
IF v_sal > 10000 THEN
-- Salary is greater than 10,000
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10,000');
ELSE
-- Salary is less than or equal to 10,000
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 10,000');
END IF;
-- Check if employee is in department 20
IF v_deptno = 20 THEN
-- Employee is in department 20
DBMS_OUTPUT.PUT_LINE('Employee is in department 20');
ELSE
-- Employee is not in department 20
DBMS_OUTPUT.PUT_LINE('Employee is not in department 20');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- No data found for employee with employee ID 7369
DBMS_OUTPUT.PUT_LINE('No data found for employee with employee ID 7369');
END;
|
DECLARE
@my_var VARCHAR(100) := 'Hello World';
BEGIN
DBMS_OUTPUT.PUT_LINE('Value of my_var before assignment: ' + isnull(@my_var, ''));
SET @my_var = 'Goodbye World';
DBMS_OUTPUT.PUT_LINE('Value of my_var after assignment: ' + isnull(@my_var, ''));
IF @my_var = 'Goodbye World' BEGIN
DBMS_OUTPUT.PUT_LINE('my_var has been correctly assigned.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('my_var has not been correctly assigned.');
END
BEGIN
DBMS_OUTPUT.PUT_LINE('Nested block started.');
SET @my_var = isnull(@my_var, '') + '!';
DBMS_OUTPUT.PUT_LINE('Value of my_var inside the nested block: ' + isnull(@my_var, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred in the nested block: ' + ISNULL(SQLERRM, ''));
END;
DBMS_OUTPUT.PUT_LINE('Value of my_var after the nested block: ' + isnull(@my_var, ''));
END;
|
DECLARE
my_var VARCHAR2(100) := 'Hello World';
BEGIN
DBMS_OUTPUT.PUT_LINE('Value of my_var before assignment: ' || my_var);
my_var := 'Goodbye World';
DBMS_OUTPUT.PUT_LINE('Value of my_var after assignment: ' || my_var);
IF my_var = 'Goodbye World' THEN
DBMS_OUTPUT.PUT_LINE('my_var has been correctly assigned.');
ELSE
DBMS_OUTPUT.PUT_LINE('my_var has not been correctly assigned.');
END IF;
BEGIN
DBMS_OUTPUT.PUT_LINE('Nested block started.');
my_var := my_var || '!';
DBMS_OUTPUT.PUT_LINE('Value of my_var inside the nested block: ' || my_var);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred in the nested block: ' || SQLERRM);
END;
DBMS_OUTPUT.PUT_LINE('Value of my_var after the nested block: ' || my_var);
END;
|
BEGIN
DECLARE @v_name VARCHAR(255);
DECLARE @v_age FLOAT;
BEGIN
SELECT @v_name = name, @v_age = age FROM users WHERE id = 1;
IF @v_age > 18 BEGIN
DBMS_OUTPUT.PUT_LINE('User is an adult');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('User is not an adult');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No user found with id 1');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving user data');
END;
END; | BEGIN
DECLARE v_name VARCHAR2(255);
DECLARE v_age NUMBER;
BEGIN
SELECT name, age INTO v_name, v_age FROM users WHERE id = 1;
IF v_age > 18 THEN
DBMS_OUTPUT.PUT_LINE('User is an adult');
ELSE
DBMS_OUTPUT.PUT_LINE('User is not an adult');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No user found with id 1');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving user data');
END;
END; |
IF OBJECT_ID('p_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE p_customer_orders;
GO
CREATE PROCEDURE p_customer_orders
(@p_customer_id IN AS NUMBER)
IS
v_orders NUMBER;
BEGIN
SELECT @v_orders = COUNT(*) FROM orders WHERE customer_id = p_customer_id;
IF v_orders > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Customer has placed ' + isnull(v_orders, '') + ' orders.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Customer has not placed any orders.');
END
END;
|
CREATE OR REPLACE PROCEDURE p_customer_orders
(p_customer_id IN NUMBER)
IS
v_orders NUMBER;
BEGIN
SELECT COUNT(*) INTO v_orders FROM orders WHERE customer_id = p_customer_id;
IF v_orders > 0 THEN
DBMS_OUTPUT.PUT_LINE('Customer has placed ' || v_orders || ' orders.');
ELSE
DBMS_OUTPUT.PUT_LINE('Customer has not placed any orders.');
END IF;
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@emp_id IN AS NUMBER,
new_salary IN NUMBER
)
IS
updated_cnt NUMBER;
BEGIN
*** of the employee with the specified ID.
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
*** rows updated.
SELECT @updated_cnt = COUNT(*) FROM employees WHERE employee_id = emp_id;
*** were updated.
IF updated_cnt = 0 BEGIN
*** no rows were updated.
RAISERROR('-20001 Employee with ID ' + isnull(emp_id, '') + ' not found.', 16, 1);
END
-- Co... ***
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
emp_id IN NUMBER,
new_salary IN NUMBER
)
IS
updated_cnt NUMBER;
BEGIN
-- Update the salary of the employee with the specified ID.
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
-- Get the number of rows updated.
SELECT COUNT(*) INTO updated_cnt FROM employees WHERE employee_id = emp_id;
-- Check if any rows were updated.
IF updated_cnt = 0 THEN
-- Raise an error if no rows were updated.
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found.');
END IF;
-- Commit the changes.
COMMIT;
END;
|
CREATE PROCEDURE add_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,
p_salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary
)
VALUES (
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary
);
COMMIT;
END;
/
CREATE FUNCTION get_employee_by_id(@p_employee_id IN NUMBERDECLARE ) RETURN
BEGIN employees%ROWTYPE
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT *
FROM employees
WHERE employee_id = p_employee_id
);
END;
/
BEGIN
DECLARE
@l_employee_id FLOAT;
BEGIN
SET @l_employee_id = dbo.get_employee_by_id(100);
IF @l_employee_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
END;
/
|
CREATE PROCEDURE add_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,
p_salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary
)
VALUES (
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary
);
COMMIT;
END;
/
FUNCTION get_employee_by_id(p_employee_id IN NUMBER) RETURN employees%ROWTYPE
AS
BEGIN
RETURN (
SELECT *
FROM employees
WHERE employee_id = p_employee_id
);
END;
/
BEGIN
DECLARE
l_employee_id NUMBER;
BEGIN
l_employee_id := get_employee_by_id(100);
IF l_employee_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
END;
/
|
DECLARE
@emp_id FLOAT;
emp_name dbo.VARCHAR2(50);
salary NUMBER;
BEGIN
SELECT @emp_id = employee_id, @emp_name = employee_name, @salary = salary
FROM employees
WHERE employee_id = &emp_id;
IF salary > 50000 BEGIN
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = @emp_id;
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee Not Found', 16, 1);
END
END;
|
DECLARE
emp_id NUMBER;
emp_name VARCHAR2(50);
salary NUMBER;
BEGIN
SELECT employee_id, employee_name, salary
INTO emp_id, emp_name, salary
FROM employees
WHERE employee_id = &emp_id;
IF salary > 50000 THEN
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = emp_id;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee Not Found');
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 VARCHAR2)
AS
BEGIN
SELECT @param2 = UPPER(param1);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE my_proc (param1 IN VARCHAR2, param2 OUT VARCHAR2)
AS
BEGIN
SELECT UPPER(param1) INTO param2 FROM dual;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@deptno FLOAT := 20;
ename dbo.VARCHAR2(20);
salary NUMBER;
bonus NUMBER;
BEGIN
*** ee name, salary, and bonus for a given department number
SELECT @ename = ename, @salary = salary, @bonus = bonus
FROM emp
WHERE @deptno = @deptno;
*** s is greater than 500
IF bonus > 500 BEGIN
*** reater than 500, give the employee a raise of 10%
SET @salary = salary * 1.10;
END
*** ee's salary
UPDATE emp
SET salary = salary
WHERE ename = ename;
-- Co... ***
COMMIT;
EXCEPTION
*** that occur during the execution of the block
WHEN OTHERS THEN
*** ges that have been made
ROLLBACK;
END; | DECLARE
deptno NUMBER := 20;
ename VARCHAR2(20);
salary NUMBER;
bonus NUMBER;
BEGIN
-- Select the employee name, salary, and bonus for a given department number
SELECT ename, salary, bonus
INTO ename, salary, bonus
FROM emp
WHERE deptno = deptno;
-- Check if the bonus is greater than 500
IF bonus > 500 THEN
-- If the bonus is greater than 500, give the employee a raise of 10%
salary := salary * 1.10;
END IF;
-- Update the employee's salary
UPDATE emp
SET salary = salary
WHERE ename = ename;
-- Commit the changes
COMMIT;
EXCEPTION
-- Catch any errors that occur during the execution of the block
WHEN OTHERS THEN
-- Rollback any changes that have been made
ROLLBACK;
END; |
BEGIN
DECLARE @v_sal FLOAT;
DECLARE @v_comm FLOAT;
BEGIN
SELECT @v_sal = salary, @v_comm = commission_pct
FROM employees
WHERE employee_id = 100;
IF @v_comm IS NULL BEGIN
SET @v_comm = 0;
END
SET @v_sal = @v_sal * (1 + @v_comm);
UPDATE employees
SET salary = @v_sal
WHERE employee_id = 100;
END;
END; | BEGIN
DECLARE v_sal NUMBER;
DECLARE v_comm NUMBER;
BEGIN
SELECT salary, commission_pct INTO v_sal, v_comm
FROM employees
WHERE employee_id = 100;
IF v_comm IS NULL THEN
v_comm := 0;
END IF;
v_sal := v_sal * (1 + v_comm);
UPDATE employees
SET salary = v_sal
WHERE employee_id = 100;
END;
END; |
CREATE FUNCTION get_customer_name(@customer_id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @customer_name VARCHAR(50);
SELECT @customer_name = name FROM customers WHERE id = @customer_id;
RETURN @customer_name;
END;
CREATE PROCEDURE update_customer_balance(@customer_id FLOAT, @amount FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE customers SET balance = balance + @amount WHERE id = @customer_id;
END;
DECLARE
@customer_name VARCHAR(50);
BEGIN
SET @customer_name = dbo.get_customer_name(12345);
dbo.update_customer_balance(12345, 500);
DBMS_OUTPUT.PUT_LINE('Customer name: ' + isnull(@customer_name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
CREATE FUNCTION get_customer_name(customer_id NUMBER) RETURN VARCHAR2 IS
customer_name VARCHAR2(50);
BEGIN
SELECT name INTO customer_name FROM customers WHERE id = customer_id;
RETURN customer_name;
END;
CREATE PROCEDURE update_customer_balance(customer_id NUMBER, amount NUMBER) IS
BEGIN
UPDATE customers SET balance = balance + amount WHERE id = customer_id;
END;
DECLARE
customer_name VARCHAR2(50);
BEGIN
customer_name := get_customer_name(12345);
update_customer_balance(12345, 500);
DBMS_OUTPUT.PUT_LINE('Customer name: ' || customer_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
IF OBJECT_ID('emp_salary_update', 'P') IS NOT NULL
DROP PROCEDURE emp_salary_update;
GO
CREATE PROCEDURE emp_salary_update(
@p_emp_id FLOAT,
@p_salary FLOAT,
@OUT p_status AS VARCHAR2
)
AS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_emp_id;
SELECT @p_status = 'SUCCESS'
;
END;
BEGIN
dbo.emp_salary_update(100, 12000, @status);
END;
|
CREATE OR REPLACE PROCEDURE emp_salary_update(
p_emp_id NUMBER,
p_salary NUMBER,
OUT p_status VARCHAR2
)
AS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_emp_id;
SELECT 'SUCCESS' INTO p_status
FROM dual;
END;
BEGIN
emp_salary_update(100, 12000, @status);
END;
|
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER)
AS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, salary), ''));
END;
|
CREATE OR REPLACE PROCEDURE get_employee_salary(emp_id IN NUMBER)
AS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(salary));
END;
|
DECLARE
@v_id FLOAT;
BEGIN
SET @v_id = 1;
SELECT @v_id = id FROM table_1 WHERE id = @v_id;
IF @v_id IS NULL BEGIN
RAISERROR('-20001 Record not found', 16, 1);
END
UPDATE table_1 SET name = 'new name' WHERE id = @v_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
DECLARE
v_id NUMBER;
BEGIN
v_id := 1;
SELECT id INTO v_id FROM table_1 WHERE id = v_id;
IF v_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Record not found');
END IF;
UPDATE table_1 SET name = 'new name' WHERE id = v_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL
DROP PROCEDURE sp_update_employee;
GO
CREATE PROCEDURE sp_update_employee(
@emp_id IN AS NUMBER,
salary IN NUMBER,
bonus IN NUMBER
) AS
BEGIN
UPDATE employees SET salary = salary + ISNULL(salary * bonus / 100, 0)
WHERE employee_id = emp_id;
EXCEPTION
WHEN OTHERS THEN
RAISERROR(
'-20000 Error updating salary for employee ' + isnull(emp_id, '')
, 16, 1);
END sp_update_employee;
IF OBJECT_ID('f_get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION f_get_employee_salary;
GO
CREATE FUNCTION f_get_employee_salary(@emp_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = emp_id
);
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END f_get_employee_salary;
BEGIN
DBMS_OUTPUT.PUT_LINE(dbo.f_get_employee_salary(1001));
dbo.sp_update_employee(1001, 10, 10);
DBMS_OUTPUT.PUT_LINE(dbo.f_get_employee_salary(1001));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE sp_update_employee(
emp_id IN NUMBER,
salary IN NUMBER,
bonus IN NUMBER
) AS
BEGIN
UPDATE employees SET salary = salary + NVL(salary * bonus / 100, 0)
WHERE employee_id = emp_id;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(
-20000,
'Error updating salary for employee ' || emp_id
);
END sp_update_employee;
CREATE OR REPLACE FUNCTION f_get_employee_salary(emp_id IN NUMBER)
RETURN NUMBER AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = emp_id
);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END f_get_employee_salary;
BEGIN
DBMS_OUTPUT.PUT_LINE(f_get_employee_salary(1001));
sp_update_employee(1001, 10, 10);
DBMS_OUTPUT.PUT_LINE(f_get_employee_salary(1001));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
CREATE PROCEDURE get_employee_details (
@emp_id IN AS NUMBER,
OUT emp_fname VARCHAR2,
OUT emp_lname VARCHAR2)
IS
BEGIN
SELECT @emp_fname = first_name, @emp_lname = last_name
FROM employees
WHERE employee_id = emp_id;
END;
DECLARE
@emp_id FLOAT := 10;
emp_fname dbo.VARCHAR2(20);
emp_lname dbo.VARCHAR2(20);
BEGIN
dbo.get_employee_details(@emp_id, emp_fname, emp_lname);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_fname, '') + ' ' + isnull(emp_lname, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@emp_id, '') + ' not found');
END
END;
|
CREATE PROCEDURE get_employee_details (
emp_id IN NUMBER,
OUT emp_fname VARCHAR2,
OUT emp_lname VARCHAR2)
IS
BEGIN
SELECT first_name, last_name
INTO emp_fname, emp_lname
FROM employees
WHERE employee_id = emp_id;
END;
DECLARE
emp_id NUMBER := 10;
emp_fname VARCHAR2(20);
emp_lname VARCHAR2(20);
BEGIN
get_employee_details(emp_id, emp_fname, emp_lname);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_fname || ' ' || emp_lname);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' || emp_id || ' not found');
END;
|
CREATE PROCEDURE Proc_InsertEmployee(
@e_id IN AS NUMBER,
e_name IN VARCHAR2,
e_salary IN NUMBER,
e_department IN VARCHAR2
) IS
BEGIN
INSERT INTO employees(employee_id, employee_name, salary, department)
VALUES (e_id, e_name, e_salary, e_department);
COMMIT;
END;
|
CREATE PROCEDURE Proc_InsertEmployee(
e_id IN NUMBER,
e_name IN VARCHAR2,
e_salary IN NUMBER,
e_department IN VARCHAR2
) IS
BEGIN
INSERT INTO employees(employee_id, employee_name, salary, department)
VALUES (e_id, e_name, e_salary, e_department);
COMMIT;
END;
|
CREATE TABLE Customers(Id FLOAT, Name VARCHAR(50), PRIMARY KEY(Id));
CREATE PROCEDURE AddCustomer(@p_Name IN AS VARCHAR2)
AS
BEGIN
INSERT INTO Customers(Name) VALUES (p_Name);
COMMIT;
END;
DECLARE @v_Name VARCHAR(50);
BEGIN
SET @v_Name = 'John Smith';
dbo.AddCustomer(@v_Name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
SELECT * FROM Customers;
|
CREATE TABLE Customers(Id NUMBER, Name VARCHAR2(50), PRIMARY KEY(Id));
CREATE PROCEDURE AddCustomer(p_Name IN VARCHAR2)
AS
BEGIN
INSERT INTO Customers(Name) VALUES (p_Name);
COMMIT;
END;
DECLARE v_Name VARCHAR2(50);
BEGIN
v_Name := 'John Smith';
AddCustomer(v_Name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
SELECT * FROM Customers;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@p1 IN AS OUT VARCHAR2, p2 IN INTEGER) IS
BEGIN
set @p1 = UPPER(p1);
IF p2 < 0 BEGIN
RAISERROR('-20001 p2 must be non-negative.', 16, 1);
END
*** sing...
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END my_proc;
|
CREATE OR REPLACE PROCEDURE my_proc (p1 IN OUT VARCHAR2, p2 IN INTEGER) IS
BEGIN
p1 := UPPER(p1);
IF p2 < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'p2 must be non-negative.');
END IF;
-- Some other processing...
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END my_proc;
|
DECLARE
cursor_emp CURSOR FOR SELECT EMPNO,ENAME FROM EMP;
v_empno EMP.EMPNO%TYPE;
v_ename EMP.ENAME%TYPE;
BEGIN
OPEN cursor_emp;
WHILE 1=1 BEGIN
FETCH cursor_emp INTO @v_empno, @v_ename;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(v_empno, '') + ' : ' + isnull(v_ename, ''));
END;
CLOSE cursor_emp;
END;
/
IF OBJECT_ID('insert_emp', 'P') IS NOT NULL
DROP PROCEDURE insert_emp;
GO
CREATE PROCEDURE insert_emp(
@p_empno IN AS EMP.EMPNO%TYPE,
p_ename IN EMP.ENAME%TYPE)
IS
BEGIN
INSERT INTO EMP (EMPNO, ENAME) VALUES (p_empno, p_ename);
END;
/
|
DECLARE
cursor_emp CURSOR FOR SELECT EMPNO,ENAME FROM EMP;
v_empno EMP.EMPNO%TYPE;
v_ename EMP.ENAME%TYPE;
BEGIN
OPEN cursor_emp;
LOOP
FETCH cursor_emp INTO v_empno, v_ename;
EXIT WHEN cursor_emp%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_empno || ' : ' || v_ename);
END LOOP;
CLOSE cursor_emp;
END;
/
CREATE OR REPLACE PROCEDURE insert_emp(
p_empno IN EMP.EMPNO%TYPE,
p_ename IN EMP.ENAME%TYPE)
IS
BEGIN
INSERT INTO EMP (EMPNO, ENAME) VALUES (p_empno, p_ename);
END;
/
|
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
BEGIN
INSERT INTO table_name VALUES (i);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
NULL; *** value errors
END;
END;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | BEGIN
FOR i IN 1 .. 10 LOOP
BEGIN
INSERT INTO table_name VALUES (i);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
NULL; -- Ignore duplicate value errors
END;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
CREATE FUNCTION get_customer_info(@cust_id FLOAT) RETURNS SYS_REFCURSOR AS
BEGIN
SELECT * FROM customers WHERE customer_id = @cust_id;
RETURN cust_rc;
END;
GO
DECLARE
@TYPE refcur_type IS REF CURSOR;
cust_info refcur_type;
row_count NUMBER;
BEGIN
SET @cust_info = dbo.get_customer_info(100);
WHILE 1=1 BEGIN
FETCH cust_info INTO @customer_id, @customer_name, @customer_address;
IF @@FETCH_STATUS <> 0 BREAK;
SET @row_count = row_count + 1;
END;
DBMS_OUTPUT.PUT_LINE('Total rows fetched: ' + isnull(row_count, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the specified customer ID.');
RETURN;
END
END;
|
CREATE FUNCTION get_customer_info(cust_id NUMBER) RETURN SYS_REFCURSOR IS
BEGIN
OPEN cust_rc FOR
SELECT * FROM customers WHERE customer_id = cust_id;
RETURN cust_rc;
END;
/
DECLARE
TYPE refcur_type IS REF CURSOR;
cust_info refcur_type;
row_count NUMBER;
BEGIN
cust_info := get_customer_info(100);
LOOP
FETCH cust_info INTO customer_id, customer_name, customer_address;
EXIT WHEN cust_info%NOTFOUND;
row_count := row_count + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total rows fetched: ' || row_count);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the specified customer ID.');
RETURN;
END;
|
CREATE PROCEDURE proc_name (
@param1 IN AS NUMBER,
param2 OUT NUMBER
) AS
BEGIN
SELECT @param2 = COUNT(*) FROM table_name WHERE condition;
IF param2 > 0 BEGIN
RAISERROR('-20001 No records found', 16, 1);
END
END;
|
CREATE PROCEDURE proc_name (
param1 IN NUMBER,
param2 OUT NUMBER
) AS
BEGIN
SELECT COUNT(*) INTO param2 FROM table_name WHERE condition;
IF param2 > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No records found');
END IF;
END;
|
IF OBJECT_ID('Example_Procedure', 'P') IS NOT NULL
DROP PROCEDURE Example_Procedure;
GO
CREATE PROCEDURE Example_Procedure (
@p_input_parameter IN AS VARCHAR2,
p_output_parameter OUT VARCHAR2
) IS
BEGIN
SELECT @p_output_parameter = COUNT(*)
FROM table_name
WHERE column_name = p_input_parameter;
END Example_Procedure;
BEGIN
DECLARE
@v_input_parameter VARCHAR(100);
v_output_parameter dbo.VARCHAR2(100);
BEGIN
SET @v_input_parameter = 'John Doe';
dbo.Example_Procedure(@v_input_parameter, v_output_parameter);
DBMS_OUTPUT.PUT_LINE(v_output_parameter);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE Example_Procedure (
p_input_parameter IN VARCHAR2,
p_output_parameter OUT VARCHAR2
) IS
BEGIN
SELECT COUNT(*)
INTO p_output_parameter
FROM table_name
WHERE column_name = p_input_parameter;
END Example_Procedure;
BEGIN
DECLARE
v_input_parameter VARCHAR2(100);
v_output_parameter VARCHAR2(100);
BEGIN
v_input_parameter := 'John Doe';
Example_Procedure(v_input_parameter, v_output_parameter);
DBMS_OUTPUT.PUT_LINE(v_output_parameter);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
COMMIT;
END;
|
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
CREATE FUNCTION get_department_manager(@dept_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
RETURN (SELECT manager_id FROM departments WHERE department_id = dept_id);
END;
DECLARE
@emp_name VARCHAR(20);
BEGIN
SELECT @emp_name = first_name, last_name FROM employees WHERE employee_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;
|
PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
FUNCTION get_department_manager(dept_id IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN (SELECT manager_id FROM departments WHERE department_id = dept_id);
END;
DECLARE
emp_name VARCHAR2(20);
BEGIN
SELECT first_name, last_name INTO emp_name FROM employees WHERE employee_id = 1;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = ename FROM emp WHERE job = 'MANAGER';
IF @v_empno IS NULL BEGIN
RAISERROR('-20101 No manager found', 16, 1);
END
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT ename INTO v_empno FROM emp WHERE job = 'MANAGER';
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20101, 'No manager found');
END IF;
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
END;
|
CREATE TABLE t1 (id FLOAT, name VARCHAR(255))
INSERT INTO t1 VALUES (1, 'John Doe')
CREATE PROCEDURE p1 (@p_id IN AS NUMBER, p_name OUT VARCHAR2)
AS
BEGIN
SELECT @p_name = name FROM t1 WHERE id = p_id;
END;
BEGIN
DECLARE @v_name VARCHAR(255);
DECLARE @v_err FLOAT;
BEGIN
dbo.p1(1, @v_name);
DBMS_OUTPUT.PUT_LINE(@v_name);
EXCEPTION
WHEN OTHERS THEN
SET @v_err = @@ERROR;
DBMS_OUTPUT.PUT_LINE(@v_err);
END;
END;
DROP PROCEDURE p1
DROP TABLE t1
|
CREATE TABLE t1 (id NUMBER, name VARCHAR2(255))
INSERT INTO t1 VALUES (1, 'John Doe')
CREATE PROCEDURE p1 (p_id IN NUMBER, p_name OUT VARCHAR2)
AS
BEGIN
SELECT name INTO p_name FROM t1 WHERE id = p_id;
END;
BEGIN
DECLARE v_name VARCHAR2(255);
DECLARE v_err NUMBER;
BEGIN
p1(1, v_name);
DBMS_OUTPUT.PUT_LINE(v_name);
EXCEPTION
WHEN OTHERS THEN
v_err := SQLCODE;
DBMS_OUTPUT.PUT_LINE(v_err);
END;
END;
DROP PROCEDURE p1
DROP TABLE t1
|
DECLARE
@v_count FLOAT;
BEGIN
SELECT @v_count = COUNT(*) FROM employees;
IF @v_count > 10
BEGIN
RAISERROR('-20001 Employee count is greater than 10', 16, 1);
END
UPDATE employees SET salary = salary * 1.1;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM employees;
IF v_count > 10
THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee count is greater than 10');
END IF;
UPDATE employees SET salary = salary * 1.1;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@emp_id FLOAT;
emp_name dbo.VARCHAR2(30);
salary NUMBER;
BEGIN
*** information
SELECT @emp_name = emp_name, @salary = salary FROM employees WHERE @emp_id = 10;
*** esult
IF emp_name IS NULL BEGIN
RAISERROR('-20100 Employee not found', 16, 1);
END
*** alary
SET @salary = salary * 1.10;
UPDATE employees SET salary = salary WHERE @emp_id = 10;
-- Lo... ***
INSERT INTO audit_log (action, emp_id, emp_name, salary) VALUES ('UPDATE', 10, emp_name, salary);
-- Co... ***
COMMIT;
*** alary
DBMS_OUTPUT.PUT_LINE('Updated salary: ' + isnull(salary, ''));
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
emp_id NUMBER;
emp_name VARCHAR2(30);
salary NUMBER;
BEGIN
-- Retrieve employee information
SELECT emp_name, salary INTO emp_name, salary FROM employees WHERE emp_id = 10;
-- Check for empty result
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20100, 'Employee not found');
END IF;
-- Update employee salary
salary := salary * 1.10;
UPDATE employees SET salary = salary WHERE emp_id = 10;
-- Log the update
INSERT INTO audit_log (action, emp_id, emp_name, salary) VALUES ('UPDATE', 10, emp_name, salary);
-- Commit changes
COMMIT;
-- Display updated salary
DBMS_OUTPUT.PUT_LINE('Updated salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
SELECT @v_empno = empno, @v_ename = ename
FROM emp
WHERE empno = 7369;
IF @v_empno IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee number: ' + isnull(@v_empno, ''));
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_ename, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
SELECT empno, ename
INTO v_empno, v_ename
FROM emp
WHERE empno = 7369;
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee number: ' || v_empno);
DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_ename);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
|
IF OBJECT_ID('emp_details', 'P') IS NOT NULL
DROP PROCEDURE emp_details;
GO
CREATE PROCEDURE emp_details (@empno IN AS NUMBER)
AS
BEGIN
SELECT * FROM employees
WHERE employee_id = empno;
END;
/
IF OBJECT_ID('emp_salary', 'FN') IS NOT NULL
DROP FUNCTION emp_salary;
GO
CREATE FUNCTION emp_salary (@empno IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = empno
);
END;
/
BEGIN
DECLARE rec CURSOR FOR
SELECT * FROM employees;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_last_name, '') + ', ' + isnull(@rec_first_name, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/
DECLARE
@emp_count FLOAT;
BEGIN
SELECT @emp_count = COUNT(*)
FROM employees;
IF @emp_count > 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Too many employees!');
END
ELSE IF @emp_count = 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found!');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee count: ' + isnull(@emp_count, ''));
END
END;
/
DECLARE
@err_msg VARCHAR(200);
BEGIN
BEGIN
SELECT * FROM non_existing_table;
IF @@FETCH_STATUS <> 0 BEGIN
SET @err_msg = 'Table not found';
END
WHEN OTHERS THEN
SET @err_msg = 'An error occurred: ' + ISNULL(SQLERRM, '');
END;
DBMS_OUTPUT.PUT_LINE(@err_msg);
END;
/
|
CREATE OR REPLACE PROCEDURE emp_details (empno IN NUMBER)
AS
BEGIN
SELECT * FROM employees
WHERE employee_id = empno;
END;
/
CREATE OR REPLACE FUNCTION emp_salary (empno IN NUMBER)
RETURN NUMBER
AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = empno
);
END;
/
BEGIN
FOR rec IN (
SELECT * FROM employees
) LOOP
DBMS_OUTPUT.PUT_LINE(rec.last_name || ', ' || rec.first_name);
END LOOP;
END;
/
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO emp_count
FROM employees;
IF emp_count > 10 THEN
DBMS_OUTPUT.PUT_LINE('Too many employees!');
ELSIF emp_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('No employees found!');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee count: ' || emp_count);
END IF;
END;
/
DECLARE
err_msg VARCHAR(200);
BEGIN
BEGIN
SELECT * FROM non_existing_table;
EXCEPTION
WHEN NO_DATA_FOUND THEN
err_msg := 'Table not found';
WHEN OTHERS THEN
err_msg := 'An error occurred: ' || SQLERRM;
END;
DBMS_OUTPUT.PUT_LINE(err_msg);
END;
/
|
CREATE PROCEDURE proc_example(@a IN AS NUMBER, b IN VARCHAR2)
AS
v_c CONSTANT dbo.VARCHAR2(20) := 'Hello, world!';
BEGIN
DBMS_OUTPUT.put_line('Input parameters: ' + isnull(a, '') + ', ' + isnull(b, ''));
DBMS_OUTPUT.put_line(v_c);
END proc_example;
|
CREATE PROCEDURE proc_example(a IN NUMBER, b IN VARCHAR2)
AS
v_c CONSTANT VARCHAR2(20) := 'Hello, world!';
BEGIN
DBMS_OUTPUT.put_line('Input parameters: ' || a || ', ' || b);
DBMS_OUTPUT.put_line(v_c);
END proc_example;
|
BEGIN
-- De... ***
DECLARE @v_emp_id FLOAT;
DECLARE @v_emp_name VARCHAR(100);
DECLARE @v_salary FLOAT;
BEGIN
*** ils
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 100;
*** details
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@v_salary, ''));
END;
|
BEGIN
-- Declare variables
DECLARE v_emp_id NUMBER;
DECLARE v_emp_name VARCHAR2(100);
DECLARE v_salary NUMBER;
BEGIN
-- Get employee details
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 100;
-- Display employee details
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END;
|
sql
DECLARE
@v_result FLOAT;
BEGIN
SELECT @v_result = COUNT(*) FROM employee;
IF @v_result > 0 BEGIN
RAISERROR('-20001 Table employee not empty', 16, 1);
END
TRUNCATE TABLE employee;
END;
/
| sql
DECLARE
v_result NUMBER;
BEGIN
SELECT COUNT(*) INTO v_result FROM employee;
IF v_result > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Table employee not empty');
END IF;
TRUNCATE TABLE employee;
END;
/
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(30);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = &emp_id;
IF v_salary > 5000 BEGIN
SET @v_bonus = v_salary * 0.1;
END
ELSE IF v_salary > 3000 BEGIN
SET @v_bonus = v_salary * 0.05;
END
ELSE BEGIN
SET @v_bonus = 0;
END
UPDATE employees SET bonus = v_bonus WHERE emp_id = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' with ID ' + isnull(@v_emp_id, '') + ' has received a bonus of ' + isnull(v_bonus, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(30);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT emp_id, emp_name, salary INTO v_emp_id, v_emp_name, v_salary FROM employees WHERE emp_id = &emp_id;
IF v_salary > 5000 THEN
v_bonus := v_salary * 0.1;
ELSIF v_salary > 3000 THEN
v_bonus := v_salary * 0.05;
ELSE
v_bonus := 0;
END IF;
UPDATE employees SET bonus = v_bonus WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' with ID ' || v_emp_id || ' has received a bonus of ' || v_bonus);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
CREATE TABLE USERS ( ID FLOAT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, EMAIL VARCHAR(100) UNIQUE, PASSWORD VARCHAR(50) NOT NULL );
INSERT INTO USERS ( ID, NAME, EMAIL, PASSWORD ) VALUES ( 1, 'John Doe', 'johndoe@example.com', 'password' );
IF OBJECT_ID('GET_USER', 'P') IS NOT NULL
DROP PROCEDURE GET_USER;
GO
CREATE PROCEDURE GET_USER ( @P_ID IN AS NUMBER, OUT P_NAME VARCHAR2, OUT P_EMAIL VARCHAR2 ) AS BEGIN SELECT @P_NAME = NAME, @P_EMAIL = EMAIL FROM USERS WHERE ID = P_ID; END;
DECLARE @V_ID FLOAT := 1;
DECLARE @V_NAME VARCHAR(50);
DECLARE @V_EMAIL VARCHAR(100);
BEGIN
dbo.GET_USER( @V_ID, @V_NAME, @V_EMAIL );
DBMS_OUTPUT.PUT_LINE( 'Name: ' + ISNULL(@V_NAME, '') );
DBMS_OUTPUT.PUT_LINE( 'Email: ' + ISNULL(@V_EMAIL, '') );
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE( 'No user with ID ' + ISNULL(@V_ID, '') + ' found.' );
END
END; |
CREATE TABLE USERS ( ID NUMBER PRIMARY KEY, NAME VARCHAR2(50) NOT NULL, EMAIL VARCHAR2(100) UNIQUE, PASSWORD VARCHAR2(50) NOT NULL );
INSERT INTO USERS ( ID, NAME, EMAIL, PASSWORD ) VALUES ( 1, 'John Doe', 'johndoe@example.com', 'password' );
CREATE OR REPLACE PROCEDURE GET_USER ( P_ID IN NUMBER, OUT P_NAME VARCHAR2, OUT P_EMAIL VARCHAR2 ) AS BEGIN SELECT NAME, EMAIL INTO P_NAME, P_EMAIL FROM USERS WHERE ID = P_ID; END;
DECLARE V_ID NUMBER := 1;
DECLARE V_NAME VARCHAR2(50);
DECLARE V_EMAIL VARCHAR2(100);
BEGIN
GET_USER( V_ID, V_NAME, V_EMAIL );
DBMS_OUTPUT.PUT_LINE( 'Name: ' || V_NAME );
DBMS_OUTPUT.PUT_LINE( 'Email: ' || V_EMAIL );
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE( 'No user with ID ' || V_ID || ' found.' );
END;
|
CREATE TABLE employees (
employee_id FLOAT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
salary FLOAT
);
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (1, 'John', 'Doe', 10000);
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (2, 'Jane', 'Smith', 20000);
CREATE PROCEDURE get_employee_by_id (
@p_employee_id IN AS NUMBER,
OUT p_first_name VARCHAR2,
OUT p_last_name VARCHAR2,
OUT p_salary NUMBER
) AS
BEGIN
SELECT @p_first_name = first_name, @p_last_name = last_name, @p_salary = salary FROM employees WHERE employee_id = p_employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_first_name = NULL;
set @p_last_name = NULL;
set @p_salary = NULL;
END
END;
DECLARE
@first_name VARCHAR(50);
last_name dbo.VARCHAR2(50);
salary NUMBER;
BEGIN
dbo.get_employee_by_id(1, @first_name, last_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(@first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
salary NUMBER
);
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (1, 'John', 'Doe', 10000);
INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (2, 'Jane', 'Smith', 20000);
CREATE PROCEDURE get_employee_by_id (
p_employee_id IN NUMBER,
OUT p_first_name VARCHAR2,
OUT p_last_name VARCHAR2,
OUT p_salary NUMBER
) AS
BEGIN
SELECT first_name, last_name, salary INTO p_first_name, p_last_name, p_salary FROM employees WHERE employee_id = p_employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_first_name := NULL;
p_last_name := NULL;
p_salary := NULL;
END;
DECLARE
first_name VARCHAR2(50);
last_name VARCHAR2(50);
salary NUMBER;
BEGIN
get_employee_by_id(1, first_name, last_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
v_bonus NUMBER;
cursor c1 is
SELECT empno, salary
FROM emp
WHERE deptno = 20;
BEGIN
DECLARE i CURSOR FOR c1 LOOP
SET @v_empno = c1.empno;
SET @v_salary = c1.salary;
SET @v_bonus = v_salary * 0.10;
UPDATE emp
SET bonus = v_bonus
WHERE empno = @v_empno;
FETCH i INTO;
END;
CLOSE i;
DEALLOCATE i;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for department 20');
END
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
cursor c1 is
SELECT empno, salary
FROM emp
WHERE deptno = 20;
BEGIN
FOR i IN c1 LOOP
v_empno := c1.empno;
v_salary := c1.salary;
v_bonus := v_salary * 0.10;
UPDATE emp
SET bonus = v_bonus
WHERE empno = v_empno;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for department 20');
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@employee_id IN AS NUMBER,
salary_increment IN NUMBER
) IS
BEGIN
UPDATE employees
SET salary = salary + salary_increment
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1);
END
END;
/
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(
@employee_id IN NUMBER
DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
emp_name dbo.VARCHAR2(255);
BEGIN
SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = employee_id;
RETURN emp_name;
IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 Employee not found', 16, 1);
END
END;
/
BEGIN
DECLARE
@emp_id FLOAT;
emp_name dbo.VARCHAR2(255);
salary_incr NUMBER;
BEGIN
SELECT @emp_id = employee_id FROM employees WHERE first_name = 'John';
SET @emp_name = dbo.get_employee_name(@emp_id);
SET @salary_incr = 5000;
DBMS_OUTPUT.PUT_LINE('Updating salary for ' + isnull(emp_name, '') + ' by ' + isnull(salary_incr, ''));
dbo.update_employee_salary(@emp_id, salary_incr);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
employee_id IN NUMBER,
salary_increment IN NUMBER
) IS
BEGIN
UPDATE employees
SET salary = salary + salary_increment
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
/
CREATE OR REPLACE FUNCTION get_employee_name(
employee_id IN NUMBER
) RETURN VARCHAR2 IS
emp_name VARCHAR2(255);
BEGIN
SELECT first_name || ' ' || last_name INTO emp_name
FROM employees
WHERE employee_id = employee_id;
RETURN emp_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
/
BEGIN
DECLARE
emp_id NUMBER;
emp_name VARCHAR2(255);
salary_incr NUMBER;
BEGIN
SELECT employee_id INTO emp_id FROM employees WHERE first_name = 'John';
emp_name := get_employee_name(emp_id);
salary_incr := 5000;
DBMS_OUTPUT.PUT_LINE('Updating salary for ' || emp_name || ' by ' || salary_incr);
update_employee_salary(emp_id, salary_incr);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
/
|
IF OBJECT_ID('p_update_employee', 'P') IS NOT NULL
DROP PROCEDURE p_update_employee;
GO
CREATE PROCEDURE p_update_employee(
@p_employee_id IN AS NUMBER,
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2
)
AS
BEGIN
*** ee table
UPDATE employees SET first_name = p_first_name, last_name = p_last_name
WHERE employee_id = p_employee_id;
*** n if the employee does not exist
IF SQL%NOTFOUND BEGIN
RAISE NO_DATA_FOUND;
END
END;
|
CREATE OR REPLACE PROCEDURE p_update_employee(
p_employee_id IN NUMBER,
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2
)
AS
BEGIN
-- Update the employee table
UPDATE employees SET first_name = p_first_name, last_name = p_last_name
WHERE employee_id = p_employee_id;
-- Raise an exception if the employee does not exist
IF SQL%NOTFOUND THEN
RAISE NO_DATA_FOUND;
END IF;
END;
|
*** with IN OUT Parameters
IF OBJECT_ID('UpdateEmployee', 'P') IS NOT NULL
DROP PROCEDURE UpdateEmployee;
GO
CREATE PROCEDURE UpdateEmployee(
@IN emp_id AS NUMBER,
INOUT emp_name VARCHAR2,
OUT emp_salary NUMBER)
AS
BEGIN
UPDATE employees SET emp_name = emp_name, emp_salary = emp_salary
WHERE emp_id = emp_id;
SELECT @emp_salary = emp_salary FROM employees WHERE emp_id = emp_id;
END;
*** g a Cursor
IF OBJECT_ID('GetEmployees', 'FN') IS NOT NULL
DROP FUNCTION GetEmployees;
GO
CREATE FUNCTION GetEmployees() RETURNS SYS_REFCURSOR
AS
BEGIN
SELECT emp_id, emp_name, emp_salary
FROM employees;
RETURN c_employees;
END;
*** truct (IF-ELSE)
BEGIN
IF emp_id IS NOT NULL BEGIN
EXECUTE sp_executesql @'SELECT emp_name FROM employees WHERE emp_id = :id', N'@emp_name output, @emp_id',
@emp_name output
@emp_id;
END
ELSE BEGIN
set @emp_name = 'Unknown';
END
END;
*** th EXCEPTION Block
BEGIN
BEGIN
*** se an exception
...
EXCEPTION
WHEN OTHERS THEN
*** g code
...
END;
END;
|
-- Stored Procedure with IN OUT Parameters
CREATE OR REPLACE PROCEDURE UpdateEmployee(
IN emp_id NUMBER,
INOUT emp_name VARCHAR2,
OUT emp_salary NUMBER)
AS
BEGIN
UPDATE employees SET emp_name = emp_name, emp_salary = emp_salary
WHERE emp_id = emp_id;
SELECT emp_salary INTO emp_salary FROM employees WHERE emp_id = emp_id;
END;
-- Function Returning a Cursor
CREATE OR REPLACE FUNCTION GetEmployees() RETURN SYS_REFCURSOR
AS
BEGIN
OPEN c_employees FOR
SELECT emp_id, emp_name, emp_salary
FROM employees;
RETURN c_employees;
END;
-- Control Flow Construct (IF-ELSE)
BEGIN
IF emp_id IS NOT NULL THEN
EXECUTE IMMEDIATE 'SELECT emp_name FROM employees WHERE emp_id = :id'
INTO emp_name
USING emp_id;
ELSE
emp_name := 'Unknown';
END IF;
END;
-- Error Handling with EXCEPTION Block
BEGIN
BEGIN
-- Code that may raise an exception
...
EXCEPTION
WHEN OTHERS THEN
-- Exception handling code
...
END;
END;
|
CREATE PROCEDURE GetEmployeeDetails(@empId IN AS NUMBER) IS
v_emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_emp_name = emp_name FROM employees WHERE emp_id = empId;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + ISNULL(empId, ''));
END
END; | CREATE PROCEDURE GetEmployeeDetails(empId IN NUMBER) IS
v_emp_name VARCHAR2(50);
BEGIN
SELECT emp_name INTO v_emp_name FROM employees WHERE emp_id = empId;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || empId);
END; |
CREATE PROCEDURE prc_example(
@p_id IN AS NUMBER,
p_name IN VARCHAR2,
OUT p_age OUT NUMBER
)
IS
l_age NUMBER;
BEGIN
SELECT @l_age = age FROM PERSON WHERE id = p_id;
set @p_age = l_age;
IF p_age IS NULL BEGIN
RAISERROR('-20001 No person found with ID: ' + isnull(p_id, ''), 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No data found', 16, 1);
END
END;
CREATE FUNCTION func_example(@p_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
l_name dbo.VARCHAR2(100);
BEGIN
SELECT @l_name = name FROM PERSON WHERE id = p_id;
RETURN l_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@l_id FLOAT := 10;
l_name dbo.VARCHAR2(100);
BEGIN
SELECT @l_name = name FROM PERSON WHERE id = @l_id;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(l_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No person found with ID: ' + isnull(@l_id, ''));
END
END;
|
CREATE PROCEDURE prc_example(
p_id IN NUMBER,
p_name IN VARCHAR2,
OUT p_age OUT NUMBER
)
IS
l_age NUMBER;
BEGIN
SELECT age INTO l_age FROM PERSON WHERE id = p_id;
p_age := l_age;
IF p_age IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No person found with ID: ' || p_id);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No data found');
END;
CREATE FUNCTION func_example(p_id IN NUMBER) RETURN VARCHAR2
IS
l_name VARCHAR2(100);
BEGIN
SELECT name INTO l_name FROM PERSON WHERE id = p_id;
RETURN l_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
l_id NUMBER := 10;
l_name VARCHAR2(100);
BEGIN
SELECT name INTO l_name FROM PERSON WHERE id = l_id;
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No person found with ID: ' || l_id);
END;
|
DECLARE
@v_emp_id FLOAT := 100;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = @v_emp_id;
IF v_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
END
ELSE IF v_salary > 5000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is between 5000 and 10000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than 5000');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_emp_id NUMBER := 100;
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id;
IF v_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
ELSIF v_salary > 5000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is between 5000 and 10000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than 5000');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
BEGIN
DECLARE r CURSOR FOR
SELECT * FROM employees
WHERE lastName = 'Smith';
OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE employeeId = @r_employeeId;
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
BEGIN
*** duplicate data
INSERT INTO employees (employeeId, lastName, salary) VALUES (10, 'Jones', 30000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
*** value error
NULL;
END;
END;
|
BEGIN
FOR r IN (
SELECT * FROM employees
WHERE lastName = 'Smith'
) LOOP
UPDATE employees SET salary = salary * 1.10
WHERE employeeId = r.employeeId;
END LOOP;
BEGIN
-- Attempt to insert duplicate data
INSERT INTO employees (employeeId, lastName, salary) VALUES (10, 'Jones', 30000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
-- Handle duplicate value error
NULL;
END;
END;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
v_comm NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary, @v_comm = comm
FROM emp
WHERE empno = 7839;
IF v_comm IS NULL BEGIN
UPDATE emp
SET comm = 0
WHERE empno = 7839;
END
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
v_comm NUMBER;
BEGIN
SELECT empno, salary, comm
INTO v_empno, v_salary, v_comm
FROM emp
WHERE empno = 7839;
IF v_comm IS NULL THEN
UPDATE emp
SET comm = 0
WHERE empno = 7839;
END IF;
END;
|
IF OBJECT_ID('emp_salary_update', 'P') IS NOT NULL
DROP PROCEDURE emp_salary_update;
GO
CREATE PROCEDURE emp_salary_update
(
@emp_id IN AS NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
*** for the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
*** s to the database
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE emp_salary_update
(
emp_id IN NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
-- Update the salary for the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
-- Commit the changes to the database
COMMIT;
END;
|
BEGIN
*** iables
v_empno NUMBER;
v_salary NUMBER;
v_comm NUMBER;
v_found SET @BOOLEAN = FALSE;
*** fetch employee data
DECLARE rec CURSOR FOR
SELECT empno, salary, comm
FROM emp
WHERE ename = 'KING';
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** number, salary, and commission
set @v_empno = @rec_empno;
set @v_salary = @rec_salary;
set @v_comm = @rec_comm;
*** ission is greater than 0
IF v_comm > 0 BEGIN
set @v_found = TRUE;
*** a 10% raise
UPDATE emp
SET salary = salary * 1.10
WHERE empno = v_empno;
-- Co... ***
COMMIT;
*** message
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_empno, '') + ' given a 10% raise.');
END
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
*** oyee was found
IF NOT v_found BEGIN
*** essage
DBMS_OUTPUT.PUT_LINE('Employee with name KING not found.');
END
END;
|
BEGIN
-- Declare local variables
v_empno NUMBER;
v_salary NUMBER;
v_comm NUMBER;
v_found BOOLEAN := FALSE;
-- Open a cursor to fetch employee data
FOR rec IN (
SELECT empno, salary, comm
FROM emp
WHERE ename = 'KING'
) LOOP
-- Get the employee number, salary, and commission
v_empno := rec.empno;
v_salary := rec.salary;
v_comm := rec.comm;
-- Check if the commission is greater than 0
IF v_comm > 0 THEN
v_found := TRUE;
-- Give the employee a 10% raise
UPDATE emp
SET salary = salary * 1.10
WHERE empno = v_empno;
-- Commit the changes
COMMIT;
-- Output a success message
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' given a 10% raise.');
END IF;
END LOOP;
-- Check if the employee was found
IF NOT v_found THEN
-- Output an error message
DBMS_OUTPUT.PUT_LINE('Employee with name KING not found.');
END IF;
END;
|
BEGIN
DECLARE
@emp_id INT;
salary ROW_NUMBER(10,2);
BEGIN
SELECT @emp_id = employee_id, @salary = salary
FROM employees
WHERE last_name = 'Scott';
dbms_output.put_line('Employee ' + isnull(@emp_id, '') + ' earns ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No employee found with last name ''Scott''');
END
END;
END;
|
BEGIN
DECLARE
emp_id NUMBER(6);
salary NUMBER(10,2);
BEGIN
SELECT employee_id, salary
INTO emp_id, salary
FROM employees
WHERE last_name = 'Scott';
dbms_output.put_line('Employee ' || emp_id || ' earns ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employee found with last name ''Scott''');
END;
END;
|
CREATE PROCEDURE my_proc(@p1 IN AS VARCHAR2, p2 IN NUMBER) AS
v1 dbo.VARCHAR2(100);
v2 NUMBER;
BEGIN
SELECT @v1 = name FROM employees WHERE emp_id = p1;
SELECT @v2 = salary FROM employees WHERE emp_id = p2;
UPDATE employees SET salary = v2 WHERE emp_id = p1;
UPDATE employees SET salary = v1 WHERE emp_id = p2;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE my_proc(p1 IN VARCHAR2, p2 IN NUMBER) AS
v1 VARCHAR2(100);
v2 NUMBER;
BEGIN
SELECT name INTO v1 FROM employees WHERE emp_id = p1;
SELECT salary INTO v2 FROM employees WHERE emp_id = p2;
UPDATE employees SET salary = v2 WHERE emp_id = p1;
UPDATE employees SET salary = v1 WHERE emp_id = p2;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@i FLOAT;
BEGIN
SELECT @i = COUNT(*) FROM orders;
IF @i > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Orders table is not empty.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Orders table is empty.');
END
END; | DECLARE
i NUMBER;
BEGIN
SELECT COUNT(*) INTO i FROM orders;
IF i > 0 THEN
DBMS_OUTPUT.PUT_LINE('Orders table is not empty.');
ELSE
DBMS_OUTPUT.PUT_LINE('Orders table is empty.');
END IF;
END; |
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 10;
IF v_salary < 1000 BEGIN
RAISERROR('-20001 Salary too low', 16, 1);
END
UPDATE emp SET salary = v_salary + 100 WHERE empno = @v_empno;
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 10;
IF v_salary < 1000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too low');
END IF;
UPDATE emp SET salary = v_salary + 100 WHERE empno = v_empno;
END;
|
CREATE PROCEDURE create_emp(@p_emp_id IN AS NUMBER,
p_ename IN VARCHAR2,
p_job IN VARCHAR2,
p_mgr IN NUMBER,
p_hiredate IN DATE,
p_sal IN NUMBER,
p_comm IN NUMBER,
p_dept_id IN NUMBER)
IS
BEGIN
INSERT INTO emp (empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno)
VALUES (p_emp_id,
p_ename,
p_job,
p_mgr,
p_hiredate,
p_sal,
p_comm,
p_dept_id);
END;
*** n manager name given employee id
CREATE FUNCTION get_manager_name(@p_emp_id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
v_mgr_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_mgr_name = mgr,
ename
FROM emp
WHERE empno = p_emp_id;
RETURN v_mgr_name;
END;
*** rol flow construct - a loop
DECLARE
BEGIN
DECLARE emp_rec CURSOR FOR SELECT *
FROM emp;
OPEN emp_rec;
FETCH emp_rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(@emp_rec_ename);
FETCH emp_rec INTO;
END;
CLOSE emp_rec;
DEALLOCATE emp_rec;
END;
*** or handling scenario
BEGIN
*** a duplicate employee record
INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
VALUES (100,
'John Doe',
'Manager',
7839,
GETDATE(),
7500,
NULL,
20);
*** that may occur
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate employee record');
END;
|
PROCEDURE create_emp(p_emp_id IN NUMBER,
p_ename IN VARCHAR2,
p_job IN VARCHAR2,
p_mgr IN NUMBER,
p_hiredate IN DATE,
p_sal IN NUMBER,
p_comm IN NUMBER,
p_dept_id IN NUMBER)
IS
BEGIN
INSERT INTO emp (empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno)
VALUES (p_emp_id,
p_ename,
p_job,
p_mgr,
p_hiredate,
p_sal,
p_comm,
p_dept_id);
END;
-- Function to return manager name given employee id
FUNCTION get_manager_name(p_emp_id IN NUMBER)
RETURN VARCHAR2
IS
v_mgr_name VARCHAR2(100);
BEGIN
SELECT mgr,
ename
INTO v_mgr_name
FROM emp
WHERE empno = p_emp_id;
RETURN v_mgr_name;
END;
-- Example of a control flow construct - a loop
DECLARE
emp_rec emp%ROWTYPE;
BEGIN
FOR emp_rec IN (SELECT *
FROM emp)
LOOP
DBMS_OUTPUT.PUT_LINE(emp_rec.ename);
END LOOP;
END;
-- Example of an error handling scenario
BEGIN
-- Attempt to insert a duplicate employee record
INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
VALUES (100,
'John Doe',
'Manager',
7839,
SYSDATE,
7500,
NULL,
20);
-- Handle any errors that may occur
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate employee record');
END;
|
BEGIN
BEGIN
/* Some code here */
EXCEPTION WHEN error_type THEN
/* *** tuation */
/* *** displaying error message */
END;
IF parameter_value = value BEGIN
/* *** ed if parameter_value is equal to value */
END
ELSE IF parameter_value = value2 BEGIN
/* *** ed if parameter_value is equal to value2 */
END
ELSE BEGIN
/* *** ed if parameter_value is not equal to any of the values */
END
WHILE NOT done BEGIN
/* *** ed within the loop */
IF condition BEGIN
set @done = TRUE;
END
END;
RETURN result;
END; | BEGIN
BEGIN
/* Some code here */
EXCEPTION WHEN error_type THEN
/* Handling error situation */
/* Logging error or displaying error message */
END;
IF parameter_value = value THEN
/* Code to be executed if parameter_value is equal to value */
ELSIF parameter_value = value2 THEN
/* Code to be executed if parameter_value is equal to value2 */
ELSE
/* Code to be executed if parameter_value is not equal to any of the values */
END IF;
WHILE NOT done DO
/* Code to be executed within the loop */
IF condition THEN
done := TRUE;
END IF;
END WHILE;
RETURN result;
END; |
BEGIN
DECLARE
@total FLOAT;
BEGIN
DECLARE customer_id CURSOR FOR SELECT customer_id FROM customers; OPEN customer_id;
FETCH customer_id INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** al sales for the customer
SELECT @total = SUM(sales)
FROM sales
WHERE customer_id = customer_id;
*** er's total sales
UPDATE customers SET total_sales = @total WHERE customer_id = customer_id;
FETCH customer_id INTO;
END;
CLOSE customer_id;
DEALLOCATE customer_id;
END;
EXCEPTION WHEN OTHERS THEN
*** that occurred during the loop
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
BEGIN
DECLARE
total NUMBER;
BEGIN
FOR customer_id IN (SELECT customer_id FROM customers) LOOP
-- Calculate the total sales for the customer
SELECT SUM(sales) INTO total
FROM sales
WHERE customer_id = customer_id;
-- Update the customer's total sales
UPDATE customers SET total_sales = total WHERE customer_id = customer_id;
END LOOP;
END;
EXCEPTION WHEN OTHERS THEN
-- Handle any errors that occurred during the loop
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
BEGIN
-- De... ***
v_id NUMBER;
v_name dbo.VARCHAR2(50);
v_salary NUMBER;
*** a table
SELECT @v_id = employee_id, @v_name = first_name, @v_salary = salary
FROM employees
WHERE employee_id = 10;
-- Up... ***
UPDATE employees
SET salary = v_salary + 100
WHERE employee_id = v_id;
-- In... ***
INSERT INTO employees (employee_id, first_name, salary)
VALUES (11, 'New Employee', 20000);
-- De... ***
DELETE FROM employees
WHERE employee_id = 12;
-- Co... ***
COMMIT;
EXCEPTION
-- Ha... ***
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
-- Declare variables
v_id NUMBER;
v_name VARCHAR2(50);
v_salary NUMBER;
-- Select data from a table
SELECT employee_id, first_name, salary
INTO v_id, v_name, v_salary
FROM employees
WHERE employee_id = 10;
-- Update the salary
UPDATE employees
SET salary = v_salary + 100
WHERE employee_id = v_id;
-- Insert a new row
INSERT INTO employees (employee_id, first_name, salary)
VALUES (11, 'New Employee', 20000);
-- Delete a row
DELETE FROM employees
WHERE employee_id = 12;
-- Commit the changes
COMMIT;
EXCEPTION
-- Handle errors
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE TABLE sample_table (id FLOAT, name VARCHAR(100));
INSERT INTO sample_table VALUES (1, 'John Doe');
INSERT INTO sample_table VALUES (2, 'Jane Smith');
DECLARE
@emp_name VARCHAR(100);
BEGIN
SELECT @emp_name = name FROM sample_table WHERE id = 1;
DBMS_OUTPUT.PUT_LINE(@emp_name);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
IF OBJECT_ID('sum_numbers', 'FN') IS NOT NULL
DROP FUNCTION sum_numbers;
GO
CREATE FUNCTION sum_numbers(@num1 IN NUMBERDECLARE , num2
begin IN
RETURN NULL;
END; NUMBER) RETURN NUMBER IS
BEGIN
RETURN num1 + num2;
END;
BEGIN
DECLARE
@result FLOAT;
BEGIN
SET @result = dbo.sum_numbers(10, 20);
DBMS_OUTPUT.PUT_LINE(@result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while calculating sum.');
END;
END;
|
CREATE TABLE sample_table (id NUMBER, name VARCHAR2(100));
INSERT INTO sample_table VALUES (1, 'John Doe');
INSERT INTO sample_table VALUES (2, 'Jane Smith');
DECLARE
emp_name VARCHAR2(100);
BEGIN
SELECT name INTO emp_name FROM sample_table WHERE id = 1;
DBMS_OUTPUT.PUT_LINE(emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
CREATE OR REPLACE FUNCTION sum_numbers(num1 IN NUMBER, num2 IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN num1 + num2;
END;
BEGIN
DECLARE
result NUMBER;
BEGIN
result := sum_numbers(10, 20);
DBMS_OUTPUT.PUT_LINE(result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while calculating sum.');
END;
END;
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure(
@p_name IN AS VARCHAR2,
p_emp_code IN NUMBER
) IS
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary FROM employees WHERE name = p_name;
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_code = p_emp_code;
COMMIT;
END;
/
IF OBJECT_ID('get_age', 'FN') IS NOT NULL
DROP FUNCTION get_age;
GO
CREATE FUNCTION get_age(
@p_dob DATE
) RETURNS FLOAT AS
BEGIN
DECLARE @v_age FLOAT;
SELECT @v_age = CAST(CONVERT(VARCHAR(4), GETDATE(), 112) - CONVERT(VARCHAR(4), @p_dob, 112) AS FLOAT);
RETURN @v_age;
END;
GO
SELECT name, dbo.get_age(dob) AS age FROM employees WHERE age > 18;
DECLARE
@v_success BIT;
BEGIN
BEGIN
INSERT INTO employees (name, emp_code, salary, dob) VALUES ('John', 1001, 1000, '1980-01-01');
COMMIT;
SET @v_success = TRUE;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
SET @v_success = FALSE;
END;
IF @v_success = TRUE BEGIN
DBMS_OUTPUT.PUT_LINE('Record inserted successfully.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Error occurred while inserting the record.');
END
END;
/
|
CREATE OR REPLACE PROCEDURE my_procedure(
p_name IN VARCHAR2,
p_emp_code IN NUMBER
) IS
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE name = p_name;
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_code = p_emp_code;
COMMIT;
END;
/
CREATE OR REPLACE FUNCTION get_age(
p_dob DATE
) RETURN NUMBER IS
v_age NUMBER;
BEGIN
SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'YYYY') - TO_CHAR(p_dob, 'YYYY')) INTO v_age;
RETURN v_age;
END;
/
SELECT name, get_age(dob) AS age FROM employees WHERE age > 18;
DECLARE
v_success BOOLEAN;
BEGIN
BEGIN
INSERT INTO employees (name, emp_code, salary, dob) VALUES ('John', 1001, 1000, '1980-01-01');
COMMIT;
v_success := TRUE;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
v_success := FALSE;
END;
IF v_success = TRUE THEN
DBMS_OUTPUT.PUT_LINE('Record inserted successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Error occurred while inserting the record.');
END IF;
END;
/
|
CREATE PROCEDURE my_proc(@a IN AS NUMBER, b OUT NUMBER)
IS
c NUMBER;
BEGIN
set @c = a + 1;
set @b = c;
IF b > 10 BEGIN
RAISE exception;
END
END;
|
CREATE PROCEDURE my_proc(a IN NUMBER, b OUT NUMBER)
IS
c NUMBER;
BEGIN
c := a + 1;
b := c;
IF b > 10 THEN
RAISE exception;
END IF;
END;
|
DECLARE
@cursor c_emp IS
SELECT empno
FROM emp;
BEGIN
DECLARE rec CURSOR FOR c_emp
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE ('Empno: ' + isnull(@rec_empno, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE ('No more employees found.');
EXIT;
END
END;
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/
|
DECLARE
cursor c_emp IS
SELECT empno
FROM emp;
BEGIN
FOR rec IN c_emp
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE ('Empno: ' || rec.empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('No more employees found.');
EXIT;
END;
END LOOP;
END;
/
|
CREATE PROCEDURE FindCustomerOrders(@p_customer_id IN AS NUMBER) IS
CURSOR order_cursor IS
SELECT o.order_id
FROM orders o
WHERE o.customer_id = p_customer_id;
BEGIN
DECLARE order_row CURSOR FOR order_cursor LOOP
DBMS_OUTPUT.PUT_LINE(@order_row_order_id);
FETCH order_row INTO;
END;
CLOSE order_row;
DEALLOCATE order_row;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(p_customer_id, ''));
END
END;
|
PROCEDURE FindCustomerOrders(p_customer_id IN NUMBER) IS
CURSOR order_cursor IS
SELECT o.order_id
FROM orders o
WHERE o.customer_id = p_customer_id;
BEGIN
FOR order_row IN order_cursor LOOP
DBMS_OUTPUT.PUT_LINE(order_row.order_id);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || p_customer_id);
END;
|
IF OBJECT_ID('test_proc', 'P') IS NOT NULL
DROP PROCEDURE test_proc;
GO
CREATE PROCEDURE test_proc(@p_id IN AS NUMBER) AS
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = p_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
dbo.test_proc(1000);
END;
|
CREATE OR REPLACE PROCEDURE test_proc(p_id IN NUMBER) AS
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = p_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
test_proc(1000);
END;
|
DECLARE
@v_name VARCHAR(20);
v_age ROW_NUMBER(3);
BEGIN
SELECT @v_name = name, @v_age = age
FROM employees
WHERE id = 10;
IF v_age > 30 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is older than 30');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is younger than 30');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with id 10');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred');
END;
END;
|
DECLARE
v_name VARCHAR2(20);
v_age NUMBER(3);
BEGIN
SELECT name, age INTO v_name, v_age
FROM employees
WHERE id = 10;
IF v_age > 30 THEN
DBMS_OUTPUT.PUT_LINE('Employee is older than 30');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee is younger than 30');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with id 10');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred');
END;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_name = ename FROM emp WHERE empno = @v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_name, ''));
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value found on index.');
END;
END;
/
|
DECLARE
v_empno NUMBER := 10;
v_name VARCHAR2(20);
BEGIN
SELECT ename INTO v_name FROM emp WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value found on index.');
END;
END;
/
|
IF OBJECT_ID('proc_example', 'P') IS NOT NULL
DROP PROCEDURE proc_example;
GO
CREATE PROCEDURE proc_example(@p_input IN AS VARCHAR2) AS
v_result OUT VARCHAR2;
BEGIN
SELECT @v_result = isnull(p_input, '') + ' processed';
COMMIT;
RAISERROR(
'-20000 Custom error message: ' + isnull(v_result, '')
, 16, 1);
END;
IF OBJECT_ID('func_example', 'FN') IS NOT NULL
DROP FUNCTION func_example;
GO
CREATE FUNCTION func_example(@p_input IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN REGEXP_REPLACE(CONVERT(VARCHAR, p_input), '0', 'X');
END;
BEGIN
WHILE 1=1 BEGIN
EXIT WHEN DBMS_RANDOM.VALUE(1, 10) = 1;
EXECUTE sp_executesql @'INSERT INTO t_example VALUES (' || ISNULL(DBMS_RANDOM.STRING('S', 10), '') + ', NULL, ' + ISNULL(DBMS_RANDOM.VALUE(1, 10), '') + ')';
END;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE proc_example(p_input IN VARCHAR2) AS
v_result OUT VARCHAR2;
BEGIN
SELECT p_input || ' processed' INTO v_result FROM dual;
COMMIT;
RAISE_APPLICATION_ERROR(
-20000,
'Custom error message: ' || v_result
);
END;
CREATE OR REPLACE FUNCTION func_example(p_input IN NUMBER) RETURN VARCHAR2 AS
BEGIN
RETURN REGEXP_REPLACE(TO_CHAR(p_input), '0', 'X');
END;
BEGIN
LOOP
EXIT WHEN DBMS_RANDOM.VALUE(1, 10) = 1;
EXECUTE IMMEDIATE 'INSERT INTO t_example VALUES (' || DBMS_RANDOM.STRING('S', 10) || ', NULL, ' || DBMS_RANDOM.VALUE(1, 10) || ')';
END LOOP;
COMMIT;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(20);
v_emp_salary NUMBER;
v_emp_dept dbo.VARCHAR2(20);
v_emp_bonus NUMBER;
BEGIN
SELECT @v_emp_id = emp_id,
@v_emp_name = emp_name,
@v_emp_salary = emp_salary,
@v_emp_dept = emp_dept,
@v_emp_bonus = emp_bonus
FROM employees
WHERE emp_id = 1;
IF v_emp_salary < 10000
BEGIN
SET @v_emp_bonus = v_emp_salary * 0.1;
END
ELSE BEGIN
SET @v_emp_bonus = v_emp_salary * 0.2;
END
UPDATE employees
SET emp_bonus = v_emp_bonus
WHERE emp_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(20);
v_emp_salary NUMBER;
v_emp_dept VARCHAR2(20);
v_emp_bonus NUMBER;
BEGIN
SELECT emp_id,
emp_name,
emp_salary,
emp_dept,
emp_bonus
INTO v_emp_id,
v_emp_name,
v_emp_salary,
v_emp_dept,
v_emp_bonus
FROM employees
WHERE emp_id = 1;
IF v_emp_salary < 10000
THEN
v_emp_bonus := v_emp_salary * 0.1;
ELSE
v_emp_bonus := v_emp_salary * 0.2;
END IF;
UPDATE employees
SET emp_bonus = v_emp_bonus
WHERE emp_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE sp_update_employee
(
@IN employee_id AS NUMBER,
IN last_name VARCHAR2,
IN email VARCHAR2
)
IS
BEGIN
UPDATE Employees
SET last_name = last_name,
email = email
WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END sp_update_employee;
|
CREATE PROCEDURE sp_update_employee
(
IN employee_id NUMBER,
IN last_name VARCHAR2,
IN email VARCHAR2
)
IS
BEGIN
UPDATE Employees
SET last_name = last_name,
email = email
WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END sp_update_employee;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
COMMIT;
END;
GO
DECLARE
@total_salary FLOAT;
BEGIN
SELECT @total_salary = SUM(salary) FROM employees;
DBMS_OUTPUT.PUT_LINE('Total salary: ' + isnull(@total_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER)
AS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
/
DECLARE
total_salary NUMBER;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees;
DBMS_OUTPUT.PUT_LINE('Total salary: ' || total_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
|
BEGIN
DECLARE
@valid_dept_prefix VARCHAR(2) := 'IT';
dept_no SET @NUMBER = 30;
BEGIN
SELECT @i = COUNT(dept_no)
FROM departments
WHERE dept_prefix = @valid_dept_prefix
AND dept_no = dept_no;
IF i = 0 BEGIN
RAISERROR('-20001 Invalid department.', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
RAISERROR(-20000, SQLERRM, 16, 1);
END;
commit;
END; | BEGIN
DECLARE
valid_dept_prefix VARCHAR2(2) := 'IT';
dept_no NUMBER := 30;
BEGIN
SELECT COUNT(dept_no) INTO i
FROM departments
WHERE dept_prefix = valid_dept_prefix
AND dept_no = dept_no;
IF i = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid department.');
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, SQLERRM);
END;
commit;
END; |
CREATE PROCEDURE sp_example(
@p_in_num IN AS NUMBER,
p_out_num OUT NUMBER
) IS
BEGIN
set @p_out_num = p_in_num * 2;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 An error occurred in sp_example.', 16, 1);
END;
DECLARE
@v_num FLOAT := 10;
v_result NUMBER;
BEGIN
dbo.sp_example(@v_num, v_result);
DBMS_OUTPUT.PUT_LINE(v_result);
END;
|
CREATE PROCEDURE sp_example(
p_in_num IN NUMBER,
p_out_num OUT NUMBER
) IS
BEGIN
p_out_num := p_in_num * 2;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'An error occurred in sp_example.');
END;
DECLARE
v_num NUMBER := 10;
v_result NUMBER;
BEGIN
sp_example(v_num, v_result);
DBMS_OUTPUT.PUT_LINE(v_result);
END;
|
CREATE PROCEDURE update_employee(@emp_id IN AS NUMBER, new_salary IN NUMBER)
AS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value found in the index.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
CREATE PROCEDURE update_employee(emp_id IN NUMBER, new_salary IN NUMBER)
AS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value found in the index.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
DECLARE
@sum_sal FLOAT;
BEGIN
SELECT @sum_sal = SUM(salary)
FROM employees;
IF @sum_sal > 10000
BEGIN
RAISERROR('-20001 Total salary exceeds 10,000', 16, 1);
END
END;
|
DECLARE
sum_sal NUMBER;
BEGIN
SELECT SUM(salary)
INTO sum_sal
FROM employees;
IF sum_sal > 10000
THEN
RAISE_APPLICATION_ERROR(-20001, 'Total salary exceeds 10,000');
END IF;
END;
|
IF OBJECT_ID('sp_UpdateEmployee', 'P') IS NOT NULL
DROP PROCEDURE sp_UpdateEmployee;
GO
CREATE PROCEDURE sp_UpdateEmployee(
@p_emp_id IN AS NUMBER,
p_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = p_salary
WHERE emp_id = p_emp_id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE sp_UpdateEmployee(
p_emp_id IN NUMBER,
p_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = p_salary
WHERE emp_id = p_emp_id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('get_employee_data', 'P') IS NOT NULL
DROP PROCEDURE get_employee_data;
GO
CREATE PROCEDURE get_employee_data(
@employee_id IN AS NUMBER,
out_last_name OUT VARCHAR2,
out_salary OUT NUMBER
) AS
BEGIN
SELECT @out_last_name = last_name, @out_salary = salary
FROM employees
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @out_last_name = NULL;
set @out_salary = NULL;
END
END;
DECLARE
@employee_id FLOAT := 10;
last_name dbo.VARCHAR2(20);
salary NUMBER;
BEGIN
dbo.get_employee_data(@employee_id, last_name, salary);
dbms_output.put_line('Employee last name: ' + isnull(last_name, ''));
dbms_output.put_line('Employee salary: ' + isnull(salary, ''));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error getting employee data');
END;
|
CREATE OR REPLACE PROCEDURE get_employee_data(
employee_id IN NUMBER,
out_last_name OUT VARCHAR2,
out_salary OUT NUMBER
) AS
BEGIN
SELECT last_name, salary
INTO out_last_name, out_salary
FROM employees
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
out_last_name := NULL;
out_salary := NULL;
END;
DECLARE
employee_id NUMBER := 10;
last_name VARCHAR2(20);
salary NUMBER;
BEGIN
get_employee_data(employee_id, last_name, salary);
dbms_output.put_line('Employee last name: ' || last_name);
dbms_output.put_line('Employee salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error getting employee data');
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE deptno = 10;
IF @v_empno IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('No employees in department 10');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empno, '') + ' found in department 10');
END
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE deptno = 10;
IF v_empno IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No employees in department 10');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' found in department 10');
END IF;
END;
|
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_bonus SET @NUMBER = 0;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM emp WHERE emp_id = i;
IF v_salary > 10000 BEGIN
SET @v_bonus = 0.1 * v_salary;
END
UPDATE emp SET salary = v_salary + v_bonus WHERE emp_id = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' + isnull(@v_emp_id, '') + ' to ' + ISNULL((v_salary + v_bonus), ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee ' + isnull(i, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary for employee ' + isnull(i, ''));
END;
END;
END; | BEGIN
FOR i IN 1..10 LOOP
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_bonus NUMBER := 0;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM emp WHERE emp_id = i;
IF v_salary > 10000 THEN
v_bonus := 0.1 * v_salary;
END IF;
UPDATE emp SET salary = v_salary + v_bonus WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' || v_emp_id || ' to ' || (v_salary + v_bonus));
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee ' || i);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary for employee ' || i);
END;
END LOOP;
END; |
BEGIN
-- De... ***
DECLARE
@v_empno FLOAT := 10;
v_ename dbo.VARCHAR2(20) := 'SCOTT';
v_sal SET @NUMBER = 8000;
BEGIN
*** oyee
INSERT INTO emp (empno, ename, sal)
VALUES (@v_empno, v_ename, v_sal);
*** ee's salary
UPDATE emp
SET sal = v_sal + 1000
WHERE empno = @v_empno;
-- De... ***
DELETE FROM emp
WHERE empno = @v_empno;
EXCEPTION
-- Ha... ***
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END; | BEGIN
-- Declare variables
DECLARE
v_empno NUMBER := 10;
v_ename VARCHAR2(20) := 'SCOTT';
v_sal NUMBER := 8000;
BEGIN
-- Insert a new employee
INSERT INTO emp (empno, ename, sal)
VALUES (v_empno, v_ename, v_sal);
-- Update the employee's salary
UPDATE emp
SET sal = v_sal + 1000
WHERE empno = v_empno;
-- Delete the employee
DELETE FROM emp
WHERE empno = v_empno;
EXCEPTION
-- Handle any errors
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END; |
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary
FROM emp
WHERE empno = 10;
IF v_salary > 10000 BEGIN
UPDATE emp SET salary = v_salary + 1000
WHERE empno = @v_empno;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary less than 10000', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary
INTO v_empno, v_salary
FROM emp
WHERE empno = 10;
IF v_salary > 10000 THEN
UPDATE emp SET salary = v_salary + 1000
WHERE empno = v_empno;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary less than 10000');
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE name = 'John Smith';
BEGIN
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_id = @v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 Error updating employee salary', 16, 1);
END;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE name = 'John Smith';
BEGIN
UPDATE employees SET salary = v_salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Error updating employee salary');
END;
END;
|
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@p_employee_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
v_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_name = name
FROM employees
WHERE employee_id = p_employee_id;
RETURN v_name;
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(p_employee_id IN NUMBER) RETURN VARCHAR2 IS
v_name VARCHAR2(50);
BEGIN
SELECT name INTO v_name
FROM employees
WHERE employee_id = p_employee_id;
RETURN v_name;
END;
|
BEGIN
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = department_id, @v_salary = salary
FROM employees
WHERE employee_id = 10;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
UPDATE employees
SET salary = v_salary + 1000
WHERE employee_id = 10;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No employee found with ID 10', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
END; | BEGIN
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT department_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE employee_id = 10;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
END IF;
UPDATE employees
SET salary = v_salary + 1000
WHERE employee_id = 10;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No employee found with ID 10');
WHEN OTHERS THEN
RAISE;
END;
END; |
IF OBJECT_ID('update_dept_sal', 'P') IS NOT NULL
DROP PROCEDURE update_dept_sal;
GO
CREATE PROCEDURE update_dept_sal
(
@deptno IN AS NUMBER,
incr IN NUMBER
)
IS
v_sal salaries.salary%TYPE;
v_count SET @INTEGER = 0;
BEGIN
DECLARE r CURSOR FOR SELECT salary FROM salaries WHERE deptno = deptno; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
set @v_sal = @r_salary * (1 + incr/100);
UPDATE salaries SET salary = v_sal WHERE deptno = deptno AND salary = @r_salary;
set @v_count = v_count + 1;
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
DBMS_OUTPUT.PUT_LINE('Updated ' + isnull(v_count, '') + ' rows in the salaries table.');
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating salaries: ' + ISNULL(SQLERRM, ''));
END update_dept_sal;
|
CREATE OR REPLACE PROCEDURE update_dept_sal
(
deptno IN NUMBER,
incr IN NUMBER
)
IS
v_sal salaries.salary%TYPE;
v_count INTEGER := 0;
BEGIN
FOR r IN (SELECT salary FROM salaries WHERE deptno = deptno) LOOP
v_sal := r.salary * (1 + incr/100);
UPDATE salaries SET salary = v_sal WHERE deptno = deptno AND salary = r.salary;
v_count := v_count + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Updated ' || v_count || ' rows in the salaries table.');
EXCEPTION
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating salaries: ' || SQLERRM);
END update_dept_sal;
|
CREATE PROCEDURE update_employee(@emp_id FLOAT, @new_salary FLOAT)
AS
emp_record EMPLOYEE%ROWTYPE;
BEGIN
*** record
SELECT @emp_record = * FROM employees WHERE employee_id = emp_id;
-- Up... ***
set @emp_record.salary = new_salary;
-- Up... ***
UPDATE employees SET salary = emp_record.salary
WHERE employee_id = emp_id;
-- Co... ***
COMMIT;
END;
|
CREATE PROCEDURE update_employee(emp_id NUMBER, new_salary NUMBER)
IS
emp_record EMPLOYEE%ROWTYPE;
BEGIN
-- Find the employee record
SELECT * INTO emp_record FROM employees WHERE employee_id = emp_id;
-- Update the salary
emp_record.salary := new_salary;
-- Update the database
UPDATE employees SET salary = emp_record.salary
WHERE employee_id = emp_id;
-- Commit the changes
COMMIT;
END;
|
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details(@emp_id IN AS NUMBER) AS
BEGIN
SELECT * FROM employees WHERE emp_id = emp_id INTO emp_record;
IF emp_record IS NULL BEGIN
RAISERROR('-20101 Employee not found', 16, 1);
END
RETURN emp_record;
END;
DECLARE
BEGIN
SET @emp_record = dbo.get_employee_details(1001);
DECLARE rec CURSOR FOR SELECT * FROM departments ORDER BY dept_id; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
dbms_output.put_line('Department: ' + isnull(@rec_dept_id, '') + ' - ' + isnull(@rec_dept_name, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No departments found');
END
WHEN OTHERS THEN
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE get_employee_details(emp_id IN NUMBER) AS
BEGIN
SELECT * FROM employees WHERE emp_id = emp_id INTO emp_record;
IF emp_record IS NULL THEN
RAISE_APPLICATION_ERROR(-20101, 'Employee not found');
END IF;
RETURN emp_record;
END;
DECLARE
emp_record employees%ROWTYPE;
BEGIN
emp_record := get_employee_details(1001);
FOR rec IN (SELECT * FROM departments ORDER BY dept_id) LOOP
dbms_output.put_line('Department: ' || rec.dept_id || ' - ' || rec.dept_name);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No departments found');
WHEN OTHERS THEN
RAISE;
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
) AS
BEGIN
-- So... ***
IF param1 IS NULL BEGIN
RAISERROR('-20000 Param1 cannot be null', 16, 1);
END
SELECT @param2 = COUNT(*) FROM my_table WHERE field = param1;
END;
SELECT * FROM my_table;
BEGIN
DECLARE
@my_var VARCHAR(50);
BEGIN
SELECT @my_var = field FROM my_table WHERE id = 1;
*** my_var
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END *** und scenario
END;
END;
|
CREATE OR REPLACE PROCEDURE my_proc (
param1 IN VARCHAR2,
param2 OUT NUMBER
) AS
BEGIN
-- Some complex logic
IF param1 IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Param1 cannot be null');
END IF;
SELECT COUNT(*) INTO param2 FROM my_table WHERE field = param1;
END;
SELECT * FROM my_table;
BEGIN
DECLARE
my_var VARCHAR2(50);
BEGIN
SELECT field INTO my_var FROM my_table WHERE id = 1;
-- Do something with my_var
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL; -- Handle no data found scenario
END;
END;
|
IF OBJECT_ID('MyProc', 'P') IS NOT NULL
DROP PROCEDURE MyProc;
GO
CREATE PROCEDURE MyProc(
@p_id IN AS NUMBER,
p_name IN VARCHAR2
)
AS
v_max_val NUMBER;
v_err_msg dbo.VARCHAR2(2000) := NULL;
BEGIN
set @v_max_val = Package.Max_Value(p_name);
IF p_id > v_max_val BEGIN
RAISERROR(-20000, isnull(v_err_msg, '') + ' Error: Invalid value for p_id', 16, 1);
END
ELSE BEGIN
-- Do... ***
END
END MyProc;
|
CREATE OR REPLACE PROCEDURE MyProc(
p_id IN NUMBER,
p_name IN VARCHAR2
)
AS
v_max_val NUMBER;
v_err_msg VARCHAR2(2000) := NULL;
BEGIN
v_max_val := Package.Max_Value(p_name);
IF p_id > v_max_val THEN
RAISE_APPLICATION_ERROR(-20000, v_err_msg || ' Error: Invalid value for p_id');
ELSE
-- Do something
END IF;
END MyProc;
|
if object_id('get_employee', 'P') is not null
drop procedure get_employee;
go
create procedure get_employee(@emp_id in as number, out_name out varchar2, out_salary out number) is
begin
select @out_name = name, @out_salary = salary from employees where employee_id = emp_id;
if @@fetch_status <> 0 begin
set @out_name = 'Not Found';
set @out_salary = 0;
end
end get_employee;
|
create or replace procedure get_employee(emp_id in number, out_name out varchar2, out_salary out number) is
begin
select name, salary into out_name, out_salary from employees where employee_id = emp_id;
exception
when no_data_found then
out_name := 'Not Found';
out_salary := 0;
end get_employee;
|
DECLARE
@cursor c1 is
select * from employees;
BEGIN
declare emp cursor for c1
LOOP
DECLARE dept CURSOR for select * from departments where deptno = @emp_deptno;
OPEN dept;
FETCH dept INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.put_line(isnull(@emp_ename, '') + ' works in ' + isnull(@dept_dname, ''));
FETCH dept INTO;
END;
CLOSE dept;
DEALLOCATE dept;
FETCH emp INTO;
END;
CLOSE emp;
DEALLOCATE emp;
if @@fetch_status <> 0
begin
dbms_output.put_line('No data found in the tables');
end
end; | DECLARE
cursor c1 is
select * from employees;
BEGIN
for emp in c1
LOOP
FOR dept in (select * from departments where deptno = emp.deptno)
LOOP
DBMS_OUTPUT.put_line(emp.ename || ' works in ' || dept.dname);
END LOOP;
END LOOP;
EXCEPTION
when no_data_found
then
dbms_output.put_line('No data found in the tables');
end; |
BEGIN
*** and cursors
DECLARE
@emp_id FLOAT;
first_name dbo.VARCHAR2(20);
last_name dbo.VARCHAR2(20);
salary NUMBER;
error_msg dbo.VARCHAR2(100);
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name, salary
FROM employees;
BEGIN
*** g block
BEGIN
*** data using a cursor
OPEN emp_cursor;
WHILE 1=1 BEGIN
FETCH emp_cursor INTO @emp_id, @first_name, @last_name, @salary;
IF @@FETCH_STATUS <> 0 BREAK;
*** data here...
END;
IF @@FETCH_STATUS <> 0 BEGIN
SET @error_msg = 'No employee data found.';
END
END;
-- Cl... ***
CLOSE emp_cursor;
EXCEPTION
WHEN OTHERS THEN
*** rs here...
SET @error_msg = SQLERRM;
END;
*** sage if any
IF error_msg IS NOT NULL BEGIN
RAISERROR(-20001, error_msg, 16, 1);
END
END;
|
BEGIN
-- Declare variables and cursors
DECLARE
emp_id NUMBER;
first_name VARCHAR2(20);
last_name VARCHAR2(20);
salary NUMBER;
error_msg VARCHAR2(100);
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name, salary
FROM employees;
BEGIN
-- Exception handling block
BEGIN
-- Retrieve employee data using a cursor
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_id, first_name, last_name, salary;
EXIT WHEN emp_cursor%NOTFOUND;
-- Process employee data here...
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
error_msg := 'No employee data found.';
END;
-- Close the cursor
CLOSE emp_cursor;
EXCEPTION
WHEN OTHERS THEN
-- Handle other errors here...
error_msg := SQLERRM;
END;
-- Display error message if any
IF error_msg IS NOT NULL THEN
RAISE_APPLICATION_ERROR(-20001, error_msg);
END IF;
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@emp_id IN AS NUMBER,
new_salary IN NUMBER
)
IS
emp_salary NUMBER;
BEGIN
SELECT @emp_salary = salary FROM employees WHERE employee_id = emp_id;
IF emp_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
emp_id IN NUMBER,
new_salary IN 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 not found');
END IF;
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
|
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 100 BEGIN
IF i MOD 2 = 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Even: ' + ISNULL(CONVERT(VARCHAR, i), ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Odd: ' + ISNULL(CONVERT(VARCHAR, i), ''));
END
END;
*** that may have occurred
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END; | BEGIN
FOR i IN 1 .. 100 LOOP
IF i MOD 2 = 0 THEN
DBMS_OUTPUT.PUT_LINE('Even: ' || TO_CHAR(i));
ELSE
DBMS_OUTPUT.PUT_LINE('Odd: ' || TO_CHAR(i));
END IF;
END LOOP;
-- Handle any errors that may have occurred
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END; |
DECLARE
@num_rows FLOAT;
department_name dbo.VARCHAR2(20);
BEGIN
SELECT @num_rows = COUNT(*) FROM employees;
DBMS_OUTPUT.PUT_LINE('Number of employees: ' + isnull(@num_rows, ''));
SELECT @department_name = department_name FROM departments WHERE department_id = 10;
DBMS_OUTPUT.PUT_LINE('Department name: ' + isnull(department_name, ''));
IF @num_rows > 100 BEGIN
RAISERROR('-20001 Too many employees', 16, 1);
END
END;
|
DECLARE
num_rows NUMBER;
department_name VARCHAR2(20);
BEGIN
SELECT COUNT(*) INTO num_rows FROM employees;
DBMS_OUTPUT.PUT_LINE('Number of employees: ' || num_rows);
SELECT department_name INTO department_name FROM departments WHERE department_id = 10;
DBMS_OUTPUT.PUT_LINE('Department name: ' || department_name);
IF num_rows > 100 THEN
RAISE_APPLICATION_ERROR(-20001, 'Too many employees');
END IF;
END;
|
BEGIN
-- De... ***
l_empno NUMBER;
l_ename dbo.VARCHAR2(20);
-- Op... ***
SELECT empno, ename
FROM emp;
*** cursor
WHILE 1=1 BEGIN
FETCH emp_cur INTO @l_empno, @l_ename;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(l_empno, '') + ' ' + isnull(l_ename, ''));
END;
-- Cl... ***
CLOSE emp_cur;
END;
/
|
BEGIN
-- Declare variables
l_empno NUMBER;
l_ename VARCHAR2(20);
-- Open a cursor
OPEN emp_cur FOR
SELECT empno, ename
FROM emp;
-- Loop through the cursor
LOOP
FETCH emp_cur INTO l_empno, l_ename;
EXIT WHEN emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_empno || ' ' || l_ename);
END LOOP;
-- Close the cursor
CLOSE emp_cur;
END;
/
|
IF OBJECT_ID('emp_update', 'P') IS NOT NULL
DROP PROCEDURE emp_update;
GO
CREATE PROCEDURE emp_update (
@p_empno IN AS NUMBER,
p_ename IN VARCHAR2,
p_sal IN NUMBER
) IS
emp_rec emp%ROWTYPE;
BEGIN
SELECT @emp_rec = * FROM emp WHERE empno = p_empno;
set @emp_rec.ename = p_ename;
set @emp_rec.sal = p_sal;
UPDATE emp SET ename = emp_rec.ename, sal = emp_rec.sal WHERE empno = p_empno;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE emp_update (
p_empno IN NUMBER,
p_ename IN VARCHAR2,
p_sal IN NUMBER
) IS
emp_rec emp%ROWTYPE;
BEGIN
SELECT * INTO emp_rec FROM emp WHERE empno = p_empno;
emp_rec.ename := p_ename;
emp_rec.sal := p_sal;
UPDATE emp SET ename = emp_rec.ename, sal = emp_rec.sal WHERE empno = p_empno;
COMMIT;
END;
|
DECLARE
@emp_id VARCHAR(10);
emp_name dbo.VARCHAR2(30);
sal NUMBER;
bonus NUMBER;
BEGIN
SELECT @emp_id = employee_id,
@emp_name = employee_name,
@sal = salary,
@bonus = ISNULL(bonus, 0)
FROM employees
WHERE employee_id = '100';
*** ment.
IF sal > 5000 BEGIN
SET @bonus = bonus + 500;
END
ELSE IF sal > 3000 BEGIN
SET @bonus = bonus + 200;
END
ELSE BEGIN
SET @bonus = bonus + 100;
END
*** se.
UPDATE employees
SET bonus = bonus
WHERE employee_id = @emp_id;
-- Ou... ***
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(sal, ''));
DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(bonus, ''));
-- Er... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
/
|
DECLARE
emp_id VARCHAR2(10);
emp_name VARCHAR2(30);
sal NUMBER;
bonus NUMBER;
BEGIN
SELECT employee_id,
employee_name,
salary,
NVL(bonus, 0)
INTO emp_id, emp_name, sal, bonus
FROM employees
WHERE employee_id = '100';
-- Conditional Statement.
IF sal > 5000 THEN
bonus := bonus + 500;
ELSIF sal > 3000 THEN
bonus := bonus + 200;
ELSE
bonus := bonus + 100;
END IF;
-- Update the database.
UPDATE employees
SET bonus = bonus
WHERE employee_id = emp_id;
-- Output the results.
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || sal);
DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
-- Error Handling.
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
/
|
DECLARE
@VAR1 INT;
VAR2 dbo.VARCHAR2(20);
BEGIN
SET @VAR1 = 10;
SET @VAR2 = 'Hello World';
IF @VAR1 > 5 BEGIN
DBMS_OUTPUT.PUT_LINE(VAR2);
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Condition not met');
END
CREATE PROCEDURE TEST_PROC AS
BEGIN
SET NOCOUNT ON;
DBMS_OUTPUT.PUT_LINE('Procedure called');
END;
BEGIN
TEST_PROC;
END;
CREATE FUNCTION TEST_FUNC() RETURNS VARCHAR(4000) AS
BEGIN
RETURN 'Function called';
END;
BEGIN
DBMS_OUTPUT.PUT_LINE(TEST_FUNC);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
VAR1 NUMBER(5);
VAR2 VARCHAR2(20);
BEGIN
VAR1 := 10;
VAR2 := 'Hello World';
IF VAR1 > 5 THEN
DBMS_OUTPUT.PUT_LINE(VAR2);
ELSE
DBMS_OUTPUT.PUT_LINE('Condition not met');
END IF;
PROCEDURE TEST_PROC IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Procedure called');
END;
BEGIN
TEST_PROC;
END;
FUNCTION TEST_FUNC RETURN VARCHAR2 IS
BEGIN
RETURN 'Function called';
END;
BEGIN
DBMS_OUTPUT.PUT_LINE(TEST_FUNC);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
BEGIN
-- De... ***
v_emp_id SET @NUMBER = 100;
v_emp_name dbo.VARCHAR2(20);
v_salary NUMBER;
*** ment to fetch employee data
SELECT @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = v_emp_id;
*** heck if employee exists
IF v_emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
*** alary
UPDATE employees SET salary = v_salary + 100 WHERE emp_id = v_emp_id;
*** dure to log the update
dbo.log_salary_update(v_emp_id, v_salary);
*** g to capture any errors
EXCEPTION
WHEN OTHERS THEN
*** ges made
ROLLBACK;
-- Ra... ***
RAISERROR('-20002 Error updating salary', 16, 1);
END;
|
BEGIN
-- Declare variables
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(20);
v_salary NUMBER;
-- Execute SQL statement to fetch employee data
SELECT emp_name, salary INTO v_emp_name, v_salary FROM employees WHERE emp_id = v_emp_id;
-- Control flow to check if employee exists
IF v_emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Update employee salary
UPDATE employees SET salary = v_salary + 100 WHERE emp_id = v_emp_id;
-- Call stored procedure to log the update
log_salary_update(v_emp_id, v_salary);
-- Exception handling to capture any errors
EXCEPTION
WHEN OTHERS THEN
-- Rollback any changes made
ROLLBACK;
-- Raise custom error
RAISE_APPLICATION_ERROR(-20002, 'Error updating salary');
END;
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'KING';
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'KING';
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@employee_id IN AS NUMBER,
salary IN NUMBER
)
IS
l_salary NUMBER;
BEGIN
SELECT @l_salary = salary FROM employees WHERE employee_id = employee_id;
IF l_salary < salary BEGIN
UPDATE employees SET salary = salary WHERE employee_id = employee_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary cannot be decreased', 16, 1);
END
END;
BEGIN
dbo.update_employee(100, 10000);
RETURN;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
employee_id IN NUMBER,
salary IN NUMBER
)
IS
l_salary NUMBER;
BEGIN
SELECT salary INTO l_salary FROM employees WHERE employee_id = employee_id;
IF l_salary < salary THEN
UPDATE employees SET salary = salary WHERE employee_id = employee_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be decreased');
END IF;
END;
BEGIN
update_employee(100, 10000);
RETURN;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|