sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
CREATE PROCEDURE GetEmployeeInfo(@EmployeeID INT)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
END;
BEGIN TRY
DECLARE @EmployeeID INT = 1234;
EXEC dbo.GetEmployeeInfo(@EmployeeID);
END TRY
BEGIN CATCH
SELECT dbo.ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
DECLARE @Name VARCHAR(50);
IF @EmployeeID IS NULL
BEGIN
SET @Name = 'N/A';
END
ELSE
BEGIN
SELECT @Name = Name FROM Employees WHERE EmployeeID = @EmployeeID;
END;
INSERT INTO AuditLog (EmployeeName, Action)
VALUES (@Name, 'Viewed employee information');
|
CREATE PROCEDURE GetEmployeeInfo(@EmployeeID INT)
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
END;
BEGIN TRY
DECLARE @EmployeeID INT = 1234;
EXEC GetEmployeeInfo(@EmployeeID);
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
DECLARE @Name VARCHAR(50);
IF @EmployeeID IS NULL
BEGIN
SET @Name = 'N/A';
END
ELSE
BEGIN
SET @Name = (SELECT Name FROM Employees WHERE EmployeeID = @EmployeeID);
END;
INSERT INTO AuditLog (EmployeeName, Action)
VALUES (@Name, 'Viewed employee information');
|
BEGIN
DECLARE
@sal_bonus DECIMAL(6, 2);
BEGIN
SELECT
@sal_bonus = e.salary * (1 + @rate)
FROM employee e
WHERE
e.job_id = 'SH_CLERK';
DBMS_OUTPUT.PUT_LINE(@sal_bonus);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found with job ID ' + 'SH_CLERK');
END
END;
END; | BEGIN
DECLARE
sal_bonus NUMBER(6, 2);
BEGIN
SELECT
e.salary * (1 + @rate)
INTO sal_bonus
FROM employee e
WHERE
e.job_id = 'SH_CLERK';
DBMS_OUTPUT.PUT_LINE(sal_bonus);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found with job ID ' || 'SH_CLERK');
END;
END; |
BEGIN
-- De... ***
DECLARE
@v_employee_id FLOAT;
v_employee_name dbo.VARCHAR2(200);
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
*** oyee
INSERT INTO employees (employee_id, employee_name, salary, dept_id)
VALUES (100, 'John Smith', 10000, 20);
*** e's salary
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = 100;
-- De... ***
DELETE FROM employees
WHERE employee_id = 100;
*** e's information
SELECT employee_id, employee_name, salary, dept_id
FROM employees
WHERE employee_id = 100;
-- Ha... ***
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END; | BEGIN
-- Declare variables
DECLARE
v_employee_id NUMBER;
v_employee_name VARCHAR2(200);
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
-- Insert a new employee
INSERT INTO employees (employee_id, employee_name, salary, dept_id)
VALUES (100, 'John Smith', 10000, 20);
-- Update an employee's salary
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = 100;
-- Delete an employee
DELETE FROM employees
WHERE employee_id = 100;
-- Select an employee's information
SELECT employee_id, employee_name, salary, dept_id
FROM employees
WHERE employee_id = 100;
-- Handle errors
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END; |
DECLARE
@CURSOR c_emp IS
SELECT *
FROM employees
WHERE department_id = 10;
BEGIN
DECLARE r_emp CURSOR FOR c_emp LOOP
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = @r_emp_employee_id;
FETCH r_emp INTO;
END;
CLOSE r_emp;
DEALLOCATE r_emp;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | DECLARE
CURSOR c_emp IS
SELECT *
FROM employees
WHERE department_id = 10;
BEGIN
FOR r_emp IN c_emp LOOP
UPDATE employees
SET salary = salary * 1.1
WHERE employee_id = r_emp.employee_id;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
DECLARE
@v_empno FLOAT := 100;
BEGIN
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE empno = @v_empno;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empno, '') + ' not found');
END
END;
SELECT @v_salary = salary FROM employees WHERE empno = @v_empno;
DBMS_OUTPUT.PUT_LINE('Salary after 10% raise: ' + isnull(v_salary, ''));
END;
/
|
DECLARE
v_empno NUMBER := 100;
BEGIN
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE empno = v_empno;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' not found');
END;
SELECT salary INTO v_salary FROM employees WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Salary after 10% raise: ' || v_salary);
END;
/
|
CREATE TABLE employees (
employee_id FLOAT PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
email VARCHAR(50) UNIQUE,
salary FLOAT NOT NULL,
department_id FLOAT REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id FLOAT PRIMARY KEY,
department_name VARCHAR(30) UNIQUE
);
INSERT INTO departments (department_id, department_name) VALUES (10, 'Sales');
INSERT INTO departments (department_id, department_name) VALUES (20, 'Marketing');
INSERT INTO departments (department_id, department_name) VALUES (30, 'Engineering');
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (1, 'John', 'Doe', 'jdoe@example.com', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (2, 'Jane', 'Smith', 'jsmith@example.com', 60000, 20);
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (3, 'Michael', 'Jones', 'mjones@example.com', 70000, 30);
CREATE PROCEDURE get_employee (
@p_employee_id IN AS NUMBER,
OUT p_first_name VARCHAR2,
OUT p_last_name VARCHAR2,
OUT p_email VARCHAR2,
OUT p_salary NUMBER,
OUT p_department_id NUMBER
) IS
BEGIN
SELECT @p_first_name = first_name, @p_last_name = last_name, @p_email = email, @p_salary = salary, @p_department_id = department_id
FROM employees
WHERE employee_id = p_employee_id;
END;
BEGIN
DECLARE
@v_employee_id FLOAT := 1;
v_first_name dbo.VARCHAR2(20);
v_last_name dbo.VARCHAR2(20);
v_email dbo.VARCHAR2(50);
v_salary NUMBER;
v_department_id NUMBER;
BEGIN
dbo.get_employee(@v_employee_id, v_first_name, v_last_name, v_email, v_salary, v_department_id);
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(v_first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(v_last_name, ''));
DBMS_OUTPUT.PUT_LINE('Email: ' + isnull(v_email, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.PUT_LINE('Department ID: ' + isnull(v_department_id, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(@v_employee_id, ''));
END
END;
END;
|
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(20) NOT NULL,
last_name VARCHAR2(20) NOT NULL,
email VARCHAR2(50) UNIQUE,
salary NUMBER NOT NULL,
department_id NUMBER REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(30) UNIQUE
);
INSERT INTO departments (department_id, department_name) VALUES (10, 'Sales');
INSERT INTO departments (department_id, department_name) VALUES (20, 'Marketing');
INSERT INTO departments (department_id, department_name) VALUES (30, 'Engineering');
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (1, 'John', 'Doe', 'jdoe@example.com', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (2, 'Jane', 'Smith', 'jsmith@example.com', 60000, 20);
INSERT INTO employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (3, 'Michael', 'Jones', 'mjones@example.com', 70000, 30);
CREATE PROCEDURE get_employee (
p_employee_id IN NUMBER,
OUT p_first_name VARCHAR2,
OUT p_last_name VARCHAR2,
OUT p_email VARCHAR2,
OUT p_salary NUMBER,
OUT p_department_id NUMBER
) IS
BEGIN
SELECT first_name, last_name, email, salary, department_id
INTO p_first_name, p_last_name, p_email, p_salary, p_department_id
FROM employees
WHERE employee_id = p_employee_id;
END;
BEGIN
DECLARE
v_employee_id NUMBER := 1;
v_first_name VARCHAR2(20);
v_last_name VARCHAR2(20);
v_email VARCHAR2(50);
v_salary NUMBER;
v_department_id NUMBER;
BEGIN
get_employee(v_employee_id, v_first_name, v_last_name, v_email, v_salary, v_department_id);
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('First Name: ' || v_first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || v_last_name);
DBMS_OUTPUT.PUT_LINE('Email: ' || v_email);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Department ID: ' || v_department_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_employee_id);
END;
END;
|
DECLARE
@empno FLOAT;
ename dbo.VARCHAR2(10);
BEGIN
DECLARE empno CURSOR FOR SELECT employee_id FROM employees; OPEN empno;
FETCH empno INTO;
WHILE @@FETCH_STATUS=0
BEGIN
SELECT @ename = last_name FROM employees WHERE employee_id = @empno;
DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(ename, ''));
FETCH empno INTO;
END;
CLOSE empno;
DEALLOCATE empno;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
empno NUMBER;
ename VARCHAR2(10);
BEGIN
FOR empno IN (SELECT employee_id FROM employees) LOOP
SELECT last_name INTO ename FROM employees WHERE employee_id = empno;
DBMS_OUTPUT.PUT_LINE('Employee: ' || ename);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE add_employee(@emp_id IN AS NUMBER, last_name IN VARCHAR2, first_name IN VARCHAR2,
salary IN NUMBER, department_id IN NUMBER)
AS
*** iables
affected_rows NUMBER;
BEGIN
*** into the employees table
INSERT INTO employees (employee_id, last_name, first_name, salary, department_id)
VALUES (emp_id, last_name, first_name, salary, department_id);
*** affected rows
set @affected_rows = @@ROWCOUNT;
*** n if no rows were affected
IF affected_rows = 0 BEGIN
RAISERROR('-20001 No rows affected', 16, 1);
END
-- Co... ***
COMMIT;
END add_employee;
|
PROCEDURE add_employee(emp_id IN NUMBER, last_name IN VARCHAR2, first_name IN VARCHAR2,
salary IN NUMBER, department_id IN NUMBER)
AS
-- Declare local variables
affected_rows NUMBER;
BEGIN
-- Insert a new row into the employees table
INSERT INTO employees (employee_id, last_name, first_name, salary, department_id)
VALUES (emp_id, last_name, first_name, salary, department_id);
-- Get the number of affected rows
affected_rows := SQL%ROWCOUNT;
-- Raise an exception if no rows were affected
IF affected_rows = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows affected');
END IF;
-- Commit the changes
COMMIT;
END add_employee;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@p_id IN AS NUMBER) IS
BEGIN
UPDATE my_table SET col1 = col1 + 1 WHERE id = p_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20000 No rows updated', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_proc(p_id IN NUMBER) IS
BEGIN
UPDATE my_table SET col1 = col1 + 1 WHERE id = p_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No rows updated');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
BEGIN
-- De... ***
l_empno employees.empno%TYPE;
l_salary employees.salary%TYPE;
*** nformation
SELECT @l_empno = empno, @l_salary = salary
FROM employees
WHERE empno = 10;
*** alary
IF l_salary > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.1
WHERE empno = l_empno;
END
-- Co... ***
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
ROLLBACK;
RAISE;
END;
/
|
BEGIN
-- Declare variables
l_empno employees.empno%TYPE;
l_salary employees.salary%TYPE;
-- Select employee information
SELECT empno, salary
INTO l_empno, l_salary
FROM employees
WHERE empno = 10;
-- Update employee salary
IF l_salary > 10000 THEN
UPDATE employees
SET salary = salary * 1.1
WHERE empno = l_empno;
END IF;
-- Commit changes
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Handle error
ROLLBACK;
RAISE;
END;
/
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 10;
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = 10;
END
ELSE BEGIN
RAISERROR('-20101 Salary is too low for a raise', 16, 1);
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20102 No employee found with employee_id 10', 16, 1);
END
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 10;
IF v_salary > 10000 THEN
UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = 10;
ELSE
RAISE_APPLICATION_ERROR(-20101, 'Salary is too low for a raise');
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20102, 'No employee found with employee_id 10');
END;
|
IF OBJECT_ID('square', 'FN') IS NOT NULL
DROP FUNCTION square;
GO
CREATE FUNCTION square(@x IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN x * x;
END;
/
DECLARE
@sqr_result FLOAT;
BEGIN
SET @sqr_result = square(5);
DBMS_OUTPUT.PUT_LINE('The square of 5 is: ' + isnull(@sqr_result, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found in square function');
END
END;
|
CREATE OR REPLACE FUNCTION square(x IN NUMBER) RETURN NUMBER AS
BEGIN
RETURN x * x;
END;
/
DECLARE
sqr_result NUMBER;
BEGIN
sqr_result := square(5);
DBMS_OUTPUT.PUT_LINE('The square of 5 is: ' || sqr_result);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found in square function');
END;
|
IF OBJECT_ID('get_customer_name', 'FN') IS NOT NULL
DROP FUNCTION get_customer_name;
GO
CREATE FUNCTION get_customer_name (@customer_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
v_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_name = name FROM customers WHERE customer_id = customer_id;
RETURN v_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
|
CREATE OR REPLACE FUNCTION get_customer_name (customer_id IN NUMBER) RETURN VARCHAR2 IS
v_name VARCHAR2(50);
BEGIN
SELECT name INTO v_name FROM customers WHERE customer_id = customer_id;
RETURN v_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
|
DECLARE
@v_employee_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_employee_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_employee_id;
END
ELSE BEGIN
RAISERROR('-20001 Salary too low for raise', 16, 1);
END
COMMIT;
END;
|
DECLARE
v_employee_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO v_employee_id, v_salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary > 10000 THEN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_employee_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary too low for raise');
END IF;
COMMIT;
END;
|
DECLARE
@v_name VARCHAR(255);
BEGIN
SELECT @v_name = name FROM employees WHERE id = 1;
IF @v_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@v_name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
DECLARE
v_name VARCHAR2(255);
BEGIN
SELECT name INTO v_name FROM employees WHERE id = 1;
IF v_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
/
|
CREATE FUNCTION get_employee_salary(@emp_id FLOAT)
RETURNS FLOAT
AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(100);
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
dbo.update_employee_salary(100, 50000);
DBMS_OUTPUT.PUT_LINE('Employee salary updated');
END;
|
CREATE FUNCTION get_employee_salary(emp_id NUMBER)
RETURN NUMBER
IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
BEGIN
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(100);
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
update_employee_salary(100, 50000);
DBMS_OUTPUT.PUT_LINE('Employee salary updated');
END;
|
IF OBJECT_ID('UpdateEmployeeSalary', 'P') IS NOT NULL
DROP PROCEDURE UpdateEmployeeSalary;
GO
CREATE PROCEDURE UpdateEmployeeSalary(@emp_id FLOAT, @new_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE Employees SET Salary = @new_salary
WHERE EmployeeID = @emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee with ID ' + isnull(@emp_id, '') + ' not found', 16, 1);
END
END;
GO
BEGIN
dbo.UpdateEmployeeSalary(100, 50000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE UpdateEmployeeSalary(emp_id NUMBER, new_salary NUMBER) IS
BEGIN
UPDATE Employees SET Salary = new_salary
WHERE EmployeeID = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found');
END UpdateEmployeeSalary;
/
BEGIN
UpdateEmployeeSalary(100, 50000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
@employee_id FLOAT := 10;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@employee_id);
IF salary IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Current salary for employee ' + isnull(@employee_id, '') + ': ' + isnull(salary, ''));
dbo.update_employee_salary(@employee_id, salary * 1.10);
SET @salary = dbo.get_employee_salary(@employee_id);
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' + isnull(@employee_id, '') + ': ' + isnull(salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
employee_id NUMBER := 10;
salary NUMBER;
BEGIN
salary := get_employee_salary(employee_id);
IF salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Current salary for employee ' || employee_id || ': ' || salary);
update_employee_salary(employee_id, salary * 1.10);
salary := get_employee_salary(employee_id);
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' || employee_id || ': ' || salary);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IF;
END;
|
DECLARE
@v_id BIGINT;
v_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_id = id, @v_name = name FROM table_name WHERE id = 1;
IF @v_id IS NULL BEGIN
RAISERROR('-20001 No record found for id ' + 1, 16, 1);
END
UPDATE table_name SET name = 'Updated Name' WHERE id = @v_id;
END; | DECLARE
v_id NUMBER(10);
v_name VARCHAR2(50);
BEGIN
SELECT id, name INTO v_id, v_name FROM table_name WHERE id = 1;
IF v_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No record found for id ' || 1);
END IF;
UPDATE table_name SET name = 'Updated Name' WHERE id = v_id;
END; |
DECLARE
@v_employee_id FLOAT := 10;
v_employee_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_employee_name = ename FROM emp WHERE empno = @v_employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_employee_name, ''));
IF v_employee_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_employee_id NUMBER := 10;
v_employee_name VARCHAR2(100);
BEGIN
SELECT ename INTO v_employee_name FROM emp WHERE empno = v_employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
IF v_employee_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(255);
BEGIN
DECLARE c CURSOR FOR SELECT * FROM customer; OPEN c;
FETCH c INTO;
WHILE @@FETCH_STATUS=0
BEGIN
SET @v_id = @c_id;
SET @v_name = @c_name;
DBMS_OUTPUT.PUT_LINE(isnull(@v_id, '') + ' - ' + isnull(v_name, ''));
FETCH c INTO;
END;
CLOSE c;
DEALLOCATE c;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No customers found.');
END
END;
|
DECLARE
v_id NUMBER;
v_name VARCHAR2(255);
BEGIN
FOR c IN (SELECT * FROM customer) LOOP
v_id := c.id;
v_name := c.name;
DBMS_OUTPUT.PUT_LINE(v_id || ' - ' || v_name);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No customers found.');
END;
|
DECLARE
@v_EmpID FLOAT;
BEGIN
SELECT @v_EmpID = empno
FROM emp
WHERE ename = 'KING';
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + ISNULL(@v_EmpID, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END; | DECLARE
v_EmpID NUMBER;
BEGIN
SELECT empno
INTO v_EmpID
FROM emp
WHERE ename = 'KING';
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_EmpID);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END; |
*** with Error Handling
IF OBJECT_ID('My_Procedure', 'P') IS NOT NULL
DROP PROCEDURE My_Procedure;
GO
CREATE PROCEDURE My_Procedure(@param IN AS VARCHAR2) AS
BEGIN
*** truct
IF param IS NOT NULL BEGIN
-- SQ... ***
UPDATE my_table SET col1 = col1 + 1 WHERE param = param;
END
EXCEPTION
-- Er... ***
WHEN OTHERS THEN
-- Lo... ***
DBMS_OUTPUT.PUT_LINE(ISNULL(DBMS_ERRORCODE, '') + ' - ' + ISNULL(DBMS_ERROR_MESSAGE, ''));
END;
|
-- Stored Procedure with Error Handling
CREATE OR REPLACE PROCEDURE My_Procedure(param IN VARCHAR2) AS
BEGIN
-- Control Flow Construct
IF param IS NOT NULL THEN
-- SQL Statement
UPDATE my_table SET col1 = col1 + 1 WHERE param = param;
END IF;
EXCEPTION
-- Error Handling
WHEN OTHERS THEN
-- Log Error
DBMS_OUTPUT.PUT_LINE(DBMS_ERRORCODE || ' - ' || DBMS_ERROR_MESSAGE);
END;
|
*** rocedure to calculate the factorial of a number
IF OBJECT_ID('factorial', 'P') IS NOT NULL
DROP PROCEDURE factorial;
GO
CREATE PROCEDURE factorial(@n IN AS NUMBER) AS
result SET @NUMBER = 1;
BEGIN
DECLARE i CURSOR FOR REVERSE 1..n WHILE 1=1 BEGIN
set @result = result * i;
END;
DBMS_OUTPUT.PUT_LINE('The factorial of ' + isnull(n, '') + ' is ' + isnull(result, ''));
FETCH i INTO;
END;
CLOSE i;
DEALLOCATE i;
*** to return the current date
IF OBJECT_ID('get_current_date', 'FN') IS NOT NULL
DROP FUNCTION get_current_date;
GO
CREATE FUNCTION get_current_date() RETURNS DATE AS
BEGIN
RETURN GETDATE();
END;
*** o iterate over all employees in the database
DECLARE
@emp_cursor SYS_REFCURSOR;
BEGIN
SELECT employee_id, last_name, first_name
FROM employees;
WHILE 1=1 BEGIN
FETCH emp_cursor INTO @employee_id, @last_name, @first_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(employee_id, '') + ' ' + isnull(last_name, '') + ', ' + isnull(first_name, ''));
END;
CLOSE emp_cursor;
END;
*** to update the employee's manager when the employee's department is changed
CREATE OR REPLACE TRIGGER update_employee_manager ON employees
AFTER UPDATE OF department_id
AS
BEGIN
IF dbo.UPDATING('department_id') BEGIN
UPDATE employees
SET manager_id = (SELECT manager_id FROM departments WHERE department_id = :NEW.department_id)
WHERE employee_id = :OLD.employee_id;
END
END
END;
*** to generate unique order numbers
CREATE SEQUENCE orders_seq;
*** store orders
CREATE TABLE orders (
order_id FLOAT PRIMARY KEY,
customer_id FLOAT,
order_date DATE,
total_amount FLOAT
);
*** nto the orders table using a sequence
INSERT INTO orders ( customer_id, order_date, total_amount)
VALUES ( 100, GETDATE(), 1000);
*** hen trying to insert a duplicate record
BEGIN
INSERT INTO orders ( customer_id, order_date, total_amount)
VALUES ( 100, GETDATE(), 1000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate order number');
END;
|
-- Create a stored procedure to calculate the factorial of a number
CREATE OR REPLACE PROCEDURE factorial(n IN NUMBER) AS
result NUMBER := 1;
BEGIN
FOR i IN REVERSE 1..n LOOP
result := result * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The factorial of ' || n || ' is ' || result);
END;
-- Create a function to return the current date
CREATE OR REPLACE FUNCTION get_current_date RETURN DATE AS
BEGIN
RETURN SYSDATE;
END;
-- Create a cursor to iterate over all employees in the database
DECLARE
emp_cursor SYS_REFCURSOR;
BEGIN
OPEN emp_cursor FOR
SELECT employee_id, last_name, first_name
FROM employees;
LOOP
FETCH emp_cursor INTO employee_id, last_name, first_name;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(employee_id || ' ' || last_name || ', ' || first_name);
END LOOP;
CLOSE emp_cursor;
END;
-- Create a trigger to update the employee's manager when the employee's department is changed
CREATE OR REPLACE TRIGGER update_employee_manager
AFTER UPDATE OF department_id
ON employees
FOR EACH ROW
BEGIN
IF UPDATING('department_id') THEN
UPDATE employees
SET manager_id = (SELECT manager_id FROM departments WHERE department_id = :NEW.department_id)
WHERE employee_id = :OLD.employee_id;
END IF;
END;
-- Create a sequence to generate unique order numbers
CREATE SEQUENCE orders_seq;
-- Create a table to store orders
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER,
order_date DATE,
total_amount NUMBER
);
-- Insert a record into the orders table using a sequence
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES (orders_seq.NEXTVAL, 100, SYSDATE, 1000);
-- Handle an error when trying to insert a duplicate record
BEGIN
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES (orders_seq.NEXTVAL, 100, SYSDATE, 1000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate order number');
END;
|
CREATE TABLE users (id FLOAT, name VARCHAR(50), PRIMARY KEY(id));
INSERT INTO users (id, name) VALUES (1, 'John');
INSERT INTO users (id, name) VALUES (2, 'Mary');
CREATE PROCEDURE get_user_name(@p_id IN AS NUMBER, p_name OUT VARCHAR2) AS
BEGIN
SELECT @p_name = name FROM users WHERE id = p_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_name = 'User not found';
END
END;
DECLARE
@v_name VARCHAR(50);
BEGIN
dbo.get_user_name(1, @v_name);
DBMS_OUTPUT.PUT_LINE(@v_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
DROP TABLE users;
|
CREATE TABLE users (id NUMBER, name VARCHAR2(50), PRIMARY KEY(id));
INSERT INTO users (id, name) VALUES (1, 'John');
INSERT INTO users (id, name) VALUES (2, 'Mary');
CREATE PROCEDURE get_user_name(p_id IN NUMBER, p_name OUT VARCHAR2) AS
BEGIN
SELECT name INTO p_name FROM users WHERE id = p_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_name := 'User not found';
END;
DECLARE
v_name VARCHAR2(50);
BEGIN
get_user_name(1, v_name);
DBMS_OUTPUT.PUT_LINE(v_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
DROP TABLE users;
|
DECLARE
@CURSOR c1 IS
SELECT * FROM table1;
BEGIN
DECLARE r CURSOR FOR c1 LOOP
UPDATE table2 SET col2 = col1 WHERE condition;
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
IF @@ERROR <> 0 BEGIN
RAISERROR('-20001 Update failed', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
|
DECLARE
CURSOR c1 IS
SELECT * FROM table1;
BEGIN
FOR r IN c1 LOOP
UPDATE table2 SET col2 = col1 WHERE condition;
END LOOP;
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Update failed');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
|
DECLARE
@emp_id FLOAT := 100;
emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @emp_name = name FROM employees WHERE employee_id = @emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
IF emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee with ID ' + isnull(@emp_id, ''));
END
END;
|
DECLARE
emp_id NUMBER := 100;
emp_name VARCHAR2(50);
BEGIN
SELECT name INTO emp_name FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee with ID ' || emp_id);
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@num IN AS NUMBER) AS
BEGIN
*** num
IF num > 10 BEGIN
RAISE exception;
END
END;
/
BEGIN
dbo.my_proc(11); *** n exception
EXCEPTION
WHEN OTHERS THEN
*** ion
END;
|
CREATE OR REPLACE PROCEDURE my_proc(num IN NUMBER) AS
BEGIN
-- do something with num
IF num > 10 THEN
RAISE exception;
END IF;
END;
/
BEGIN
my_proc(11); -- this will raise an exception
EXCEPTION
WHEN OTHERS THEN
-- handle the exception
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
IF salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(salary, ''));
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER)
RETURN NUMBER
IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || salary);
END;
|
BEGIN
DECLARE rec CURSOR FOR SELECT * FROM emp WHERE deptno = 10; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
dbms_output.put_line('Employee: ' + isnull(@rec_ename, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(30);
BEGIN
SELECT @v_emp_name = ename, @v_emp_id = empno FROM emp WHERE deptno = 10;
dbms_output.put_line('Employee name: ' + isnull(v_emp_name, '') + ', ID: ' + isnull(@v_emp_id, ''));
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No employees found in department 10!');
END
END;
END;
|
BEGIN
FOR rec IN (SELECT * FROM emp WHERE deptno = 10) LOOP
dbms_output.put_line('Employee: ' || rec.ename);
END LOOP;
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(30);
BEGIN
SELECT ename, empno INTO v_emp_name, v_emp_id FROM emp WHERE deptno = 10;
dbms_output.put_line('Employee name: ' || v_emp_name || ', ID: ' || v_emp_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employees found in department 10!');
END;
END;
|
BEGIN
-- De... ***
l_num NUMBER;
l_sum NUMBER;
*** les
set @l_num = 1;
set @l_sum = 0;
*** ers from 1 to 10
WHILE l_num <= 10 BEGIN
*** umber to the sum
set @l_sum = l_sum + l_num;
*** rent number
set @l_num = l_num + 1;
END;
*** the numbers
DBMS_OUTPUT.PUT_LINE('The sum of the numbers from 1 to 10 is ' + isnull(l_sum, ''));
EXCEPTION
*** that may occur
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END; | BEGIN
-- Declare variables
l_num NUMBER;
l_sum NUMBER;
-- Initialize variables
l_num := 1;
l_sum := 0;
-- Loop through numbers from 1 to 10
WHILE l_num <= 10 LOOP
-- Add the current number to the sum
l_sum := l_sum + l_num;
-- Increment the current number
l_num := l_num + 1;
END LOOP;
-- Print the sum of the numbers
DBMS_OUTPUT.PUT_LINE('The sum of the numbers from 1 to 10 is ' || l_sum);
EXCEPTION
-- Handle any errors that may occur
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END; |
CREATE PROCEDURE add_customer(@p_name IN AS VARCHAR2, p_address IN VARCHAR2)
IS
v_customer_id NUMBER;
v_birthday DATE;
v_gender dbo.VARCHAR2(1);
BEGIN
*** omer
INSERT INTO customers (name, address)
VALUES (p_name, p_address);
-- Ge... ***
SELECT @v_customer_id = MAX(customer_id) FROM customers;
*** s birthday
SELECT @v_birthday = birthday FROM customers WHERE customer_id = v_customer_id;
*** s gender
SELECT @v_gender = gender FROM customers WHERE customer_id = v_customer_id;
*** omer is valid
IF v_customer_id IS NULL BEGIN
RAISERROR('-20000 Customer not found', 16, 1);
END
*** omer is over 18
CASE WHEN v_birthday >= GETDATE() - 18 YEARS THEN
dbo.RAISE_APPLICATION_ERROR(-20001, 'Customer is not over 18');
END IF;
*** omer is male
CASE WHEN v_gender <> 'M' THEN
dbo.RAISE_APPLICATION_ERROR(-20002, 'Customer is not male');
END IF;
*** , so commit the transaction
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20003 No data found', 16, 1);
END
END;
|
PROCEDURE add_customer(p_name IN VARCHAR2, p_address IN VARCHAR2)
IS
v_customer_id NUMBER;
v_birthday DATE;
v_gender VARCHAR2(1);
BEGIN
-- Insert a new customer
INSERT INTO customers (name, address)
VALUES (p_name, p_address);
-- Get the customer ID
SELECT MAX(customer_id) INTO v_customer_id FROM customers;
-- Get the customer's birthday
SELECT birthday INTO v_birthday FROM customers WHERE customer_id = v_customer_id;
-- Get the customer's gender
SELECT gender INTO v_gender FROM customers WHERE customer_id = v_customer_id;
-- Check if the customer is valid
IF v_customer_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Customer not found');
END IF;
-- Check if the customer is over 18
IF v_birthday >= SYSDATE - 18 YEARS THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer is not over 18');
END IF;
-- Check if the customer is male
IF v_gender <> 'M' THEN
RAISE_APPLICATION_ERROR(-20002, 'Customer is not male');
END IF;
-- All checks passed, so commit the transaction
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20003, 'No data found');
END;
|
CREATE PROCEDURE update_employee(@id IN AS NUMBER, salary IN NUMBER) IS
emp_record EMP_REC;
BEGIN
SELECT @emp_record = * FROM employees WHERE id = id;
IF emp_record.salary < salary BEGIN
UPDATE employees SET salary = salary WHERE id = id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary cannot be decreased', 16, 1);
END
END;
|
PROCEDURE update_employee(id IN NUMBER, salary IN NUMBER) IS
emp_record EMP_REC;
BEGIN
SELECT * INTO emp_record FROM employees WHERE id = id;
IF emp_record.salary < salary THEN
UPDATE employees SET salary = salary WHERE id = id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be decreased');
END IF;
END;
|
IF OBJECT_ID('get_product_info', 'FN') IS NOT NULL
DROP FUNCTION get_product_info;
GO
CREATE FUNCTION get_product_info (@p_product_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
product_info SYS_REFCURSOR;
BEGIN
SELECT product_id, product_name, unit_price, units_in_stock
FROM products
WHERE product_id = p_product_id;
RETURN product_info;
END;
DECLARE
@product_id FLOAT := 1001;
product_info SYS_REFCURSOR;
product_name dbo.VARCHAR2(100);
unit_price ROW_NUMBER(10, 2);
units_in_stock NUMBER;
BEGIN
SET @product_info = dbo.get_product_info(@product_id);
WHILE 1=1 BEGIN
FETCH product_info
INTO @product_name, @unit_price, @units_in_stock;
IF @@FETCH_STATUS <> 0 BREAK;
*** eved data
DBMS_OUTPUT.PUT_LINE('Product Name: ' + isnull(product_name, ''));
DBMS_OUTPUT.PUT_LINE('Unit Price: ' + isnull(unit_price, ''));
DBMS_OUTPUT.PUT_LINE('Units In Stock: ' + isnull(units_in_stock, ''));
END;
CLOSE product_info;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No product found with ID: ' + isnull(@product_id, ''));
END
END;
|
CREATE OR REPLACE FUNCTION get_product_info (p_product_id IN NUMBER) RETURN SYS_REFCURSOR IS
product_info SYS_REFCURSOR;
BEGIN
OPEN product_info FOR
SELECT product_id, product_name, unit_price, units_in_stock
FROM products
WHERE product_id = p_product_id;
RETURN product_info;
END;
DECLARE
product_id NUMBER := 1001;
product_info SYS_REFCURSOR;
product_name VARCHAR2(100);
unit_price NUMBER(10, 2);
units_in_stock NUMBER;
BEGIN
product_info := get_product_info(product_id);
LOOP
FETCH product_info
INTO product_name, unit_price, units_in_stock;
EXIT WHEN product_info%NOTFOUND;
-- process the retrieved data
DBMS_OUTPUT.PUT_LINE('Product Name: ' || product_name);
DBMS_OUTPUT.PUT_LINE('Unit Price: ' || unit_price);
DBMS_OUTPUT.PUT_LINE('Units In Stock: ' || units_in_stock);
END LOOP;
CLOSE product_info;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No product found with ID: ' || product_id);
END;
|
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_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,
p_manager_id IN NUMBER,
p_department_id IN NUMBER
) AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
manager_id,
department_id
) VALUES (
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_manager_id,
p_department_id
);
COMMIT;
END;
/
|
CREATE OR REPLACE PROCEDURE insert_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,
p_manager_id IN NUMBER,
p_department_id IN NUMBER
) AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
manager_id,
department_id
) VALUES (
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_manager_id,
p_department_id
);
COMMIT;
END;
/
|
IF OBJECT_ID('SetEmployeeSalary', 'P') IS NOT NULL
DROP PROCEDURE SetEmployeeSalary;
GO
CREATE PROCEDURE SetEmployeeSalary
(@p_emp_id IN AS NUMBER, p_salary IN NUMBER)
AS
BEGIN
*** oyee exists
SELECT @v_count = COUNT(*) FROM employees WHERE emp_id = p_emp_id;
*** the employee does not exist
IF v_count = 0 BEGIN
raiserror('-20001 Employee does not exist', 16, 1);
END
*** ee's salary
UPDATE employees SET salary = p_salary WHERE emp_id = p_emp_id;
END;
IF OBJECT_ID('GetEmployeeName', 'FN') IS NOT NULL
DROP FUNCTION GetEmployeeName;
GO
CREATE FUNCTION GetEmployeeName(@p_emp_id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
*** oyee exists
SELECT @v_count = COUNT(*) FROM employees WHERE emp_id = p_emp_id;
*** the employee does not exist
IF v_count = 0 BEGIN
raiserror('-20002 Employee does not exist', 16, 1);
END
*** s name
SELECT @v_name = ename FROM employees WHERE emp_id = p_emp_id;
*** ee's name
RETURN v_name;
END;
DECLARE
@v_emp_id FLOAT := 10;
v_salary SET @NUMBER = 10000;
v_name dbo.VARCHAR2(50);
BEGIN
*** rocedure to set the employee's salary
dbo.SetEmployeeSalary(p_emp_id, p_salary);
*** to get the employee's name
SET @v_name = dbo.GetEmployeeName(p_emp_id);
*** e's name and salary
dbms_output.put_line('Employee Name: ' + isnull(v_name, ''));
dbms_output.put_line('Employee Salary: ' + isnull(v_salary, ''));
END; | CREATE OR REPLACE PROCEDURE SetEmployeeSalary
(p_emp_id IN NUMBER, p_salary IN NUMBER)
AS
BEGIN
-- Check if the employee exists
SELECT COUNT(*) INTO v_count FROM employees WHERE emp_id = p_emp_id;
-- Raise an error if the employee does not exist
IF v_count = 0 THEN
raise_application_error(-20001, 'Employee does not exist');
END IF;
-- Update the employee's salary
UPDATE employees SET salary = p_salary WHERE emp_id = p_emp_id;
END;
CREATE OR REPLACE FUNCTION GetEmployeeName(p_emp_id IN NUMBER)
RETURN VARCHAR2
AS
BEGIN
-- Check if the employee exists
SELECT COUNT(*) INTO v_count FROM employees WHERE emp_id = p_emp_id;
-- Raise an error if the employee does not exist
IF v_count = 0 THEN
raise_application_error(-20002, 'Employee does not exist');
END IF;
-- Get the employee's name
SELECT ename INTO v_name FROM employees WHERE emp_id = p_emp_id;
-- Return the employee's name
RETURN v_name;
END;
DECLARE
v_emp_id NUMBER := 10;
v_salary NUMBER := 10000;
v_name VARCHAR2(50);
BEGIN
-- Call the stored procedure to set the employee's salary
SetEmployeeSalary(p_emp_id, p_salary);
-- Call the function to get the employee's name
v_name := GetEmployeeName(p_emp_id);
-- Print the employee's name and salary
dbms_output.put_line('Employee Name: ' || v_name);
dbms_output.put_line('Employee Salary: ' || v_salary);
END; |
CREATE PROCEDURE GenerateInvoice(@customer_id IN AS NUMBER, total_amount IN NUMBER)
IS
invoice_number NUMBER;
BEGIN
SELECT @invoice_number = invoice_seq.NEXTVAL;
INSERT INTO invoices (invoice_number, customer_id, total_amount)
VALUES (invoice_number, customer_id, total_amount);
COMMIT;
END;
CREATE FUNCTION GetCustomerName(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
customer_name dbo.VARCHAR2(50);
BEGIN
SELECT @customer_name = name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
BEGIN
DECLARE
@total_amount FLOAT;
BEGIN
SELECT @total_amount = @total_amount FROM orders WHERE customer_id = 10;
dbo.GenerateInvoice(10, @total_amount);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No orders found for customer.', 16, 1);
END
END;
END;
|
PROCEDURE GenerateInvoice(customer_id IN NUMBER, total_amount IN NUMBER)
IS
invoice_number NUMBER;
BEGIN
SELECT invoice_seq.NEXTVAL INTO invoice_number FROM dual;
INSERT INTO invoices (invoice_number, customer_id, total_amount)
VALUES (invoice_number, customer_id, total_amount);
COMMIT;
END;
FUNCTION GetCustomerName(customer_id IN NUMBER) RETURN VARCHAR2
IS
customer_name VARCHAR2(50);
BEGIN
SELECT name INTO customer_name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
BEGIN
DECLARE
total_amount NUMBER;
BEGIN
SELECT total_amount INTO total_amount FROM orders WHERE customer_id = 10;
GenerateInvoice(10, total_amount);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No orders found for customer.');
END;
END;
|
CREATE TABLE emp (
empno SMALLINT PRIMARY KEY,
ename VARCHAR(10),
job VARCHAR(10),
sal DECIMAL(7,2),
deptno SMALLINT
);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (1, 'John', 'Clerk', 1200, 10);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (2, 'Mary', 'Manager', 2000, 20);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (3, 'Bob', 'Salesman', 1500, 30);
IF OBJECT_ID('get_emp_by_deptno', 'P') IS NOT NULL
DROP PROCEDURE get_emp_by_deptno;
GO
CREATE PROCEDURE get_emp_by_deptno (@deptno IN AS NUMBER)
AS
BEGIN
DECLARE rec CURSOR FOR
SELECT *
FROM emp
WHERE deptno = deptno;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(@rec_ename);
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/
BEGIN
dbo.get_emp_by_deptno(10);
END;
/
|
CREATE TABLE emp (
empno NUMBER(4) PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(10),
sal NUMBER(7,2),
deptno NUMBER(2)
);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (1, 'John', 'Clerk', 1200, 10);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (2, 'Mary', 'Manager', 2000, 20);
INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (3, 'Bob', 'Salesman', 1500, 30);
CREATE OR REPLACE PROCEDURE get_emp_by_deptno (deptno IN NUMBER)
AS
BEGIN
FOR rec IN (
SELECT *
FROM emp
WHERE deptno = deptno
)
LOOP
DBMS_OUTPUT.PUT_LINE(rec.ename);
END LOOP;
END;
/
BEGIN
get_emp_by_deptno(10);
END;
/
|
CREATE PROCEDURE sp_insert_employee
(
@p_empid IN AS NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (empid, name, salary)
VALUES (p_empid, p_name, p_salary);
COMMIT;
END;
CREATE FUNCTION fn_calculate_bonus
(
@p_empid IN NUMBER
DECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
bonus NUMBER;
BEGIN
SELECT @bonus = bonus
FROM bonuses
WHERE empid = p_empid;
RETURN bonus;
END;
BEGIN
IF dbo.fn_calculate_bonus(100) > 1000 BEGIN
UPDATE employees SET salary = salary + 100
WHERE empid = 100;
COMMIT;
END
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 An error occurred while updating the employee salary.', 16, 1);
END;
|
CREATE PROCEDURE sp_insert_employee
(
p_empid IN NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (empid, name, salary)
VALUES (p_empid, p_name, p_salary);
COMMIT;
END;
CREATE FUNCTION fn_calculate_bonus
(
p_empid IN NUMBER
)
RETURN NUMBER
AS
bonus NUMBER;
BEGIN
SELECT bonus INTO bonus
FROM bonuses
WHERE empid = p_empid;
RETURN bonus;
END;
BEGIN
IF fn_calculate_bonus(100) > 1000 THEN
UPDATE employees SET salary = salary + 100
WHERE empid = 100;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'An error occurred while updating the employee salary.');
END;
|
BEGIN
-- De... ***
DECLARE
@l_emp_id FLOAT;
l_emp_name dbo.VARCHAR2(20);
l_salary NUMBER;
BEGIN
*** rmation
SELECT @l_emp_id = emp_id, @l_emp_name = emp_name, @l_salary = salary
FROM employees
WHERE emp_id = 100;
*** formation
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + ISNULL(CONVERT(VARCHAR, @l_emp_id), ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(l_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, l_salary), ''));
-- Ha... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
END;
|
BEGIN
-- Declare variables
DECLARE
l_emp_id NUMBER;
l_emp_name VARCHAR2(20);
l_salary NUMBER;
BEGIN
-- Get employee information
SELECT emp_id, emp_name, salary
INTO l_emp_id, l_emp_name, l_salary
FROM employees
WHERE emp_id = 100;
-- Print employee information
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || TO_CHAR(l_emp_id));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(l_salary));
EXCEPTION
-- Handle errors
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
END;
|
*** rocedure to return a random number
CREATE PROCEDURE GetRandomNumber
AS
BEGIN
SET NOCOUNT ON;
*** number between 1 and 10
SELECT @v_random_number = FLOOR(1 + (10 * DBMS_RANDOM.VALUE));
*** number
RETURN v_random_number;
END;
*** to return the current date
CREATE FUNCTION GetCurrentDate()
RETURNS DATE
AS
BEGIN
*** t date
RETURN GETDATE();
END;
*** ement to select all rows from a table
SELECT * FROM employees;
*** flow construct to print a message if the random number is greater than 5
IF dbo.GetRandomNumber() > 5 BEGIN
DBMS_OUTPUT.PUT_LINE('The random number is greater than 5.');
END
*** andling scenario to catch and print any errors that occur
BEGIN
*** SQL statement that may fail
SELECT * FROM non_existent_table;
EXCEPTION
*** and print them
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END; | -- Create a stored procedure to return a random number
CREATE PROCEDURE GetRandomNumber
AS
BEGIN
-- Generate a random number between 1 and 10
SELECT FLOOR(1 + (10 * DBMS_RANDOM.VALUE)) INTO v_random_number FROM DUAL;
-- Return the random number
RETURN v_random_number;
END;
-- Create a function to return the current date
CREATE FUNCTION GetCurrentDate
RETURN DATE
AS
BEGIN
-- Return the current date
RETURN SYSDATE;
END;
-- Create a SQL statement to select all rows from a table
SELECT * FROM employees;
-- Create a control flow construct to print a message if the random number is greater than 5
IF GetRandomNumber() > 5 THEN
DBMS_OUTPUT.PUT_LINE('The random number is greater than 5.');
END IF;
-- Create an error handling scenario to catch and print any errors that occur
BEGIN
-- Try to execute a SQL statement that may fail
SELECT * FROM non_existent_table;
EXCEPTION
-- Catch any errors and print them
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END; |
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary NUMBERDECLARE RETURN
BEGIN NUMBER
RETURN ;
END;
salary NUMBER;
BEGIN
salary
employees
employee_id employee_id;
RETURN salary;
@ 0 BEGIN
RETURN ;
END
END;
OBJECT_ID, NOT
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary AS NUMBER, new_salary NUMBER
BEGIN
employees
SET salary new_salary
employee_id employee_id;
@ 0 BEGIN
;
END
END;
BEGIN
DECLARE FLOAT;
DECLARE FLOAT;
SET 10;
SET dbo.get_employee_salary;
NOT BEGIN
DBMS_OUTPUT.PUT_LINE , , ;
dbo.update_employee_salary, 1.1;
DBMS_OUTPUT.PUT_LINE;
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE;
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary
FROM employees
WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = employee_id;
EXCEPTION
WHEN no_data_found THEN
NULL;
END;
BEGIN
DECLARE emp_id NUMBER;
DECLARE emp_salary NUMBER;
emp_id := 10;
emp_salary := get_employee_salary(emp_id);
IF emp_salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || emp_id || ' has a salary of ' || emp_salary);
update_employee_salary(emp_id, emp_salary * 1.1);
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found');
END IF;
END;
|
DECLARE
@employee_id FLOAT := 100;
BEGIN
*** salary
UPDATE employees SET salary = salary * 1.10 WHERE @employee_id = @employee_id;
*** date failed
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
-- Co... ***
COMMIT;
END;
|
DECLARE
employee_id NUMBER := 100;
BEGIN
-- Update employee's salary
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = employee_id;
-- Raise error if update failed
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Commit changes
COMMIT;
END;
|
DECLARE
@v_deptno FLOAT := 10;
v_dname dbo.VARCHAR2(14);
BEGIN
SELECT @v_dname = dname FROM dept WHERE deptno = @v_deptno;
IF v_dname IS NULL BEGIN
RAISERROR('-20001 No such department', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(v_dname, ''));
END;
|
DECLARE
v_deptno NUMBER := 10;
v_dname VARCHAR2(14);
BEGIN
SELECT dname INTO v_dname FROM dept WHERE deptno = v_deptno;
IF v_dname IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No such department');
END IF;
DBMS_OUTPUT.PUT_LINE('Department Name: ' || v_dname);
END;
|
DECLARE
@cursor c1 IS SELECT * FROM emp WHERE deptno = 20;
BEGIN
DECLARE r CURSOR FOR c1 LOOP
DBMS_OUTPUT.PUT_LINE(@r_ename);
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
END;
/
|
DECLARE
cursor c1 IS SELECT * FROM emp WHERE deptno = 20;
BEGIN
FOR r IN c1 LOOP
DBMS_OUTPUT.PUT_LINE(r.ename);
END LOOP;
END;
/
|
DECLARE
@v_salary DECIMAL(10,2);
BEGIN
SELECT @v_salary = salary FROM emp WHERE empno = 10;
IF @v_salary > 10000 BEGIN
UPDATE emp SET salary = @v_salary * 1.05 WHERE empno = 10;
END
ELSE BEGIN
UPDATE emp SET salary = @v_salary * 1.02 WHERE empno = 10;
END
Commit;
IF SQL%FOUND BEGIN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Salary update failed.');
END
END;
|
DECLARE
v_salary NUMBER(10,2);
BEGIN
SELECT salary INTO v_salary FROM emp WHERE empno = 10;
IF v_salary > 10000 THEN
UPDATE emp SET salary = v_salary * 1.05 WHERE empno = 10;
ELSE
UPDATE emp SET salary = v_salary * 1.02 WHERE empno = 10;
END IF;
Commit;
IF SQL%FOUND THEN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE DBMS_OUTPUT.PUT_LINE('Salary update failed.');
END IF;
END;
|
BEGIN
DECLARE
@l_empno FLOAT;
l_ename dbo.VARCHAR2(20);
l_sal NUMBER;
BEGIN
SELECT @l_empno = empno, @l_ename = ename, @l_sal = sal FROM emp WHERE empno = 7839;
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(l_ename, '') + ' earns ' + isnull(l_sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with empno 7839');
END
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END; | BEGIN
DECLARE
l_empno NUMBER;
l_ename VARCHAR2(20);
l_sal NUMBER;
BEGIN
SELECT empno, ename, sal INTO l_empno, l_ename, l_sal FROM emp WHERE empno = 7839;
DBMS_OUTPUT.PUT_LINE('Employee ' || l_ename || ' earns ' || l_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with empno 7839');
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END; |
DECLARE
@v_deptno SMALLINT;
v_dname dbo.VARCHAR2(14);
v_loc dbo.VARCHAR2(13);
BEGIN
SELECT @v_deptno = department_number, @v_dname = department_name, @v_loc = location_id
FROM departments
WHERE department_number = 20;
IF v_loc IS NULL BEGIN
RAISERROR('-20001 Location not found for department ' + isnull(@v_deptno, ''), 16, 1);
END
UPDATE departments SET location_id = 'NEW YORK' WHERE department_number = @v_deptno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Department ' + isnull(v_dname, '') + ' moved to ' + isnull(v_loc, ''));
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
/ | DECLARE
v_deptno NUMBER(2);
v_dname VARCHAR2(14);
v_loc VARCHAR2(13);
BEGIN
SELECT department_number, department_name, location_id
INTO v_deptno, v_dname, v_loc
FROM departments
WHERE department_number = 20;
IF v_loc IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Location not found for department ' || v_deptno);
END IF;
UPDATE departments SET location_id = 'NEW YORK' WHERE department_number = v_deptno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Department ' || v_dname || ' moved to ' || v_loc);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/ |
CREATE PROCEDURE Calculate_Total(
@CUSTOMER_ID IN AS NUMBER,
TOTAL_AMOUNT OUT NUMBER
)
AS
BEGIN
SELECT @TOTAL_AMOUNT = SUM(AMOUNT)
FROM SALES
WHERE CUSTOMER_ID = CUSTOMER_ID;
END;
|
CREATE PROCEDURE Calculate_Total(
CUSTOMER_ID IN NUMBER,
TOTAL_AMOUNT OUT NUMBER
)
AS
BEGIN
SELECT SUM(AMOUNT)
INTO TOTAL_AMOUNT
FROM SALES
WHERE CUSTOMER_ID = CUSTOMER_ID;
END;
|
CREATE PROCEDURE AddEmployee(@in_first_name VARCHAR(4000), @in_last_name VARCHAR(4000),
@IN OUT AS emp_id OUT NUMBER)
AS
BEGIN
INSERT INTO employees(first_name, last_name)
VALUES (in_first_name, in_last_name);
SET @emp_id = SCOPE_IDENTITY();
END;
/
BEGIN
DECLARE
@emp_id FLOAT;
BEGIN
dbo.AddEmployee('John', 'Smith', @emp_id);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Employee with the same name already exists', 16, 1);
END;
END;
/
|
CREATE PROCEDURE AddEmployee(in_first_name VARCHAR2, in_last_name VARCHAR2,
IN OUT emp_id OUT NUMBER)
AS
BEGIN
INSERT INTO employees(first_name, last_name)
VALUES (in_first_name, in_last_name)
RETURNING employee_id INTO emp_id;
END;
/
BEGIN
DECLARE
emp_id NUMBER;
BEGIN
AddEmployee('John', 'Smith', emp_id);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with the same name already exists');
END;
END;
/
|
DECLARE
@emp_id FLOAT;
BEGIN
SELECT @emp_id = employee_id
FROM employees
WHERE last_name = 'King';
IF @emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @emp_id;
COMMIT;
END;
/
|
DECLARE
emp_id NUMBER;
BEGIN
SELECT employee_id
INTO emp_id
FROM employees
WHERE last_name = 'King';
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = emp_id;
COMMIT;
END;
/
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'Doe';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee with last name ''Doe'' not found', 16, 1);
END
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'Doe';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with last name ''Doe'' not found');
END IF;
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
COMMIT;
END;
|
IF OBJECT_ID('proc_update_record', 'P') IS NOT NULL
DROP PROCEDURE proc_update_record;
GO
CREATE PROCEDURE proc_update_record(@p_id IN AS NUMBER, p_name IN VARCHAR2, p_age IN NUMBER) IS
BEGIN
UPDATE employee SET name = p_name, age = p_age WHERE id = p_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No record found with the specified ID.', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE proc_update_record(p_id IN NUMBER, p_name IN VARCHAR2, p_age IN NUMBER) IS
BEGIN
UPDATE employee SET name = p_name, age = p_age WHERE id = p_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No record found with the specified ID.');
END;
|
IF OBJECT_ID('update_emp_salary', 'P') IS NOT NULL
DROP PROCEDURE update_emp_salary;
GO
CREATE PROCEDURE update_emp_salary(
@empno IN AS NUMBER,
new_salary IN NUMBER)
IS
updated_cnt NUMBER;
BEGIN
UPDATE emp SET sal = new_salary
WHERE empno = empno;
SELECT @updated_cnt = COUNT(*) FROM emp WHERE empno = empno;
IF updated_cnt = 0 BEGIN
RAISERROR('-20101 Employee not found', 16, 1);
END
COMMIT;
END;
/
|
CREATE OR REPLACE PROCEDURE update_emp_salary(
empno IN NUMBER,
new_salary IN NUMBER)
IS
updated_cnt NUMBER;
BEGIN
UPDATE emp SET sal = new_salary
WHERE empno = empno;
SELECT COUNT(*) INTO updated_cnt FROM emp WHERE empno = empno;
IF updated_cnt = 0 THEN
RAISE_APPLICATION_ERROR(-20101, 'Employee not found');
END IF;
COMMIT;
END;
/
|
DECLARE
@l_id FLOAT := 1;
l_name dbo.VARCHAR2(20);
l_exception_msg EXCEPTION;
BEGIN
SELECT @l_name = name
FROM user_info
WHERE id = @l_id;
EXCEPTION
WHEN l_exception_msg THEN
RAISERROR('-20000 Error occurred while fetching name', 16, 1);
END;
|
DECLARE
l_id NUMBER := 1;
l_name VARCHAR2(20);
l_exception_msg EXCEPTION;
BEGIN
SELECT name
INTO l_name
FROM user_info
WHERE id = l_id;
EXCEPTION
WHEN l_exception_msg THEN
RAISE_APPLICATION_ERROR(-20000, 'Error occurred while fetching name');
END;
|
CREATE PROCEDURE get_employee_salary(@employee_id IN AS NUMBER, OUT salary OUT NUMBER)
AS
BEGIN
SELECT @salary = salary
FROM employees
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @salary = NULL;
END
END;
|
CREATE PROCEDURE get_employee_salary(employee_id IN NUMBER, OUT salary OUT NUMBER)
AS
BEGIN
SELECT salary INTO salary
FROM employees
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
salary := NULL;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name
FROM emp
WHERE emp_no = 7369;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
END
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
BEGIN
SELECT emp_id, emp_name
INTO v_emp_id, v_emp_name
FROM emp
WHERE emp_no = 7369;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_emp_id = employee_id, @v_emp_name = first_name FROM employees WHERE employee_id = 1;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee id: ' + isnull(@v_emp_id, ''));
END
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(20);
BEGIN
SELECT employee_id, first_name INTO v_emp_id, v_emp_name FROM employees WHERE employee_id = 1;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee id: ' || v_emp_id);
END;
|
DECLARE
@v_name VARCHAR(50);
v_salary NUMBER;
v_result NUMBER;
BEGIN
SELECT @v_name = ename, @v_salary = sal FROM emp WHERE empno = 7839;
SET @v_result = v_salary + 1000;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.PUT_LINE('Salary with 1000$ raise: ' + isnull(v_result, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
|
DECLARE
v_name VARCHAR2(50);
v_salary NUMBER;
v_result NUMBER;
BEGIN
SELECT ename, sal INTO v_name, v_salary FROM emp WHERE empno = 7839;
v_result := v_salary + 1000;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Salary with 1000$ raise: ' || v_result);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('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 NUMBER, param2 IN VARCHAR2) AS
BEGIN
/* Do something */
IF param1 > 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Param1 is greater than 10');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Param1 is less than or equal to 10');
END
DECLARE cursor1 CURSOR FOR SELECT * FROM my_table WHERE id = param1;
DECLARE rec CURSOR FOR cursor1 OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(@rec_name);
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_proc (param1 IN NUMBER, param2 IN VARCHAR2) AS
BEGIN
/* Do something */
IF param1 > 10 THEN
DBMS_OUTPUT.PUT_LINE('Param1 is greater than 10');
ELSE
DBMS_OUTPUT.PUT_LINE('Param1 is less than or equal to 10');
END IF;
DECLARE cursor1 CURSOR FOR SELECT * FROM my_table WHERE id = param1;
FOR rec IN cursor1 LOOP
DBMS_OUTPUT.PUT_LINE(rec.name);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('inc_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE inc_employee_salary;
GO
CREATE PROCEDURE inc_employee_salary(@emp_id IN AS NUMBER)
AS
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = emp_id;
IF @@ERROR <> 0 BEGIN
RAISERROR('-20101 Employee not found', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE inc_employee_salary(emp_id IN NUMBER)
AS
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = emp_id;
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20101, 'Employee not found');
END IF;
END;
|
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_id = id, @v_name = name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(@v_id, '') + ', ' + isnull(v_name, ''));
BEGIN
UPDATE employees SET name = 'John' WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Updated employee name to John');
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
/
|
DECLARE
v_id NUMBER;
v_name VARCHAR2(20);
BEGIN
SELECT id, name INTO v_id, v_name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_id || ', ' || v_name);
BEGIN
UPDATE employees SET name = 'John' WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Updated employee name to John');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
/
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@employee_id IN AS NUMBER,
new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
employee_id IN NUMBER,
new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE PROCEDURE get_emp_salary(@emp_id IN AS NUMBER) AS
emp_salary NUMBER;
BEGIN
SELECT @emp_salary = salary FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END
END;
|
CREATE PROCEDURE get_emp_salary(emp_id IN NUMBER) AS
emp_salary NUMBER;
BEGIN
SELECT salary INTO emp_salary FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that ID.');
END;
|
BEGIN
DECLARE rec CURSOR FOR SELECT * FROM employees ORDER BY salary DESC; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_first_name, '') + ' ' + isnull(@rec_last_name, '') + ': $' + ISNULL(FORMAT(@rec_salary, 'N0'), ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
BEGIN
FOR rec IN (SELECT * FROM employees ORDER BY salary DESC) LOOP
DBMS_OUTPUT.PUT_LINE(rec.first_name || ' ' || rec.last_name || ': $' || TO_CHAR(rec.salary, '999,999'));
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
IF OBJECT_ID('process_orders', 'P') IS NOT NULL
DROP PROCEDURE process_orders;
GO
CREATE PROCEDURE process_orders
AS
BEGIN
SET NOCOUNT ON;
DECLARE r CURSOR FOR SELECT * FROM orders; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
IF @r_status = 'new' BEGIN
UPDATE orders SET status = 'processing' WHERE id = @r_id;
DBMS_OUTPUT.PUT_LINE('Processing order ' + isnull(@r_id, ''));
END
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
END;
DECLARE
@v_cust_id FLOAT;
v_order_id NUMBER;
BEGIN
SET @v_cust_id = 100;
SELECT @v_order_id = order_id FROM orders WHERE cust_id = @v_cust_id;
IF v_order_id IS NULL BEGIN
RAISERROR('-20001 No order found for customer ' + isnull(@v_cust_id, ''), 16, 1);
END
-- Pr... ***
END;
CREATE FUNCTION get_order_total(@order_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT SUM(quantity * unit_price) FROM order_items WHERE order_id = order_id
);
END;
|
CREATE OR REPLACE PROCEDURE process_orders
AS
BEGIN
FOR r IN (SELECT * FROM orders) LOOP
IF r.status = 'new' THEN
UPDATE orders SET status = 'processing' WHERE id = r.id;
DBMS_OUTPUT.PUT_LINE('Processing order ' || r.id);
END IF;
END LOOP;
END;
DECLARE
v_cust_id NUMBER;
v_order_id NUMBER;
BEGIN
v_cust_id := 100;
SELECT order_id INTO v_order_id FROM orders WHERE cust_id = v_cust_id;
IF v_order_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No order found for customer ' || v_cust_id);
END IF;
-- Process the order
END;
CREATE FUNCTION get_order_total(order_id IN NUMBER) RETURN NUMBER
AS
BEGIN
RETURN (
SELECT SUM(quantity * unit_price) FROM order_items WHERE order_id = order_id
);
END;
|
CREATE PROCEDURE GetEmployeeDetails(@emp_id IN AS NUMBER) AS
BEGIN
SELECT * FROM employees WHERE employee_id = emp_id;
END;
CREATE FUNCTION GetEmployeeName(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN (SELECT isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = emp_id);
END;
BEGIN
/* *** truct using IF statement */
IF dbo.GetEmployeeName(10) IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee found!');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found!');
END
END;
BEGIN
/* *** enario using EXCEPTION block */
BEGIN
/* *** could potentially cause an error */
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = 10;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred!');
END;
END;
|
CREATE PROCEDURE GetEmployeeDetails(emp_id IN NUMBER) AS
BEGIN
SELECT * FROM employees WHERE employee_id = emp_id;
END;
CREATE FUNCTION GetEmployeeName(emp_id IN NUMBER) RETURN VARCHAR2 AS
BEGIN
RETURN (SELECT first_name || ' ' || last_name FROM employees WHERE employee_id = emp_id);
END;
BEGIN
/* Control flow construct using IF statement */
IF GetEmployeeName(10) IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee found!');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found!');
END IF;
END;
BEGIN
/* Error handling scenario using EXCEPTION block */
BEGIN
/* Do something that could potentially cause an error */
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = 10;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred!');
END;
END;
|
CREATE PROCEDURE sp_insert_employee(
@p_name VARCHAR(4000),
@p_salary FLOAT,
@p_deptno FLOAT
) AS
*** to store temporary values
emp_id SET @NUMBER = employee_seq.NEXTVAL;
BEGIN
*** oyee into the database
INSERT INTO employees (
employee_id,
name,
salary,
dept_no
) VALUES (
emp_id,
p_name,
p_salary,
p_deptno
);
*** s to the database
COMMIT;
*** n if the insertion fails
IF emp_id IS NULL BEGIN
RAISERROR('-20000 Failed to insert new employee', 16, 1);
END
END sp_insert_employee;
|
CREATE PROCEDURE sp_insert_employee(
p_name VARCHAR2,
p_salary NUMBER,
p_deptno NUMBER
) AS
-- Declare variables to store temporary values
emp_id NUMBER := employee_seq.NEXTVAL;
BEGIN
-- Insert a new employee into the database
INSERT INTO employees (
employee_id,
name,
salary,
dept_no
) VALUES (
emp_id,
p_name,
p_salary,
p_deptno
);
-- Commit the changes to the database
COMMIT;
-- Raise an exception if the insertion fails
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Failed to insert new employee');
END IF;
END sp_insert_employee;
|
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL
DROP FUNCTION get_employee_details;
GO
CREATE FUNCTION get_employee_details (@p_empid IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
rc SYS_REFCURSOR;
BEGIN
SELECT * FROM employees WHERE employee_id = p_empid;
RETURN rc;
END;
DECLARE
@emp_id FLOAT := 123;
emp_details SYS_REFCURSOR;
BEGIN
SET @emp_details = dbo.get_employee_details(@emp_id);
WHILE 1=1 BEGIN
FETCH emp_details INTO @emp_name, @emp_salary, @emp_deptno;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
DBMS_OUTPUT.PUT_LINE('Employee Department: ' + isnull(emp_deptno, ''));
END;
CLOSE emp_details;
END;
|
CREATE OR REPLACE FUNCTION get_employee_details (p_empid IN NUMBER) RETURN SYS_REFCURSOR IS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR
SELECT * FROM employees WHERE employee_id = p_empid;
RETURN rc;
END;
DECLARE
emp_id NUMBER := 123;
emp_details SYS_REFCURSOR;
BEGIN
emp_details := get_employee_details(emp_id);
LOOP
FETCH emp_details INTO emp_name, emp_salary, emp_deptno;
EXIT WHEN emp_details%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
DBMS_OUTPUT.PUT_LINE('Employee Department: ' || emp_deptno);
END LOOP;
CLOSE emp_details;
END;
|
DECLARE
@v_deptno FLOAT;
v_dname dbo.VARCHAR2(30);
v_loc dbo.VARCHAR2(20);
BEGIN
SELECT @v_deptno = deptno, @v_dname = dname, @v_loc = loc
FROM departments
WHERE deptno = 20;
DBMS_OUTPUT.PUT_LINE('Department ' + isnull(@v_deptno, '') + ', ' + isnull(v_dname, '') + ', ' + isnull(v_loc, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
/
|
DECLARE
v_deptno NUMBER;
v_dname VARCHAR2(30);
v_loc VARCHAR2(20);
BEGIN
SELECT deptno, dname, loc
INTO v_deptno, v_dname, v_loc
FROM departments
WHERE deptno = 20;
DBMS_OUTPUT.PUT_LINE('Department ' || v_deptno || ', ' || v_dname || ', ' || v_loc);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
/
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = emp_id
);
END;
/
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(100);
IF @emp_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = emp_id
);
END;
/
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(100);
IF emp_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
BEGIN
SELECT @v_empno = empno, @v_ename = ename
FROM emp
WHERE empno = 7369;
IF @@FETCH_STATUS <> 0 BEGIN
SELECT @v_empno = 0, @v_ename = 'not found'
;
END
END;
DBMS_OUTPUT.PUT_LINE('Empno: ' + isnull(@v_empno, ''));
DBMS_OUTPUT.PUT_LINE('Ename: ' + isnull(v_ename, ''));
END;
/
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
BEGIN
SELECT empno, ename
INTO v_empno, v_ename
FROM emp
WHERE empno = 7369;
EXCEPTION
WHEN NO_DATA_FOUND THEN
SELECT 0, 'not found'
INTO v_empno, v_ename;
END;
DBMS_OUTPUT.PUT_LINE('Empno: ' || v_empno);
DBMS_OUTPUT.PUT_LINE('Ename: ' || v_ename);
END;
/
|
DECLARE
@v_empid FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empid = empid, @v_salary = salary
FROM emp
WHERE empid = 7839;
IF v_salary > 10000
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empid, '') + ' is in high salary range');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empid, '') + ' is in low salary range');
END
END;
|
DECLARE
v_empid NUMBER;
v_salary NUMBER;
BEGIN
SELECT empid, salary
INTO v_empid, v_salary
FROM emp
WHERE empid = 7839;
IF v_salary > 10000
THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empid || ' is in high salary range');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empid || ' is in low salary range');
END IF;
END;
|
DECLARE
@l_emp_id FLOAT;
BEGIN
SELECT @l_emp_id = emp_id
FROM emp
WHERE last_name = 'Scott';
END;
*** using bind variables
DECLARE
@l_emp_id FLOAT;
l_emp_name dbo.VARCHAR2(20);
BEGIN
INSERT INTO emp (emp_id, emp_name)
VALUES (:emp_id, :emp_name);
END;
*** late bonus
CREATE FUNCTION bonus (@salary FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @l_bonus FLOAT;
SET @l_bonus = @salary * 0.1;
RETURN @l_bonus;
END;
*** truct
DECLARE
@l_salary FLOAT;
BEGIN
SELECT @l_salary = salary
FROM emp
WHERE emp_id = 10;
IF @l_salary > 10000 BEGIN
DBMS_OUTPUT.put_line('High salary');
END
ELSE IF @l_salary > 5000 BEGIN
DBMS_OUTPUT.put_line('Medium salary');
END
ELSE BEGIN
DBMS_OUTPUT.put_line('Low salary');
END
END;
*** enario
DECLARE
@l_emp_id FLOAT;
BEGIN
SELECT @l_emp_id = emp_id
FROM emp
WHERE last_name = 'DoesNotExist';
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.put_line('Employee not found');
END
END;
|
DECLARE
l_emp_id NUMBER;
BEGIN
SELECT emp_id
INTO l_emp_id
FROM emp
WHERE last_name = 'Scott';
END;
-- INSERT statement using bind variables
DECLARE
l_emp_id NUMBER;
l_emp_name VARCHAR2(20);
BEGIN
INSERT INTO emp (emp_id, emp_name)
VALUES (:emp_id, :emp_name);
END;
-- Function to calculate bonus
CREATE FUNCTION bonus (salary NUMBER) RETURN NUMBER IS
l_bonus NUMBER;
BEGIN
l_bonus := salary * 0.1;
RETURN l_bonus;
END bonus;
-- Control flow construct
DECLARE
l_salary NUMBER;
BEGIN
SELECT salary
INTO l_salary
FROM emp
WHERE emp_id = 10;
IF l_salary > 10000 THEN
DBMS_OUTPUT.put_line('High salary');
ELSIF l_salary > 5000 THEN
DBMS_OUTPUT.put_line('Medium salary');
ELSE
DBMS_OUTPUT.put_line('Low salary');
END IF;
END;
-- Error handling scenario
DECLARE
l_emp_id NUMBER;
BEGIN
SELECT emp_id
INTO l_emp_id
FROM emp
WHERE last_name = 'DoesNotExist';
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line('Employee not found');
END;
|
CREATE PROCEDURE get_employee_details(@p_emp_id IN AS NUMBER)
AS
BEGIN
*** tails from the database
SELECT @emp_details = *
FROM employees
WHERE emp_id = p_emp_id;
*** oyee exists
IF emp_details.emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
*** ee details
RETURN emp_details;
END;
|
CREATE PROCEDURE get_employee_details(p_emp_id IN NUMBER)
AS
BEGIN
-- Fetch employee details from the database
SELECT * INTO emp_details
FROM employees
WHERE emp_id = p_emp_id;
-- Check if the employee exists
IF emp_details.emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Return the employee details
RETURN emp_details;
END;
|
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL
DROP PROCEDURE sp_update_employee;
GO
CREATE PROCEDURE sp_update_employee(@p_employee_id IN AS NUMBER, p_new_salary IN NUMBER, p_error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id;
set @p_error_code = '0';
EXCEPTION
WHEN OTHERS THEN
set @p_error_code = '1';
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE sp_update_employee(p_employee_id IN NUMBER, p_new_salary IN NUMBER, p_error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id;
p_error_code := '0';
EXCEPTION
WHEN OTHERS THEN
p_error_code := '1';
RAISE;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary 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
RETURN salary;
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
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
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
SET @salary = salary * 1.10;
dbo.update_employee_salary(@emp_id, salary);
EXCEPTION
WHEN OTHERS THEN
*** continue processing.
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
RETURN salary;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
salary := salary * 1.10;
update_employee_salary(emp_id, salary);
EXCEPTION
WHEN OTHERS THEN
-- Log the error and continue processing.
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
END;
|
DECLARE
@v_deptno FLOAT;
v_salary NUMBER;
v_job_title dbo.VARCHAR2(50);
BEGIN
SELECT
@v_job_title = job_title,
@v_salary = salary
FROM
employees
WHERE
dept_no = @v_deptno;
IF v_salary < 10000
BEGIN
RAISERROR('-20001 Salary less than 10000', 16, 1);
END
ELSE BEGIN
UPDATE employees SET job_title = v_job_title WHERE dept_no = @v_deptno;
END
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_deptno NUMBER;
v_salary NUMBER;
v_job_title VARCHAR2(50);
BEGIN
SELECT
job_title,
salary
INTO
v_job_title,
v_salary
FROM
employees
WHERE
dept_no = v_deptno;
IF v_salary < 10000
THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary less than 10000');
ELSE
UPDATE employees SET job_title = v_job_title WHERE dept_no = v_deptno;
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('p_demo', 'P') IS NOT NULL
DROP PROCEDURE p_demo;
GO
CREATE PROCEDURE p_demo(@empno IN AS NUMBER, sal IN OUT NUMBER)
AS
BEGIN
UPDATE emp SET sal = :sal WHERE empno = :empno;
set @sal = sal * 1.10;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE p_demo(empno IN NUMBER, sal IN OUT NUMBER)
AS
BEGIN
UPDATE emp SET sal = :sal WHERE empno = :empno;
sal := sal * 1.10;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
WHEN OTHERS THEN
RAISE;
END;
/
|
BEGIN
DECLARE c1 CURSOR FOR SELECT * FROM customers; OPEN c1;
FETCH c1 INTO;
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE customers SET balance = balance + 100
WHERE customer_id = @c1_customer_id;
FETCH c1 INTO;
END;
CLOSE c1;
DEALLOCATE c1;
COMMIT;
END; | BEGIN
FOR c1 IN (SELECT * FROM customers) LOOP
UPDATE customers SET balance = balance + 100
WHERE customer_id = c1.customer_id;
END LOOP;
COMMIT;
END; |
CREATE PROCEDURE get_customer_orders(@customer_id IN AS NUMBER) AS
orders CURSOR FOR SELECT * FROM orders WHERE customer_id = customer_id;
BEGIN
OPEN orders;
WHILE 1=1 BEGIN
FETCH orders INTO @order_record;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(order_record.order_id);
END;
CLOSE orders;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(customer_id, ''));
END
END;
|
CREATE PROCEDURE get_customer_orders(customer_id IN NUMBER) AS
orders CURSOR FOR SELECT * FROM orders WHERE customer_id = customer_id;
BEGIN
OPEN orders;
LOOP
FETCH orders INTO order_record;
EXIT WHEN orders%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(order_record.order_id);
END LOOP;
CLOSE orders;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || customer_id);
END;
|
[
DECLARE
v_emp_id NUMBER := 100;
BEGIN
SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id;
dbms_output.put_line('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employee found with ID ' || v_emp_id);
END;
] | `
DECLARE
v_emp_id NUMBER := 100;
BEGIN
SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id;
dbms_output.put_line('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employee found with ID ' || v_emp_id);
END;
` |
CREATE PROCEDURE get_customer_orders (@p_customer_id IN AS NUMBER)
AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
DECLARE rec CURSOR FOR c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@rec_order_id, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@rec_order_date, ''));
DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(@rec_total_amount, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(p_customer_id, ''));
END
END;
|
CREATE PROCEDURE get_customer_orders (p_customer_id IN NUMBER)
AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
FOR rec IN c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' || rec.order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || rec.order_date);
DBMS_OUTPUT.PUT_LINE('Total Amount: ' || rec.total_amount);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || p_customer_id);
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@p_employee_id IN AS NUMBER,
p_salary IN NUMBER,
p_dept_id IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = p_salary,
dept_id = p_dept_id
WHERE employee_id = p_employee_id;
COMMIT;
END;
DECLARE
@e_salary FLOAT;
BEGIN
SELECT @e_salary = salary
FROM employees
WHERE employee_id = 20;
IF @e_salary < 60000 BEGIN
CALL update_employee(20, 70000, 20);
END
ELSE BEGIN
RAISERROR('-20001 Salary cannot be updated', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
p_employee_id IN NUMBER,
p_salary IN NUMBER,
p_dept_id IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = p_salary,
dept_id = p_dept_id
WHERE employee_id = p_employee_id;
COMMIT;
END;
DECLARE
e_salary NUMBER;
BEGIN
SELECT salary INTO e_salary
FROM employees
WHERE employee_id = 20;
IF e_salary < 60000 THEN
CALL update_employee(20, 70000, 20);
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be updated');
END IF;
END;
|
CREATE PROCEDURE GetCustomerOrders (
@customer_id IN AS NUMBER
) AS
CURSOR c1 IS
SELECT order_id, order_date
FROM orders
WHERE customer_id = customer_id;
BEGIN
OPEN c1;
WHILE 1=1 BEGIN
FETCH c1
INTO @order_id, @order_date;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(order_id, '') + ', Order Date: ' + isnull(order_date, ''));
END;
CLOSE c1;
END;
|
CREATE PROCEDURE GetCustomerOrders (
customer_id IN NUMBER
) AS
CURSOR c1 IS
SELECT order_id, order_date
FROM orders
WHERE customer_id = customer_id;
BEGIN
OPEN c1;
LOOP
FETCH c1
INTO order_id, order_date;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_id || ', Order Date: ' || order_date);
END LOOP;
CLOSE c1;
END;
|
DECLARE
@l_emp_id FLOAT;
l_emp_name dbo.VARCHAR2(100);
l_salary NUMBER;
BEGIN
SELECT @l_emp_id = emp_id, @l_emp_name = emp_name, @l_salary = salary
FROM emp
WHERE emp_id = 100;
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
|
DECLARE
l_emp_id NUMBER;
l_emp_name VARCHAR2(100);
l_salary NUMBER;
BEGIN
SELECT emp_id, emp_name, salary
INTO l_emp_id, l_emp_name, l_salary
FROM emp
WHERE emp_id = 100;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
|
CREATE PROCEDURE get_customer_info(@customer_id IN AS NUMBER) IS
customer_name dbo.VARCHAR2(20);
customer_address dbo.VARCHAR2(50);
BEGIN
*** e to retrieve customer information
SELECT @customer_name = customer_name, @customer_address = customer_address
FROM customers
WHERE customer_id = customer_id;
*** r information
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(customer_name, ''));
DBMS_OUTPUT.PUT_LINE('Customer Address: ' + isnull(customer_address, ''));
*** that may occur
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Error: No customer found with the given ID.');
END
END;
|
CREATE PROCEDURE get_customer_info(customer_id IN NUMBER) IS
customer_name VARCHAR2(20);
customer_address VARCHAR2(50);
BEGIN
-- Query the database to retrieve customer information
SELECT customer_name, customer_address
INTO customer_name, customer_address
FROM customers
WHERE customer_id = customer_id;
-- Print the customer information
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || customer_name);
DBMS_OUTPUT.PUT_LINE('Customer Address: ' || customer_address);
EXCEPTION
-- Handle any errors that may occur
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: No customer found with the given ID.');
END;
|
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary(@employee_id IN AS NUMBER) AS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE(salary);
END;
|
CREATE OR REPLACE PROCEDURE get_employee_salary(employee_id IN NUMBER) AS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE(salary);
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
*** value error
WHEN OTHERS THEN
-- Ha... ***
END;
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
IF salary IS NOT NULL BEGIN
dbo.update_employee_salary(@emp_id, salary * 1.10);
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
-- Handle duplicate value error
WHEN OTHERS THEN
-- Handle other errors
END;
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
IF salary IS NOT NULL THEN
update_employee_salary(emp_id, salary * 1.10);
END IF;
END;
|
DECLARE
@emp_id DECIMAL(19,4) /* Use -meta option employees.employee_id%TYPE */;
salary employees.salary%TYPE;
BEGIN
SELECT @emp_id = employee_id, @salary = salary FROM employees WHERE last_name = 'Smith';
IF salary < 10000 BEGIN
SET @salary = 10000;
UPDATE employees SET salary = salary WHERE employee_id = @emp_id;
END
END; | DECLARE
emp_id employees.employee_id%TYPE;
salary employees.salary%TYPE;
BEGIN
SELECT employee_id, salary INTO emp_id, salary FROM employees WHERE last_name = 'Smith';
IF salary < 10000 THEN
salary := 10000;
UPDATE employees SET salary = salary WHERE employee_id = emp_id;
END IF;
END; |
IF OBJECT_ID('p_emp_details', 'P') IS NOT NULL
DROP PROCEDURE p_emp_details;
GO
CREATE PROCEDURE p_emp_details(@empid IN AS NUMBER) IS
emp_name VARCHAR(20);
salary NUMBER;
BEGIN
SELECT @emp_name = ename, @salary = sal FROM emp WHERE empno = empid;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
CREATE OR REPLACE PROCEDURE p_emp_details(empid IN NUMBER) IS
emp_name VARCHAR(20);
salary NUMBER;
BEGIN
SELECT ename, sal INTO emp_name, salary FROM emp WHERE empno = empid;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@l_cursor SYS_REFCURSOR;
v_num NUMBER;
BEGIN
SELECT * FROM employees;
FETCH l_cursor INTO @l_num;
IF l_num > 0 BEGIN
WHILE 1=1 BEGIN
FETCH l_cursor INTO @l_num;
EXIT WHEN l_num IS NULL;
*** ere
END;
END
CLOSE l_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
END
*** und exception
WHEN OTHERS THEN
*** ception
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END; | DECLARE
l_cursor SYS_REFCURSOR;
v_num NUMBER;
BEGIN
OPEN l_cursor FOR
SELECT * FROM employees;
FETCH l_cursor INTO l_num;
IF l_num > 0 THEN
LOOP
FETCH l_cursor INTO l_num;
EXIT WHEN l_num IS NULL;
-- Process the row here
END LOOP;
END IF;
CLOSE l_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Handle no data found exception
WHEN OTHERS THEN
-- Handle generic exception
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END; |
DECLARE
@v_name VARCHAR(100);
v_empno NUMBER;
BEGIN
SELECT @v_name = ename, @v_empno = empno
FROM emp
WHERE job = 'CLERK';
dbms_output.put_line('Employee name: ' + isnull(@v_name, ''));
dbms_output.put_line('Employee number: ' + isnull(v_empno, ''));
IF v_empno > 100
BEGIN
dbms_output.put_line('Employee number is greater than 100.');
END
ELSE BEGIN
dbms_output.put_line('Employee number is less than or equal to 100.');
END
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No employees found with the job title CLERK.');
END
END; | DECLARE
v_name VARCHAR2(100);
v_empno NUMBER;
BEGIN
SELECT ename, empno
INTO v_name, v_empno
FROM emp
WHERE job = 'CLERK';
dbms_output.put_line('Employee name: ' || v_name);
dbms_output.put_line('Employee number: ' || v_empno);
IF v_empno > 100
THEN
dbms_output.put_line('Employee number is greater than 100.');
ELSE
dbms_output.put_line('Employee number is less than or equal to 100.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employees found with the job title CLERK.');
END; |
IF OBJECT_ID('usp_demo', 'P') IS NOT NULL
DROP PROCEDURE usp_demo;
GO
CREATE PROCEDURE usp_demo
(
@p1 IN AS NUMBER,
p2 IN VARCHAR2
)
IS
l_var dbo.VARCHAR2(20);
BEGIN
SELECT @l_var = MIN(name) FROM employees WHERE dept_id = p1;
DBMS_OUTPUT.PUT_LINE(isnull(l_var, '') + ' ' + isnull(p2, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END usp_demo;
DECLARE
@v_num FLOAT;
v_str dbo.VARCHAR2(20);
v_emp_id PLS_INTEGER;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SET @v_num = 20;
SET @v_str = 'Test';
EXECUTE sp_executesql @'INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (' || isnull(@v_num, '') + ', ' + isnull(v_str, '') + ', 10)';
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_name = v_str;
DBMS_OUTPUT.PUT_LINE('emp_id: ' + isnull(v_emp_id, '') + ' ' + 'emp_name: ' + isnull(v_emp_name, ''));
END;
BEGIN
IF GETDATE() > '2023-01-01' BEGIN
DBMS_OUTPUT.PUT_LINE('It is after 2023-01-01'); END
ELSE IF GETDATE() < '2023-01-01' BEGIN
DBMS_OUTPUT.PUT_LINE('It is before 2023-01-01'); END
ELSE
DBMS_OUTPUT.PUT_LINE('It is 2023-01-01'); END
END;
|
CREATE OR REPLACE PROCEDURE usp_demo
(
p1 IN NUMBER,
p2 IN VARCHAR2
)
IS
l_var VARCHAR2(20);
BEGIN
SELECT MIN(name) INTO l_var FROM employees WHERE dept_id = p1;
DBMS_OUTPUT.PUT_LINE(l_var || ' ' || p2);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END usp_demo;
DECLARE
v_num NUMBER;
v_str VARCHAR2(20);
v_emp_id PLS_INTEGER;
v_emp_name VARCHAR2(20);
BEGIN
v_num := 20;
v_str := 'Test';
EXECUTE IMMEDIATE 'INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (' || v_num || ', ' || v_str || ', 10)';
SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_name = v_str;
DBMS_OUTPUT.PUT_LINE('emp_id: ' || v_emp_id || ' ' || 'emp_name: ' || v_emp_name);
END;
BEGIN
CASE WHEN SYSDATE > '2023-01-01' THEN
DBMS_OUTPUT.PUT_LINE('It is after 2023-01-01');
WHEN SYSDATE < '2023-01-01' THEN
DBMS_OUTPUT.PUT_LINE('It is before 2023-01-01');
ELSE
DBMS_OUTPUT.PUT_LINE('It is 2023-01-01');
END CASE;
END;
|
-- Cr... ***
CREATE TABLE employees(
id FLOAT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
-- In... ***
INSERT INTO employees VALUES (1, 'John Doe', 10000);
INSERT INTO employees VALUES (2, 'Jane Smith', 12000);
INSERT INTO employees VALUES (3, 'Bill Gates', 100000);
*** rocedure to get the average salary
CREATE PROCEDURE get_avg_salary(
@OUT avg_salary AS NUMBER
) AS
BEGIN
SELECT @avg_salary = AVG(salary) FROM employees;
END;
*** to get the employee with the highest salary
CREATE FUNCTION get_max_salary_employee() RETURNS FLOAT AS
BEGIN
SELECT id FROM employees ORDER BY salary DESC;
RETURN NULL;
END;
*** rocedure and function
DECLARE
@avg_salary FLOAT;
max_salary_employee NUMBER;
BEGIN
dbo.get_avg_salary(:avg_salary);
SET @max_salary_employee = dbo.get_max_salary_employee();
DBMS_OUTPUT.PUT_LINE('Average salary: ' + isnull(@avg_salary, ''));
DBMS_OUTPUT.PUT_LINE('Employee with the highest salary: ' + isnull(max_salary_employee, ''));
END;
-- Ha... ***
BEGIN
/* *** y raise an error */
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
-- Create a table
CREATE TABLE employees(
id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
salary NUMBER(10,2) NOT NULL
);
-- Insert some data
INSERT INTO employees VALUES (1, 'John Doe', 10000);
INSERT INTO employees VALUES (2, 'Jane Smith', 12000);
INSERT INTO employees VALUES (3, 'Bill Gates', 100000);
-- Create a stored procedure to get the average salary
CREATE PROCEDURE get_avg_salary(
OUT avg_salary NUMBER
) AS
BEGIN
SELECT AVG(salary) INTO avg_salary FROM employees;
END;
-- Create a function to get the employee with the highest salary
CREATE FUNCTION get_max_salary_employee() RETURN NUMBER AS
BEGIN
SELECT id FROM employees ORDER BY salary DESC;
END;
-- Call the stored procedure and function
DECLARE
avg_salary NUMBER;
max_salary_employee NUMBER;
BEGIN
get_avg_salary(:avg_salary);
max_salary_employee := get_max_salary_employee();
DBMS_OUTPUT.PUT_LINE('Average salary: ' || avg_salary);
DBMS_OUTPUT.PUT_LINE('Employee with the highest salary: ' || max_salary_employee);
END;
-- Handle errors
BEGIN
/* Some code that may raise an error */
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(100);
IF @emp_salary IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, ''));
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(100);
IF emp_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
END;
|
IF OBJECT_ID('calculate_bonus', 'P') IS NOT NULL
DROP PROCEDURE calculate_bonus;
GO
CREATE PROCEDURE calculate_bonus
(
@employee_id IN AS NUMBER
)
IS
bonus NUMBER;
BEGIN
SELECT @bonus = bonus
FROM employee_bonuses
WHERE employee_id = employee_id;
IF bonus IS NULL BEGIN
raiserror('-20001 No bonus found for employee.', 16, 1);
END
UPDATE employees
SET salary = salary + bonus
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
raiserror('-20002 An error occurred while calculating bonus.', 16, 1);
END;
|
CREATE OR REPLACE PROCEDURE calculate_bonus
(
employee_id IN NUMBER
)
IS
bonus NUMBER;
BEGIN
SELECT bonus
INTO bonus
FROM employee_bonuses
WHERE employee_id = employee_id;
IF bonus IS NULL THEN
raise_application_error(-20001, 'No bonus found for employee.');
END IF;
UPDATE employees
SET salary = salary + bonus
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
raise_application_error(-20002, 'An error occurred while calculating bonus.');
END;
|
IF OBJECT_ID('validate_user', 'P') IS NOT NULL
DROP PROCEDURE validate_user;
GO
CREATE PROCEDURE validate_user (
@p_username IN AS VARCHAR2,
p_password IN VARCHAR2,
OUT p_valid BOOLEAN
) AS
BEGIN
SELECT @p_valid = COUNT(*)
FROM users
WHERE username = p_username
AND password = p_password;
IF p_valid > 0 BEGIN
set @p_valid = TRUE;
END
ELSE BEGIN
set @p_valid = FALSE;
END
END;
/
|
CREATE OR REPLACE PROCEDURE validate_user (
p_username IN VARCHAR2,
p_password IN VARCHAR2,
OUT p_valid BOOLEAN
) AS
BEGIN
SELECT COUNT(*) INTO p_valid
FROM users
WHERE username = p_username
AND password = p_password;
IF p_valid > 0 THEN
p_valid := TRUE;
ELSE
p_valid := FALSE;
END IF;
END;
/
|
IF OBJECT_ID('random_procedure', 'P') IS NOT NULL
DROP PROCEDURE random_procedure;
GO
CREATE PROCEDURE random_procedure (
@param1 IN AS VARCHAR2,
param2 IN NUMBER,
param3 OUT VARCHAR2
) AS
BEGIN
-- So... ***
set @param3 = 'hello';
END;
/
|
CREATE OR REPLACE PROCEDURE random_procedure (
param1 IN VARCHAR2,
param2 IN NUMBER,
param3 OUT VARCHAR2
) AS
BEGIN
-- Some logic here
param3 := 'hello';
END;
/
|
DECLARE
@x INTEGER := 1;
y SET @INTEGER = 2;
max_value INTEGER;
cust_id SET @NUMBER = 12345;
cust_name dbo.VARCHAR2(50);
BEGIN
SET @max_value = GREATEST(@x, y);
SELECT @cust_name = name FROM customers WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('name = ' + isnull(cust_name, ''));
IF max_value > 1 BEGIN
SET @x = @x + max_value;
SET @y = y + max_value;
END
IF max_value < 3 BEGIN
RAISERROR('-20001 max_value less than 3', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('x = ' + isnull(@x, ''));
DBMS_OUTPUT.PUT_LINE('y = ' + isnull(y, ''));
EXCEPTION
WHEN application_error THEN
DBMS_OUTPUT.PUT_LINE('error occurred');
END;
|
DECLARE
x INTEGER := 1;
y INTEGER := 2;
max_value INTEGER;
cust_id NUMBER := 12345;
cust_name VARCHAR2(50);
BEGIN
max_value := GREATEST(x, y);
SELECT name INTO cust_name FROM customers WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('name = ' || cust_name);
IF max_value > 1 THEN
x := x + max_value;
y := y + max_value;
END IF;
IF max_value < 3 THEN
RAISE_APPLICATION_ERROR(-20001, 'max_value less than 3');
END IF;
DBMS_OUTPUT.PUT_LINE('x = ' || x);
DBMS_OUTPUT.PUT_LINE('y = ' || y);
EXCEPTION
WHEN application_error THEN
DBMS_OUTPUT.PUT_LINE('error occurred');
END;
|