sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details(
@emp_id IN AS NUMBER,
OUT emp_name VARCHAR2,
OUT emp_salary NUMBER,
OUT err_msg OUT VARCHAR2
) AS
BEGIN
SELECT @emp_name = ename, @emp_salary = esal
FROM emp
WHERE empno = emp_id;
set @err_msg = NULL;
IF @@FETCH_STATUS <> 0 BEGIN
set @err_msg = 'Employee not found';
END
END;
BEGIN
dbo.get_employee_details(100,
emp_name => :emp_name,
emp_salary => :emp_salary,
err_msg => :err_msg);
IF :err_msg IS NOT NULL BEGIN
RAISERROR(-20000, :err_msg, 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE get_employee_details(
emp_id IN NUMBER,
OUT emp_name VARCHAR2,
OUT emp_salary NUMBER,
OUT err_msg OUT VARCHAR2
) AS
BEGIN
SELECT ename, esal INTO emp_name, emp_salary
FROM emp
WHERE empno = emp_id;
err_msg := NULL;
EXCEPTION
WHEN NO_DATA_FOUND THEN
err_msg := 'Employee not found';
END;
BEGIN
get_employee_details(100,
emp_name => :emp_name,
emp_salary => :emp_salary,
err_msg => :err_msg);
IF :err_msg IS NOT NULL THEN
RAISE_APPLICATION_ERROR(-20000, :err_msg);
END IF;
END;
|
CREATE PROCEDURE my_stored_procedure (
@in_param_1 IN AS VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Input parameter: ' + isnull(in_param_1, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END my_stored_procedure;
|
CREATE PROCEDURE my_stored_procedure (
in_param_1 IN VARCHAR2
)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Input parameter: ' || in_param_1);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END my_stored_procedure;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SET @v_empno = 1000;
SELECT @v_salary = salary FROM emp WHERE empno = @v_empno;
IF v_salary > 2000
BEGIN
SET @v_bonus = v_salary * 0.1;
END
ELSE BEGIN
SET @v_bonus = 0;
END
UPDATE emp SET bonus = v_bonus WHERE empno = @v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
v_empno := 1000;
SELECT salary INTO v_salary FROM emp WHERE empno = v_empno;
IF v_salary > 2000
THEN
v_bonus := v_salary * 0.1;
ELSE
v_bonus := 0;
END IF;
UPDATE emp SET bonus = v_bonus WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_empno FLOAT := 101;
BEGIN
-- Fe... SQLINES DEMO ***
SELECT *
FROM employees
WHERE empno = @v_empno;
END;
*** ock
BEGIN
*** e employee record
INSERT INTO employees (empno, ename)
VALUES (@v_empno, 'New Employee');
*** ction
COMMIT;
EXCEPTION
WHEN dup_val_on_index THEN
-- Ha... SQLINES DEMO ***
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Duplicate employee record found.');
END;
*** to get employee details
IF OBJECT_ID('get_emp_details', 'P') IS NOT NULL
DROP PROCEDURE get_emp_details;
GO
CREATE PROCEDURE get_emp_details (
@p_empno IN AS NUMBER,
out_ename OUT VARCHAR2
) IS
BEGIN
SELECT @out_ename = ename
FROM employees
WHERE empno = p_empno;
END;
*** cursor
DECLARE
c_emp CURSOR FOR
SELECT *
FROM employees;
BEGIN
*** cursor
DECLARE r CURSOR FOR c_emp OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** in the cursor
DBMS_OUTPUT.PUT_LINE(isnull(@r_empno, '') + ', ' + isnull(@r_ename, ''));
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
END;
|
DECLARE
v_empno NUMBER := 101;
BEGIN
-- Fetch employee data
SELECT *
FROM employees
WHERE empno = v_empno;
END;
-- Error handling block
BEGIN
-- Insert a duplicate employee record
INSERT INTO employees (empno, ename)
VALUES (v_empno, 'New Employee');
-- Commit the transaction
COMMIT;
EXCEPTION
WHEN dup_val_on_index THEN
-- Handle the error
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Duplicate employee record found.');
END;
-- Stored procedure to get employee details
CREATE OR REPLACE PROCEDURE get_emp_details (
p_empno IN NUMBER,
out_ename OUT VARCHAR2
) IS
BEGIN
SELECT ename
INTO out_ename
FROM employees
WHERE empno = p_empno;
END;
-- Looping through a cursor
DECLARE
c_emp CURSOR FOR
SELECT *
FROM employees;
BEGIN
-- Loop through the cursor
FOR r IN c_emp LOOP
-- Process each row in the cursor
DBMS_OUTPUT.PUT_LINE(r.empno || ', ' || r.ename);
END LOOP;
END;
|
declare
@v_rowCount int;
v_employee_id row_number(5);
v_salary row_number(10);
begin
select @v_rowCount = count(*) from employees;
dbms_output.put_line('Row count: ' + ISNULL(@v_rowCount, ''));
select @v_employee_id = employee_id, @v_salary = salary from employees where last_name like 'J%';
dbms_output.put_line('Employee ID: ' + isnull(v_employee_id, '') + ', Salary: ' + isnull(v_salary, ''));
set @v_salary= v_salary * 1.10;
update employees set salary = v_salary where employee_id = v_employee_id;
dbms_output.put_line('Salary updated for employee ID: ' + isnull(v_employee_id, ''));
if @@fetch_status <> 0 begin
dbms_output.put_line('No data found for the specified criteria.');
end
when update_error then
dbms_output.put_line('Error updating the employee's salary.' | declare
v_rowCount number(5);
v_employee_id number(5);
v_salary number(10);
begin
select count(*) into v_rowCount from employees;
dbms_output.put_line('Row count: ' || v_rowCount);
select employee_id, salary into v_employee_id, v_salary from employees where last_name like 'J%';
dbms_output.put_line('Employee ID: ' || v_employee_id || ', Salary: ' || v_salary);
v_salary:= v_salary * 1.10;
update employees set salary = v_salary where employee_id = v_employee_id;
dbms_output.put_line('Salary updated for employee ID: ' || v_employee_id);
exception
when no_data_found then
dbms_output.put_line('No data found for the specified criteria.');
when update_error then
dbms_output.put_line('Error updating the employee's salary.');
end; |
IF OBJECT_ID('calculate_average', 'P') IS NOT NULL
DROP PROCEDURE calculate_average;
GO
CREATE PROCEDURE calculate_average(@emp_id IN AS NUMBER, OUT average_salary OUT NUMBER) AS
total_salary SET @NUMBER = 0;
num_rows SET @NUMBER = 0;
BEGIN
SELECT @total_salary = SUM(salary) COUNT(*) INTO num_rows
FROM employee
WHERE manager_id = emp_id;
set @average_salary = total_salary / num_rows;
IF num_rows = 0 BEGIN
RAISERROR('-20001 No employees found for this manager', 16, 1);
END
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
average_salary NUMBER;
BEGIN
dbo.calculate_average(@emp_id, average_salary);
DBMS_OUTPUT.PUT_LINE('Average salary: ' + isnull(average_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found for this manager');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE PROCEDURE calculate_average(emp_id IN NUMBER, OUT average_salary OUT NUMBER) AS
total_salary NUMBER := 0;
num_rows NUMBER := 0;
BEGIN
SELECT SUM(salary) INTO total_salary, COUNT(*) INTO num_rows
FROM employee
WHERE manager_id = emp_id;
average_salary := total_salary / num_rows;
IF num_rows = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No employees found for this manager');
END IF;
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
average_salary NUMBER;
BEGIN
calculate_average(emp_id, average_salary);
DBMS_OUTPUT.PUT_LINE('Average salary: ' || average_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found for this manager');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|
CREATE PROCEDURE ProcessOrders
(
@IN_OrderID IN AS NUMBER,
OUT_Status OUT VARCHAR2,
OUT_ErrorMessage OUT VARCHAR2
)
IS
V_CurrentStatus dbo.VARCHAR2(20);
BEGIN
SELECT @V_CurrentStatus = Status
FROM Orders
WHERE OrderID = IN_OrderID;
IF V_CurrentStatus = 'Pending' BEGIN
UPDATE Orders SET Status = 'Processing'
WHERE OrderID = IN_OrderID;
SET @OUT_Status = 'Processing';
END
ELSE IF V_CurrentStatus = 'Processing' BEGIN
SET @OUT_Status = 'Processing';
END
ELSE IF V_CurrentStatus = 'Shipped' BEGIN
SET @OUT_Status = 'Shipped';
END
ELSE BEGIN
SET @OUT_Status = 'Error: Invalid OrderID';
SET @OUT_ErrorMessage = 'The specified OrderID is not valid.';
END
IF @@FETCH_STATUS <> 0 BEGIN
SET @OUT_Status = 'Error: OrderID not found';
SET @OUT_ErrorMessage = 'The specified OrderID does not exist in the database.';
END
END;
|
CREATE PROCEDURE ProcessOrders
(
IN_OrderID IN NUMBER,
OUT_Status OUT VARCHAR2,
OUT_ErrorMessage OUT VARCHAR2
)
IS
V_CurrentStatus VARCHAR2(20);
BEGIN
SELECT Status INTO V_CurrentStatus
FROM Orders
WHERE OrderID = IN_OrderID;
IF V_CurrentStatus = 'Pending' THEN
UPDATE Orders SET Status = 'Processing'
WHERE OrderID = IN_OrderID;
OUT_Status := 'Processing';
ELSIF V_CurrentStatus = 'Processing' THEN
OUT_Status := 'Processing';
ELSIF V_CurrentStatus = 'Shipped' THEN
OUT_Status := 'Shipped';
ELSE
OUT_Status := 'Error: Invalid OrderID';
OUT_ErrorMessage := 'The specified OrderID is not valid.';
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
OUT_Status := 'Error: OrderID not found';
OUT_ErrorMessage := 'The specified OrderID does not exist in the database.';
END;
|
BEGIN
DECLARE @lv_x INTEGER;
BEGIN
SELECT @lv_x = MAX(ID) FROM MY_TABLE;
END;
IF @lv_x IS NULL BEGIN
SET @lv_x = 0;
END
SET @lv_x = @lv_x + 1;
UPDATE MY_TABLE SET ID = @lv_x;
END;
/
|
BEGIN
DECLARE lv_x INTEGER;
BEGIN
SELECT MAX(ID) INTO lv_x FROM MY_TABLE;
END;
IF lv_x IS NULL THEN
lv_x := 0;
END IF;
lv_x := lv_x + 1;
UPDATE MY_TABLE SET ID = lv_x;
END;
/
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_id = 10;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET emp_name = 'Updated Name' WHERE emp_id = @v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
BEGIN
SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_id = 10;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET emp_name = 'Updated Name' WHERE emp_id = v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
RAISE;
END;
|
IF OBJECT_ID('calc_total_price', 'FN') IS NOT NULL
DROP FUNCTION calc_total_price;
GO
CREATE FUNCTION calc_total_price(@product_id FLOAT, @quantity FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @total_price FLOAT;
SELECT @total_price = price * @quantity
FROM products
WHERE @product_id = @product_id;
RETURN @total_price;
END;
IF OBJECT_ID('update_product_stock', 'P') IS NOT NULL
DROP PROCEDURE update_product_stock;
GO
CREATE PROCEDURE update_product_stock(@product_id FLOAT, @quantity FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE products
SET stock = stock - @quantity
WHERE @product_id = @product_id;
END;
DECLARE
@product_id FLOAT := 1;
quantity SET @NUMBER = 5;
total_price NUMBER;
BEGIN
SET @total_price = dbo.calc_total_price(@product_id, quantity);
IF total_price > 100 BEGIN
RAISERROR('-20001 Order total exceeds $100', 16, 1);
END
ELSE BEGIN
dbo.update_product_stock(@product_id, quantity);
END
END; | CREATE OR REPLACE FUNCTION calc_total_price(product_id NUMBER, quantity NUMBER) RETURN NUMBER IS
total_price NUMBER;
BEGIN
SELECT price * quantity
INTO total_price
FROM products
WHERE product_id = product_id;
RETURN total_price;
END;
CREATE OR REPLACE PROCEDURE update_product_stock(product_id NUMBER, quantity NUMBER) IS
BEGIN
UPDATE products
SET stock = stock - quantity
WHERE product_id = product_id;
END;
DECLARE
product_id NUMBER := 1;
quantity NUMBER := 5;
total_price NUMBER;
BEGIN
total_price := calc_total_price(product_id, quantity);
IF total_price > 100 THEN
RAISE_APPLICATION_ERROR(-20001, 'Order total exceeds $100');
ELSE
update_product_stock(product_id, quantity);
END IF;
END; |
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (
@p_num IN AS NUMBER
) AS
BEGIN
*** iables
l_var1 NUMBER;
l_var2 dbo.VARCHAR2(100);
-- Er... SQLINES DEMO ***
IF p_num < 0 BEGIN
RAISERROR('-20001 Invalid parameter value', 16, 1);
END
-- Co... SQLINES DEMO ***
IF p_num > 0 BEGIN
set @l_var1 = p_num * 2; END
ELSE
set @l_var1 = p_num / 2; END
SELECT @l_var2 = name FROM my_table WHERE id = p_num;
-- SQ... SQLINES DEMO ***
UPDATE my_table SET name = l_var2 WHERE id = p_num;
END;
|
CREATE OR REPLACE PROCEDURE my_proc (
p_num IN NUMBER
) AS
BEGIN
-- Declare local variables
l_var1 NUMBER;
l_var2 VARCHAR2(100);
-- Error handling
IF p_num < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid parameter value');
END IF;
-- Control flow
CASE WHEN p_num > 0 THEN
l_var1 := p_num * 2;
ELSE
l_var1 := p_num / 2;
END CASE;
SELECT name INTO l_var2 FROM my_table WHERE id = p_num;
-- SQL statement
UPDATE my_table SET name = l_var2 WHERE id = p_num;
END;
|
BEGIN
DECLARE
@emp_id FLOAT;
emp_salary NUMBER;
BEGIN
SELECT @emp_id = employee_id, @emp_salary = salary
FROM employees
WHERE last_name = 'King';
IF @emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
ELSE IF emp_salary > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @emp_id;
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee with that last name found.');
END
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('More than one employee with that last name found.');
END;
END; | BEGIN
DECLARE
emp_id NUMBER;
emp_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO emp_id, emp_salary
FROM employees
WHERE last_name = 'King';
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
ELSIF emp_salary > 10000 THEN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = emp_id;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with that last name found.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('More than one employee with that last name found.');
END;
END; |
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_job dbo.VARCHAR2(20);
v_mgr NUMBER;
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
v_max_sal NUMBER;
BEGIN
SELECT
@v_empno = empno,
@v_ename = ename,
@v_job = job,
@v_mgr = mgr,
@v_sal = sal,
@v_comm = comm,
@v_deptno = deptno
FROM
emp
WHERE
empno = :employee_id;
IF FOUND BEGIN
UPDATE
emp
SET
ename = :new_ename
WHERE
empno = :employee_id;
SET @v_max_sal = (
SELECT
MAX(sal)
FROM
emp
WHERE
deptno = v_deptno
);
IF v_sal > v_max_sal BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
COMMIT;
END
ELSE BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_job VARCHAR2(20);
v_mgr NUMBER;
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
v_max_sal NUMBER;
BEGIN
SELECT
empno,
ename,
job,
mgr,
sal,
comm,
deptno
INTO
v_empno,
v_ename,
v_job,
v_mgr,
v_sal,
v_comm,
v_deptno
FROM
emp
WHERE
empno = :employee_id;
IF FOUND THEN
UPDATE
emp
SET
ename = :new_ename
WHERE
empno = :employee_id;
v_max_sal := (
SELECT
MAX(sal)
FROM
emp
WHERE
deptno = v_deptno
);
IF v_sal > v_max_sal THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
END IF;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END IF;
END;
|
BEGIN
DECLARE
@v_name VARCHAR(50);
v_age NUMBER;
BEGIN
SELECT @v_name = name, @v_age = age FROM employees WHERE id = 1;
IF v_age < 18 BEGIN
RAISERROR('-20001 Age must be 18 or older', 16, 1);
END
*** rations
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No employee found with id 1', 16, 1);
END
WHEN TOO_MANY_ROWS THEN
RAISERROR('-20003 Multiple employees found with id 1', 16, 1);
END;
END; | BEGIN
DECLARE
v_name VARCHAR2(50);
v_age NUMBER;
BEGIN
SELECT name, age INTO v_name, v_age FROM employees WHERE id = 1;
IF v_age < 18 THEN
RAISE_APPLICATION_ERROR(-20001, 'Age must be 18 or older');
END IF;
-- Perform other operations
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No employee found with id 1');
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20003, 'Multiple employees found with id 1');
END;
END; |
DECLARE
@v_dept_id FLOAT;
BEGIN
SELECT @v_dept_id = deptno FROM dept WHERE dname = 'Sales';
IF @v_dept_id IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Sales department exists.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Sales department does not exist.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No department found with the specified name.');
END
END;
|
DECLARE
v_dept_id NUMBER;
BEGIN
SELECT deptno INTO v_dept_id FROM dept WHERE dname = 'Sales';
IF v_dept_id IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Sales department exists.');
ELSE
DBMS_OUTPUT.PUT_LINE('Sales department does not exist.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No department found with the specified name.');
END;
|
IF OBJECT_ID('calculate_salary', 'FN') IS NOT NULL
DROP FUNCTION calculate_salary;
GO
CREATE FUNCTION calculate_salary(@employee_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE @employee_id = @employee_id;
IF @salary IS NULL BEGIN
RAISERROR('-20000 Invalid employee ID', 16, 1);
END
RETURN @salary * 1.10;
END;
GO
DECLARE
@employee_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.calculate_salary(@employee_id);
DBMS_OUTPUT.PUT_LINE('Salary for employee ' + isnull(@employee_id, '') + ': ' + isnull(salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE OR REPLACE FUNCTION calculate_salary(employee_id NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Invalid employee ID');
END IF;
RETURN salary * 1.10;
END;
/
DECLARE
employee_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := calculate_salary(employee_id);
DBMS_OUTPUT.PUT_LINE('Salary for employee ' || employee_id || ': ' || salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
IF OBJECT_ID('calculate_order_total', 'P') IS NOT NULL
DROP PROCEDURE calculate_order_total;
GO
CREATE PROCEDURE calculate_order_total(@order_id IN AS NUMBER)
AS
v_order_total NUMBER;
BEGIN
SELECT @v_order_total = SUM(quantity * unit_price) FROM order_details WHERE order_id = order_id;
IF v_order_total IS NULL BEGIN
RAISERROR('-20001 Invalid order ID', 16, 1);
END
UPDATE orders SET total_amount = v_order_total WHERE order_id = order_id;
END;
/ | CREATE OR REPLACE PROCEDURE calculate_order_total(order_id IN NUMBER)
AS
v_order_total NUMBER;
BEGIN
SELECT SUM(quantity * unit_price) INTO v_order_total FROM order_details WHERE order_id = order_id;
IF v_order_total IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid order ID');
END IF;
UPDATE orders SET total_amount = v_order_total WHERE order_id = order_id;
END;
/ |
CREATE PROCEDURE Emp_Bonus(@emp_id IN AS NUMBER) AS
BEGIN
UPDATE Employees SET Salary = Salary * 1.10
WHERE EmployeeID = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE Emp_Bonus(emp_id IN NUMBER) AS
BEGIN
UPDATE Employees SET Salary = Salary * 1.10
WHERE EmployeeID = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('usp_UpdateEmployeeInfo', 'P') IS NOT NULL
DROP PROCEDURE usp_UpdateEmployeeInfo;
GO
CREATE PROCEDURE usp_UpdateEmployeeInfo (
@p_employee_id IN AS NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER,
p_department_id IN NUMBER,
p_error_code OUT NUMBER,
p_error_message OUT VARCHAR2
) AS
BEGIN
*** ee information
UPDATE employees SET name = p_name, salary = p_salary, department_id = p_department_id WHERE employee_id = p_employee_id;
*** te was successful
IF @@ROWCOUNT = 0 BEGIN
-- Up... SQLINES DEMO ***
set @p_error_code = -1;
set @p_error_message = 'Employee not found';
END
ELSE BEGIN
-- Up... SQLINES DEMO ***
set @p_error_code = 0;
set @p_error_message = NULL;
END
-- Co... SQLINES DEMO ***
COMMIT;
END usp_UpdateEmployeeInfo;
|
CREATE OR REPLACE PROCEDURE usp_UpdateEmployeeInfo (
p_employee_id IN NUMBER,
p_name IN VARCHAR2,
p_salary IN NUMBER,
p_department_id IN NUMBER,
p_error_code OUT NUMBER,
p_error_message OUT VARCHAR2
) AS
BEGIN
-- Update the employee information
UPDATE employees SET name = p_name, salary = p_salary, department_id = p_department_id WHERE employee_id = p_employee_id;
-- Check if the update was successful
IF SQL%ROWCOUNT = 0 THEN
-- Update failed
p_error_code := -1;
p_error_message := 'Employee not found';
ELSE
-- Update successful
p_error_code := 0;
p_error_message := NULL;
END IF;
-- Commit the changes
COMMIT;
END usp_UpdateEmployeeInfo;
|
DECLARE
@l_num_rows FLOAT;
BEGIN
SELECT @l_num_rows = COUNT(*) FROM emp;
IF @l_num_rows = 0 BEGIN
RAISERROR('-20001 No rows found in emp table', 16, 1);
END
UPDATE emp SET sal = sal * 1.10 WHERE deptno = 20;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salaries increased for department 20.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred. Salaries were not increased.');
END;
|
DECLARE
l_num_rows NUMBER;
BEGIN
SELECT COUNT(*) INTO l_num_rows FROM emp;
IF l_num_rows = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found in emp table');
END IF;
UPDATE emp SET sal = sal * 1.10 WHERE deptno = 20;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salaries increased for department 20.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred. Salaries were not increased.');
END;
|
CREATE TABLE employee (
id FLOAT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary FLOAT,
department VARCHAR(100)
);
INSERT INTO employee (id, name, salary, department) VALUES (1, 'John Doe', 10000, 'Sales');
INSERT INTO employee (id, name, salary, department) VALUES (2, 'Jane Smith', 12000, 'Marketing');
INSERT INTO employee (id, name, salary, department) VALUES (3, 'David Jones', 15000, 'Engineering');
CREATE FUNCTION get_employee_salary(@id FLOAT) RETURNS FLOAT
AS
BEGIN
SELECT @result = salary FROM employee WHERE @id = @id;
RETURN result;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary for employee 1: ' + isnull(@emp_salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while getting employee salary: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE employee (
id NUMBER PRIMARY KEY,
name VARCHAR2(100) NOT NULL,
salary NUMBER,
department VARCHAR2(100)
);
INSERT INTO employee (id, name, salary, department) VALUES (1, 'John Doe', 10000, 'Sales');
INSERT INTO employee (id, name, salary, department) VALUES (2, 'Jane Smith', 12000, 'Marketing');
INSERT INTO employee (id, name, salary, department) VALUES (3, 'David Jones', 15000, 'Engineering');
CREATE FUNCTION get_employee_salary(id NUMBER) RETURN NUMBER DETERMINISTIC
AS
BEGIN
SELECT salary INTO result FROM employee WHERE id = id;
RETURN result;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary for employee 1: ' || emp_salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while getting employee salary: ' || SQLERRM);
END;
|
DECLARE
@v_emp_id INT;
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;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No data found', 16, 1);
END
WHEN TOO_MANY_ROWS THEN
RAISERROR('-20003 Too many rows returned', 16, 1);
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
v_emp_id NUMBER(6);
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;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No data found');
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20003, 'Too many rows returned');
WHEN OTHERS THEN
RAISE;
END;
|
CREATE FUNCTION get_employee_salary(@employee_id FLOAT) RETURNS FLOAT
AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE @employee_id = @employee_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE @bonus FLOAT := 1000;
BEGIN
SET @bonus = @bonus * 2;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
RAISERROR('-20001 Too many rows returned.', 16, 1);
END;
DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(@bonus, ''));
END;
WHILE 1=1 BEGIN
EXIT WHEN employees.employee_id IS NULL;
UPDATE employees SET salary = salary + 100 WHERE employee_id = employees.employee_id;
FETCH employees INTO @employees;
END;
|
CREATE FUNCTION get_employee_salary(employee_id NUMBER) RETURN NUMBER
AS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE bonus NUMBER := 1000;
BEGIN
bonus := bonus * 2;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20001, 'Too many rows returned.');
END;
DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
END;
LOOP
EXIT WHEN employees.employee_id IS NULL;
UPDATE employees SET salary = salary + 100 WHERE employee_id = employees.employee_id;
FETCH employees INTO employees;
END LOOP;
|
DECLARE
@v_dept_id FLOAT;
v_dept_name dbo.VARCHAR2(20);
BEGIN
SET @v_dept_id = 10;
SELECT @v_dept_name = dname FROM dept WHERE deptno = @v_dept_id;
IF v_dept_name IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(v_dept_name, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Department does not exist');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the given department ID');
END
END;
/ | DECLARE
v_dept_id NUMBER;
v_dept_name VARCHAR2(20);
BEGIN
v_dept_id := 10;
SELECT dname INTO v_dept_name FROM dept WHERE deptno = v_dept_id;
IF v_dept_name IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Department Name: ' || v_dept_name);
ELSE
DBMS_OUTPUT.PUT_LINE('Department does not exist');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the given department ID');
END;
/ |
DECLARE
@v_empno FLOAT;
BEGIN
SET @v_empno = 7788;
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 Employee not found', 16, 1);
END;
END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name (@p_empno IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
v_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_name = ename FROM emp WHERE empno = p_empno;
RETURN v_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
|
DECLARE
v_empno NUMBER;
BEGIN
v_empno := 7788;
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
END;
CREATE OR REPLACE FUNCTION get_employee_name (p_empno IN NUMBER)
RETURN VARCHAR2 IS
v_name VARCHAR2(50);
BEGIN
SELECT ename INTO v_name FROM emp WHERE empno = p_empno;
RETURN v_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
|
IF OBJECT_ID('GetCustomerInfo', 'P') IS NOT NULL
DROP PROCEDURE GetCustomerInfo;
GO
CREATE PROCEDURE GetCustomerInfo(@cust_id IN AS NUMBER) IS
cust_name dbo.VARCHAR2(50);
BEGIN
SELECT @cust_name = name FROM customer WHERE id = cust_id;
IF cust_name IS NULL BEGIN
RAISERROR('-20001 Customer not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Customer name: ' + isnull(cust_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for customer with id: ' + isnull(cust_id, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END GetCustomerInfo;
|
CREATE OR REPLACE PROCEDURE GetCustomerInfo(cust_id IN NUMBER) IS
cust_name VARCHAR2(50);
BEGIN
SELECT name INTO cust_name FROM customer WHERE id = cust_id;
IF cust_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Customer name: ' || cust_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for customer with id: ' || cust_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END GetCustomerInfo;
|
CREATE PROCEDURE update_employee (
@employee_id IN AS NUMBER,
new_salary IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 Error updating employee salary', 16, 1);
END;
|
CREATE PROCEDURE update_employee (
employee_id IN NUMBER,
new_salary IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = employee_id;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'Error updating employee salary');
END;
|
CREATE PROCEDURE get_employee_salaries
AS
emp_ref_cursor SYS_REFCURSOR;
BEGIN
SELECT employee_id,
salary
FROM employees;
RETURN emp_ref_cursor;
END;
|
CREATE PROCEDURE get_employee_salaries
AS
emp_ref_cursor SYS_REFCURSOR;
BEGIN
OPEN emp_ref_cursor FOR
SELECT employee_id,
salary
FROM employees;
RETURN emp_ref_cursor;
END;
|
BEGIN
DECLARE
@num_rows FLOAT;
BEGIN
*** into a table
INSERT INTO employees (employee_id, last_name, email)
VALUES (1, 'Jones', 'jones@example.com');
-- Co... SQLINES DEMO ***
COMMIT;
EXCEPTION
WHEN OTHERS THEN
*** ges if an error occurs
ROLLBACK;
RAISE;
END;
*** rows in the table
SELECT @num_rows = COUNT(*) FROM employees;
*** r of rows
DBMS_OUTPUT.PUT_LINE('Number of rows: ' + isnull(@num_rows, ''));
END; | BEGIN
DECLARE
num_rows NUMBER;
BEGIN
-- Insert some data into a table
INSERT INTO employees (employee_id, last_name, email)
VALUES (1, 'Jones', 'jones@example.com');
-- Commit the changes
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Rollback any changes if an error occurs
ROLLBACK;
RAISE;
END;
-- Get the number of rows in the table
SELECT COUNT(*) INTO num_rows FROM employees;
-- Display the number of rows
DBMS_OUTPUT.PUT_LINE('Number of rows: ' || num_rows);
END; |
DECLARE
@v_deptno FLOAT;
BEGIN
SELECT @v_deptno = deptno FROM dept WHERE dname = 'Sales';
IF @v_deptno IS NULL BEGIN
RAISERROR('-20001 Department not found', 16, 1);
END
ELSE BEGIN
UPDATE emp SET deptno = @v_deptno WHERE empno = 7369;
END
END;
/
|
DECLARE
v_deptno NUMBER;
BEGIN
SELECT deptno INTO v_deptno FROM dept WHERE dname = 'Sales';
IF v_deptno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Department not found');
ELSE
UPDATE emp SET deptno = v_deptno WHERE empno = 7369;
END IF;
END;
/
|
DECLARE
@v_sal DECIMAL(10, 2);
BEGIN
SELECT @v_sal = salary
FROM employees
WHERE last_name = 'King';
IF @v_sal > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE last_name = 'King';
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low to give raise', 16, 1);
END
END;
|
DECLARE
v_sal NUMBER(10, 2);
BEGIN
SELECT salary
INTO v_sal
FROM employees
WHERE last_name = 'King';
IF v_sal > 10000 THEN
UPDATE employees
SET salary = salary * 1.10
WHERE last_name = 'King';
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to give raise');
END IF;
END;
|
if dbo.obj_exists('procedure', 'pr_update_employee') begin
dbms_output.put_line('Procedure exists!');
drop procedure pr_update_employee;
end
create procedure pr_update_employee(
@p_employee_id in as number,
p_first_name in varchar2,
p_salary in number
)
as
update employees
set first_name = p_first_name,
salary = p_salary
where employee_id = p_employee_id;
/
execute dbo.pr_update_employee(100, 'John', 50000);
commit;
|
if obj_exists('procedure', 'pr_update_employee') then
dbms_output.put_line('Procedure exists!');
drop procedure pr_update_employee;
end if;
create procedure pr_update_employee(
p_employee_id in number,
p_first_name in varchar2,
p_salary in number
)
as
update employees
set first_name = p_first_name,
salary = p_salary
where employee_id = p_employee_id;
/
execute pr_update_employee(100, 'John', 50000);
commit;
|
BEGIN
DECLARE
@employee_id FLOAT;
salary NUMBER;
BEGIN
SELECT @employee_id = @employee_id, @salary = salary
FROM employees
WHERE employee_name = 'John Doe';
IF salary < 50000 BEGIN
RAISERROR('-20001 Salary too low', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
END;
|
BEGIN
DECLARE
employee_id NUMBER;
salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO employee_id, salary
FROM employees
WHERE employee_name = 'John Doe';
IF salary < 50000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too low');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
END;
|
IF OBJECT_ID('emp_salary_raise', 'P') IS NOT NULL
DROP PROCEDURE emp_salary_raise;
GO
CREATE PROCEDURE emp_salary_raise(@emp_id IN AS NUMBER, raise_amt IN NUMBER)
AS
new_salary NUMBER;
BEGIN
SELECT @new_salary = salary FROM employees WHERE employee_id = emp_id;
UPDATE employees SET salary = new_salary + raise_amt WHERE employee_id = emp_id;
COMMIT;
END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
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 = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = emp_id;
RETURN emp_name;
END;
DECLARE
@total_salary FLOAT;
dept_id SET @NUMBER = 20;
BEGIN
SELECT @total_salary = SUM(salary) FROM employees WHERE department_id = dept_id;
IF @total_salary > 100000 BEGIN
RAISERROR('-20001 Total salary exceeds budget', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE emp_salary_raise(emp_id IN NUMBER, raise_amt IN NUMBER)
AS
new_salary NUMBER;
BEGIN
SELECT salary INTO new_salary FROM employees WHERE employee_id = emp_id;
UPDATE employees SET salary = new_salary + raise_amt WHERE employee_id = emp_id;
COMMIT;
END;
CREATE OR REPLACE FUNCTION get_employee_name(emp_id IN NUMBER) RETURN VARCHAR2
AS
emp_name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name INTO emp_name FROM employees WHERE employee_id = emp_id;
RETURN emp_name;
END;
DECLARE
total_salary NUMBER;
dept_id NUMBER := 20;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees WHERE department_id = dept_id;
IF total_salary > 100000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Total salary exceeds budget');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE SayHello(@name IN AS VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, ' + isnull(name, ''));
END SayHello;
|
PROCEDURE SayHello(name IN VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, ' || name);
END SayHello;
|
CREATE PROCEDURE Test_Emp(@p_Empid IN AS NUMBER) AS
BEGIN
SELECT * FROM Employees WHERE Empid = p_Empid;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
|
CREATE PROCEDURE Test_Emp(p_Empid IN NUMBER) AS
BEGIN
SELECT * FROM Employees WHERE Empid = p_Empid;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
|
CREATE TABLE T1 (ID FLOAT, NAME VARCHAR(20), SALARY FLOAT);
INSERT INTO T1 VALUES (1, 'John', 1000);
INSERT INTO T1 VALUES (2, 'Mary', 2000);
-- St... SQLINES DEMO ***
IF OBJECT_ID('GET_SALARY', 'P') IS NOT NULL
DROP PROCEDURE GET_SALARY;
GO
CREATE PROCEDURE GET_SALARY (@emp_id IN AS NUMBER, OUT salary OUT NUMBER) IS
BEGIN
SELECT @salary = SALARY FROM T1 WHERE ID = emp_id;
END;
/
*** dure
DECLARE
@emp_id FLOAT := 1;
salary NUMBER;
BEGIN
dbo.GET_SALARY(@emp_id, salary);
END;
-- Fu... SQLINES DEMO ***
IF OBJECT_ID('GET_BONUS', 'FN') IS NOT NULL
DROP FUNCTION GET_BONUS;
GO
CREATE FUNCTION GET_BONUS (@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
RETURN SALARY * 0.1;
END;
/
-- Ca... SQLINES DEMO ***
DECLARE
@emp_id FLOAT := 1;
bonus NUMBER;
BEGIN
SET @bonus = dbo.GET_BONUS(@emp_id);
END;
*** tructs
IF salary > 1500 BEGIN
SET @bonus = salary * 0.2;
END
ELSE IF salary > 1000 BEGIN
SET @bonus = salary * 0.1;
END
ELSE BEGIN
SET @bonus = 0;
END
*** enarios
BEGIN
DELETE FROM T1 WHERE ID = 3;
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END -- Ig... SQLINES DEMO ***
END;
|
CREATE TABLE T1 (ID NUMBER, NAME VARCHAR2(20), SALARY NUMBER);
INSERT INTO T1 VALUES (1, 'John', 1000);
INSERT INTO T1 VALUES (2, 'Mary', 2000);
-- Stored procedure
CREATE OR REPLACE PROCEDURE GET_SALARY (emp_id IN NUMBER, OUT salary OUT NUMBER) IS
BEGIN
SELECT SALARY INTO salary FROM T1 WHERE ID = emp_id;
END;
/
-- Call stored procedure
DECLARE
emp_id NUMBER := 1;
salary NUMBER;
BEGIN
GET_SALARY(emp_id, salary);
END;
-- Function
CREATE OR REPLACE FUNCTION GET_BONUS (emp_id IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN SALARY * 0.1;
END;
/
-- Call function
DECLARE
emp_id NUMBER := 1;
bonus NUMBER;
BEGIN
bonus := GET_BONUS(emp_id);
END;
-- Control flow constructs
IF salary > 1500 THEN
bonus := salary * 0.2;
ELSIF salary > 1000 THEN
bonus := salary * 0.1;
ELSE
bonus := 0;
END IF;
-- Error handling scenarios
BEGIN
DELETE FROM T1 WHERE ID = 3;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL; -- Ignore the error
END;
|
IF OBJECT_ID('add_employee', 'P') IS NOT NULL
DROP PROCEDURE add_employee;
GO
CREATE PROCEDURE add_employee(
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2
) AS
BEGIN
INSERT INTO employees (first_name, last_name, email)
VALUES (first_name, last_name, email);
COMMIT;
END;
/
DECLARE
@emp_id FLOAT;
BEGIN
SELECT @emp_id = employee_id FROM employees WHERE email = 'john.doe@example.com';
IF @emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET last_name = 'Smith' WHERE employee_id = @emp_id;
COMMIT;
END;
/
|
CREATE OR REPLACE PROCEDURE add_employee(
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2
) AS
BEGIN
INSERT INTO employees (first_name, last_name, email)
VALUES (first_name, last_name, email);
COMMIT;
END;
/
DECLARE
emp_id NUMBER;
BEGIN
SELECT employee_id INTO emp_id FROM employees WHERE email = 'john.doe@example.com';
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET last_name = 'Smith' WHERE employee_id = emp_id;
COMMIT;
END;
/
|
IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL
DROP PROCEDURE get_employee_info;
GO
CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER) AS
BEGIN
*** to store employee information
emp_name dbo.VARCHAR2(100);
salary ROW_NUMBER(10,2);
hire_date DATE;
*** e to get employee information
SELECT @emp_name = name, @salary = salary, @hire_date = hire_date
FROM employees
WHERE emp_id = emp_id;
*** ee information
DBMS_OUTPUT.put_line('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.put_line('Salary: ' + isnull(salary, ''));
DBMS_OUTPUT.put_line('Hire Date: ' + isnull(hire_date, ''));
END get_employee_info;
|
CREATE OR REPLACE PROCEDURE get_employee_info(emp_id IN NUMBER) AS
BEGIN
-- Declare variables to store employee information
emp_name VARCHAR2(100);
salary NUMBER(10,2);
hire_date DATE;
-- Query the database to get employee information
SELECT name, salary, hire_date
INTO emp_name, salary, hire_date
FROM employees
WHERE emp_id = emp_id;
-- Output the employee information
DBMS_OUTPUT.put_line('Employee Name: ' || emp_name);
DBMS_OUTPUT.put_line('Salary: ' || salary);
DBMS_OUTPUT.put_line('Hire Date: ' || hire_date);
END get_employee_info;
|
DECLARE
@salary FLOAT;
BEGIN
SET @salary = 5000;
IF @salary > 1000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is good');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is low');
END
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Iteration '+ isnull(i, ''));
END;
END; | DECLARE
salary NUMBER;
BEGIN
salary := 5000;
IF salary > 1000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is good');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is low');
END IF;
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration '|| i);
END LOOP;
END; |
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 100 BEGIN
IF i MOD 2 = 0 BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE id = i;
END
ELSE BEGIN
UPDATE employees SET salary = salary * 0.90 WHERE id = i;
END
END;
COMMIT;
END; | BEGIN
FOR i IN 1..100 LOOP
IF i MOD 2 = 0 THEN
UPDATE employees SET salary = salary * 1.10 WHERE id = i;
ELSE
UPDATE employees SET salary = salary * 0.90 WHERE id = i;
END IF;
END LOOP;
COMMIT;
END; |
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@employee_id FLOAT) RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @emp_name VARCHAR(100);
SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE @employee_id = @employee_id;
IF @emp_name IS NULL BEGIN
RAISERROR('-20001 Invalid employee ID', 16, 1);
END
RETURN @emp_name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id FLOAT, @new_salary FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employees
SET salary = @new_salary
WHERE @employee_id = @employee_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20002 Invalid employee ID', 16, 1);
END
END;
BEGIN
DECLARE @emp_name VARCHAR(100);
BEGIN
SET @emp_name = dbo.get_employee_name(100);
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@emp_name, ''));
END;
BEGIN
dbo.update_employee_salary(200, 20000);
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(employee_id NUMBER) RETURN VARCHAR2
AS
emp_name VARCHAR2(100);
BEGIN
SELECT first_name || ' ' || last_name
INTO emp_name
FROM employees
WHERE employee_id = employee_id;
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid employee ID');
END IF;
RETURN emp_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END get_employee_name;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id NUMBER, new_salary NUMBER)
AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = employee_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Invalid employee ID');
END IF;
END update_employee_salary;
BEGIN
DECLARE emp_name VARCHAR2(100);
BEGIN
emp_name := get_employee_name(100);
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name);
END;
BEGIN
update_employee_salary(200, 20000);
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; | EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END; |
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
BEGIN
RETURN (SELECT salary FROM employees WHERE employee_id = employee_id);
END;
BEGIN
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(100);
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN (SELECT salary FROM employees WHERE employee_id = employee_id);
END;
BEGIN
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(100);
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@employee_id IN AS NUMBER,
salary_increment IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = salary + salary_increment
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
employee_id IN NUMBER,
salary_increment IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = salary + salary_increment
WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@employee_id IN AS NUMBER,
new_salary IN NUMBER,
OUT error_code OUT VARCHAR2
)
IS
updated_rows NUMBER;
BEGIN
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
SELECT @updated_rows = COUNT(*) FROM employees
WHERE employee_id = employee_id AND salary = new_salary;
IF updated_rows = 0 BEGIN
set @error_code = 'EMP_NOT_FOUND';
END
ELSE BEGIN
set @error_code = 'SUCCESS';
END
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
employee_id IN NUMBER,
new_salary IN NUMBER,
OUT error_code OUT VARCHAR2
)
IS
updated_rows NUMBER;
BEGIN
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
SELECT COUNT(*) INTO updated_rows FROM employees
WHERE employee_id = employee_id AND salary = new_salary;
IF updated_rows = 0 THEN
error_code := 'EMP_NOT_FOUND';
ELSE
error_code := 'SUCCESS';
END IF;
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = 1;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary cannot be updated. Threshold not met.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
IF v_salary > 10000 THEN
UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = 1;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary cannot be updated. Threshold not met.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('increment_sales', 'P') IS NOT NULL
DROP PROCEDURE increment_sales;
GO
CREATE PROCEDURE increment_sales(@product_id IN AS NUMBER, increment_amount IN NUMBER)
AS
BEGIN
UPDATE SALES SET quantity = quantity + increment_amount WHERE product_id = product_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Product not found', 16, 1);
END
END;
IF OBJECT_ID('get_total_sales', 'FN') IS NOT NULL
DROP FUNCTION get_total_sales;
GO
CREATE FUNCTION get_total_sales() RETURNS FLOAT
AS
BEGIN
RETURN (
SELECT SUM(quantity) FROM SALES
);
END;
DECLARE
@total_sales FLOAT;
BEGIN
SET @total_sales = dbo.get_total_sales();
IF @total_sales > 100000 BEGIN
COMMIT;
END
ELSE BEGIN
ROLLBACK;
END
END;
|
CREATE OR REPLACE PROCEDURE increment_sales(product_id IN NUMBER, increment_amount IN NUMBER)
AS
BEGIN
UPDATE SALES SET quantity = quantity + increment_amount WHERE product_id = product_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Product not found');
END IF;
END;
CREATE OR REPLACE FUNCTION get_total_sales() RETURN NUMBER
AS
BEGIN
RETURN (
SELECT SUM(quantity) FROM SALES
);
END;
DECLARE
total_sales NUMBER;
BEGIN
total_sales := get_total_sales();
IF total_sales > 100000 THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@a IN AS NUMBER, b IN OUT NUMBER) IS
BEGIN
set @b = a * 2;
EXCEPTION
WHEN OTHERS THEN
set @b = NULL;
RAISERROR('-20000 An error occurred in my_proc', 16, 1);
END;
|
CREATE OR REPLACE PROCEDURE my_proc (a IN NUMBER, b IN OUT NUMBER) IS
BEGIN
b := a * 2;
EXCEPTION
WHEN OTHERS THEN
b := NULL;
RAISE_APPLICATION_ERROR(-20000, 'An error occurred in my_proc');
END;
|
IF OBJECT_ID('EXCEEDING_BUDGET', 'P') IS NOT NULL
DROP PROCEDURE EXCEEDING_BUDGET;
GO
CREATE PROCEDURE EXCEEDING_BUDGET (
@department_id IN AS NUMBER
)
AS
BEGIN
*** xpenses for the department
DECLARE @total_expenses FLOAT;
BEGIN
SELECT @total_expenses = SUM(expense_amount)
FROM expenses
WHERE department_id = department_id;
IF @@FETCH_STATUS <> 0 BEGIN
SET @total_expenses = 0;
END
END;
*** exceed budget
DECLARE @budget_amount FLOAT;
BEGIN
SELECT @budget_amount = @budget_amount
FROM departments
WHERE department_id = department_id;
IF @@FETCH_STATUS <> 0 BEGIN
SET @budget_amount = 0;
END
END;
IF @total_expenses > @budget_amount BEGIN
*** email
DBMS_OUTPUT.PUT_LINE('Expenses for Department ' + isnull(department_id, '') + ' exceed budget.');
END
END;
|
CREATE OR REPLACE PROCEDURE EXCEEDING_BUDGET (
department_id IN NUMBER
)
AS
BEGIN
-- Calculate total expenses for the department
DECLARE total_expenses NUMBER;
BEGIN
SELECT SUM(expense_amount)
INTO total_expenses
FROM expenses
WHERE department_id = department_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
total_expenses := 0;
END;
-- Check if expenses exceed budget
DECLARE budget_amount NUMBER;
BEGIN
SELECT budget_amount
INTO budget_amount
FROM departments
WHERE department_id = department_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
budget_amount := 0;
END;
IF total_expenses > budget_amount THEN
-- Send notification email
DBMS_OUTPUT.PUT_LINE('Expenses for Department ' || department_id || ' exceed budget.');
END IF;
END;
|
CREATE TABLE products (
id FLOAT,
name VARCHAR(255),
price FLOAT,
CONSTRAINT products_pk PRIMARY KEY (id)
);
INSERT INTO products (id, name, price) VALUES (1, 'Product 1', 10);
INSERT INTO products (id, name, price) VALUES (2, 'Product 2', 20);
INSERT INTO products (id, name, price) VALUES (3, 'Product 3', 30);
CREATE PROCEDURE get_product_by_id (
@IN p_id AS NUMBER,
OUT p_name VARCHAR(255),
OUT p_price NUMBER
)
IS
BEGIN
SELECT @p_name = name, @p_price = price
FROM products
WHERE id = p_id;
END;
DECLARE
@l_name VARCHAR(255);
l_price NUMBER;
BEGIN
dbo.get_product_by_id(1, @l_name, l_price);
DBMS_OUTPUT.PUT_LINE(isnull(@l_name, '') + ', ' + isnull(l_price, ''));
END;
|
CREATE TABLE products (
id NUMBER,
name VARCHAR(255),
price NUMBER,
CONSTRAINT products_pk PRIMARY KEY (id)
);
INSERT INTO products (id, name, price) VALUES (1, 'Product 1', 10);
INSERT INTO products (id, name, price) VALUES (2, 'Product 2', 20);
INSERT INTO products (id, name, price) VALUES (3, 'Product 3', 30);
CREATE PROCEDURE get_product_by_id (
IN p_id NUMBER,
OUT p_name VARCHAR(255),
OUT p_price NUMBER
)
IS
BEGIN
SELECT name, price
INTO p_name, p_price
FROM products
WHERE id = p_id;
END;
DECLARE
l_name VARCHAR(255);
l_price NUMBER;
BEGIN
get_product_by_id(1, l_name, l_price);
DBMS_OUTPUT.PUT_LINE(l_name || ', ' || l_price);
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(25);
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_salary = salary
FROM emp
WHERE empno = 7369;
DBMS_OUTPUT.PUT_LINE('Employee Number: ' + isnull(@v_empno, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(25);
v_salary NUMBER;
BEGIN
SELECT empno, ename, salary
INTO v_empno, v_ename, v_salary
FROM emp
WHERE empno = 7369;
DBMS_OUTPUT.PUT_LINE('Employee Number: ' || v_empno);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL
DROP PROCEDURE sample_proc;
GO
CREATE PROCEDURE sample_proc(@name IN AS VARCHAR2) AS
BEGIN
*** iables
counter SET @NUMBER = 0;
*** name
DECLARE @i INT = 1 ;
WHILE @i <= LEN(name) BEGIN
*** nter for each character
set @counter = counter + 1;
END;
-- Ou... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('The name ' + isnull(name, '') + ' has ' + isnull(counter, '') + ' characters.');
END;
-- Ca... SQLINES DEMO ***
BEGIN
dbo.sample_proc('John Doe');
END;
|
CREATE OR REPLACE PROCEDURE sample_proc(name IN VARCHAR2) AS
BEGIN
-- Declare local variables
counter NUMBER := 0;
-- Loop through the name
FOR i IN 1 .. LENGTH(name) LOOP
-- Increment the counter for each character
counter := counter + 1;
END LOOP;
-- Output the counter
DBMS_OUTPUT.PUT_LINE('The name ' || name || ' has ' || counter || ' characters.');
END;
-- Call the procedure
BEGIN
sample_proc('John Doe');
END;
|
DECLARE
@v_empno SMALLINT;
v_sal ROW_NUMBER(7,2);
BEGIN
SELECT @v_empno = empno,
@v_sal = salary
FROM emp
WHERE empno = 7839;
IF v_sal > 10000 BEGIN
UPDATE emp
SET salary = v_sal * 1.10
WHERE empno = @v_empno;
END
ELSE BEGIN
RAISERROR('-20101 Salary must be greater than 10000', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER(4);
v_sal NUMBER(7,2);
BEGIN
SELECT empno,
salary
INTO v_empno,
v_sal
FROM emp
WHERE empno = 7839;
IF v_sal > 10000 THEN
UPDATE emp
SET salary = v_sal * 1.10
WHERE empno = v_empno;
ELSE
RAISE_APPLICATION_ERROR(-20101, 'Salary must be greater than 10000');
END IF;
END;
|
CREATE PROCEDURE fetch_employee_salary(@emp_id IN AS VARCHAR2) AS
BEGIN
DECLARE @salary FLOAT;
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;
END;
|
CREATE PROCEDURE fetch_employee_salary(emp_id IN VARCHAR2) AS
BEGIN
DECLARE 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;
END;
|
CREATE TABLE employee(
id FLOAT,
name VARCHAR(30),
salary FLOAT,
PRIMARY KEY (id)
);
INSERT INTO employee VALUES (1, 'John Doe', 1000);
INSERT INTO employee VALUES (2, 'Jane Doe', 1500);
CREATE PROCEDURE get_employee(@in_id FLOAT, @out_name VARCHAR(4000), @out_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
SELECT @out_name = name, @out_salary = salary FROM employee WHERE id = @in_id;
END; |
CREATE TABLE employee(
id NUMBER,
name VARCHAR2(30),
salary NUMBER,
PRIMARY KEY (id)
);
INSERT INTO employee VALUES (1, 'John Doe', 1000);
INSERT INTO employee VALUES (2, 'Jane Doe', 1500);
CREATE PROCEDURE get_employee(in_id NUMBER, out_name VARCHAR2, out_salary NUMBER) AS
BEGIN
SELECT name, salary INTO out_name, out_salary FROM employee WHERE id = in_id;
END;
|
BEGIN
-- De... SQLINES DEMO ***
DECLARE
@emp_id DECIMAL(19,4) /* Use -meta option EMPLOYEE.employee_id%TYPE */;
salary NUMBER;
BEGIN
-- Ge... SQLINES DEMO ***
SELECT @emp_id = employee_id
FROM EMPLOYEE
WHERE last_name = 'Smith';
*** exists
IF @emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
-- Ge... SQLINES DEMO ***
SELECT @salary = salary
FROM SALARY
WHERE employee_id = @emp_id;
-- Di... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
END;
END; | BEGIN
-- Declare variables
DECLARE
emp_id EMPLOYEE.employee_id%TYPE;
salary NUMBER;
BEGIN
-- Get employee ID
SELECT employee_id
INTO emp_id
FROM EMPLOYEE
WHERE last_name = 'Smith';
-- Check if employee exists
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Get salary
SELECT salary
INTO salary
FROM SALARY
WHERE employee_id = emp_id;
-- Display salary
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
END;
END; |
IF OBJECT_ID('proc_test', 'P') IS NOT NULL
DROP PROCEDURE proc_test;
GO
CREATE PROCEDURE proc_test (@arg1 IN AS VARCHAR2, arg2 IN NUMBER)
IS
cnt NUMBER;
BEGIN
SELECT @cnt = COUNT(*) FROM test_table WHERE col1 = arg1;
IF cnt = 0 BEGIN
RAISERROR('-20000 No rows found for ' + isnull(arg1, ''), 16, 1);
END
UPDATE test_table SET col2 = arg2 WHERE col1 = arg1;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE proc_test (arg1 IN VARCHAR2, arg2 IN NUMBER)
IS
cnt NUMBER;
BEGIN
SELECT COUNT(*) INTO cnt FROM test_table WHERE col1 = arg1;
IF cnt = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No rows found for ' || arg1);
END IF;
UPDATE test_table SET col2 = arg2 WHERE col1 = arg1;
COMMIT;
END;
|
CREATE TABLE users (id FLOAT, name VARCHAR(255));
INSERT INTO users VALUES (1, 'John');
INSERT INTO users VALUES (2, 'Jane');
CREATE PROCEDURE get_user (@user_id IN AS NUMBER, user_name OUT VARCHAR2) IS
BEGIN
SELECT @user_name = name FROM users WHERE id = user_id;
END;
BEGIN
DECLARE @user_name VARCHAR(255);
dbo.get_user(2, @user_name);
DBMS_OUTPUT.PUT_LINE('User name: ' + isnull(@user_name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting user: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE users (id NUMBER, name VARCHAR2(255));
INSERT INTO users VALUES (1, 'John');
INSERT INTO users VALUES (2, 'Jane');
CREATE PROCEDURE get_user (user_id IN NUMBER, user_name OUT VARCHAR2) IS
BEGIN
SELECT name INTO user_name FROM users WHERE id = user_id;
END;
BEGIN
DECLARE user_name VARCHAR2(255);
get_user(2, user_name);
DBMS_OUTPUT.PUT_LINE('User name: ' || user_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting user: ' || SQLERRM);
END;
|
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL
DROP FUNCTION get_customer_orders;
GO
CREATE FUNCTION get_customer_orders(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
rc SYS_REFCURSOR;
BEGIN
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN rc;
END;
DECLARE
@customer_id FLOAT := 123;
orders SYS_REFCURSOR;
BEGIN
SET @orders = dbo.get_customer_orders(@customer_id);
WHILE 1=1 BEGIN
FETCH orders INTO @order_id, @order_date, @total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
*** ails here
END;
CLOSE orders;
IF @@FETCH_STATUS <> 0 BEGIN
END
*** found scenario
END;
|
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR IS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN rc;
END;
DECLARE
customer_id NUMBER := 123;
orders SYS_REFCURSOR;
BEGIN
orders := get_customer_orders(customer_id);
LOOP
FETCH orders INTO order_id, order_date, total_amount;
EXIT WHEN orders%NOTFOUND;
-- Process order details here
END LOOP;
CLOSE orders;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Handle no orders found scenario
END;
|
CREATE PROCEDURE GetCustomerOrders(@custid IN AS NUMBER) AS
CURSOR MyCursor IS
SELECT orderid, orderdate, totalamount
FROM orders
WHERE custid = custid;
BEGIN
DECLARE order_record CURSOR FOR MyCursor LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@order_record_orderid, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@order_record_orderdate, ''));
DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(@order_record_totalamount, ''));
FETCH order_record INTO;
END;
CLOSE order_record;
DEALLOCATE order_record;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(custid, ''));
END
END;
/
|
CREATE PROCEDURE GetCustomerOrders(custid IN NUMBER) AS
CURSOR MyCursor IS
SELECT orderid, orderdate, totalamount
FROM orders
WHERE custid = custid;
BEGIN
FOR order_record IN MyCursor LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_record.orderid);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || order_record.orderdate);
DBMS_OUTPUT.PUT_LINE('Total Amount: ' || order_record.totalamount);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || custid);
END;
/
|
CREATE PROCEDURE get_emp_by_dept(@deptno IN AS NUMBER) AS
BEGIN
SELECT empno, ename, job, sal
FROM emp
WHERE deptno = deptno;
END;
/
DECLARE
@v_sal FLOAT;
BEGIN
SELECT @v_sal = sal FROM emp WHERE empno = 7788;
IF @v_sal > 20000
BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 20,000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 20,000');
END
END;
/
CREATE FUNCTION get_avg_sal(@deptno IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (SELECT AVG(sal) FROM emp WHERE deptno = deptno);
END;
/
|
CREATE PROCEDURE get_emp_by_dept(deptno IN NUMBER) AS
BEGIN
SELECT empno, ename, job, sal
FROM emp
WHERE deptno = deptno;
END;
/
DECLARE
v_sal NUMBER;
BEGIN
SELECT sal INTO v_sal FROM emp WHERE empno = 7788;
IF v_sal > 20000
THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 20,000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 20,000');
END IF;
END;
/
CREATE FUNCTION get_avg_sal(deptno IN NUMBER) RETURN NUMBER AS
BEGIN
RETURN (SELECT AVG(sal) FROM emp WHERE deptno = deptno);
END;
/
|
BEGIN
DECLARE
@v_cursor SYS_REFCURSOR;
v_name VARCHAR(100);
v_age NUMBER;
BEGIN
SELECT name, age
FROM employee
WHERE age > 30;
WHILE 1=1 BEGIN
FETCH v_cursor INTO @v_name, @v_age;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(v_name, '') + ', Age: ' + isnull(v_age, ''));
END;
CLOSE v_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found with age greater than 30');
END
END;
END; | BEGIN
DECLARE
v_cursor SYS_REFCURSOR;
v_name VARCHAR(100);
v_age NUMBER;
BEGIN
OPEN v_cursor FOR
SELECT name, age
FROM employee
WHERE age > 30;
LOOP
FETCH v_cursor INTO v_name, v_age;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ', Age: ' || v_age);
END LOOP;
CLOSE v_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found with age greater than 30');
END;
END; |
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details (@emp_id IN AS NUMBER) IS
emp_name dbo.VARCHAR2(255);
emp_salary NUMBER;
BEGIN
SELECT @emp_name = first_name, @emp_salary = last_name, salary
FROM employees
WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID');
END
END;
|
CREATE OR REPLACE PROCEDURE get_employee_details (emp_id IN NUMBER) IS
emp_name VARCHAR2(255);
emp_salary NUMBER;
BEGIN
SELECT first_name, last_name, salary
INTO emp_name, emp_salary
FROM employees
WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID');
END;
|
IF OBJECT_ID('proc_example', 'P') IS NOT NULL
DROP PROCEDURE proc_example;
GO
CREATE PROCEDURE proc_example(
@p_id IN AS NUMBER,
p_name IN VARCHAR2
) AS
BEGIN
*** iables
l_id NUMBER;
l_name dbo.VARCHAR2(20);
*** local variables
SELECT @l_id = id, @l_name = name FROM table_name WHERE id = p_id;
-- Er... SQLINES DEMO ***
IF l_id IS NULL BEGIN
RAISERROR('-20001 No record found for ID: ' + isnull(p_id, ''), 16, 1);
END
*** local variables
UPDATE table_name SET name = p_name WHERE id = l_id;
-- Co... SQLINES DEMO ***
COMMIT;
END;
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(20);
BEGIN
*** in a cursor
DECLARE cur_row CURSOR FOR
SELECT id, name FROM table_name;
OPEN cur_row;
FETCH cur_row INTO;
WHILE @@FETCH_STATUS=0
BEGIN
SET @v_id = @cur_row_id;
SET @v_name = @cur_row_name;
*** dure
dbo.proc_example(@v_id, v_name);
FETCH cur_row INTO;
END;
CLOSE cur_row;
DEALLOCATE cur_row;
END;
|
CREATE OR REPLACE PROCEDURE proc_example(
p_id IN NUMBER,
p_name IN VARCHAR2
) AS
BEGIN
-- Declare local variables
l_id NUMBER;
l_name VARCHAR2(20);
-- Select data into local variables
SELECT id, name INTO l_id, l_name FROM table_name WHERE id = p_id;
-- Error handling
IF l_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No record found for ID: ' || p_id);
END IF;
-- Update data using local variables
UPDATE table_name SET name = p_name WHERE id = l_id;
-- Commit changes
COMMIT;
END;
DECLARE
v_id NUMBER;
v_name VARCHAR2(20);
BEGIN
-- Loop through rows in a cursor
FOR cur_row IN (
SELECT id, name FROM table_name
) LOOP
v_id := cur_row.id;
v_name := cur_row.name;
-- Call stored procedure
proc_example(v_id, v_name);
END LOOP;
END;
|
IF OBJECT_ID('insert_emp', 'P') IS NOT NULL
DROP PROCEDURE insert_emp;
GO
CREATE PROCEDURE insert_emp(
@emp_id IN AS NUMBER,
first_name IN VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER,
dept_id IN NUMBER
) AS
l_emp_id NUMBER;
BEGIN
SELECT @l_emp_id = emp_id
FROM emp
WHERE emp_id = emp_id;
IF l_emp_id IS NULL BEGIN
INSERT INTO emp (
emp_id,
first_name,
last_name,
salary,
dept_id
)
VALUES (
emp_id,
first_name,
last_name,
salary,
dept_id
);
END
ELSE BEGIN
RAISERROR('-20000 Employee already exists', 16, 1);
END
END;
/
BEGIN
dbo.insert_emp(1001, 'John', 'Doe', 50000, 20);
COMMIT;
END;
/
|
CREATE OR REPLACE PROCEDURE insert_emp(
emp_id IN NUMBER,
first_name IN VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER,
dept_id IN NUMBER
) AS
l_emp_id NUMBER;
BEGIN
SELECT emp_id
INTO l_emp_id
FROM emp
WHERE emp_id = emp_id;
IF l_emp_id IS NULL THEN
INSERT INTO emp (
emp_id,
first_name,
last_name,
salary,
dept_id
)
VALUES (
emp_id,
first_name,
last_name,
salary,
dept_id
);
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Employee already exists');
END IF;
END;
/
BEGIN
insert_emp(1001, 'John', 'Doe', 50000, 20);
COMMIT;
END;
/
|
IF OBJECT_ID('proc_demo', 'P') IS NOT NULL
DROP PROCEDURE proc_demo;
GO
CREATE PROCEDURE proc_demo(@var1 IN AS NUMBER)
AS
BEGIN
IF var1 > 0 BEGIN
dbms_output.put_line('Number is positive');
END
ELSE IF var1 < 0 BEGIN
RAISE exception_demo('Number is negative');
END
ELSE BEGIN
dbms_output.put_line('Number is zero');
END
dbms_output.put_line('Procedure completed');
EXCEPTION
WHEN exception_demo THEN
dbms_output.put_line('Exception occurred');
END proc_demo;
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 5 BEGIN
dbo.proc_demo(i);
END;
END;
/
|
CREATE OR REPLACE PROCEDURE proc_demo(var1 IN NUMBER)
AS
BEGIN
IF var1 > 0 THEN
dbms_output.put_line('Number is positive');
ELSEIF var1 < 0 THEN
RAISE exception_demo('Number is negative');
ELSE
dbms_output.put_line('Number is zero');
END IF;
dbms_output.put_line('Procedure completed');
EXCEPTION
WHEN exception_demo THEN
dbms_output.put_line('Exception occurred');
END proc_demo;
BEGIN
FOR i IN 1..5 LOOP
proc_demo(i);
END LOOP;
END;
/
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary, @v_dept_id = dept_id
FROM employees
WHERE emp_id = 100;
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = v_salary + 1000
WHERE emp_id = @v_emp_id;
END
ELSE IF v_salary > 5000 BEGIN
UPDATE employees
SET salary = v_salary + 500
WHERE emp_id = @v_emp_id;
END
ELSE BEGIN
UPDATE employees
SET salary = v_salary + 200
WHERE emp_id = @v_emp_id;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
v_dept_id NUMBER;
BEGIN
SELECT emp_id, emp_name, salary, dept_id
INTO v_emp_id, v_emp_name, v_salary, v_dept_id
FROM employees
WHERE emp_id = 100;
IF v_salary > 10000 THEN
UPDATE employees
SET salary = v_salary + 1000
WHERE emp_id = v_emp_id;
ELSIF v_salary > 5000 THEN
UPDATE employees
SET salary = v_salary + 500
WHERE emp_id = v_emp_id;
ELSE
UPDATE employees
SET salary = v_salary + 200
WHERE emp_id = v_emp_id;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @salary = NULL;
END
END;
CREATE FUNCTION get_employee_name(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN (SELECT isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = emp_id);
END;
BEGIN
*** truct: IF-THEN-ELSE
IF dbo.get_employee_name(1) = 'John Smith' BEGIN
*** DATE
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = 1;
COMMIT;
END
ELSE BEGIN
*** AISE_APPLICATION_ERROR
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
|
CREATE PROCEDURE get_employee_salary(emp_id IN NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
salary := NULL;
END;
CREATE FUNCTION get_employee_name(emp_id IN NUMBER) RETURN VARCHAR2 AS
BEGIN
RETURN (SELECT first_name || ' ' || last_name FROM employees WHERE employee_id = emp_id);
END;
BEGIN
-- Control flow construct: IF-THEN-ELSE
IF get_employee_name(1) = 'John Smith' THEN
-- SQL statement: UPDATE
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = 1;
COMMIT;
ELSE
-- Error handling: RAISE_APPLICATION_ERROR
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
|
BEGIN
-- De... SQLINES DEMO ***
v_emp_id NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
v_error_code NUMBER;
v_error_msg dbo.VARCHAR2(200);
*** rom user
SELECT @v_emp_id = emp_id
FROM employees
WHERE last_name = 'Smith';
*** and bonus
set @v_salary = 10000;
set @v_bonus = v_salary * 0.1;
*** salary and bonus
BEGIN
UPDATE employees
SET salary = v_salary,
bonus = v_bonus
WHERE emp_id = v_emp_id;
EXCEPTION
WHEN OTHERS THEN
set @v_error_code = @@ERROR;
set @v_error_msg = SQLERRM;
*** notify the user
DBMS_OUTPUT.PUT_LINE('Error updating employee: ' + isnull(v_error_code, '') + ' - ' + isnull(v_error_msg, ''));
END;
-- Co... SQLINES DEMO ***
COMMIT;
*** ed salary and bonus
SELECT @v_salary = salary, @v_bonus = bonus
FROM employees
WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.PUT_LINE('Updated bonus: ' + isnull(v_bonus, ''));
END;
|
BEGIN
-- Declare variables
v_emp_id NUMBER;
v_salary NUMBER;
v_bonus NUMBER;
v_error_code NUMBER;
v_error_msg VARCHAR2(200);
-- Get employee ID from user
SELECT emp_id
INTO v_emp_id
FROM employees
WHERE last_name = 'Smith';
-- Calculate salary and bonus
v_salary := 10000;
v_bonus := v_salary * 0.1;
-- Update employee's salary and bonus
BEGIN
UPDATE employees
SET salary = v_salary,
bonus = v_bonus
WHERE emp_id = v_emp_id;
EXCEPTION
WHEN OTHERS THEN
v_error_code := SQLCODE;
v_error_msg := SQLERRM;
-- Log the error and notify the user
DBMS_OUTPUT.PUT_LINE('Error updating employee: ' || v_error_code || ' - ' || v_error_msg);
END;
-- Commit the changes
COMMIT;
-- Display the updated salary and bonus
SELECT salary, bonus
INTO v_salary, v_bonus
FROM employees
WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Updated salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Updated bonus: ' || v_bonus);
END;
|
BEGIN
/* SQLINES DEMO *** and cursors */
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(30);
rs_emps SYS_REFCURSOR;
BEGIN
/* SQLINES DEMO *** tch employee data */
SELECT empno, ename
FROM emp;
/* SQLINES DEMO *** ursor and display */
FETCH rs_emps INTO @v_empno, @v_ename;
WHILE rs_emps%FOUND BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_empno, '') + ' - ' + isnull(v_ename, ''));
FETCH rs_emps INTO @v_empno, @v_ename;
END;
/* Close cursor */
CLOSE rs_emps;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END
END;
END;
|
BEGIN
/* Declare variables and cursors */
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(30);
rs_emps SYS_REFCURSOR;
BEGIN
/* Open cursor to fetch employee data */
OPEN rs_emps FOR
SELECT empno, ename
FROM emp;
/* Fetch data from cursor and display */
FETCH rs_emps INTO v_empno, v_ename;
WHILE rs_emps%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(v_empno || ' - ' || v_ename);
FETCH rs_emps INTO v_empno, v_ename;
END LOOP;
/* Close cursor */
CLOSE rs_emps;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END;
END;
|
DECLARE
@v_emp_id FLOAT := 100;
v_emp_name dbo.VARCHAR2(50);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT @v_emp_name = emp_name, @v_salary = salary, @v_bonus = bonus
FROM employees
WHERE emp_id = @v_emp_id;
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, ''));
IF v_bonus > 5000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for special bonus.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@v_emp_id, '') + ' not found.');
END
END;
|
DECLARE
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(50);
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT emp_name, salary, bonus
INTO v_emp_name, v_salary, v_bonus
FROM employees
WHERE emp_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Bonus: ' || v_bonus);
IF v_bonus > 5000 THEN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for special bonus.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' || v_emp_id || ' not found.');
END;
|
DECLARE
@v_num FLOAT := 10;
v_str dbo.VARCHAR2(20) := 'Hello World';
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= @v_num BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(v_str, '') + ' ' + isnull(i, ''));
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_num NUMBER := 10;
v_str VARCHAR2(20) := 'Hello World';
BEGIN
FOR i IN 1 .. v_num LOOP
DBMS_OUTPUT.PUT_LINE(v_str || ' ' || i);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('show_employees_by_dept', 'P') IS NOT NULL
DROP PROCEDURE show_employees_by_dept;
GO
CREATE PROCEDURE show_employees_by_dept(@dept_id IN AS NUMBER) AS
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = dept_id;
BEGIN
DECLARE emp_rec CURSOR FOR emp_cursor LOOP
dbms_output.put_line(isnull(@emp_rec_employee_id, '') + ' - ' + isnull(@emp_rec_first_name, '') + ' ' + isnull(@emp_rec_last_name, ''));
FETCH emp_rec INTO;
END;
CLOSE emp_rec;
DEALLOCATE emp_rec;
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No employees found in department ' + isnull(dept_id, ''));
END
END;
/
|
CREATE OR REPLACE PROCEDURE show_employees_by_dept(dept_id IN NUMBER) AS
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = dept_id;
BEGIN
FOR emp_rec IN emp_cursor LOOP
dbms_output.put_line(emp_rec.employee_id || ' - ' || emp_rec.first_name || ' ' || emp_rec.last_name);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No employees found in department ' || dept_id);
END;
/
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF @v_salary > 10000 BEGIN
RAISERROR('-20001 Salary is too high', 16, 1);
END
UPDATE employees SET salary = @v_salary + 1000 WHERE employee_id = 1;
COMMIT;
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
RAISE_APPLICATION_ERROR(-20001, 'Salary is too high');
END IF;
UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(250);
BEGIN
SELECT @v_id = id, @v_name = name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
IF @v_id > 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID is greater than 10');
END
ELSE IF @v_id < 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID is less than 10');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID is equal to 10');
END
END; | BEGIN
DECLARE
v_id NUMBER;
v_name VARCHAR2(250);
BEGIN
SELECT id, name INTO v_id, v_name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
IF v_id > 10 THEN
DBMS_OUTPUT.PUT_LINE('Employee ID is greater than 10');
ELSIF v_id < 10 THEN
DBMS_OUTPUT.PUT_LINE('Employee ID is less than 10');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ID is equal to 10');
END IF;
END; |
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(200);
v_salary NUMBER;
v_dept_id NUMBER;
CURSOR c_emp
IS
SELECT emp_id, emp_name, salary, dept_id
FROM employees;
BEGIN
-- Fe... SQLINES DEMO ***
DECLARE emp_row CURSOR FOR c_emp
LOOP
SET @v_emp_id = @emp_row_emp_id;
SET @v_emp_name = @emp_row_emp_name;
SET @v_salary = @emp_row_salary;
SET @v_dept_id = @emp_row_dept_id;
*** the employee data...
FETCH emp_row INTO;
END;
CLOSE emp_row;
DEALLOCATE emp_row;
*** salary
SELECT @v_salary = MAX(salary)
FROM employees;
*** ment of an employee
UPDATE employees
SET dept_id = 10
WHERE emp_id = 1;
-- Ha... SQLINES DEMO ***
BEGIN
*** might cause an error...
IF @@FETCH_STATUS <> 0 BEGIN
END
-- Ha... SQLINES DEMO ***
END;
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(200);
v_salary NUMBER;
v_dept_id NUMBER;
CURSOR c_emp
IS
SELECT emp_id, emp_name, salary, dept_id
FROM employees;
BEGIN
-- Fetch all employees
FOR emp_row IN c_emp
LOOP
v_emp_id := emp_row.emp_id;
v_emp_name := emp_row.emp_name;
v_salary := emp_row.salary;
v_dept_id := emp_row.dept_id;
-- Do something with the employee data...
END LOOP;
-- Find the highest salary
SELECT MAX(salary)
INTO v_salary
FROM employees;
-- Update the department of an employee
UPDATE employees
SET dept_id = 10
WHERE emp_id = 1;
-- Handle an error
BEGIN
-- Do something that might cause an error...
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Handle the error...
END;
END;
|
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL
DROP PROCEDURE sp_update_employee;
GO
CREATE PROCEDURE sp_update_employee(
@in_emp_id IN AS NUMBER,
in_last_name IN VARCHAR2,
in_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET last_name = in_last_name,
salary = in_salary
WHERE employee_id = in_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 Employee not found', 16, 1);
END;
|
CREATE OR REPLACE PROCEDURE sp_update_employee(
in_emp_id IN NUMBER,
in_last_name IN VARCHAR2,
in_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET last_name = in_last_name,
salary = in_salary
WHERE employee_id = in_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (
@param1 IN AS VARCHAR2,
param2 IN NUMBER,
OUT param3 OUT VARCHAR2
)
IS
BEGIN
-- So... SQLINES DEMO ***
IF param2 > 10 BEGIN
set @param3 = 'Greater than 10';
END
ELSE BEGIN
set @param3 = 'Less than or equal to 10';
END
-- Er... SQLINES DEMO ***
IF DBMS_ERRORCODE = 100 BEGIN
RAISERROR(
'-20000 Error occurred: ' + ISNULL(DBMS_ERROR_TEXT, '')
, 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE my_proc (
param1 IN VARCHAR2,
param2 IN NUMBER,
OUT param3 OUT VARCHAR2
)
IS
BEGIN
-- Some processing
IF param2 > 10 THEN
param3 := 'Greater than 10';
ELSE
param3 := 'Less than or equal to 10';
END IF;
-- Error handling
IF DBMS_ERRORCODE = 100 THEN
RAISE_APPLICATION_ERROR(
-20000,
'Error occurred: ' || DBMS_ERROR_TEXT
);
END IF;
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;
|
DECLARE
@v_emp_id DECIMAL(19,4) /* Use -meta option EMP.emp_id%TYPE */;
v_emp_name EMP.emp_name%TYPE;
v_salary ROW_NUMBER(10,2);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM EMP
WHERE emp_id = 7839;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20101 Employee not found.', 16, 1);
END
DBMS_OUTPUT.PUT_LINE(isnull(v_emp_name, '') + ' earns ' + ISNULL(CONVERT(VARCHAR, v_salary), '') + ' per month.');
END;
/
|
DECLARE
v_emp_id EMP.emp_id%TYPE;
v_emp_name EMP.emp_name%TYPE;
v_salary NUMBER(10,2);
BEGIN
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM EMP
WHERE emp_id = 7839;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20101, 'Employee not found.');
END IF;
DBMS_OUTPUT.PUT_LINE(v_emp_name || ' earns ' || TO_CHAR(v_salary) || ' per month.');
END;
/
|
CREATE TABLE orders (order_id FLOAT, product_id FLOAT, quantity FLOAT, PRIMARY KEY (order_id));
INSERT INTO orders VALUES (1, 10, 5);
INSERT INTO orders VALUES (2, 20, 10);
INSERT INTO orders VALUES (3, 30, 15);
CREATE PROCEDURE get_order_total (@order_id IN AS NUMBER, total_out OUT NUMBER) AS
BEGIN
SELECT @total_out = SUM(quantity) FROM orders WHERE order_id = order_id;
END;
BEGIN
DECLARE
@order_id FLOAT := 1;
total NUMBER;
BEGIN
dbo.get_order_total(@order_id, total);
DBMS_OUTPUT.PUT_LINE('Total for order ' + isnull(@order_id, '') + ' is ' + isnull(total, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for order ' + isnull(@order_id, ''));
END
END;
END;
|
CREATE TABLE orders (order_id NUMBER, product_id NUMBER, quantity NUMBER, PRIMARY KEY (order_id));
INSERT INTO orders VALUES (1, 10, 5);
INSERT INTO orders VALUES (2, 20, 10);
INSERT INTO orders VALUES (3, 30, 15);
CREATE PROCEDURE get_order_total (order_id IN NUMBER, total_out OUT NUMBER) AS
BEGIN
SELECT SUM(quantity) INTO total_out FROM orders WHERE order_id = order_id;
END;
BEGIN
DECLARE
order_id NUMBER := 1;
total NUMBER;
BEGIN
get_order_total(order_id, total);
DBMS_OUTPUT.PUT_LINE('Total for order ' || order_id || ' is ' || total);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for order ' || order_id);
END;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'King';
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_emp_id;
END
ELSE BEGIN
RAISERROR(
'-20001 Salary must be greater than 10000 for a raise'
, 16, 1);
END
END; | DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'King';
IF v_salary > 10000 THEN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_emp_id;
ELSE
RAISE_APPLICATION_ERROR(
-20001,
'Salary must be greater than 10000 for a raise'
);
END IF;
END; |
CREATE PROCEDURE update_dept_salary
(
@deptno IN AS NUMBER,
percent_change IN NUMBER
)
AS
v_salary SET @NUMBER = 0;
BEGIN
DECLARE emp_rec CURSOR FOR
SELECT
empno,
ename,
job,
sal
FROM
emp
WHERE
deptno = deptno;
OPEN emp_rec;
FETCH emp_rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
set @v_salary = @emp_rec_sal * (1 + percent_change / 100);
UPDATE emp
SET sal = v_salary
WHERE
empno = @emp_rec_empno;
DBMS_OUTPUT.PUT_LINE(
'Updated salary for ' + isnull(@emp_rec_ename, '') + ' (' + isnull(@emp_rec_job, '') + '): ' + isnull(v_salary, '')
);
FETCH emp_rec INTO;
END;
CLOSE emp_rec;
DEALLOCATE emp_rec;
END;
/
DECLARE
@i FLOAT := 1;
total_salary SET @NUMBER = 0;
BEGIN
WHILE 1=1 BEGIN
SELECT
@total_salary = sal
FROM
emp
WHERE
empno = @i;
EXIT
WHEN total_salary IS NULL;
SET @i = @i + 1;
END;
DBMS_OUTPUT.PUT_LINE('Total salary for all employees: ' + isnull(total_salary, ''));
END;
/
|
CREATE PROCEDURE update_dept_salary
(
deptno IN NUMBER,
percent_change IN NUMBER
)
AS
v_salary NUMBER := 0;
BEGIN
FOR emp_rec IN (
SELECT
empno,
ename,
job,
sal
FROM
emp
WHERE
deptno = deptno
)
LOOP
v_salary := emp_rec.sal * (1 + percent_change / 100);
UPDATE emp
SET sal = v_salary
WHERE
empno = emp_rec.empno;
DBMS_OUTPUT.PUT_LINE(
'Updated salary for ' || emp_rec.ename || ' (' || emp_rec.job || '): ' || v_salary
);
END LOOP;
END;
/
DECLARE
i NUMBER := 1;
total_salary NUMBER := 0;
BEGIN
LOOP
SELECT
sal
INTO
total_salary
FROM
emp
WHERE
empno = i;
EXIT
WHEN total_salary IS NULL;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total salary for all employees: ' || total_salary);
END;
/
|
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_employee(
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
phone_number IN VARCHAR2,
hire_date IN DATE,
job_id IN VARCHAR2,
salary IN NUMBER,
commission_pct IN NUMBER,
manager_id IN NUMBER,
department_id IN NUMBER)
IS
BEGIN
INSERT INTO employees(
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id)
VALUES(
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE insert_employee(
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
phone_number IN VARCHAR2,
hire_date IN DATE,
job_id IN VARCHAR2,
salary IN NUMBER,
commission_pct IN NUMBER,
manager_id IN NUMBER,
department_id IN NUMBER)
IS
BEGIN
INSERT INTO employees(
employee_id,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id)
VALUES(
employees_seq.NEXTVAL,
first_name,
last_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE get_customer_orders(@p_customer_id IN AS NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
DECLARE r_order CURSOR FOR c_orders LOOP
SELECT @v_order_id = order_id, @v_order_date = order_date, @v_total_amount = total_amount
FROM r_order;
FETCH r_order INTO;
END;
CLOSE r_order;
DEALLOCATE r_order;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20100 No orders found for customer', 16, 1);
END
END;
/
DECLARE
@l_order_id FLOAT;
l_order_date DATE;
l_total_amount NUMBER;
BEGIN
SELECT @l_order_id = order_id, @l_order_date = order_date, @l_total_amount = total_amount
FROM orders
WHERE customer_id = 1;
DBMS_OUTPUT.put_line('Order ID: ' + isnull(@l_order_id, ''));
DBMS_OUTPUT.put_line('Order Date: ' + isnull(l_order_date, ''));
DBMS_OUTPUT.put_line('Total Amount: ' + isnull(l_total_amount, ''));
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20100 No orders found for customer', 16, 1);
END
END;
/
|
CREATE PROCEDURE get_customer_orders(p_customer_id IN NUMBER) AS
CURSOR c_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
FOR r_order IN c_orders LOOP
SELECT order_id, order_date, total_amount
INTO v_order_id, v_order_date, v_total_amount
FROM r_order;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20100, 'No orders found for customer');
END;
/
DECLARE
l_order_id NUMBER;
l_order_date DATE;
l_total_amount NUMBER;
BEGIN
SELECT order_id, order_date, total_amount
INTO l_order_id, l_order_date, l_total_amount
FROM orders
WHERE customer_id = 1;
DBMS_OUTPUT.put_line('Order ID: ' || l_order_id);
DBMS_OUTPUT.put_line('Order Date: ' || l_order_date);
DBMS_OUTPUT.put_line('Total Amount: ' || l_total_amount);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20100, 'No orders found for customer');
END;
/
|
CREATE PROCEDURE example_proc (
@id_in IN AS NUMBER,
name_out OUT VARCHAR2
)
IS
l_name dbo.VARCHAR2(255);
BEGIN
SELECT @l_name = name FROM table_name WHERE id = id_in;
set @name_out = l_name;
IF @@FETCH_STATUS <> 0 BEGIN
set @name_out = NULL;
END
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE example_proc (
id_in IN NUMBER,
name_out OUT VARCHAR2
)
IS
l_name VARCHAR2(255);
BEGIN
SELECT name INTO l_name FROM table_name WHERE id = id_in;
name_out := l_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
name_out := NULL;
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_salary FLOAT;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_salary = salary, @v_emp_name = ename FROM emp WHERE empno = 1;
DBMS_OUTPUT.put_line('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.put_line('Salary: ' + isnull(@v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.put_line('Employee not found');
END
END;
|
DECLARE
v_salary NUMBER;
v_emp_name VARCHAR2(20);
BEGIN
SELECT salary, ename INTO v_salary, v_emp_name FROM emp WHERE empno = 1;
DBMS_OUTPUT.put_line('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.put_line('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line('Employee not found');
END;
|
IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL
DROP PROCEDURE get_employee_info;
GO
CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER) AS
BEGIN
SELECT
@emp_info = employee_id,
first_name,
last_name,
email,
phone_number
FROM
employees
WHERE
employee_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE get_employee_info(emp_id IN NUMBER) AS
BEGIN
SELECT
employee_id,
first_name,
last_name,
email,
phone_number
INTO
emp_info
FROM
employees
WHERE
employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END;
|
DECLARE
@month INTEGER;
@day INTEGER;
@year INTEGER;
BEGIN
SELECT @month = CONVERT(DATETIME, CONVERT(DATE, GETDATE())) @day, @year;
IF @month IN (1, 2, 3) BEGIN
UPDATE WEATHER SET RAIN_AMOUNT = RAIN_AMOUNT + 1 WHERE CITY = 'Seattle';
END
ELSE IF @month IN (7, 8, 9) BEGIN
UPDATE WEATHER SET SUNSHINE_HOURS = SUNSHINE_HOURS + 1 WHERE CITY = 'Portland';
END
ELSE IF @month IN (10, 11, 12) BEGIN
UPDATE WEATHER SET SNOW_AMOUNT = SNOW_AMOUNT + 1 WHERE CITY = 'Boise';
END
END; | DECLARE
@month INTEGER;
@day INTEGER;
@year INTEGER;
BEGIN
SELECT TRUNC(SYSDATE) INTO @month, @day, @year;
IF @month IN (1, 2, 3) THEN
UPDATE WEATHER SET RAIN_AMOUNT = RAIN_AMOUNT + 1 WHERE CITY = 'Seattle';
ELSIF @month IN (7, 8, 9) THEN
UPDATE WEATHER SET SUNSHINE_HOURS = SUNSHINE_HOURS + 1 WHERE CITY = 'Portland';
ELSIF @month IN (10, 11, 12) THEN
UPDATE WEATHER SET SNOW_AMOUNT = SNOW_AMOUNT + 1 WHERE CITY = 'Boise';
END IF;
END; |
BEGIN
DECLARE @x INT := 5;
DECLARE @y INT := 10;
DECLARE @z INT;
BEGIN
SET @z = @x + @y;
DBMS_OUTPUT.PUT_LINE('The sum is ' + isnull(@z, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
|
BEGIN
DECLARE x NUMBER(6) := 5;
DECLARE y NUMBER(6) := 10;
DECLARE z NUMBER(6);
BEGIN
z := x + y;
DBMS_OUTPUT.PUT_LINE('The sum is ' || z);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
|
DECLARE
@l_emp_id FLOAT;
l_emp_name dbo.VARCHAR2(50);
l_emp_salary NUMBER;
BEGIN
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 No employee found with the specified ID', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
SELECT @l_emp_id = emp_id, @l_emp_name = emp_name, @l_emp_salary = emp_salary
FROM employees
WHERE emp_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@l_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(l_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(l_emp_salary, ''));
END;
|
DECLARE
l_emp_id NUMBER;
l_emp_name VARCHAR2(50);
l_emp_salary NUMBER;
BEGIN
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'No employee found with the specified ID');
WHEN OTHERS THEN
RAISE;
END;
SELECT emp_id, emp_name, emp_salary
INTO l_emp_id, l_emp_name, l_emp_salary
FROM employees
WHERE emp_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || l_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || l_emp_salary);
END;
|
CREATE PROCEDURE get_customer(@cust_id IN AS NUMBER) IS
customer_name dbo.VARCHAR2(50);
BEGIN
SELECT @customer_name = name FROM customers WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(customer_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END
END;
|
PROCEDURE get_customer(cust_id IN NUMBER) IS
customer_name VARCHAR2(50);
BEGIN
SELECT name INTO customer_name FROM customers WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || customer_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END;
|
BEGIN
DECLARE
@n FLOAT;
BEGIN
SELECT @n = COUNT(*) FROM employees;
IF @n > 0 BEGIN
RAISERROR('-20000 Employees table is not empty', 16, 1);
END
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows found in employees table');
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found');
END
END;
BEGIN
INSERT INTO employees (last_name, first_name, department_id)
VALUES ('Doe', 'John', 10);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Employee with that last name already exists');
END;
END; | BEGIN
DECLARE
n NUMBER;
BEGIN
SELECT COUNT(*) INTO n FROM employees;
IF n > 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'Employees table is not empty');
END IF;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows found in employees table');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found');
END;
BEGIN
INSERT INTO employees (last_name, first_name, department_id)
VALUES ('Doe', 'John', 10);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Employee with that last name already exists');
END;
END; |
DECLARE
@v_deptno FLOAT;
v_salary NUMBER;
v_employee dbo.VARCHAR2(20);
BEGIN
SELECT TOP 1 @v_deptno = deptno, @v_salary = salary, @v_employee = employee
FROM emp
;
IF @v_deptno IS NULL BEGIN
DBMS_OUTPUT.put_line('No records found.');
END
ELSE BEGIN
DBMS_OUTPUT.put_line('Department Number: ' + isnull(@v_deptno, ''));
DBMS_OUTPUT.put_line('Salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.put_line('Employee: ' + isnull(v_employee, ''));
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.put_line('No records found.');
END
END;
/
|
DECLARE
v_deptno NUMBER;
v_salary NUMBER;
v_employee VARCHAR2(20);
BEGIN
SELECT deptno, salary, employee INTO v_deptno, v_salary, v_employee
FROM emp
WHERE rownum = 1;
IF v_deptno IS NULL THEN
DBMS_OUTPUT.put_line('No records found.');
ELSE
DBMS_OUTPUT.put_line('Department Number: ' || v_deptno);
DBMS_OUTPUT.put_line('Salary: ' || v_salary);
DBMS_OUTPUT.put_line('Employee: ' || v_employee);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line('No records found.');
END;
/
|
CREATE PROCEDURE GetCustomerOrders(
@customer_ID IN AS NUMBER
)
AS
BEGIN
*** related to the customer.
SELECT order_ID, order_date, total_amount
FROM Orders
WHERE customer_ID = customer_ID;
END GetCustomerOrders;
/ | CREATE PROCEDURE GetCustomerOrders(
customer_ID IN NUMBER
)
AS
BEGIN
-- Fetch all orders related to the customer.
SELECT order_ID, order_date, total_amount
FROM Orders
WHERE customer_ID = customer_ID;
END GetCustomerOrders;
/ |
DECLARE
@num FLOAT := 10;
BEGIN
BEGIN
SELECT @num = COUNT(*) FROM employees WHERE job_id = 'CLERK';
IF @@FETCH_STATUS <> 0 BEGIN SET @num = 0;
END
END;
IF @num > 0 BEGIN
DECLARE emp CURSOR FOR
SELECT employee_id, first_name, last_name
FROM employees
WHERE job_id = 'CLERK';
OPEN emp;
FETCH emp INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(@emp_first_name, '') + ' ' + isnull(@emp_last_name, ''));
FETCH emp INTO;
END;
CLOSE emp;
DEALLOCATE emp;
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No clerks found');
END
END;
|
DECLARE
num NUMBER := 10;
BEGIN
BEGIN
SELECT COUNT(*) INTO num FROM employees WHERE job_id = 'CLERK';
EXCEPTION
WHEN NO_DATA_FOUND THEN num := 0;
END;
IF num > 0 THEN
FOR emp IN (
SELECT employee_id, first_name, last_name
FROM employees
WHERE job_id = 'CLERK'
) LOOP
DBMS_OUTPUT.PUT_LINE('Employee: ' || emp.first_name || ' ' || emp.last_name);
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('No clerks found');
END IF;
END;
|
CREATE PROCEDURE prc_emp_salary_update(
@p_emp_id IN AS NUMBER,
p_new_salary IN NUMBER
) IS
sal_diff NUMBER;
BEGIN
SELECT @sal_diff = salary FROM emp WHERE empno = p_emp_id;
IF p_new_salary < sal_diff BEGIN
RAISERROR('-20501 Invalid salary: must be greater than old salary', 16, 1);
END
ELSE BEGIN
UPDATE emp SET salary = p_new_salary
WHERE empno = p_emp_id;
COMMIT;
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE prc_emp_salary_update(
p_emp_id IN NUMBER,
p_new_salary IN NUMBER
) IS
sal_diff NUMBER;
BEGIN
SELECT salary INTO sal_diff FROM emp WHERE empno = p_emp_id;
IF p_new_salary < sal_diff THEN
RAISE_APPLICATION_ERROR(-20501, 'Invalid salary: must be greater than old salary');
ELSE
UPDATE emp SET salary = p_new_salary
WHERE empno = p_emp_id;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
BEGIN
INSERT INTO employees (emp_id, name, salary) VALUES (1234, 'John Doe', 10000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
@emp_name VARCHAR(50);
BEGIN
SELECT @emp_name = name FROM employees WHERE emp_id = 1234;
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END; | BEGIN
BEGIN
INSERT INTO employees (emp_id, name, salary) VALUES (1234, 'John Doe', 10000);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
emp_name VARCHAR2(50);
BEGIN
SELECT name INTO emp_name FROM employees WHERE emp_id = 1234;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END; |
IF OBJECT_ID('greet_user', 'P') IS NOT NULL
DROP PROCEDURE greet_user;
GO
CREATE PROCEDURE greet_user(@fullname IN AS VARCHAR2)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, ' + isnull(fullname, '') + '!');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
DECLARE
@fullname VARCHAR(30) := 'John Doe';
BEGIN
dbo.greet_user(@fullname);
END;
|
CREATE OR REPLACE PROCEDURE greet_user(fullname IN VARCHAR2)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, ' || fullname || '!');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
DECLARE
fullname VARCHAR2(30) := 'John Doe';
BEGIN
greet_user(fullname);
END;
|