sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
*** e to store the result
DECLARE @v_result FLOAT;
*** rocedure
IF OBJECT_ID('calculate_result', 'P') IS NOT NULL
DROP PROCEDURE calculate_result;
GO
CREATE PROCEDURE calculate_result(
*** ameters
@p_operand1 IN AS NUMBER,
p_operand2 IN NUMBER
)
AS
BEGIN
*** ult
set @v_result = p_operand1 + p_operand2;
END;
*** rocedure
CALL calculate_result(1, 2);
-- Pr... ***
DBMS_OUTPUT.PUT_LINE(v_result);
|
-- Declare a variable to store the result
DECLARE v_result NUMBER;
-- Create a stored procedure
CREATE OR REPLACE PROCEDURE calculate_result(
-- Declare input parameters
p_operand1 IN NUMBER,
p_operand2 IN NUMBER
)
AS
BEGIN
-- Calculate the result
v_result := p_operand1 + p_operand2;
END;
-- Call the stored procedure
CALL calculate_result(1, 2);
-- Print the result
DBMS_OUTPUT.PUT_LINE(v_result);
|
DECLARE
@v_name VARCHAR(20);
v_salary NUMBER;
BEGIN
SELECT @v_name = name, @v_salary = salary FROM employees WHERE employee_id = 100;
IF v_salary > 5000 BEGIN
RAISERROR('-20001 Salary is too high', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@v_name, '') + ', Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100');
END
END;
|
DECLARE
v_name VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT name, salary INTO v_name, v_salary FROM employees WHERE employee_id = 100;
IF v_salary > 5000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary is too high');
END IF;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ', Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100');
END;
|
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
) AS
BEGIN
INSERT INTO employees(
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
manager_id
)
VALUES(
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_manager_id
);
COMMIT;
END;
/
CREATE FUNCTION get_employee_salary(
@p_employee_id IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
SELECT salary
FROM employees
WHERE employee_id = p_employee_id;
END;
/
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
/
|
CREATE 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
) AS
BEGIN
INSERT INTO employees(
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
manager_id
)
VALUES(
p_first_name,
p_last_name,
p_email,
p_phone_number,
p_hire_date,
p_job_id,
p_salary,
p_manager_id
);
COMMIT;
END;
/
CREATE FUNCTION get_employee_salary(
p_employee_id IN NUMBER
) RETURN NUMBER AS
BEGIN
SELECT salary
FROM employees
WHERE employee_id = p_employee_id;
END;
/
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
/
|
BEGIN
IF (SELECT COUNT(*) FROM EMPLOYEES WHERE SALARY > 20000) > 0 BEGIN
UPDATE EMPLOYEES SET SALARY = SALARY * 1.10
WHERE SALARY > 20000;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 No employees with salary greater than 20000 found.', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | BEGIN
IF (SELECT COUNT(*) FROM EMPLOYEES WHERE SALARY > 20000) > 0 THEN
UPDATE EMPLOYEES SET SALARY = SALARY * 1.10
WHERE SALARY > 20000;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'No employees with salary greater than 20000 found.');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
CREATE TABLE employees (
employee_id FLOAT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary FLOAT,
department_id FLOAT REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id FLOAT PRIMARY KEY,
department_name VARCHAR(50),
location VARCHAR(50)
);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (100, 'John', 'Doe', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (101, 'Jane', 'Smith', 45000, 20);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (102, 'Mark', 'Jones', 60000, 30);
IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL
DROP PROCEDURE get_employee_info;
GO
CREATE PROCEDURE get_employee_info (
@p_employee_id IN AS NUMBER,
p_first_name OUT VARCHAR2,
p_last_name OUT VARCHAR2,
p_salary OUT NUMBER
)
AS
BEGIN
SELECT @p_first_name = first_name, @p_last_name = last_name, @p_salary = salary FROM employees WHERE employee_id = p_employee_id;
END;
IF OBJECT_ID('get_salary', 'FN') IS NOT NULL
DROP FUNCTION get_salary;
GO
CREATE FUNCTION get_salary (
@p_employee_id IN NUMBER
DECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
RETURN (SELECT salary FROM employees WHERE employee_id = p_employee_id);
END;
BEGIN
DECLARE
@v_first_name VARCHAR(50);
v_last_name dbo.VARCHAR2(50);
v_salary NUMBER;
BEGIN
dbo.get_employee_info(100, @v_first_name, v_last_name, v_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_first_name, '') + ' ' + isnull(v_last_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END; | CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
salary NUMBER,
department_id NUMBER REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(50),
location VARCHAR2(50)
);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (100, 'John', 'Doe', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (101, 'Jane', 'Smith', 45000, 20);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (102, 'Mark', 'Jones', 60000, 30);
CREATE OR REPLACE PROCEDURE get_employee_info (
p_employee_id IN NUMBER,
p_first_name OUT VARCHAR2,
p_last_name OUT VARCHAR2,
p_salary OUT NUMBER
)
AS
BEGIN
SELECT first_name, last_name, salary INTO p_first_name, p_last_name, p_salary FROM employees WHERE employee_id = p_employee_id;
END;
CREATE OR REPLACE FUNCTION get_salary (
p_employee_id IN NUMBER
)
RETURN NUMBER
AS
BEGIN
RETURN (SELECT salary FROM employees WHERE employee_id = p_employee_id);
END;
BEGIN
DECLARE
v_first_name VARCHAR2(50);
v_last_name VARCHAR2(50);
v_salary NUMBER;
BEGIN
get_employee_info(100, v_first_name, v_last_name, v_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_first_name || ' ' || v_last_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END; |
CREATE PROCEDURE GetCustomerInfo (@cust_id IN AS NUMBER)
AS
customer_name dbo.VARCHAR2(50);
address dbo.VARCHAR2(200);
BEGIN
SELECT @customer_name = name, @address = address
FROM customers
WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(customer_name, ''));
DBMS_OUTPUT.PUT_LINE('Address: ' + isnull(address, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.');
END
END GetCustomerInfo;
|
CREATE PROCEDURE GetCustomerInfo (cust_id IN NUMBER)
AS
customer_name VARCHAR2(50);
address VARCHAR2(200);
BEGIN
SELECT name, address
INTO customer_name, address
FROM customers
WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || customer_name);
DBMS_OUTPUT.PUT_LINE('Address: ' || address);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.');
END GetCustomerInfo;
|
IF OBJECT_ID('get_max_salary_by_dept', 'FN') IS NOT NULL
DROP FUNCTION get_max_salary_by_dept;
GO
CREATE FUNCTION get_max_salary_by_dept(@deptno FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @max_salary FLOAT;
SELECT @max_salary = MAX(salary)
FROM employees
WHERE @deptno = @deptno;
RETURN @max_salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@deptno FLOAT := 10;
max_salary NUMBER;
BEGIN
SET @max_salary = dbo.get_max_salary_by_dept(@deptno);
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' + isnull(@deptno, '') + ': ' + isnull(max_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting maximum salary: ' + ISNULL(SQLERRM, ''));
END; | CREATE OR REPLACE FUNCTION get_max_salary_by_dept(deptno NUMBER) RETURNS NUMBER IS
max_salary NUMBER;
BEGIN
SELECT MAX(salary) INTO max_salary
FROM employees
WHERE deptno = deptno;
RETURN max_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
deptno NUMBER := 10;
max_salary NUMBER;
BEGIN
max_salary := get_max_salary_by_dept(deptno);
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' || deptno || ': ' || max_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting maximum salary: ' || SQLERRM);
END; |
DECLARE
@l_empno FLOAT;
BEGIN
SELECT @l_empno = empno FROM emp WHERE ename = 'SMITH';
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found!', 16, 1);
END
END;
UPDATE emp SET sal = sal * 1.10 WHERE empno = @l_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary for employee ' + isnull(@l_empno, '') + ' has been increased by 10%.');
END;
|
DECLARE
l_empno NUMBER;
BEGIN
SELECT empno INTO l_empno FROM emp WHERE ename = 'SMITH';
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found!');
END;
UPDATE emp SET sal = sal * 1.10 WHERE empno = l_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary for employee ' || l_empno || ' has been increased by 10%.');
END;
|
CREATE TABLE customers (id FLOAT PRIMARY KEY, name VARCHAR(50), address VARCHAR(200));
IF OBJECT_ID('get_customer_name', 'FN') IS NOT NULL
DROP FUNCTION get_customer_name;
GO
CREATE FUNCTION get_customer_name(@id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
RETURN (SELECT name FROM customers WHERE @id = @id);
END;
DECLARE
@customer_name VARCHAR(50);
BEGIN
SET @customer_name = dbo.get_customer_name(1);
IF @customer_name IS NULL BEGIN
RAISERROR('-20001 Customer not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Customer name: ' + isnull(@customer_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the given ID');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE customers (id NUMBER PRIMARY KEY, name VARCHAR2(50), address VARCHAR2(200));
CREATE OR REPLACE FUNCTION get_customer_name(id NUMBER) RETURN VARCHAR2 IS
BEGIN
RETURN (SELECT name FROM customers WHERE id = id);
END;
DECLARE
customer_name VARCHAR2(50);
BEGIN
customer_name := get_customer_name(1);
IF customer_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Customer name: ' || customer_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given ID');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary ROW_NUMBER(10, 2);
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'SMITH';
IF v_salary > 10000
BEGIN
UPDATE employees
SET salary = v_salary + (v_salary * 0.10)
WHERE employee_id = @v_emp_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary too low for raise', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER(10, 2);
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'SMITH';
IF v_salary > 10000
THEN
UPDATE employees
SET salary = v_salary + (v_salary * 0.10)
WHERE employee_id = v_emp_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary too low for raise');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE TABLE departments (
deptno SMALLINT PRIMARY KEY,
dname VARCHAR(20) UNIQUE,
mgr_empno INT REFERENCES employees(empno),
loc VARCHAR(20)
);
CREATE TABLE employees (
empno INT PRIMARY KEY,
ename VARCHAR(10) NOT NULL UNIQUE,
job VARCHAR(9) NOT NULL,
mgr INT REFERENCES employees(empno),
hiredate DATE,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno SMALLINT REFERENCES departments(deptno)
);
CREATE OR REPLACE STORED CREATE PROCEDURE get_employee_by_deptno(
@deptno_in IN AS NUMBER,
OUT emp_cur OUT SYS_REFCURSOR
)
AS
BEGIN
SELECT *
FROM employees
WHERE deptno = deptno_in;
END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@empno_in IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT sal
FROM employees
WHERE empno = empno_in
);
END;
BEGIN
DECLARE emp_rec CURSOR FOR SELECT * FROM employees; OPEN emp_rec;
FETCH emp_rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
IF @emp_rec_sal > 10000 BEGIN
UPDATE employees SET comm = 0.1 * @emp_rec_sal WHERE empno = @emp_rec_empno;
END
FETCH emp_rec INTO;
END;
CLOSE emp_rec;
DEALLOCATE emp_rec;
END;
|
CREATE TABLE departments (
deptno NUMBER(2) PRIMARY KEY,
dname VARCHAR2(20) UNIQUE,
mgr_empno NUMBER(6) REFERENCES employees(empno),
loc VARCHAR2(20)
);
CREATE TABLE employees (
empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(10) NOT NULL UNIQUE,
job VARCHAR2(9) NOT NULL,
mgr NUMBER(6) REFERENCES employees(empno),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2) REFERENCES departments(deptno)
);
CREATE OR REPLACE STORED PROCEDURE get_employee_by_deptno(
deptno_in IN NUMBER,
OUT emp_cur OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN emp_cur FOR
SELECT *
FROM employees
WHERE deptno = deptno_in;
END;
CREATE OR REPLACE FUNCTION get_employee_salary(empno_in IN NUMBER)
RETURN NUMBER
AS
BEGIN
RETURN (
SELECT sal
FROM employees
WHERE empno = empno_in
);
END;
BEGIN
FOR emp_rec IN (SELECT * FROM employees) LOOP
IF emp_rec.sal > 10000 THEN
UPDATE employees SET comm = 0.1 * emp_rec.sal WHERE empno = emp_rec.empno;
END IF;
END LOOP;
END;
|
DECLARE
@v_salary FLOAT := 2000;
BEGIN
BEGIN
UPDATE employees SET salary = @v_salary
WHERE employee_id = 10;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
SET @v_salary = @v_salary * 1.1;
END;
|
DECLARE
v_salary NUMBER := 2000;
BEGIN
BEGIN
UPDATE employees SET salary = v_salary
WHERE employee_id = 10;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
v_salary := v_salary * 1.1;
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;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE @emp_id FLOAT := 100;
DECLARE @salary FLOAT;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
IF @salary IS NULL BEGIN
RAISERROR('-20002 Employee salary not found', 16, 1);
END
dbo.update_employee_salary(@emp_id, @salary + 1000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
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 NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
BEGIN
DECLARE emp_id NUMBER := 100;
DECLARE salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee salary not found');
END IF;
update_employee_salary(emp_id, salary + 1000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END;
|
IF OBJECT_ID('calc_salary', 'P') IS NOT NULL
DROP PROCEDURE calc_salary;
GO
CREATE PROCEDURE calc_salary(@emp_id IN AS NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT @salary = salary FROM employees WHERE emp_id = emp_id;
IF salary IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
ELSE BEGIN
set @salary = salary * 1.10; -- 10... ***
END
END;
BEGIN
*** e and handle any errors
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
dbo.calc_salary(@emp_id, salary);
DBMS_OUTPUT.PUT_LINE('New salary: ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE OR REPLACE PROCEDURE calc_salary(emp_id IN NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT salary INTO salary FROM employees WHERE emp_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
ELSE
salary := salary * 1.10; -- 10% raise
END IF;
END;
BEGIN
-- Call the procedure and handle any errors
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
calc_salary(emp_id, salary);
DBMS_OUTPUT.PUT_LINE('New salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
|
CREATE PROCEDURE my_procedure (
@p_in IN AS VARCHAR2,
p_out OUT NUMBER
) AS
BEGIN
*** ness logic
SELECT @p_out = COUNT(*) FROM some_table WHERE col1 = p_in;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_out = -1;
END
END;
DECLARE
@v_result FLOAT;
BEGIN
dbo.my_procedure('my_value', @v_result);
IF @v_result > 0 BEGIN
-- Do... ***
END
ELSE BEGIN
-- Do... ***
END
END;
|
CREATE PROCEDURE my_procedure (
p_in IN VARCHAR2,
p_out OUT NUMBER
) AS
BEGIN
-- Some complex business logic
SELECT COUNT(*) INTO p_out FROM some_table WHERE col1 = p_in;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_out := -1;
END;
DECLARE
v_result NUMBER;
BEGIN
my_procedure('my_value', v_result);
IF v_result > 0 THEN
-- Do something
ELSE
-- Do something else
END IF;
END;
|
BEGIN
DECLARE
@emp_id FLOAT := 12345;
emp_name dbo.VARCHAR2(255);
BEGIN
SELECT @emp_name = name FROM employees WHERE employee_id = @emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
END; | BEGIN
DECLARE
emp_id NUMBER := 12345;
emp_name VARCHAR2(255);
BEGIN
SELECT name INTO emp_name FROM employees WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
END; |
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details(@emp_id FLOAT) AS
emp_data RECORD;
BEGIN
SELECT @emp_data = * FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_data.first_name, '') + ' ' + isnull(emp_data.last_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_data.salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while getting employee details.');
END;
|
CREATE OR REPLACE PROCEDURE get_employee_details(emp_id NUMBER) AS
emp_data RECORD;
BEGIN
SELECT * INTO emp_data FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_data.first_name || ' ' || emp_data.last_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_data.salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while getting employee details.');
END;
|
DECLARE
@v_dept_id FLOAT := 10;
BEGIN
BEGIN
*** oyee
INSERT INTO employees (employee_id, first_name, last_name, department_id, salary)
VALUES (100, 'John', 'Doe', @v_dept_id, 50000);
EXCEPTION
WHEN OTHERS THEN
*** entry error
ROLLBACK;
RAISERROR('-20001 Duplicate employee ID', 16, 1);
END;
*** salary for a department
SELECT AVG(salary)
FROM employees
WHERE department_id = @v_dept_id;
*** rsor
DECLARE emp_row CURSOR FOR SELECT * FROM employees; OPEN emp_row;
FETCH emp_row INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** oyee record
DBMS_OUTPUT.PUT_LINE(isnull(@emp_row_first_name, '') + ' ' + isnull(@emp_row_last_name, ''));
FETCH emp_row INTO;
END;
CLOSE emp_row;
DEALLOCATE emp_row;
END;
|
DECLARE
v_dept_id NUMBER := 10;
BEGIN
BEGIN
-- Insert a new employee
INSERT INTO employees (employee_id, first_name, last_name, department_id, salary)
VALUES (100, 'John', 'Doe', v_dept_id, 50000);
EXCEPTION
WHEN OTHERS THEN
-- Handle duplicate entry error
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Duplicate employee ID');
END;
-- Calculate average salary for a department
SELECT AVG(salary)
FROM employees
WHERE department_id = v_dept_id;
-- Loop through a cursor
FOR emp_row IN (SELECT * FROM employees) LOOP
-- Process each employee record
DBMS_OUTPUT.PUT_LINE(emp_row.first_name || ' ' || emp_row.last_name);
END LOOP;
END;
|
IF OBJECT_ID('sp_increment_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE sp_increment_employee_salary;
GO
CREATE PROCEDURE sp_increment_employee_salary(
@p_emp_id IN AS NUMBER,
p_increment_amount IN NUMBER
)
IS
l_salary NUMBER;
BEGIN
SELECT @l_salary = salary FROM employees WHERE employee_id = p_emp_id;
UPDATE employees SET salary = l_salary + p_increment_amount WHERE employee_id = p_emp_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE sp_increment_employee_salary(
p_emp_id IN NUMBER,
p_increment_amount IN NUMBER
)
IS
l_salary NUMBER;
BEGIN
SELECT salary INTO l_salary FROM employees WHERE employee_id = p_emp_id;
UPDATE employees SET salary = l_salary + p_increment_amount WHERE employee_id = p_emp_id;
COMMIT;
END;
|
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL
DROP FUNCTION get_employee_details;
GO
CREATE FUNCTION get_employee_details (@p_employee_id IN NUMBERDECLARE ) RETURN
BEGIN employee%ROWTYPE
RETURN NULL;
END; IS
l_emp employee%ROWTYPE;
BEGIN
SELECT @l_emp = * FROM employees WHERE employee_id = p_employee_id;
RETURN l_emp;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No employee found with the given ID.', 16, 1);
END
END;
/
BEGIN
DECLARE
BEGIN
SET @l_emp = dbo.get_employee_details(100);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@l_emp_first_name, '') + ' ' + isnull(@l_emp_last_name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while fetching employee details.');
END;
END;
/
|
CREATE OR REPLACE FUNCTION get_employee_details (p_employee_id IN NUMBER) RETURN employee%ROWTYPE IS
l_emp employee%ROWTYPE;
BEGIN
SELECT * INTO l_emp FROM employees WHERE employee_id = p_employee_id;
RETURN l_emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with the given ID.');
END;
/
BEGIN
DECLARE
l_emp employee%ROWTYPE;
BEGIN
l_emp := get_employee_details(100);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_emp.first_name || ' ' || l_emp.last_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while fetching employee details.');
END;
END;
/
|
BEGIN
DECLARE
@v_employee_name VARCHAR(255);
v_employee_salary NUMBER;
BEGIN
*** ee name and salary.
SELECT
@v_employee_name = e.employee_name,
@v_employee_salary = e.salary
FROM
employees
WHERE
e.employee_id = 12345;
*** n if the employee is not found.
IF @v_employee_name IS NULL BEGIN
RAISERROR('-20001 Employee not found with ID ' + 12345, 16, 1);
END
*** yee name and salary.
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_employee_name, '') + ', Salary: ' + isnull(v_employee_salary, ''));
EXCEPTION
WHEN OTHERS THEN
*** errors that may occur.
DBMS_OUTPUT.PUT_LINE('An error occurred during the operation.');
END;
END; | BEGIN
DECLARE
v_employee_name VARCHAR2(255);
v_employee_salary NUMBER;
BEGIN
-- Select the employee name and salary.
SELECT
e.employee_name,
e.salary
INTO
v_employee_name,
v_employee_salary
FROM
employees
WHERE
e.employee_id = 12345;
-- Raise an exception if the employee is not found.
IF v_employee_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found with ID ' || 12345);
END IF;
-- Display the employee name and salary.
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name || ', Salary: ' || v_employee_salary);
EXCEPTION
WHEN OTHERS THEN
-- Handle any other errors that may occur.
DBMS_OUTPUT.PUT_LINE('An error occurred during the operation.');
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 IN AS NUMBER, new_salary IN NUMBER) AS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) AS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF @v_salary > 5000 BEGIN
dbo.RAISE_SALARY(1, 1000);
END
ELSE BEGIN
dbo.DISPLAY('Salary is too low to raise');
END
END; | DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
IF v_salary > 5000 THEN
RAISE_SALARY(1, 1000);
ELSE
DISPLAY('Salary is too low to raise');
END IF;
END; |
IF OBJECT_ID('TestProcedure', 'P') IS NOT NULL
DROP PROCEDURE TestProcedure;
GO
CREATE PROCEDURE TestProcedure(@IN_VAR IN AS VARCHAR2, OUT_VAR OUT VARCHAR2) AS
BEGIN
SELECT @OUT_VAR = 'Hello, ' + ISNULL(IN_VAR, '');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END TestProcedure;
DECLARE
@VAR_1 VARCHAR(100);
BEGIN
dbo.TestProcedure('John Doe', @VAR_1);
DBMS_OUTPUT.PUT_LINE(@VAR_1);
END;
|
CREATE OR REPLACE PROCEDURE TestProcedure(IN_VAR IN VARCHAR2, OUT_VAR OUT VARCHAR2) AS
BEGIN
SELECT 'Hello, ' || IN_VAR INTO OUT_VAR FROM DUAL;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END TestProcedure;
DECLARE
VAR_1 VARCHAR2(100);
BEGIN
TestProcedure('John Doe', VAR_1);
DBMS_OUTPUT.PUT_LINE(VAR_1);
END;
|
CREATE PROCEDURE transfer_funds(
@sender_acct_id IN AS NUMBER,
receiver_acct_id IN NUMBER,
amount IN NUMBER
) AS
BEGIN
*** sufficient funds
DECLARE @sender_balance FLOAT;
SELECT @sender_balance = balance FROM accounts WHERE acct_id = sender_acct_id;
IF @sender_balance < amount BEGIN
RAISERROR('-20001 Insufficient funds', 16, 1);
END
-- Tr... ***
UPDATE accounts SET balance = balance - amount WHERE acct_id = sender_acct_id;
UPDATE accounts SET balance = balance + amount WHERE acct_id = receiver_acct_id;
END;
|
CREATE PROCEDURE transfer_funds(
sender_acct_id IN NUMBER,
receiver_acct_id IN NUMBER,
amount IN NUMBER
) AS
BEGIN
-- Check sender has sufficient funds
DECLARE sender_balance NUMBER;
SELECT balance INTO sender_balance FROM accounts WHERE acct_id = sender_acct_id;
IF sender_balance < amount THEN
RAISE_APPLICATION_ERROR(-20001, 'Insufficient funds');
END IF;
-- Transfer funds
UPDATE accounts SET balance = balance - amount WHERE acct_id = sender_acct_id;
UPDATE accounts SET balance = balance + amount WHERE acct_id = receiver_acct_id;
END;
|
CREATE PROCEDURE Get_Employee_Details(@IN p_emp_id AS NUMBER) AS
BEGIN
DECLARE
@v_first_name VARCHAR(25);
v_last_name dbo.VARCHAR2(25);
v_salary NUMBER;
v_err_msg dbo.VARCHAR2(200);
BEGIN
SELECT @v_first_name = first_name, @v_last_name = last_name, @v_salary = salary FROM employees
WHERE emp_id = p_emp_id;
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(@v_first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(v_last_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_err_msg = 'Employee with ID ' + isnull(p_emp_id, '') + ' does not exist.';
DBMS_OUTPUT.PUT_LINE(v_err_msg);
END
END;
END;
|
CREATE PROCEDURE Get_Employee_Details(IN p_emp_id NUMBER) AS
BEGIN
DECLARE
v_first_name VARCHAR2(25);
v_last_name VARCHAR2(25);
v_salary NUMBER;
v_err_msg VARCHAR2(200);
BEGIN
SELECT first_name, last_name, salary INTO v_first_name, v_last_name, v_salary FROM employees
WHERE emp_id = p_emp_id;
DBMS_OUTPUT.PUT_LINE('First Name: ' || v_first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || v_last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_err_msg := 'Employee with ID ' || p_emp_id || ' does not exist.';
DBMS_OUTPUT.PUT_LINE(v_err_msg);
END;
END;
|
IF OBJECT_ID('update_salary', 'P') IS NOT NULL
DROP PROCEDURE update_salary;
GO
CREATE PROCEDURE update_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_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
@v_cursor SYS_REFCURSOR;
v_emp_name dbo.VARCHAR2(100);
BEGIN
SELECT isnull(first_name, '') + ' ' + isnull(last_name, '') AS emp_name
FROM employees;
WHILE 1=1 BEGIN
FETCH v_cursor INTO @v_emp_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(v_emp_name);
END;
CLOSE v_cursor;
END;
|
DECLARE
v_cursor SYS_REFCURSOR;
v_emp_name VARCHAR2(100);
BEGIN
OPEN v_cursor FOR
SELECT first_name || ' ' || last_name AS emp_name
FROM employees;
LOOP
FETCH v_cursor INTO v_emp_name;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_name);
END LOOP;
CLOSE v_cursor;
END;
|
DECLARE
@v_num_rows FLOAT;
v_emp_id NUMBER;
v_emp_name dbo.VARCHAR2(100);
BEGIN
DECLARE rec CURSOR FOR SELECT emp_id, emp_name FROM employees; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
SET @v_emp_id = @rec_emp_id;
SET @v_emp_name = @rec_emp_name;
*** the employee data
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
-- Er... ***
BEGIN
SET @v_num_rows = 1 / 0;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
DECLARE
v_num_rows NUMBER;
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
BEGIN
FOR rec IN (SELECT emp_id, emp_name FROM employees) LOOP
v_emp_id := rec.emp_id;
v_emp_name := rec.emp_name;
-- Do something with the employee data
END LOOP;
-- Error handling
BEGIN
v_num_rows := 1 / 0;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
/
|
DECLARE
@v_empid FLOAT := 100;
v_salary SET @NUMBER = 30000;
BEGIN
UPDATE employees SET salary = v_salary WHERE employee_id = @v_empid;
IF @@ROWCOUNT = 1 BEGIN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error in updating salary.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END; | DECLARE
v_empid NUMBER := 100;
v_salary NUMBER := 30000;
BEGIN
UPDATE employees SET salary = v_salary WHERE employee_id = v_empid;
IF SQL%ROWCOUNT = 1 THEN
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error in updating salary.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
ROLLBACK;
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 id = emp_id;
IF salary IS NULL BEGIN
RETURN -1; *** r non-existent employee
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
BEGIN
UPDATE employees SET salary = new_salary
WHERE id = emp_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 No employee with the specified ID', 16, 1); *** r non-existent employee
END
END;
BEGIN
DECLARE @emp_id FLOAT := 1;
DECLARE @new_salary FLOAT := 1000;
DBMS_OUTPUT.PUT_LINE('Current salary: ' + isnull(dbo.get_employee_salary(@emp_id), ''));
dbo.update_employee_salary(@emp_id, @new_salary);
DBMS_OUTPUT.PUT_LINE('Updated salary: ' + isnull(dbo.get_employee_salary(@emp_id), ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); *** r any other errors
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 id = emp_id;
IF salary IS NULL THEN
RETURN -1; -- Error handling for non-existent employee
END IF;
RETURN salary;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary
WHERE id = emp_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee with the specified ID'); -- Error handling for non-existent employee
END IF;
END;
BEGIN
DECLARE emp_id NUMBER := 1;
DECLARE new_salary NUMBER := 1000;
DBMS_OUTPUT.PUT_LINE('Current salary: ' || get_employee_salary(emp_id));
update_employee_salary(emp_id, new_salary);
DBMS_OUTPUT.PUT_LINE('Updated salary: ' || get_employee_salary(emp_id));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); -- Error handling for any other errors
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = salary + 1000 WHERE employee_id = 1;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low for a raise', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
IF v_salary > 10000 THEN
UPDATE employees SET salary = salary + 1000 WHERE employee_id = 1;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low for a raise');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (
@p_in IN AS VARCHAR2,
p_out OUT VARCHAR2
) IS
BEGIN
*** logic here
SELECT @p_out = name FROM employee WHERE id = p_in;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_out = NULL;
END
END;
|
CREATE OR REPLACE PROCEDURE my_proc (
p_in IN VARCHAR2,
p_out OUT VARCHAR2
) IS
BEGIN
-- Store procedure's logic here
SELECT name INTO p_out FROM employee WHERE id = p_in;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_out := NULL;
END;
|
CREATE TABLE customers (id FLOAT PRIMARY KEY, name VARCHAR(50), age FLOAT);
INSERT INTO customers (id, name, age) VALUES (1, 'John', 30);
INSERT INTO customers (id, name, age) VALUES (2, 'Jane', 25);
INSERT INTO customers (id, name, age) VALUES (3, 'Tom', 35);
CREATE PROCEDURE get_customer
(@id IN AS NUMBER,
name OUT VARCHAR2,
age OUT NUMBER)
AS
BEGIN
SELECT @name = name, @age = age FROM customers WHERE id = id;
END;
DECLARE
@c_name VARCHAR(50);
c_age NUMBER;
BEGIN
dbo.get_customer(id => 2, name => @c_name, age => c_age);
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(@c_name, ''));
DBMS_OUTPUT.PUT_LINE('Customer Age: ' + isnull(c_age, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END
END; | CREATE TABLE customers (id NUMBER PRIMARY KEY, name VARCHAR2(50), age NUMBER);
INSERT INTO customers (id, name, age) VALUES (1, 'John', 30);
INSERT INTO customers (id, name, age) VALUES (2, 'Jane', 25);
INSERT INTO customers (id, name, age) VALUES (3, 'Tom', 35);
CREATE PROCEDURE get_customer
(id IN NUMBER,
name OUT VARCHAR2,
age OUT NUMBER)
AS
BEGIN
SELECT name, age INTO name, age FROM customers WHERE id = id;
END;
DECLARE
c_name VARCHAR2(50);
c_age NUMBER;
BEGIN
get_customer(id => 2, name => c_name, age => c_age);
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || c_name);
DBMS_OUTPUT.PUT_LINE('Customer Age: ' || c_age);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END; |
*** rocedure to insert a new employee
IF OBJECT_ID('InsertEmployee', 'P') IS NOT NULL
DROP PROCEDURE InsertEmployee;
GO
CREATE PROCEDURE InsertEmployee (
@p_first_name IN AS VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_salary IN NUMBER,
p_department_id IN NUMBER
)
AS
BEGIN
*** into the Employees table
INSERT INTO Employees (first_name, last_name, email, salary, department_id)
VALUES (p_first_name, p_last_name, p_email, p_salary, p_department_id);
END;
/
*** rocedure
DROP PROCEDURE InsertEmployee;
/
*** to calculate the average salary of employees in a department
IF OBJECT_ID('GetAvgSalaryByDept', 'FN') IS NOT NULL
DROP FUNCTION GetAvgSalaryByDept;
GO
CREATE FUNCTION GetAvgSalaryByDept (
@p_department_id IN NUMBER
DECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
*** rage salary using a subquery
l_avg_salary SET @NUMBER = (SELECT AVG(salary) FROM Employees WHERE department_id = p_department_id);
BEGIN
RETURN l_avg_salary;
END;
/
-- Dr... ***
DROP FUNCTION GetAvgSalaryByDept;
/
*** atement to retrieve all employees
SELECT * FROM Employees;
*** constructs to iterate through a cursor
DECLARE
@CURSOR c_employees IS
SELECT employee_id, first_name, last_name
FROM Employees;
l_employee_id NUMBER;
l_first_name dbo.VARCHAR2(255);
l_last_name dbo.VARCHAR2(255);
BEGIN
-- Op... ***
OPEN c_employees;
-- Fe... ***
FETCH c_employees INTO @l_employee_id, @l_first_name, @l_last_name;
*** remaining rows
WHILE c_employees%FOUND BEGIN
*** nt row
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(l_employee_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(l_first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(l_last_name, ''));
-- Fe... ***
FETCH c_employees INTO @l_employee_id, @l_first_name, @l_last_name;
END;
-- Cl... ***
CLOSE c_employees;
END;
/
*** cenario using an exception
BEGIN
*** e an invalid SQL statement
SELECT * FROM NonExistentTable;
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
-- Create a stored procedure to insert a new employee
CREATE OR REPLACE PROCEDURE InsertEmployee (
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_salary IN NUMBER,
p_department_id IN NUMBER
)
AS
BEGIN
-- Insert a new row into the Employees table
INSERT INTO Employees (first_name, last_name, email, salary, department_id)
VALUES (p_first_name, p_last_name, p_email, p_salary, p_department_id);
END;
/
-- Drop the stored procedure
DROP PROCEDURE InsertEmployee;
/
-- Create a function to calculate the average salary of employees in a department
CREATE OR REPLACE FUNCTION GetAvgSalaryByDept (
p_department_id IN NUMBER
)
RETURN NUMBER
AS
-- Calculate the average salary using a subquery
l_avg_salary NUMBER := (SELECT AVG(salary) FROM Employees WHERE department_id = p_department_id);
BEGIN
RETURN l_avg_salary;
END;
/
-- Drop the function
DROP FUNCTION GetAvgSalaryByDept;
/
-- Execute an SQL statement to retrieve all employees
SELECT * FROM Employees;
-- Use control flow constructs to iterate through a cursor
DECLARE
CURSOR c_employees IS
SELECT employee_id, first_name, last_name
FROM Employees;
l_employee_id NUMBER;
l_first_name VARCHAR2(255);
l_last_name VARCHAR2(255);
BEGIN
-- Open the cursor
OPEN c_employees;
-- Fetch the first row
FETCH c_employees INTO l_employee_id, l_first_name, l_last_name;
-- Loop through the remaining rows
WHILE c_employees%FOUND LOOP
-- Process the current row
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || l_employee_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || l_first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || l_last_name);
-- Fetch the next row
FETCH c_employees INTO l_employee_id, l_first_name, l_last_name;
END LOOP;
-- Close the cursor
CLOSE c_employees;
END;
/
-- Handle an error scenario using an exception
BEGIN
-- Attempt to execute an invalid SQL statement
SELECT * FROM NonExistentTable;
EXCEPTION
WHEN OTHERS THEN
-- Handle the error
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
IF OBJECT_ID('prc_test', 'P') IS NOT NULL
DROP PROCEDURE prc_test;
GO
CREATE PROCEDURE prc_test(
@p_param1 IN AS VARCHAR2
) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Parameter value: ' + isnull(p_param1, ''));
BEGIN
UPDATE emp SET sal = sal * 1.10
WHERE empno = 7788;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
END prc_test;
|
CREATE OR REPLACE PROCEDURE prc_test(
p_param1 IN VARCHAR2
) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Parameter value: ' || p_param1);
BEGIN
UPDATE emp SET sal = sal * 1.10
WHERE empno = 7788;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
END prc_test;
|
DECLARE
@dept_name VARCHAR(30);
emp_name dbo.VARCHAR2(30);
salary ROW_NUMBER(10, 2);
BEGIN
SELECT @dept_name = department_name
FROM departments
WHERE department_id = 20;
SELECT @emp_name = employee_name
FROM employees
WHERE employee_id = 100;
SELECT @salary = salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(@dept_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, salary), ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the given criteria.');
END
END;
|
DECLARE
dept_name VARCHAR2(30);
emp_name VARCHAR2(30);
salary NUMBER(10, 2);
BEGIN
SELECT department_name
INTO dept_name
FROM departments
WHERE department_id = 20;
SELECT employee_name
INTO emp_name
FROM employees
WHERE employee_id = 100;
SELECT salary
INTO salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Department Name: ' || dept_name);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(salary));
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given criteria.');
END;
|
CREATE PROCEDURE prc_example(
@p_in IN AS VARCHAR2,
p_out OUT VARCHAR2
) IS
BEGIN
SELECT @p_out = upper(p_in);
IF @@FETCH_STATUS <> 0 BEGIN
set @p_out = 'No data found';
END
END prc_example;
BEGIN
DECLARE
@v_in VARCHAR(20) := 'hello';
v_out dbo.VARCHAR2(20);
BEGIN
dbo.prc_example(@v_in, v_out);
DBMS_OUTPUT.PUT_LINE(v_out);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Procedure failed');
END;
END;
DECLARE
@l_cursor SYS_REFCURSOR;
BEGIN
SELECT * FROM table_a;
WHILE 1=1 BEGIN
FETCH l_cursor INTO @v_a, @v_b;
IF @@FETCH_STATUS <> 0 BREAK;
-- Pr... ***
END;
CLOSE l_cursor;
END;
|
CREATE PROCEDURE prc_example(
p_in IN VARCHAR2,
p_out OUT VARCHAR2
) IS
BEGIN
SELECT upper(p_in) INTO p_out FROM dual;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_out := 'No data found';
END prc_example;
BEGIN
DECLARE
v_in VARCHAR2(20) := 'hello';
v_out VARCHAR2(20);
BEGIN
prc_example(v_in, v_out);
DBMS_OUTPUT.PUT_LINE(v_out);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Procedure failed');
END;
END;
DECLARE
l_cursor SYS_REFCURSOR;
BEGIN
OPEN l_cursor FOR SELECT * FROM table_a;
LOOP
FETCH l_cursor INTO v_a, v_b;
EXIT WHEN l_cursor%NOTFOUND;
-- Process row
END LOOP;
CLOSE l_cursor;
END;
|
BEGIN
DECLARE @num_rows FLOAT;
BEGIN
SET @num_rows = INSERT INTO dbo.employees (employee_id, first_name, last_name, salary)
dbo.VALUES (10003, 'John', 'Doe', 25000);
DBMS_OUTPUT.PUT_LINE('Inserted ' + isnull(@num_rows, '') + ' rows.');
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Error: Too many rows inserted.');
END;
END; | BEGIN
DECLARE num_rows NUMBER;
BEGIN
num_rows := INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES (10003, 'John', 'Doe', 25000);
DBMS_OUTPUT.PUT_LINE('Inserted ' || num_rows || ' rows.');
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Error: Too many rows inserted.');
END;
END; |
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary (
@emp_id IN AS NUMBER
) AS
salary OUT 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
END;
|
CREATE OR REPLACE PROCEDURE get_employee_salary (
emp_id IN NUMBER
) AS
salary OUT 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;
END;
|
DEFINE @num_rows := @@ROWCOUNT;
DECLARE
@result_cursor SYS_REFCURSOR;
dept_threshold SET @NUMBER = 15;
dept_name dbo.VARCHAR2(30);
BEGIN
-- Ex... ***
SELECT dname
FROM dept
WHERE empno > dept_threshold
GROUP BY dname
HAVING COUNT(*) > 1;
*** y results
WHILE 1=1 BEGIN
FETCH result_cursor INTO @dept_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(dept_name);
END;
-- Cl... ***
CLOSE result_cursor;
IF @num_rows = 0 BEGIN
RAISERROR('-20000 No departments found meeting the criteria', 16, 1);
END
END; | DEFINE num_rows := SQL%ROWCOUNT;
DECLARE
result_cursor SYS_REFCURSOR;
dept_threshold NUMBER := 15;
dept_name VARCHAR2(30);
BEGIN
-- Execute query
OPEN result_cursor FOR
SELECT dname
FROM dept
WHERE empno > dept_threshold
GROUP BY dname
HAVING COUNT(*) > 1;
-- Loop through query results
LOOP
FETCH result_cursor INTO dept_name;
EXIT WHEN result_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(dept_name);
END LOOP;
-- Close query cursor
CLOSE result_cursor;
IF num_rows = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No departments found meeting the criteria');
END IF;
END; |
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
*** lary and bonus
SELECT @v_salary = salary, @v_bonus = bonus FROM employees WHERE emp_id = 1;
*** salary
SET @v_salary = v_salary + v_bonus;
*** ee's salary
UPDATE employees SET salary = v_salary WHERE emp_id = 1;
-- Co... ***
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
-- Get employee's salary and bonus
SELECT salary, bonus INTO v_salary, v_bonus FROM employees WHERE emp_id = 1;
-- Calculate the new salary
v_salary := v_salary + v_bonus;
-- Update the employee's salary
UPDATE employees SET salary = v_salary WHERE emp_id = 1;
-- Commit the changes
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Handle the error
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
ROLLBACK;
END;
|
DECLARE
@l_emp_id FLOAT;
l_salary NUMBER;
BEGIN
SELECT @l_emp_id = emp_id, @l_salary = salary
FROM employees
WHERE first_name = 'John';
IF l_salary < 30000 BEGIN
RAISERROR('-20001 Salary is too low!', 16, 1);
END
UPDATE employees SET salary = l_salary + 1000
WHERE emp_id = @l_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
l_emp_id NUMBER;
l_salary NUMBER;
BEGIN
SELECT emp_id, salary
INTO l_emp_id, l_salary
FROM employees
WHERE first_name = 'John';
IF l_salary < 30000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low!');
END IF;
UPDATE employees SET salary = l_salary + 1000
WHERE emp_id = l_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@emp_id FLOAT;
BEGIN
SELECT @emp_id = employee_id FROM employees WHERE last_name = 'King';
IF @emp_id IS NULL BEGIN
RAISERROR(
'-20000 Employee with last name ''King'' 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(
-20000,
'Employee with last name ''King'' not found'
);
END IF;
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = emp_id;
COMMIT;
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
BEGIN
*** for the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
*** te was successful
IF @@ROWCOUNT = 0 BEGIN
*** the update failed
RAISERROR('-20001 Employee not found', 16, 1);
END
*** s to the database
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
emp_id IN NUMBER,
new_salary IN NUMBER
)
IS
BEGIN
-- Update the salary for the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
-- Check if the update was successful
IF SQL%ROWCOUNT = 0 THEN
-- Raise an error if the update failed
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Commit the changes to the database
COMMIT;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_ename dbo.VARCHAR2(10);
v_sal NUMBER;
v_comm NUMBER;
BEGIN
SELECT @v_ename = ename, @v_sal = sal, @v_comm = comm
FROM emp
WHERE empno = @v_empno;
IF v_sal > 5000 BEGIN
SET @v_comm = v_sal * 0.1;
END
UPDATE emp
SET comm = v_comm
WHERE empno = @v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_ename, '') + ' commission updated to ' + isnull(v_comm, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_empno NUMBER := 10;
v_ename VARCHAR2(10);
v_sal NUMBER;
v_comm NUMBER;
BEGIN
SELECT ename, sal, comm
INTO v_ename, v_sal, v_comm
FROM emp
WHERE empno = v_empno;
IF v_sal > 5000 THEN
v_comm := v_sal * 0.1;
END IF;
UPDATE emp
SET comm = v_comm
WHERE empno = v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee ' || v_ename || ' commission updated to ' || v_comm);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_name VARCHAR(20);
v_salary NUMBER;
BEGIN
SELECT @v_name = name, @v_salary = salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@v_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.');
END
END;
|
DECLARE
v_name VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT name, salary INTO v_name, v_salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.');
END;
|
DECLARE
@emp_id INT;
emp_name dbo.VARCHAR2(30);
salary ROW_NUMBER(8,2);
BEGIN
SELECT @emp_id = employee_id, @emp_name = first_name, @salary = salary
FROM employees
WHERE employee_id = 100;
IF salary > 10000
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(emp_name, '') + ' is a high earner.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(emp_name, '') + ' is a low earner.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with employee_id = ' + isnull(@emp_id, ''));
END
END;
|
DECLARE
emp_id NUMBER(6);
emp_name VARCHAR2(30);
salary NUMBER(8,2);
BEGIN
SELECT employee_id, first_name, salary
INTO emp_id, emp_name, salary
FROM employees
WHERE employee_id = 100;
IF salary > 10000
THEN
DBMS_OUTPUT.PUT_LINE(emp_name || ' is a high earner.');
ELSE
DBMS_OUTPUT.PUT_LINE(emp_name || ' is a low earner.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with employee_id = ' || emp_id);
END;
|
IF OBJECT_ID('update_customer', 'P') IS NOT NULL
DROP PROCEDURE update_customer;
GO
CREATE PROCEDURE update_customer (
@in_customer_id IN AS NUMBER,
in_name IN VARCHAR2,
in_email IN VARCHAR2,
out_affected_rows OUT NUMBER
)
IS
BEGIN
UPDATE customers
SET name = in_name,
email = in_email
WHERE customer_id = in_customer_id;
set @out_affected_rows = @@ROWCOUNT;
END;
/
BEGIN
dbo.update_customer(1, 'John Doe', 'john.doe@example.com', :affected_rows);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Rows affected: ' + isnull(:affected_rows, ''));
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE OR REPLACE PROCEDURE update_customer (
in_customer_id IN NUMBER,
in_name IN VARCHAR2,
in_email IN VARCHAR2,
out_affected_rows OUT NUMBER
)
IS
BEGIN
UPDATE customers
SET name = in_name,
email = in_email
WHERE customer_id = in_customer_id;
out_affected_rows := SQL%ROWCOUNT;
END;
/
BEGIN
update_customer(1, 'John Doe', 'john.doe@example.com', :affected_rows);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Rows affected: ' || :affected_rows);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
|
CREATE PROCEDURE my_proc (@x FLOAT) AS
v_result NUMBER;
BEGIN
IF x > 10 BEGIN
set @v_result = x * 2;
END
ELSE BEGIN
set @v_result = x / 2;
END
DBMS_OUTPUT.PUT_LINE('Result: ' + isnull(v_result, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
CREATE PROCEDURE my_proc (x NUMBER) AS
v_result NUMBER;
BEGIN
IF x > 10 THEN
v_result := x * 2;
ELSE
v_result := x / 2;
END IF;
DBMS_OUTPUT.PUT_LINE('Result: ' || v_result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
/
|
BEGIN
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_error_flag SET @NUMBER = 0;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary IS NULL BEGIN
SET @v_error_flag = 1;
RAISERROR('-20001 Employee not found.', 16, 1);
END
UPDATE employees
SET salary = v_salary * 1.10
WHERE employee_id = @v_emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_error_flag = 1;
RAISERROR('-20002 No employees with last name Smith.', 16, 1);
END
WHEN OTHERS THEN
SET @v_error_flag = 1;
Raise;
END;
IF v_error_flag = 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Error occurred during salary update.');
END
END;
|
BEGIN
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_error_flag NUMBER := 0;
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'Smith';
IF v_salary IS NULL THEN
v_error_flag := 1;
Raise_Application_Error(-20001, 'Employee not found.');
END IF;
UPDATE employees
SET salary = v_salary * 1.10
WHERE employee_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_error_flag := 1;
Raise_Application_Error(-20002, 'No employees with last name Smith.');
WHEN OTHERS THEN
v_error_flag := 1;
Raise;
END;
IF v_error_flag = 0 THEN
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Error occurred during salary update.');
END IF;
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@p_employee_id IN AS NUMBER,
p_new_salary IN NUMBER
)
AS BEGIN
BEGIN
UPDATE employees SET salary = p_new_salary
WHERE employee_id = p_employee_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1);
END
END;
END; | CREATE OR REPLACE PROCEDURE update_employee_salary(
p_employee_id IN NUMBER,
p_new_salary IN NUMBER
)
AS BEGIN
BEGIN
UPDATE employees SET salary = p_new_salary
WHERE employee_id = p_employee_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
END; |
IF OBJECT_ID('AvgSalaryByDept', 'P') IS NOT NULL
DROP PROCEDURE AvgSalaryByDept;
GO
CREATE PROCEDURE AvgSalaryByDept(@dept_id IN AS NUMBER) AS
avg_salary NUMBER;
BEGIN
SELECT @avg_salary = AVG(salary) FROM employees WHERE department_id = dept_id;
DBMS_OUTPUT.PUT_LINE('Average salary for department ' + isnull(dept_id, '') + ': ' + isnull(avg_salary, ''));
END;
/
|
CREATE OR REPLACE PROCEDURE AvgSalaryByDept(dept_id IN NUMBER) AS
avg_salary NUMBER;
BEGIN
SELECT AVG(salary) INTO avg_salary FROM employees WHERE department_id = dept_id;
DBMS_OUTPUT.PUT_LINE('Average salary for department ' || dept_id || ': ' || avg_salary);
END;
/
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id
FROM employees
WHERE last_name = 'Scott';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id
INTO v_emp_id
FROM employees
WHERE last_name = 'Scott';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees
SET salary = salary * 1.10
WHERE emp_id = v_emp_id;
COMMIT;
END;
|
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END;
AS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
END;
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID 123456 salary: ' + isnull(dbo.get_employee_salary(123456), ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting employee salary: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER
AS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
END;
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID 123456 salary: ' || get_employee_salary(123456));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting employee salary: ' || SQLERRM);
END;
|
DECLARE
@emp_id FLOAT := 10;
sal NUMBER;
BEGIN
SELECT @sal = salary FROM employees WHERE employee_id = @emp_id;
IF sal IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
ELSE BEGIN
UPDATE employees SET salary = sal * 1.10 WHERE employee_id = @emp_id;
COMMIT;
END
END;
/
|
DECLARE
emp_id NUMBER := 10;
sal NUMBER;
BEGIN
SELECT salary INTO sal FROM employees WHERE employee_id = emp_id;
IF sal IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
ELSE
UPDATE employees SET salary = sal * 1.10 WHERE employee_id = emp_id;
COMMIT;
END IF;
END;
/
|
IF OBJECT_ID('sp_update_customer', 'P') IS NOT NULL
DROP PROCEDURE sp_update_customer;
GO
CREATE PROCEDURE sp_update_customer(
@p_id IN AS NUMBER,
p_name IN VARCHAR2,
p_address IN VARCHAR2,
p_city IN VARCHAR2,
p_postal_code IN VARCHAR2
) IS
BEGIN
UPDATE customers
SET name = p_name,
address = p_address,
city = p_city,
postal_code = p_postal_code
WHERE id = p_id;
COMMIT;
END sp_update_customer;
/
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(100);
v_error_msg dbo.VARCHAR2(200);
BEGIN
SELECT @v_id = id, @v_name = name
FROM customers
WHERE id = 1;
dbo.sp_update_customer(@v_id, v_name, '123 Main Street', 'Anytown', '12345');
IF SQL%NOTFOUND BEGIN
SET @v_error_msg = 'Customer not found';
RAISERROR(-20000, v_error_msg, 16, 1);
END
END;
/
|
CREATE OR REPLACE PROCEDURE sp_update_customer(
p_id IN NUMBER,
p_name IN VARCHAR2,
p_address IN VARCHAR2,
p_city IN VARCHAR2,
p_postal_code IN VARCHAR2
) IS
BEGIN
UPDATE customers
SET name = p_name,
address = p_address,
city = p_city,
postal_code = p_postal_code
WHERE id = p_id;
COMMIT;
END sp_update_customer;
/
DECLARE
v_id NUMBER;
v_name VARCHAR2(100);
v_error_msg VARCHAR2(200);
BEGIN
SELECT id, name
INTO v_id, v_name
FROM customers
WHERE id = 1;
sp_update_customer(v_id, v_name, '123 Main Street', 'Anytown', '12345');
IF SQL%NOTFOUND THEN
v_error_msg := 'Customer not found';
RAISE_APPLICATION_ERROR(-20000, v_error_msg);
END IF;
END;
/
|
DECLARE
@v_result FLOAT;
BEGIN
*** rd into the table
INSERT INTO employees (employee_id, first_name, last_name, email)
VALUES (100, 'John', 'Doe', 'john.doe@example.com');
*** er of rows in the table
SELECT @v_result = COUNT(*) FROM employees;
-- Ou... ***
DBMS_OUTPUT.PUT_LINE('Number of rows in the table: ' + isnull(@v_result, ''));
-- Ha... ***
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_result NUMBER;
BEGIN
-- Insert a new record into the table
INSERT INTO employees (employee_id, first_name, last_name, email)
VALUES (100, 'John', 'Doe', 'john.doe@example.com');
-- Retrieve the number of rows in the table
SELECT COUNT(*) INTO v_result FROM employees;
-- Output the result
DBMS_OUTPUT.PUT_LINE('Number of rows in the table: ' || v_result);
-- Handle exception
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
DECLARE
@l_empno FLOAT;
l_ename dbo.VARCHAR2(20);
l_salary NUMBER;
BEGIN
SELECT @l_ename = ename, @l_salary = salary
FROM emp
WHERE empno = 7934;
IF l_salary > 20000 BEGIN
UPDATE emp
SET salary = l_salary + 1000
WHERE empno = 7934;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low to increase', 16, 1);
END
END;
/
|
DECLARE
l_empno NUMBER;
l_ename VARCHAR2(20);
l_salary NUMBER;
BEGIN
SELECT ename, salary
INTO l_ename, l_salary
FROM emp
WHERE empno = 7934;
IF l_salary > 20000 THEN
UPDATE emp
SET salary = l_salary + 1000
WHERE empno = 7934;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to increase');
END IF;
END;
/
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SET @v_emp_id = 100;
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_emp_id;
IF @v_emp_id IN (SELECT employee_id FROM managers) BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_emp_id, '') + ' is a manager.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_emp_id, '') + ' is not a manager.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee with ID ' + isnull(@v_emp_id, '') + ' found.');
ROLLBACK;
END
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
v_emp_id := 100;
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_emp_id;
IF v_emp_id IN (SELECT employee_id FROM managers) THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_id || ' is a manager.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_id || ' is not a manager.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with ID ' || v_emp_id || ' found.');
ROLLBACK;
END;
|
*** e to demonstrate various functionalities
*** environment
-- De... ***
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(30);
v_salary NUMBER;
BEGIN
*** ata from a table
SELECT @v_empno = empno, @v_ename = ename, @v_salary = salary
FROM emp
WHERE empno = 7369;
*** alary
UPDATE emp
SET salary = v_salary + 100
WHERE empno = @v_empno;
-- Ch... ***
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20101 No employee found with empno ' + isnull(@v_empno, ''), 16, 1);
END
*** into a table
INSERT INTO emp (empno, ename, salary)
VALUES (9999, 'New Employee', 50000);
*** a table
DELETE FROM emp
WHERE empno = 9999;
*** cedure
dbo.my_proc(@v_empno, v_ename);
-- Ca... ***
SET @v_average_salary = dbo.my_func(@v_empno);
*** tructs
IF v_average_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is above average');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is below average');
END
*** rsor
DECLARE rec CURSOR FOR SELECT * FROM emp; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_empno, '') + ', ' + isnull(@rec_ename, '') + ', ' + isnull(@rec_salary, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
-- Er... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
-- Sample PL/SQL code to demonstrate various functionalities
-- within a database environment
-- Declare variables
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(30);
v_salary NUMBER;
BEGIN
-- Select employee data from a table
SELECT empno, ename, salary
INTO v_empno, v_ename, v_salary
FROM emp
WHERE empno = 7369;
-- Update employee salary
UPDATE emp
SET salary = v_salary + 100
WHERE empno = v_empno;
-- Check for errors
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20101, 'No employee found with empno ' || v_empno);
END IF;
-- Insert a new row into a table
INSERT INTO emp (empno, ename, salary)
VALUES (9999, 'New Employee', 50000);
-- Delete a row from a table
DELETE FROM emp
WHERE empno = 9999;
-- Call a stored procedure
my_proc(v_empno, v_ename);
-- Call a function
v_average_salary := my_func(v_empno);
-- Control flow constructs
IF v_average_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is above average');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is below average');
END IF;
-- Loop through a cursor
FOR rec IN (SELECT * FROM emp) LOOP
DBMS_OUTPUT.PUT_LINE(rec.empno || ', ' || rec.ename || ', ' || rec.salary);
END LOOP;
EXCEPTION
-- Error handling
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
IF OBJECT_ID('count_employees', 'P') IS NOT NULL
DROP PROCEDURE count_employees;
GO
CREATE PROCEDURE count_employees(@max_salary IN AS NUMBER, dept_id IN NUMBER)
AS
emp_count NUMBER;
BEGIN
SELECT @emp_count = COUNT(*)
FROM employees
WHERE salary <= max_salary AND department_id = dept_id;
DBMS_OUTPUT.PUT_LINE('Number of employees with salary <= ' + isnull(max_salary, '') + ' in department ' + isnull(dept_id, '') + ': ' + isnull(emp_count, ''));
IF emp_count = 0 BEGIN
RAISERROR('-20000 No employees found', 16, 1);
END
END;
/
DECLARE
@max_salary FLOAT := 10000;
dept_id SET @NUMBER = 20;
BEGIN
dbo.count_employees(@max_salary, dept_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
/ | CREATE OR REPLACE PROCEDURE count_employees(max_salary IN NUMBER, dept_id IN NUMBER)
AS
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count
FROM employees
WHERE salary <= max_salary AND department_id = dept_id;
DBMS_OUTPUT.PUT_LINE('Number of employees with salary <= ' || max_salary || ' in department ' || dept_id || ': ' || emp_count);
IF emp_count = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No employees found');
END IF;
END;
/
DECLARE
max_salary NUMBER := 10000;
dept_id NUMBER := 20;
BEGIN
count_employees(max_salary, dept_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
/ |
CREATE PROCEDURE get_employee_info(
@emp_id IN AS NUMBER,
OUT emp_name OUT VARCHAR2,
OUT job_title OUT VARCHAR2,
OUT salary OUT NUMBER
)
IS
CURSOR c_emp IS
SELECT name, job, salary
FROM employees
WHERE id = emp_id;
BEGIN
OPEN c_emp;
FETCH c_emp INTO @emp_name, @job_title, @salary;
CLOSE c_emp;
IF @@FETCH_STATUS <> 0 BEGIN
set @emp_name = NULL;
set @job_title = NULL;
set @salary = NULL;
END
END;
DECLARE
@v_emp_name VARCHAR(20);
v_job_title dbo.VARCHAR2(20);
v_salary NUMBER;
BEGIN
dbo.get_employee_info(100, @v_emp_name, v_job_title, v_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Job Title: ' + isnull(v_job_title, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
END;
|
CREATE PROCEDURE get_employee_info(
emp_id IN NUMBER,
OUT emp_name OUT VARCHAR2,
OUT job_title OUT VARCHAR2,
OUT salary OUT NUMBER
)
IS
CURSOR c_emp IS
SELECT name, job, salary
FROM employees
WHERE id = emp_id;
BEGIN
OPEN c_emp;
FETCH c_emp INTO emp_name, job_title, salary;
CLOSE c_emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
emp_name := NULL;
job_title := NULL;
salary := NULL;
END;
DECLARE
v_emp_name VARCHAR2(20);
v_job_title VARCHAR2(20);
v_salary NUMBER;
BEGIN
get_employee_info(100, v_emp_name, v_job_title, v_salary);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Job Title: ' || v_job_title);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END;
|
DECLARE
*** to store input and output values
@v_id FLOAT;
v_name dbo.VARCHAR2(50);
v_salary NUMBER;
BEGIN
*** the user using DBMS_OUTPUT
DBMS_OUTPUT.PUT_LINE('Enter employee ID: ');
SET @v_id = CAST(DBMS_INPUT.GET_LINE AS FLOAT);
DBMS_OUTPUT.PUT_LINE('Enter employee name: ');
SET @v_name = DBMS_INPUT.GET_LINE;
DBMS_OUTPUT.PUT_LINE('Enter employee salary: ');
SET @v_salary = CAST(DBMS_INPUT.GET_LINE AS FLOAT);
*** nto the database using SQL statement
INSERT INTO employees (emp_id, emp_name, salary)
VALUES (@v_id, v_name, v_salary);
*** s to the database
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee data inserted successfully.');
EXCEPTION
*** that might occur
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END; | DECLARE
-- Declare variables to store input and output values
v_id NUMBER;
v_name VARCHAR2(50);
v_salary NUMBER;
BEGIN
-- Input values from the user using DBMS_OUTPUT
DBMS_OUTPUT.PUT_LINE('Enter employee ID: ');
v_id := TO_NUMBER(DBMS_INPUT.GET_LINE);
DBMS_OUTPUT.PUT_LINE('Enter employee name: ');
v_name := DBMS_INPUT.GET_LINE;
DBMS_OUTPUT.PUT_LINE('Enter employee salary: ');
v_salary := TO_NUMBER(DBMS_INPUT.GET_LINE);
-- Insert the data into the database using SQL statement
INSERT INTO employees (emp_id, emp_name, salary)
VALUES (v_id, v_name, v_salary);
-- Commit the changes to the database
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee data inserted successfully.');
EXCEPTION
-- Handle any errors that might occur
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END; |
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc AS
num_rows NUMBER;
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE department_id = 10;
SELECT @num_rows = COUNT(*) FROM employees;
DBMS_OUTPUT.PUT_LINE('Updated ' + isnull(num_rows, '') + ' rows.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
ROLLBACK;
END;
|
CREATE OR REPLACE PROCEDURE my_proc AS
num_rows NUMBER;
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE department_id = 10;
SELECT COUNT(*) INTO num_rows FROM employees;
DBMS_OUTPUT.PUT_LINE('Updated ' || num_rows || ' rows.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
ROLLBACK;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE employee_id = 1;
IF v_salary > 50000 BEGIN
SET @v_bonus = 0.1 * v_salary;
END
ELSE IF v_salary > 30000 BEGIN
SET @v_bonus = 0.05 * v_salary;
END
ELSE BEGIN
SET @v_bonus = 0;
END
EXECUTE sp_executesql
@'UPDATE employees SET bonus = :bonus WHERE employee_id = :id', N'@v_bonus, @v_emp_id',
@v_bonus, @v_emp_id;
DBMS_OUTPUT.put_line('Bonus updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('An error occurred. Please contact the administrator.');
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM employees
WHERE employee_id = 1;
IF v_salary > 50000 THEN
v_bonus := 0.1 * v_salary;
ELSIF v_salary > 30000 THEN
v_bonus := 0.05 * v_salary;
ELSE
v_bonus := 0;
END IF;
EXECUTE IMMEDIATE
'UPDATE employees SET bonus = :bonus WHERE employee_id = :id'
USING v_bonus, v_emp_id;
DBMS_OUTPUT.put_line('Bonus updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('An error occurred. Please contact the administrator.');
END;
|
BEGIN
-- De... ***
DECLARE
@l_empno SMALLINT;
l_ename dbo.VARCHAR2(10);
BEGIN
*** nformation
SELECT @l_empno = empno, @l_ename = ename
FROM emp
WHERE empno = 7788;
*** nformation
UPDATE emp
SET ename = 'SCOTT'
WHERE empno = @l_empno;
-- In... ***
INSERT INTO emp (empno, ename)
VALUES (8888, 'NEW_EMP');
-- De... ***
DELETE FROM emp
WHERE empno = 8888;
-- Co... ***
COMMIT;
EXCEPTION
-- Ha... ***
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
-- Declare variables
DECLARE
l_empno NUMBER(4);
l_ename VARCHAR2(10);
BEGIN
-- Select employee information
SELECT empno, ename
INTO l_empno, l_ename
FROM emp
WHERE empno = 7788;
-- Update employee information
UPDATE emp
SET ename = 'SCOTT'
WHERE empno = l_empno;
-- Insert new employee
INSERT INTO emp (empno, ename)
VALUES (8888, 'NEW_EMP');
-- Delete employee
DELETE FROM emp
WHERE empno = 8888;
-- Commit changes
COMMIT;
EXCEPTION
-- Handle errors
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_name VARCHAR(255);
v_num NUMBER;
BEGIN
SELECT @v_name = name, @v_num = num FROM table_name WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@v_name, ''));
DBMS_OUTPUT.PUT_LINE('Number: ' + isnull(v_num, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
DBMS_OUTPUT.PUT_LINE('Error code: ' + ISNULL(@@ERROR, ''));
END
END;
|
DECLARE
v_name VARCHAR2(255);
v_num NUMBER;
BEGIN
SELECT name, num INTO v_name, v_num FROM table_name WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Number: ' || v_num);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
END;
|
BEGIN
-- De... ***
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
*** rmation
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 10;
-- Ca... ***
SET @v_bonus = v_salary * 0.1;
*** nformation
UPDATE employees
SET bonus = v_bonus
WHERE emp_id = 10;
-- Co... ***
COMMIT;
-- Ou... ***
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(v_bonus, ''));
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
END; | BEGIN
-- Declare variables
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
-- Get employee information
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 10;
-- Calculate bonus
v_bonus := v_salary * 0.1;
-- Update employee information
UPDATE employees
SET bonus = v_bonus
WHERE emp_id = 10;
-- Commit the changes
COMMIT;
-- Output the results
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Bonus: ' || v_bonus);
EXCEPTION
WHEN OTHERS THEN
-- Handle errors
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
ROLLBACK;
END;
END; |
BEGIN
-- De... ***
v_emp_id SET @NUMBER = 10;
v_emp_name dbo.VARCHAR2(50);
v_salary NUMBER;
*** formation.
SELECT @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = v_emp_id;
*** information.
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
*** n if the salary is less than 10000.
IF v_salary < 10000 BEGIN
RAISERROR('-20001 Salary cannot be less than 10000', 16, 1);
END
*** alary.
UPDATE employees SET salary = v_salary + 1000 WHERE emp_id = v_emp_id;
-- Co... ***
COMMIT;
-- Ha... ***
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(v_emp_id, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
END;
|
BEGIN
-- Declare variables.
v_emp_id NUMBER := 10;
v_emp_name VARCHAR2(50);
v_salary NUMBER;
-- Fetch employee information.
SELECT emp_name, salary INTO v_emp_name, v_salary FROM employees WHERE emp_id = v_emp_id;
-- Display employee information.
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
-- Raise an exception if the salary is less than 10000.
IF v_salary < 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be less than 10000');
END IF;
-- Update employee salary.
UPDATE employees SET salary = v_salary + 1000 WHERE emp_id = v_emp_id;
-- Commit changes.
COMMIT;
EXCEPTION
-- Handle errors.
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_emp_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
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;
|
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;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(30);
v_sal NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal FROM emp WHERE empno = 10;
IF v_sal > 3000 BEGIN
UPDATE emp SET sal = v_sal * 1.1 WHERE empno = @v_empno;
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20101 No employee found with that ID', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END; | DECLARE
v_empno NUMBER;
v_ename VARCHAR2(30);
v_sal NUMBER;
BEGIN
SELECT empno, ename, sal INTO v_empno, v_ename, v_sal FROM emp WHERE empno = 10;
IF v_sal > 3000 THEN
UPDATE emp SET sal = v_sal * 1.1 WHERE empno = v_empno;
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20101, 'No employee found with that ID');
WHEN OTHERS THEN
RAISE;
END; |
CREATE TABLE orders (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT,
order_date DATE,
order_total DECIMAL(10,2)
);
CREATE TABLE customers (
customer_id BIGINT PRIMARY KEY,
customer_name VARCHAR(50),
customer_address VARCHAR(100)
);
CREATE PROCEDURE create_order (
@p_customer_id IN AS NUMBER,
p_order_date IN DATE,
p_order_total IN NUMBER
) AS
BEGIN
INSERT INTO orders (customer_id, order_date, order_total)
VALUES (p_customer_id, p_order_date, p_order_total);
END;
CREATE FUNCTION get_order_total (
@p_order_id IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
SELECT order_total
FROM orders
WHERE order_id = p_order_id;
END;
BEGIN
DECLARE
@v_customer_id FLOAT := 10;
v_order_date SET @DATE = CONVERT(DATETIME, '2023-03-08');
v_order_total SET @NUMBER = 100;
BEGIN
dbo.create_order(p_customer_id, v_order_date, v_order_total);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate value in index');
END;
SELECT * FROM orders;
END;
|
CREATE TABLE orders (
order_id NUMBER(10) PRIMARY KEY,
customer_id NUMBER(10),
order_date DATE,
order_total NUMBER(10,2)
);
CREATE TABLE customers (
customer_id NUMBER(10) PRIMARY KEY,
customer_name VARCHAR2(50),
customer_address VARCHAR2(100)
);
CREATE PROCEDURE create_order (
p_customer_id IN NUMBER,
p_order_date IN DATE,
p_order_total IN NUMBER
) AS
BEGIN
INSERT INTO orders (customer_id, order_date, order_total)
VALUES (p_customer_id, p_order_date, p_order_total);
END;
CREATE FUNCTION get_order_total (
p_order_id IN NUMBER
) RETURN NUMBER AS
BEGIN
SELECT order_total
FROM orders
WHERE order_id = p_order_id;
END;
BEGIN
DECLARE
v_customer_id NUMBER := 10;
v_order_date DATE := TO_DATE('2023-03-08', 'YYYY-MM-DD');
v_order_total NUMBER := 100;
BEGIN
create_order(p_customer_id, v_order_date, v_order_total);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate value in index');
END;
SELECT * FROM orders;
END;
|
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
DECLARE
@v_emp_id INT;
v_emp_name dbo.VARCHAR2(30);
BEGIN
SELECT @v_emp_id = employee_id, @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
END;
/ | BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
DECLARE
v_emp_id NUMBER(6);
v_emp_name VARCHAR2(30);
BEGIN
SELECT employee_id, first_name || ' ' || last_name
INTO v_emp_id, v_emp_name
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
END;
/ |
BEGIN
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
v_deptno NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary, @v_deptno = deptno
FROM emp
WHERE empno = 12345;
IF v_salary > 10000 BEGIN
UPDATE emp
SET salary = salary + 1000
WHERE empno = @v_empno;
END
IF v_deptno = 20 BEGIN
INSERT INTO bonus
VALUES (@v_empno, 1000);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20002 Duplicate employee number', 16, 1);
WHEN OTHERS THEN
RAISE;
END;
END;
|
BEGIN
DECLARE
v_empno NUMBER;
v_salary NUMBER;
v_deptno NUMBER;
BEGIN
SELECT empno, salary, deptno
INTO v_empno, v_salary, v_deptno
FROM emp
WHERE empno = 12345;
IF v_salary > 10000 THEN
UPDATE emp
SET salary = salary + 1000
WHERE empno = v_empno;
END IF;
IF v_deptno = 20 THEN
INSERT INTO bonus
VALUES (v_empno, 1000);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20002, 'Duplicate employee number');
WHEN OTHERS THEN
RAISE;
END;
END;
|
IF OBJECT_ID('add_employee', 'P') IS NOT NULL
DROP PROCEDURE add_employee;
GO
CREATE PROCEDURE add_employee(
@fname IN AS VARCHAR2,
lname IN VARCHAR2,
salary IN NUMBER
)
AS
empno INTEGER;
BEGIN
INSERT INTO employees( fname, lname, salary)
VALUES( fname, lname, salary);
SELECT @empno = employees_seq.currval;
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(empno, '') + ' added.');
END;
|
CREATE OR REPLACE PROCEDURE add_employee(
fname IN VARCHAR2,
lname IN VARCHAR2,
salary IN NUMBER
)
AS
empno INTEGER;
BEGIN
INSERT INTO employees(empno, fname, lname, salary)
VALUES(employees_seq.nextval, fname, lname, salary);
SELECT employees_seq.currval INTO empno FROM DUAL;
DBMS_OUTPUT.PUT_LINE('Employee ' || empno || ' added.');
END;
|
declare
@l_empno smallint;
l_job job;
begin
select @l_empno = empno, @l_job = job from emp where empno = 7839;
dbms_output.put_line('Empno: ' + isnull(@l_empno, '') + ' Job: ' + isnull(l_job, ''));
if @@fetch_status <> 0 begin
dbms_output.put_line('Employee not found');
end
end;
|
declare
l_empno number(4);
l_job job;
begin
select empno, job into l_empno, l_job from emp where empno = 7839;
dbms_output.put_line('Empno: ' || l_empno || ' Job: ' || l_job);
exception
when no_data_found then
dbms_output.put_line('Employee not found');
end;
|
CREATE PROCEDURE get_employee_details(@emp_id IN AS NUMBER)
AS
emp_name dbo.VARCHAR2(255);
emp_salary NUMBER;
BEGIN
SELECT @emp_name = name, @emp_salary = salary
FROM employees
WHERE id = emp_id;
IF emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found.', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
END;
BEGIN
dbo.get_employee_details(10);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the specified employee ID.');
END
END;
|
CREATE PROCEDURE get_employee_details(emp_id IN NUMBER)
AS
emp_name VARCHAR2(255);
emp_salary NUMBER;
BEGIN
SELECT name, salary INTO emp_name, emp_salary
FROM employees
WHERE id = emp_id;
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found.');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
END;
BEGIN
get_employee_details(10);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the specified employee ID.');
END;
|
DECLARE
@v_employee_id INTEGER;
v_salary ROW_NUMBER(10, 2);
BEGIN
SET @v_employee_id = 100;
SET @v_salary = 50000;
*** exists
IF @v_employee_id NOT IN (SELECT employee_id FROM employees) BEGIN
*** exist, raise exception
RAISERROR('-20001 Employee does not exist', 16, 1);
END
-- Up... ***
UPDATE employees SET salary = v_salary WHERE employee_id = @v_employee_id;
-- Co... ***
COMMIT;
END;
/
|
DECLARE
v_employee_id INTEGER;
v_salary NUMBER(10, 2);
BEGIN
v_employee_id := 100;
v_salary := 50000;
-- Check if employee exists
IF v_employee_id NOT IN (SELECT employee_id FROM employees) THEN
-- Employee does not exist, raise exception
RAISE_APPLICATION_ERROR(-20001, 'Employee does not exist');
END IF;
-- Update salary
UPDATE employees SET salary = v_salary WHERE employee_id = v_employee_id;
-- Commit changes
COMMIT;
END;
/
|
IF OBJECT_ID('Insert_Employee', 'P') IS NOT NULL
DROP PROCEDURE Insert_Employee;
GO
CREATE PROCEDURE Insert_Employee(
@p_id IN AS NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER,
p_dept IN VARCHAR2) IS
BEGIN
INSERT INTO employee (id, name, salary, department)
VALUES (p_id, p_name, p_salary, p_dept);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE Insert_Employee(
p_id IN NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER,
p_dept IN VARCHAR2) IS
BEGIN
INSERT INTO employee (id, name, salary, department)
VALUES (p_id, p_name, p_salary, p_dept);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(
@p_in_param IN AS VARCHAR2,
p_out_param OUT VARCHAR2
)
IS
BEGIN
SELECT @p_out_param = 'Processed: ' + isnull(p_in_param, '');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE my_proc(
p_in_param IN VARCHAR2,
p_out_param OUT VARCHAR2
)
IS
BEGIN
SELECT 'Processed: ' || p_in_param INTO p_out_param FROM DUAL;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
IF i MOD 2 = 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Even number: ' + isnull(i, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Odd number: ' + isnull(i, ''));
END
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
BEGIN
FOR i IN 1 .. 10 LOOP
IF i MOD 2 = 0 THEN
DBMS_OUTPUT.PUT_LINE('Even number: ' || i);
ELSE
DBMS_OUTPUT.PUT_LINE('Odd number: ' || i);
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
DECLARE
@v_var_a FLOAT;
v_var_b dbo.VARCHAR2(20);
BEGIN
SET @v_var_a = 10;
SET @v_var_b = 'Hello World';
DBMS_OUTPUT.PUT_LINE(isnull(@v_var_a, '') + ' ' + isnull(v_var_b, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END; | DECLARE
v_var_a NUMBER;
v_var_b VARCHAR2(20);
BEGIN
v_var_a := 10;
v_var_b := 'Hello World';
DBMS_OUTPUT.PUT_LINE(v_var_a || ' ' || v_var_b);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END; |
DECLARE
@v_sal DECIMAL(10,2);
BEGIN
BEGIN
SELECT @v_sal = salary FROM emp WHERE empno = 7788;
SET @v_sal = @v_sal * 1.10;
UPDATE emp SET salary = @v_sal WHERE empno = 7788;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee does not exist!');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred!');
END;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
END; | DECLARE
v_sal NUMBER(10,2);
BEGIN
BEGIN
SELECT salary INTO v_sal FROM emp WHERE empno = 7788;
v_sal := v_sal * 1.10;
UPDATE emp SET salary = v_sal WHERE empno = 7788;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee does not exist!');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred!');
END;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
END; |
sql
CREATE TABLE users (
id int PRIMARY KEY,
name varchar(255) UNIQUE NOT NULL,
email varchar(255) UNIQUE NOT NULL
);
CREATE TABLE posts (
id int PRIMARY KEY,
title varchar(255) NOT NULL,
content varchar(max) NOT NULL,
user_id int REFERENCES users(id),
created_at datetime2 DEFAULT GETDATE()
);
CREATE INDEX idx_users_name ON users(name);
CREATE FUNCTION get_user_posts(@user_id int)
RETURNS TABLE DECLARE (
id
begin int
return null;
end;,
title varchar(255),
content text
)
BEGIN
RETURN QUERY
SELECT id, title, content
FROM posts
WHERE user_id = user_id;
END;;
CREATE PROCEDURE add_user(
@name varchar(255),
@email varchar(255)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @user_id int;
INSERT INTO users(name, email)
VALUES (@name, @email);
SET @user_id = SCOPE_IDENTITY();
COMMIT;
SELECT @user_id FROM users WHERE id = @user_id;
END;;
CREATE VIEW user_posts AS
SELECT
u.id,
u.name,
p.id AS post_id,
p.title,
p.content
FROM
users u
JOIN
posts p ON u.id = p.user_id;
BEGIN
BEGIN
INSERT INTO users(name, email)
VALUES ('John', 'john@example.com');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
INSERT INTO posts(title, content, user_id)
VALUES ('My first post', 'This is my first post.', 1);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END;;
| sql
CREATE TABLE users (
id int PRIMARY KEY,
name varchar(255) UNIQUE NOT NULL,
email varchar(255) UNIQUE NOT NULL
);
CREATE TABLE posts (
id int PRIMARY KEY,
title varchar(255) NOT NULL,
content text NOT NULL,
user_id int REFERENCES users(id),
created_at timestamp DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_users_name ON users(name);
CREATE FUNCTION get_user_posts(user_id int)
RETURNS TABLE (
id int,
title varchar(255),
content text
)
BEGIN
RETURN QUERY
SELECT id, title, content
FROM posts
WHERE user_id = user_id;
END;;
CREATE PROCEDURE add_user(
name varchar(255),
email varchar(255)
)
BEGIN
DECLARE user_id int;
INSERT INTO users(name, email)
VALUES (name, email)
RETURNING id INTO user_id;
COMMIT;
SELECT user_id FROM users WHERE id = user_id;
END;;
CREATE VIEW user_posts AS
SELECT
u.id,
u.name,
p.id AS post_id,
p.title,
p.content
FROM
users u
JOIN
posts p ON u.id = p.user_id;
BEGIN
BEGIN
INSERT INTO users(name, email)
VALUES ('John', 'john@example.com');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
INSERT INTO posts(title, content, user_id)
VALUES ('My first post', 'This is my first post.', 1);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
END;;
|
CREATE PROCEDURE usp_GetCustomerOrders(
@p_customer_id IN AS NUMBER
) AS
BEGIN
SELECT order_id, product_id, quantity
FROM orders
WHERE customer_id = p_customer_id;
END;
CREATE FUNCTION fn_GetTotalSales(
@p_customer_id IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (
SELECT SUM(order_total)
FROM orders
WHERE customer_id = p_customer_id
);
END;
BEGIN
DECLARE @TotalSales FLOAT;
BEGIN
SET @TotalSales = dbo.fn_GetTotalSales(25);
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20100 Error getting total sales', 16, 1);
END;
DBMS_OUTPUT.PUT_LINE('Total sales for customer 25: ' + ISNULL(@TotalSales, ''));
END;
|
CREATE PROCEDURE usp_GetCustomerOrders(
p_customer_id IN NUMBER
) AS
BEGIN
SELECT order_id, product_id, quantity
FROM orders
WHERE customer_id = p_customer_id;
END;
CREATE FUNCTION fn_GetTotalSales(
p_customer_id IN NUMBER
) RETURN NUMBER AS
BEGIN
RETURN (
SELECT SUM(order_total)
FROM orders
WHERE customer_id = p_customer_id
);
END;
BEGIN
DECLARE TotalSales NUMBER;
BEGIN
TotalSales := fn_GetTotalSales(25);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20100, 'Error getting total sales');
END;
DBMS_OUTPUT.PUT_LINE('Total sales for customer 25: ' || TotalSales);
END;
|
DECLARE
@v_empno FLOAT;
v_name dbo.VARCHAR2(30);
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_name = ename, @v_salary = sal
FROM emp
WHERE empno = 100;
IF v_salary > 5000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END
IF v_name = 'SCOTT' BEGIN
UPDATE emp
SET sal = sal * 1.10
WHERE empno = @v_empno;
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the given employee number');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
DECLARE
v_empno NUMBER;
v_name VARCHAR2(30);
v_salary NUMBER;
BEGIN
SELECT empno, ename, sal
INTO v_empno, v_name, v_salary
FROM emp
WHERE empno = 100;
IF v_salary > 5000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END IF;
IF v_name = 'SCOTT' THEN
UPDATE emp
SET sal = sal * 1.10
WHERE empno = v_empno;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given employee number');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
IF OBJECT_ID('get_max_salary', 'FN') IS NOT NULL
DROP FUNCTION get_max_salary;
GO
CREATE FUNCTION get_max_salary(@deptno IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
max_salary NUMBER;
BEGIN
SELECT @max_salary = MAX(salary) FROM emp WHERE deptno = deptno;
RETURN max_salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN 0;
END
END;
DECLARE
@deptno FLOAT;
BEGIN
SET @deptno = 20;
IF dbo.get_max_salary(@deptno) > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' + isnull(@deptno, '') + ' exceeds $10,000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' + isnull(@deptno, '') + ' is below $10,000');
END
END;
|
CREATE OR REPLACE FUNCTION get_max_salary(deptno IN NUMBER)
RETURN NUMBER AS
max_salary NUMBER;
BEGIN
SELECT MAX(salary) INTO max_salary FROM emp WHERE deptno = deptno;
RETURN max_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
DECLARE
deptno NUMBER;
BEGIN
deptno := 20;
IF get_max_salary(deptno) > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' || deptno || ' exceeds $10,000');
ELSE
DBMS_OUTPUT.PUT_LINE('Maximum salary in department ' || deptno || ' is below $10,000');
END IF;
END;
|
IF OBJECT_ID('check_balance', 'P') IS NOT NULL
DROP PROCEDURE check_balance;
GO
CREATE PROCEDURE check_balance(@acct_num IN AS NUMBER, amt OUT NUMBER) IS
BEGIN
SELECT @amt = balance FROM accounts WHERE account_number = acct_num;
IF amt < 0 BEGIN
RAISERROR('-20001 Insufficient funds', 16, 1);
END
END;
DECLARE
@emp_id FLOAT := 12345;
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = @emp_id;
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 check_balance(acct_num IN NUMBER, amt OUT NUMBER) IS
BEGIN
SELECT balance INTO amt FROM accounts WHERE account_number = acct_num;
IF amt < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Insufficient funds');
END IF;
END;
DECLARE
emp_id NUMBER := 12345;
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
v_comm NUMBER;
BEGIN
SELECT @v_salary = salary, @v_comm = commission_pct
FROM employees
WHERE employee_id = @v_emp_id;
IF v_comm IS NULL BEGIN
SET @v_comm = 0;
END
SET @v_salary = v_salary * (1 + v_comm / 100);
UPDATE employees
SET salary = v_salary
WHERE employee_id = @v_emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
v_comm NUMBER;
BEGIN
SELECT salary, commission_pct INTO v_salary, v_comm
FROM employees
WHERE employee_id = v_emp_id;
IF v_comm IS NULL THEN
v_comm := 0;
END IF;
v_salary := v_salary * (1 + v_comm / 100);
UPDATE employees
SET salary = v_salary
WHERE employee_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
@l_empno FLOAT;
l_sal NUMBER;
BEGIN
SET @l_empno = 7902;
SET @l_sal = 10000;
UPDATE emp SET sal = l_sal WHERE empno = @l_empno;
IF @l_empno <> 7902 BEGIN
RAISERROR('-20001 Empno is invalid', 16, 1);
END
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
/ | DECLARE
l_empno NUMBER;
l_sal NUMBER;
BEGIN
l_empno := 7902;
l_sal := 10000;
UPDATE emp SET sal = l_sal WHERE empno = l_empno;
IF l_empno <> 7902 THEN
RAISE_APPLICATION_ERROR(-20001, 'Empno is invalid');
END IF;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
/ |
IF OBJECT_ID('sp_update_customer', 'P') IS NOT NULL
DROP PROCEDURE sp_update_customer;
GO
CREATE PROCEDURE sp_update_customer(
@p_customer_id IN AS NUMBER,
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2)
IS
BEGIN
UPDATE customers
SET first_name = p_first_name,
last_name = p_last_name
WHERE customer_id = p_customer_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
ROLLBACK;
RAISERROR('-20001 Customer not found', 16, 1);
END
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
IF OBJECT_ID('fn_get_customer_name', 'FN') IS NOT NULL
DROP FUNCTION fn_get_customer_name;
GO
CREATE FUNCTION fn_get_customer_name(@p_customer_id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
v_first_name dbo.VARCHAR2(20);
v_last_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_first_name = first_name, @v_last_name = last_name
FROM customers
WHERE customer_id = p_customer_id;
RETURN isnull(v_first_name, '') + ' ' + isnull(v_last_name, '');
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
/* PACKAGE body pkg_customers */
IF OBJECT_ID('pkg_customers_sp_update_customer', 'P') IS NOT NULL
DROP PROCEDURE pkg_customers_sp_update_customer;
GO
CREATE PROCEDURE pkg_customers_sp_update_customer(
@p_customer_id IN AS NUMBERDECLARE ,
p_first_name DECLARE @IN VARCHARDECLARE ,
p_last_name DECLARE @IN VARCHARDECLARE ) OVERRIDE;
DECLARE @END;
|
CREATE OR REPLACE PROCEDURE sp_update_customer(
p_customer_id IN NUMBER,
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2)
IS
BEGIN
UPDATE customers
SET first_name = p_first_name,
last_name = p_last_name
WHERE customer_id = p_customer_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Customer not found');
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE OR REPLACE FUNCTION fn_get_customer_name(p_customer_id IN NUMBER)
RETURN VARCHAR2
IS
v_first_name VARCHAR2(20);
v_last_name VARCHAR2(20);
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM customers
WHERE customer_id = p_customer_id;
RETURN v_first_name || ' ' || v_last_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PACKAGE body pkg_customers
IS
PROCEDURE sp_update_customer(
p_customer_id IN NUMBER,
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2) OVERRIDE;
END;
|
CREATE PROCEDURE update_employee(@emp_id IN AS NUMBER, emp_name IN VARCHAR2, emp_salary IN NUMBER)
AS
BEGIN
UPDATE employees SET emp_name = emp_name, emp_salary = emp_salary
WHERE emp_id = emp_id;
COMMIT;
END;
DECLARE
@emp_id FLOAT;
emp_name dbo.VARCHAR2(50);
emp_salary NUMBER;
BEGIN
SELECT @emp_id = e.emp_id e.emp_name INTO emp_name, e.emp_salary INTO emp_salary
FROM employees e
WHERE e.emp_id = :emp_id;
UPDATE employees SET emp_name = :emp_name, emp_salary = :emp_salary
WHERE @emp_id = :emp_id;
COMMIT;
END;
CREATE FUNCTION get_employee_name(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @emp_name = e.emp_name
FROM employees e
WHERE e.emp_id = emp_id;
RETURN emp_name;
END;
BEGIN
DECLARE
@error_code FLOAT;
BEGIN
UPDATE employees SET emp_salary = emp_salary * 1.10
WHERE emp_id > 1000;
IF @@FETCH_STATUS <> 0 BEGIN
SET @error_code = -1;
END
WHEN OTHERS THEN
SET @error_code = -2;
END;
IF @error_code < 0 BEGIN
RAISERROR('-20000 Error updating employee salaries', 16, 1);
END
END;
|
CREATE PROCEDURE update_employee(emp_id IN NUMBER, emp_name IN VARCHAR2, emp_salary IN NUMBER)
AS
BEGIN
UPDATE employees SET emp_name = emp_name, emp_salary = emp_salary
WHERE emp_id = emp_id;
COMMIT;
END;
DECLARE
emp_id NUMBER;
emp_name VARCHAR2(50);
emp_salary NUMBER;
BEGIN
SELECT e.emp_id INTO emp_id, e.emp_name INTO emp_name, e.emp_salary INTO emp_salary
FROM employees e
WHERE e.emp_id = :emp_id;
UPDATE employees SET emp_name = :emp_name, emp_salary = :emp_salary
WHERE emp_id = :emp_id;
COMMIT;
END;
CREATE FUNCTION get_employee_name(emp_id IN NUMBER) RETURN VARCHAR2
AS
emp_name VARCHAR2(50);
BEGIN
SELECT e.emp_name INTO emp_name
FROM employees e
WHERE e.emp_id = emp_id;
RETURN emp_name;
END;
BEGIN
DECLARE
error_code NUMBER;
BEGIN
UPDATE employees SET emp_salary = emp_salary * 1.10
WHERE emp_id > 1000;
EXCEPTION
WHEN NO_DATA_FOUND THEN
error_code := -1;
WHEN OTHERS THEN
error_code := -2;
END;
IF error_code < 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'Error updating employee salaries');
END IF;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
SELECT @v_empno = empno, @v_ename = ename FROM emp WHERE deptno = 10;
IF @v_empno IS NULL BEGIN
RAISERROR('-20001 Employee not found in department 10', 16, 1);
END
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
SELECT empno, ename INTO v_empno, v_ename FROM emp WHERE deptno = 10;
IF v_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found in department 10');
END IF;
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
END;
|
IF OBJECT_ID('transfer_funds', 'P') IS NOT NULL
DROP PROCEDURE transfer_funds;
GO
CREATE PROCEDURE transfer_funds(@IN sender_id AS NUMBER, IN receiver_id NUMBER, IN amount NUMBER) AS
BEGIN
DECLARE @sender_balance FLOAT;
DECLARE @receiver_balance FLOAT;
BEGIN
SELECT @sender_balance = balance FROM accounts WHERE account_id = sender_id FOR UPDATE;
SELECT @receiver_balance = balance FROM accounts WHERE account_id = receiver_id FOR UPDATE;
IF @sender_balance >= amount BEGIN
UPDATE accounts SET balance = balance - amount WHERE account_id = sender_id;
UPDATE accounts SET balance = balance + amount WHERE account_id = receiver_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Transfer successful!');
END
ELSE BEGIN
RAISERROR('-20001 Insufficient funds in sender account', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Account not found', 16, 1);
END
WHEN OTHER THEN
ROLLBACK;
RAISERROR('-20003 An unexpected error occurred.', 16, 1);
END;
END;
|
CREATE OR REPLACE PROCEDURE transfer_funds(IN sender_id NUMBER, IN receiver_id NUMBER, IN amount NUMBER) AS
BEGIN
DECLARE sender_balance NUMBER;
DECLARE receiver_balance NUMBER;
BEGIN
SELECT balance INTO sender_balance FROM accounts WHERE account_id = sender_id FOR UPDATE;
SELECT balance INTO receiver_balance FROM accounts WHERE account_id = receiver_id FOR UPDATE;
IF sender_balance >= amount THEN
UPDATE accounts SET balance = balance - amount WHERE account_id = sender_id;
UPDATE accounts SET balance = balance + amount WHERE account_id = receiver_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Transfer successful!');
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Insufficient funds in sender account');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Account not found');
WHEN OTHER THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20003, 'An unexpected error occurred.');
END;
END;
|
CREATE PROCEDURE IncrementCounter(@amount IN AS INTEGER) IS
counter INTEGER;
BEGIN
SELECT @counter = value FROM counter_table WHERE id = 1;
UPDATE counter_table SET value = counter + amount WHERE id = 1;
IF counter + amount > 100 BEGIN
RAISERROR('-20000 Counter exceeded limit', 16, 1);
END
END IncrementCounter;
|
PROCEDURE IncrementCounter(amount IN INTEGER) IS
counter INTEGER;
BEGIN
SELECT value INTO counter FROM counter_table WHERE id = 1;
UPDATE counter_table SET value = counter + amount WHERE id = 1;
IF counter + amount > 100 THEN
RAISE_APPLICATION_ERROR(-20000, 'Counter exceeded limit');
END IF;
END IncrementCounter;
|
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 = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with 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 = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_emp_id);
END;
|
CREATE PROCEDURE GetCustomerOrders (@CUST_ID IN AS NUMBER) IS
TYPE order_cursor IS REF CURSOR;
order_data order_cursor;
BEGIN
SELECT *
FROM orders
WHERE customer_id = CUST_ID;
WHILE 1=1 BEGIN
FETCH order_data INTO @order_record;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(order_record.order_id, '') + ' - ' + isnull(order_record.order_date, ''));
END;
CLOSE order_data;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + ISNULL(CUST_ID, ''));
END
END;
|
CREATE PROCEDURE GetCustomerOrders (CUST_ID IN NUMBER) IS
TYPE order_cursor IS REF CURSOR;
order_data order_cursor;
BEGIN
OPEN order_data FOR
SELECT *
FROM orders
WHERE customer_id = CUST_ID;
LOOP
FETCH order_data INTO order_record;
EXIT WHEN order_data%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(order_record.order_id || ' - ' || order_record.order_date);
END LOOP;
CLOSE order_data;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || CUST_ID);
END;
|
CREATE PROCEDURE sp_Test
(
@p_Input IN AS VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Input: ' + ISNULL(p_Input, ''));
IF p_Input = 'ERROR' BEGIN
RAISERROR('-20000 Error raised in SP', 16, 1);
END
END sp_Test;
/
BEGIN
dbo.sp_Test('Test');
dbo.sp_Test('ERROR');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE PROCEDURE sp_Test
(
p_Input IN VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Input: ' || p_Input);
IF p_Input = 'ERROR' THEN
RAISE_APPLICATION_ERROR(-20000, 'Error raised in SP');
END IF;
END sp_Test;
/
BEGIN
sp_Test('Test');
sp_Test('ERROR');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
|
IF OBJECT_ID('emp_details', 'P') IS NOT NULL
DROP PROCEDURE emp_details;
GO
CREATE PROCEDURE emp_details (@emp_id IN AS NUMBER, OUT emp_name VARCHAR2, OUT salary NUMBER) IS
BEGIN
SELECT @emp_name = ename, @salary = sal FROM emp WHERE empno = emp_id;
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 emp_details (emp_id IN NUMBER, OUT emp_name VARCHAR2, OUT salary NUMBER) IS
BEGIN
SELECT ename, sal INTO emp_name, salary FROM emp WHERE empno = emp_id;
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;
|