sqlserver
stringlengths
59
30k
plsql
stringlengths
54
4.67k
IF OBJECT_ID('ExampleProcedure', 'P') IS NOT NULL DROP PROCEDURE ExampleProcedure; GO CREATE PROCEDURE ExampleProcedure ( @p_Param1 IN AS NUMBER, p_Param2 IN VARCHAR2 ) AS BEGIN -- So... SQLINES DEMO *** IF p_Param1 > 10 BEGIN RAISERROR('-20000 Error: p_Param1 is greater than 10', 16, 1); END -- So... SQLINES DEMO *** UPDATE MyTable SET Column1 = p_Param2 WHERE Column2 = p_Param1; COMMIT; END ExampleProcedure;
CREATE OR REPLACE PROCEDURE ExampleProcedure ( p_Param1 IN NUMBER, p_Param2 IN VARCHAR2 ) AS BEGIN -- Some code here IF p_Param1 > 10 THEN RAISE_APPLICATION_ERROR(-20000, 'Error: p_Param1 is greater than 10'); END IF; -- Some more code here UPDATE MyTable SET Column1 = p_Param2 WHERE Column2 = p_Param1; COMMIT; END ExampleProcedure;
DECLARE @emp_id FLOAT; salary NUMBER; BEGIN SELECT @emp_id = employee_id, @salary = salary FROM employees WHERE last_name = 'King'; IF salary > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('Salary is greater than 10,000'); END ELSE BEGIN raise_error(-20100, 'Salary is not greater than 10,000'); END END; IF OBJECT_ID('update_employee', 'P') IS NOT NULL DROP PROCEDURE update_employee; GO CREATE PROCEDURE update_employee (@p_emp_id IN AS NUMBER, p_salary IN NUMBER) IS l_emp_id NUMBER; l_salary NUMBER; BEGIN SELECT @l_emp_id = employee_id, @l_salary = salary FROM employees WHERE employee_id = p_emp_id; IF l_emp_id IS NOT NULL BEGIN UPDATE employees SET salary = p_salary WHERE employee_id = p_emp_id; END ELSE BEGIN raise_error(-20101, 'Employee not found'); END END;
DECLARE emp_id NUMBER; salary NUMBER; BEGIN SELECT employee_id, salary INTO emp_id, salary FROM employees WHERE last_name = 'King'; IF salary > 10000 THEN DBMS_OUTPUT.PUT_LINE('Salary is greater than 10,000'); ELSE raise_error(-20100, 'Salary is not greater than 10,000'); END IF; END; CREATE OR REPLACE PROCEDURE update_employee (p_emp_id IN NUMBER, p_salary IN NUMBER) IS l_emp_id NUMBER; l_salary NUMBER; BEGIN SELECT employee_id, salary INTO l_emp_id, l_salary FROM employees WHERE employee_id = p_emp_id; IF l_emp_id IS NOT NULL THEN UPDATE employees SET salary = p_salary WHERE employee_id = p_emp_id; ELSE raise_error(-20101, 'Employee not found'); END IF; END;
CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id; RETURN @salary; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR( '-20000 Employee with id ' + isnull(@emp_id, '') + ' not found' , 16, 1); END END; CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id; COMMIT; EXCEPTION WHEN DUPLICATE_VALUE_ON_INDEX THEN RAISERROR( '-20001 Employee with id ' + isnull(@emp_id, '') + ' already has salary ' + isnull(@new_salary, '') , 16, 1); END; DECLARE @emp_id FLOAT := 100; salary NUMBER; BEGIN SET @salary = dbo.get_employee_salary(@emp_id); dbo.update_employee_salary(@emp_id, salary + 500); END;
CREATE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR( -20000, 'Employee with id ' || emp_id || ' not found' ); END; CREATE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN DUPLICATE_VALUE_ON_INDEX THEN RAISE_APPLICATION_ERROR( -20001, 'Employee with id ' || emp_id || ' already has salary ' || new_salary ); END; DECLARE emp_id NUMBER := 100; salary NUMBER; BEGIN salary := get_employee_salary(emp_id); update_employee_salary(emp_id, salary + 500); END;
IF OBJECT_ID('emp_salary_update', 'P') IS NOT NULL DROP PROCEDURE emp_salary_update; GO CREATE PROCEDURE emp_salary_update(@emp_id IN AS NUMBER, pay_raise IN NUMBER) AS BEGIN UPDATE employees SET salary = salary * (1 + pay_raise / 100) WHERE employee_id = emp_id; COMMIT; END; DECLARE @total_salary FLOAT; BEGIN SELECT @total_salary = SUM(salary) FROM employees; DBMS_OUTPUT.PUT_LINE('Total salary for all employees: ' + isnull(@total_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END; DECLARE @dept_name VARCHAR(20); BEGIN DECLARE dept_name CURSOR FOR SELECT department_name FROM departments; OPEN dept_name; FETCH dept_name INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE(@dept_name); FETCH dept_name INTO; END; CLOSE dept_name; DEALLOCATE dept_name; END; BEGIN IF (emp_id IS NULL OR emp_id <= 0) BEGIN RAISERROR('-20001 Invalid employee ID', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID'); END END;
CREATE OR REPLACE PROCEDURE emp_salary_update(emp_id IN NUMBER, pay_raise IN NUMBER) AS BEGIN UPDATE employees SET salary = salary * (1 + pay_raise / 100) WHERE employee_id = emp_id; COMMIT; END; DECLARE total_salary NUMBER; BEGIN SELECT SUM(salary) INTO total_salary FROM employees; DBMS_OUTPUT.PUT_LINE('Total salary for all employees: ' || total_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END; DECLARE dept_name VARCHAR2(20); BEGIN FOR dept_name IN (SELECT department_name FROM departments) LOOP DBMS_OUTPUT.PUT_LINE(dept_name); END LOOP; END; BEGIN IF (emp_id IS NULL OR emp_id <= 0) THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid employee ID'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID'); END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = employee_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20000 Employee not found', 16, 1); END END; BEGIN DECLARE @emp_id FLOAT; emp_salary NUMBER; BEGIN SET @emp_id = 100; SET @emp_salary = dbo.get_employee_salary(@emp_id); IF emp_salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END ELSE BEGIN SET @emp_salary = emp_salary + 100; dbo.update_employee_salary(@emp_id, emp_salary); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END; END;
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20000, 'Employee not found'); END; BEGIN DECLARE emp_id NUMBER; emp_salary NUMBER; BEGIN emp_id := 100; emp_salary := get_employee_salary(emp_id); IF emp_salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); ELSE emp_salary := emp_salary + 100; update_employee_salary(emp_id, emp_salary); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; END;
IF OBJECT_ID('get_employee_data', 'P') IS NOT NULL DROP PROCEDURE get_employee_data; GO CREATE PROCEDURE get_employee_data(@p_emp_id IN AS NUMBER, OUT p_emp_name OUT VARCHAR2, OUT p_salary OUT NUMBER) IS BEGIN SELECT @p_emp_name = ename, @p_salary = sal FROM emp WHERE empno = p_emp_id; END; *** late bonus based on salary IF OBJECT_ID('get_bonus', 'FN') IS NOT NULL DROP FUNCTION get_bonus; GO CREATE FUNCTION get_bonus(@p_salary IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS BEGIN RETURN CASE WHEN p_salary <= 10000 THEN p_salary * 0.1 WHEN p_salary > 10000 AND p_salary <= 20000 THEN p_salary * 0.15 ELSE p_salary * 0.2 END; END; *** ple DECLARE @v_num FLOAT := 10; BEGIN IF @v_num > 5 BEGIN -- Do... SQLINES DEMO *** END ELSE BEGIN -- Do... SQLINES DEMO *** END END; *** ample BEGIN *** se an error EXCEPTION WHEN OTHERS THEN -- Er... SQLINES DEMO *** END;
CREATE OR REPLACE PROCEDURE get_employee_data(p_emp_id IN NUMBER, OUT p_emp_name OUT VARCHAR2, OUT p_salary OUT NUMBER) IS BEGIN SELECT ename, sal INTO p_emp_name, p_salary FROM emp WHERE empno = p_emp_id; END; -- Function to calculate bonus based on salary CREATE OR REPLACE FUNCTION get_bonus(p_salary IN NUMBER) RETURN NUMBER IS BEGIN RETURN CASE WHEN p_salary <= 10000 THEN p_salary * 0.1 WHEN p_salary > 10000 AND p_salary <= 20000 THEN p_salary * 0.15 ELSE p_salary * 0.2 END; END; -- Control flow example DECLARE v_num NUMBER := 10; BEGIN IF v_num > 5 THEN -- Do something ELSE -- Do something else END IF; END; -- Error handling example BEGIN -- Code that may cause an error EXCEPTION WHEN OTHERS THEN -- Error handling code END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = emp_id, @v_salary = salary FROM emp WHERE last_name = 'King'; IF v_salary < 10000 BEGIN RAISERROR('-20001 Salary is too low.', 16, 1); END ELSE IF v_salary > 20000 BEGIN UPDATE emp SET salary = salary * 1.10 WHERE emp_id = @v_emp_id; END END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT emp_id, salary INTO v_emp_id, v_salary FROM emp WHERE last_name = 'King'; IF v_salary < 10000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary is too low.'); ELSIF v_salary > 20000 THEN UPDATE emp SET salary = salary * 1.10 WHERE emp_id = v_emp_id; END IF; END;
CREATE PROCEDURE update_employee_salary( @IN employee_id AS NUMBER, IN new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20101 Employee not found', 16, 1); END END;
CREATE PROCEDURE update_employee_salary( IN employee_id NUMBER, IN new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20101, 'Employee not found'); END;
BEGIN DECLARE @v_emp_id FLOAT := 10; v_emp_name dbo.VARCHAR2(20); BEGIN SELECT @v_emp_name = ename FROM emp WHERE empno = @v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@v_emp_id, '') + ' not found'); END END; END;
BEGIN DECLARE v_emp_id NUMBER := 10; v_emp_name VARCHAR2(20); BEGIN SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee with ID ' || v_emp_id || ' not found'); END; END;
DECLARE @v_employee_id FLOAT; v_salary NUMBER; BEGIN *** nd salary SELECT @v_employee_id = employee_id, @v_salary = salary FROM employees WHERE employee_name = 'John Doe'; *** exists IF @v_employee_id IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END *** s between $10,000 and $20,000 IF v_salary BETWEEN 10000 AND 20000 BEGIN -- Gi... SQLINES DEMO *** UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_employee_id; -- Lo... SQLINES DEMO *** INSERT INTO employee_raises (employee_id, raise_date, raise_amount) VALUES (@v_employee_id, GETDATE(), 0.10); END ELSE BEGIN *** lary is outside the specified range RAISERROR('-20002 Salary is outside the specified range', 16, 1); END -- Co... SQLINES DEMO *** COMMIT; END;
DECLARE v_employee_id NUMBER; v_salary NUMBER; BEGIN -- Get employee ID and salary SELECT employee_id, salary INTO v_employee_id, v_salary FROM employees WHERE employee_name = 'John Doe'; -- Check if employee exists IF v_employee_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; -- Check if salary is between $10,000 and $20,000 IF v_salary BETWEEN 10000 AND 20000 THEN -- Give raise of 10% UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_employee_id; -- Log the raise INSERT INTO employee_raises (employee_id, raise_date, raise_amount) VALUES (v_employee_id, SYSDATE, 0.10); ELSE -- Raise error if salary is outside the specified range RAISE_APPLICATION_ERROR(-20002, 'Salary is outside the specified range'); END IF; -- Commit changes COMMIT; END;
DECLARE @v_cursor SYS_REFCURSOR; BEGIN SELECT *; WHILE 1=1 BEGIN FETCH v_cursor INTO @v_val; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(v_val); END; CLOSE v_cursor; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found'); END END;
DECLARE v_cursor SYS_REFCURSOR; BEGIN OPEN v_cursor FOR SELECT * FROM dual; LOOP FETCH v_cursor INTO v_val; EXIT WHEN v_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(v_val); END LOOP; CLOSE v_cursor; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found'); END;
BEGIN -- De... SQLINES DEMO *** l_emp_id NUMBER; l_emp_name dbo.VARCHAR2(100); l_salary NUMBER; l_bonus NUMBER; l_dept_id NUMBER; *** variables set @l_emp_id = 100; set @l_emp_name = 'John Doe'; set @l_salary = 10000; set @l_bonus = 500; set @l_dept_id = 20; *** the employee table INSERT INTO employees (emp_id, emp_name, salary, bonus, dept_id) VALUES (l_emp_id, l_emp_name, l_salary, l_bonus, l_dept_id); *** ction COMMIT; EXCEPTION -- Ha... SQLINES DEMO *** WHEN OTHERS THEN ROLLBACK; RAISE; END;
BEGIN -- Declare variables l_emp_id NUMBER; l_emp_name VARCHAR2(100); l_salary NUMBER; l_bonus NUMBER; l_dept_id NUMBER; -- Assign values to variables l_emp_id := 100; l_emp_name := 'John Doe'; l_salary := 10000; l_bonus := 500; l_dept_id := 20; -- Insert data into the employee table INSERT INTO employees (emp_id, emp_name, salary, bonus, dept_id) VALUES (l_emp_id, l_emp_name, l_salary, l_bonus, l_dept_id); -- Commit the transaction COMMIT; EXCEPTION -- Handle any errors WHEN OTHERS THEN ROLLBACK; RAISE; 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; AS orders_cursor SYS_REFCURSOR; BEGIN SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END; DECLARE orders_ref CURSOR FOR get_customer_orders(12345); BEGIN DECLARE ord CURSOR FOR orders_ref OPEN ord; FETCH ord INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@ord_order_id, '')); DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@ord_order_date, '')); DBMS_OUTPUT.PUT_LINE('Customer ID: ' + isnull(@ord_customer_id, '')); FETCH ord INTO; END; CLOSE ord; DEALLOCATE ord; END; BEGIN BEGIN raiserror('-20001 Operation failed', 16, 1); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END; END;
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR AS orders_cursor SYS_REFCURSOR; BEGIN OPEN orders_cursor FOR SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END; DECLARE orders_ref CURSOR FOR get_customer_orders(12345); BEGIN FOR ord IN orders_ref LOOP DBMS_OUTPUT.PUT_LINE('Order ID: ' || ord.order_id); DBMS_OUTPUT.PUT_LINE('Order Date: ' || ord.order_date); DBMS_OUTPUT.PUT_LINE('Customer ID: ' || ord.customer_id); END LOOP; END; BEGIN BEGIN raise_application_error(-20001, 'Operation failed'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END; END;
IF OBJECT_ID('update_employee', 'P') IS NOT NULL DROP PROCEDURE update_employee; GO CREATE PROCEDURE update_employee( @p_employee_id IN AS NUMBER, p_new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
CREATE OR REPLACE PROCEDURE update_employee( p_employee_id IN NUMBER, p_new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
BEGIN -- De... SQLINES DEMO *** v_emp_id employee.emp_id%TYPE; v_emp_name employee.emp_name%TYPE; v_salary employee.salary%TYPE; -- Ge... SQLINES DEMO *** SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary FROM employee WHERE emp_id = 1; *** exists IF v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END *** alary UPDATE employee SET salary = v_salary * 1.10 WHERE emp_id = v_emp_id; -- Co... SQLINES DEMO *** COMMIT; END;
BEGIN -- Declare variables v_emp_id employee.emp_id%TYPE; v_emp_name employee.emp_name%TYPE; v_salary employee.salary%TYPE; -- Get employee data SELECT emp_id, emp_name, salary INTO v_emp_id, v_emp_name, v_salary FROM employee WHERE emp_id = 1; -- Check if employee exists IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; -- Update employee salary UPDATE employee SET salary = v_salary * 1.10 WHERE emp_id = v_emp_id; -- Commit changes COMMIT; END;
CREATE PROCEDURE my_proc (@x IN AS NUMBER) AS BEGIN DECLARE @y FLOAT; BEGIN SELECT @y = 1 WHERE x > 10; IF @@FETCH_STATUS <> 0 BEGIN ROLLBACK; RAISERROR('-20000 No matching rows found', 16, 1); END END; dbms_output.put_line(@y); END;
CREATE PROCEDURE my_proc (x IN NUMBER) AS BEGIN DECLARE y NUMBER; BEGIN SELECT 1 INTO y FROM dual WHERE x > 10; EXCEPTION WHEN NO_DATA_FOUND THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20000, 'No matching rows found'); END; dbms_output.put_line(y); END;
BEGIN DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = employee_id, @v_salary = salary FROM employees WHERE last_name = 'King'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with that last name.'); END END; END;
BEGIN 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'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with that last name.'); END; END;
DECLARE @v_empno FLOAT := 10; v_salary NUMBER; BEGIN SELECT @v_salary = salary FROM employees WHERE empno = @v_empno; DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empno, '') + ' has a salary of ' + isnull(v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END; /
DECLARE v_empno NUMBER := 10; v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE empno = v_empno; DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' has a salary of ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END; /
DECLARE @v_num FLOAT; BEGIN SELECT @v_num = COUNT(*) FROM employees; IF @v_num > 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employees table is not empty'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employees table is empty'); END END;
DECLARE v_num NUMBER; BEGIN SELECT COUNT(*) INTO v_num FROM employees; IF v_num > 0 THEN DBMS_OUTPUT.PUT_LINE('Employees table is not empty'); ELSE DBMS_OUTPUT.PUT_LINE('Employees table is empty'); END IF; END;
CREATE PROCEDURE sp_update_employee( @p_employee_id IN AS NUMBER, p_salary IN NUMBER, p_error_code OUT NUMBER, p_error_message OUT VARCHAR2 ) AS BEGIN BEGIN UPDATE employees SET salary = p_salary WHERE employee_id = p_employee_id; set @p_error_code = 0; set @p_error_message = NULL; EXCEPTION WHEN OTHERS THEN set @p_error_code = @@ERROR; set @p_error_message = SQLERRM; END; END sp_update_employee;
CREATE PROCEDURE sp_update_employee( p_employee_id IN NUMBER, p_salary IN NUMBER, p_error_code OUT NUMBER, p_error_message OUT VARCHAR2 ) AS BEGIN BEGIN UPDATE employees SET salary = p_salary WHERE employee_id = p_employee_id; p_error_code := 0; p_error_message := NULL; EXCEPTION WHEN OTHERS THEN p_error_code := SQLCODE; p_error_message := SQLERRM; END; END sp_update_employee;
if object_id('get_employee_salary', 'fn') is not null drop function get_employee_salary; go create function get_employee_salary(@employee_id in numberDECLARE ) return begin number return null; end; is salary number; begin select @salary = salary from employees where employee_id = employee_id; return salary; if @@fetch_status <> 0 begin return null; end end; / declare @salary float; begin set @salary = dbo.get_employee_salary(100); if @salary is not null begin dbms_output.put_line('Salary: ' + isnull(convert(varchar, @salary), '')); end else begin dbms_output.put_line('Employee not found.'); end end; /
create or replace function get_employee_salary(employee_id in number) return number is salary number; begin select salary into salary from employees where employee_id = employee_id; return salary; exception when no_data_found then return null; end; / declare salary number; begin salary := get_employee_salary(100); if salary is not null then dbms_output.put_line('Salary: ' || to_char(salary)); else dbms_output.put_line('Employee not found.'); end if; end; /
DECLARE @salary_bonus FLOAT := 1000; emp_id SET @NUMBER = 10; BEGIN UPDATE employees SET salary = salary + @salary_bonus WHERE employee_id = emp_id; IF @@ROWCOUNT > 0 BEGIN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE salary_bonus NUMBER := 1000; emp_id NUMBER := 10; BEGIN UPDATE employees SET salary = salary + salary_bonus WHERE employee_id = emp_id; IF SQL%ROWCOUNT > 0 THEN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); ELSE DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
IF OBJECT_ID('sp_UpdateCustomer', 'P') IS NOT NULL DROP PROCEDURE sp_UpdateCustomer; GO CREATE PROCEDURE sp_UpdateCustomer(@customer_id IN AS NUMBER, new_name IN VARCHAR2) IS BEGIN UPDATE customers SET name = new_name WHERE customer_id = customer_id; COMMIT; END; IF OBJECT_ID('fn_GetCustomerBalance', 'FN') IS NOT NULL DROP FUNCTION fn_GetCustomerBalance; GO CREATE FUNCTION fn_GetCustomerBalance(@customer_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS l_balance NUMBER; BEGIN SELECT @l_balance = balance FROM accounts WHERE customer_id = customer_id; RETURN l_balance; END; BEGIN DECLARE @l_customer_id FLOAT := 10; l_balance NUMBER; BEGIN SET @l_balance = dbo.fn_GetCustomerBalance(@l_customer_id); DBMS_OUTPUT.PUT_LINE('Customer balance: ' + isnull(l_balance, '')); dbo.sp_UpdateCustomer(@l_customer_id, 'John Doe'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Customer not found.'); END END; END;
CREATE OR REPLACE PROCEDURE sp_UpdateCustomer(customer_id IN NUMBER, new_name IN VARCHAR2) IS BEGIN UPDATE customers SET name = new_name WHERE customer_id = customer_id; COMMIT; END; CREATE OR REPLACE FUNCTION fn_GetCustomerBalance(customer_id IN NUMBER) RETURN NUMBER IS l_balance NUMBER; BEGIN SELECT balance INTO l_balance FROM accounts WHERE customer_id = customer_id; RETURN l_balance; END; BEGIN DECLARE l_customer_id NUMBER := 10; l_balance NUMBER; BEGIN l_balance := fn_GetCustomerBalance(l_customer_id); DBMS_OUTPUT.PUT_LINE('Customer balance: ' || l_balance); sp_UpdateCustomer(l_customer_id, 'John Doe'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Customer not found.'); END; END;
DECLARE @v_count FLOAT; v_max_id NUMBER; BEGIN SELECT @v_count = COUNT(*) FROM users; SELECT @v_max_id = MAX(id) FROM users; IF @v_count > 100 BEGIN RAISERROR('-20000 Too many users', 16, 1); END INSERT INTO users (id, username, password) VALUES (v_max_id + 1, 'new_user', 'new_pass'); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN RAISERROR('-20100 Username already exists', 16, 1); END;
DECLARE v_count NUMBER; v_max_id NUMBER; BEGIN SELECT COUNT(*) INTO v_count FROM users; SELECT MAX(id) INTO v_max_id FROM users; IF v_count > 100 THEN RAISE_APPLICATION_ERROR(-20000, 'Too many users'); END IF; INSERT INTO users (id, username, password) VALUES (v_max_id + 1, 'new_user', 'new_pass'); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN RAISE_APPLICATION_ERROR(-20100, 'Username already exists'); END;
IF OBJECT_ID('update_salary', 'P') IS NOT NULL DROP PROCEDURE update_salary; GO CREATE PROCEDURE update_salary(@emp_id FLOAT, @new_salary FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id; COMMIT; END; IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@emp_id FLOAT) RETURNS VARCHAR(4000) AS BEGIN SELECT isnull(last_name, '') + ', ' + isnull(first_name, '') FROM employees WHERE employee_id = @emp_id; RETURN NULL; END; DECLARE @salary FLOAT; name dbo.VARCHAR2(255); BEGIN SET @salary = 10000; SET @name = dbo.get_employee_name(1); IF @salary > 5000 BEGIN UPDATE employees SET bonus = @salary * 0.1 WHERE employee_id = 1; COMMIT; END ELSE BEGIN RAISERROR('-20001 Salary too low for bonus', 16, 1); END END;
CREATE OR REPLACE PROCEDURE update_salary(emp_id NUMBER, new_salary NUMBER) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; CREATE OR REPLACE FUNCTION get_employee_name(emp_id NUMBER) RETURN VARCHAR2 AS BEGIN SELECT last_name || ', ' || first_name FROM employees WHERE employee_id = emp_id; END; DECLARE salary NUMBER; name VARCHAR2(255); BEGIN salary := 10000; name := get_employee_name(1); IF salary > 5000 THEN UPDATE employees SET bonus = salary * 0.1 WHERE employee_id = 1; COMMIT; ELSE RAISE_APPLICATION_ERROR(-20001, 'Salary too low for bonus'); END IF; END;
BEGIN DECLARE @i INT = 1 ; WHILE @i <= 10 BEGIN UPDATE employees SET salary = salary * 1.10 WHERE department_id = i; COMMIT; END; END;
BEGIN FOR i IN 1 .. 10 LOOP UPDATE employees SET salary = salary * 1.10 WHERE department_id = i; COMMIT; END LOOP; END;
IF OBJECT_ID('increment_salary', 'P') IS NOT NULL DROP PROCEDURE increment_salary; GO CREATE PROCEDURE increment_salary(@employee_id IN AS NUMBER) AS BEGIN UPDATE employees SET salary = salary + 100 WHERE employee_id = employee_id; COMMIT; END INCREMENT_SALARY; IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@employee_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; AS BEGIN RETURN (SELECT name FROM employees WHERE employee_id = employee_id); END GET_EMPLOYEE_NAME; DECLARE @v_salary FLOAT; v_name dbo.VARCHAR2(20); BEGIN SET @v_salary = dbo.get_employee_salary(10); SET @v_name = dbo.get_employee_name(10); dbms_output.put_line('Employee Name: ' + isnull(v_name, '')); dbms_output.put_line('Employee Salary: ' + isnull(@v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN dbms_output.put_line('No employee found with the given ID.'); END END;
CREATE OR REPLACE PROCEDURE increment_salary(employee_id IN NUMBER) AS BEGIN UPDATE employees SET salary = salary + 100 WHERE employee_id = employee_id; COMMIT; END INCREMENT_SALARY; CREATE OR REPLACE FUNCTION get_employee_name(employee_id IN NUMBER) RETURN VARCHAR2 AS BEGIN RETURN (SELECT name FROM employees WHERE employee_id = employee_id); END GET_EMPLOYEE_NAME; DECLARE v_salary NUMBER; v_name VARCHAR2(20); BEGIN v_salary := get_employee_salary(10); v_name := get_employee_name(10); dbms_output.put_line('Employee Name: ' || v_name); dbms_output.put_line('Employee Salary: ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('No employee found with the given ID.'); END;
DECLARE @var1 VARCHAR(10); BEGIN SET @var1 = 'Test'; IF EXISTS (SELECT * FROM table WHERE field = @var1) BEGIN UPDATE table SET field = 'Updated' WHERE field = @var1; END ELSE BEGIN INSERT INTO table (field) VALUES (@var1); END END
DECLARE @var1 VARCHAR(10); BEGIN SET @var1 = 'Test'; IF EXISTS (SELECT * FROM table WHERE field = @var1) BEGIN UPDATE table SET field = 'Updated' WHERE field = @var1; END ELSE BEGIN INSERT INTO table (field) VALUES (@var1); END END
DECLARE @v_employee_id FLOAT; v_employee_name dbo.VARCHAR2(100); v_salary NUMBER; BEGIN SELECT @v_employee_id = employee_id, @v_employee_name = employee_name, @v_salary = salary FROM employees WHERE employee_id = 1; IF v_salary > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_employee_name, '') + ' earns a high salary'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_employee_name, '') + ' earns a low salary'); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee with employee_id ' + isnull(@v_employee_id, '') + ' not found'); END END;
DECLARE v_employee_id NUMBER; v_employee_name VARCHAR2(100); v_salary NUMBER; BEGIN SELECT employee_id, employee_name, salary INTO v_employee_id, v_employee_name, v_salary FROM employees WHERE employee_id = 1; IF v_salary > 10000 THEN DBMS_OUTPUT.PUT_LINE('Employee ' || v_employee_name || ' earns a high salary'); ELSE DBMS_OUTPUT.PUT_LINE('Employee ' || v_employee_name || ' earns a low salary'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee with employee_id ' || v_employee_id || ' not found'); END;
BEGIN INSERT INTO TABLE1 (COLUMN1, COLUMN2) VALUES (10, 20); UPDATE TABLE2 SET COLUMN3 = COLUMN3 + 1 WHERE COLUMN4 = 5; DELETE FROM TABLE3 WHERE COLUMN5 = 'ABC'; SELECT COLUMN6 FROM TABLE4 WHERE COLUMN7 = 10 INTO V_COLUMN6; IF V_COLUMN6 > 100 BEGIN COMMIT; END ELSE BEGIN ROLLBACK; END EXCEPTION WHEN OTHERS THEN RAISE; END;
BEGIN INSERT INTO TABLE1 (COLUMN1, COLUMN2) VALUES (10, 20); UPDATE TABLE2 SET COLUMN3 = COLUMN3 + 1 WHERE COLUMN4 = 5; DELETE FROM TABLE3 WHERE COLUMN5 = 'ABC'; SELECT COLUMN6 FROM TABLE4 WHERE COLUMN7 = 10 INTO V_COLUMN6; IF V_COLUMN6 > 100 THEN COMMIT; ELSE ROLLBACK; END IF; EXCEPTION WHEN OTHERS THEN RAISE; END;
DECLARE @l_empno VARCHAR(4000) /* Use -meta option EMP.empno%TYPE */; l_ename EMP.ename%TYPE; l_sal EMP.sal%TYPE; BEGIN SELECT @l_empno = empno, @l_ename = ename, @l_sal = sal FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee Number: ' + isnull(@l_empno, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(l_ename, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(l_sal, '')); IF l_sal < 10000 BEGIN DBMS_OUTPUT.PUT_LINE('Employee is underpaid.'); END BEGIN UPDATE emp SET sal = sal * 1.10 WHERE empno = @l_empno; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating salary.'); END; END;
DECLARE l_empno EMP.empno%TYPE; l_ename EMP.ename%TYPE; l_sal EMP.sal%TYPE; BEGIN SELECT empno, ename, sal INTO l_empno, l_ename, l_sal FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee Number: ' || l_empno); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_ename); DBMS_OUTPUT.PUT_LINE('Salary: ' || l_sal); IF l_sal < 10000 THEN DBMS_OUTPUT.PUT_LINE('Employee is underpaid.'); END IF; BEGIN UPDATE emp SET sal = sal * 1.10 WHERE empno = l_empno; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating salary.'); END; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc( @first_name IN AS VARCHAR2, last_name IN VARCHAR2, salary IN NUMBER ) AS *** iables emp_id NUMBER; BEGIN *** ee record INSERT INTO employees (first_name, last_name, salary) VALUES (first_name, last_name, salary); *** ID of the newly inserted employee SELECT @emp_id = MAX(employee_id) FROM employees; *** n if the employee ID is null IF emp_id IS NULL BEGIN RAISERROR('-20000 Cannot get employee ID', 16, 1); END *** ction COMMIT; END my_proc;
CREATE OR REPLACE PROCEDURE my_proc( first_name IN VARCHAR2, last_name IN VARCHAR2, salary IN NUMBER ) AS -- Declare local variables emp_id NUMBER; BEGIN -- Insert new employee record INSERT INTO employees (first_name, last_name, salary) VALUES (first_name, last_name, salary); -- Get the employee ID of the newly inserted employee SELECT MAX(employee_id) INTO emp_id FROM employees; -- Raise an exception if the employee ID is null IF emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'Cannot get employee ID'); END IF; -- Commit the transaction COMMIT; END my_proc;
IF OBJECT_ID('get_employee_count', 'FN') IS NOT NULL DROP FUNCTION get_employee_count; GO CREATE FUNCTION get_employee_count(@deptno IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS emp_count NUMBER; BEGIN SELECT @emp_count = COUNT(*) FROM employees WHERE department_id = deptno; RETURN emp_count; END; / BEGIN DECLARE @deptno FLOAT := 10; emp_count NUMBER; BEGIN SET @emp_count = dbo.get_employee_count(@deptno); IF emp_count > 10 BEGIN raiserror('-20001 Too many employees in department ' + isnull(@deptno, ''), 16, 1); END EXCEPTION WHEN OTHERS THEN -- Ha... SQLINES DEMO *** END; END; /
CREATE OR REPLACE FUNCTION get_employee_count(deptno IN NUMBER) RETURN NUMBER IS emp_count NUMBER; BEGIN SELECT COUNT(*) INTO emp_count FROM employees WHERE department_id = deptno; RETURN emp_count; END; / BEGIN DECLARE deptno NUMBER := 10; emp_count NUMBER; BEGIN emp_count := get_employee_count(deptno); IF emp_count > 10 THEN raise_application_error(-20001, 'Too many employees in department ' || deptno); END IF; EXCEPTION WHEN OTHERS THEN -- Handle error here END; END; /
CREATE PROCEDURE usp_AddCustomer( @p_CustomerName IN AS VARCHAR2, p_CustomerAddress IN VARCHAR2, p_CustomerPhone IN VARCHAR2 ) AS BEGIN INSERT INTO Customers(CustomerName, CustomerAddress, CustomerPhone) VALUES(p_CustomerName, p_CustomerAddress, p_CustomerPhone); COMMIT; END; /
CREATE PROCEDURE usp_AddCustomer( p_CustomerName IN VARCHAR2, p_CustomerAddress IN VARCHAR2, p_CustomerPhone IN VARCHAR2 ) AS BEGIN INSERT INTO Customers(CustomerName, CustomerAddress, CustomerPhone) VALUES(p_CustomerName, p_CustomerAddress, p_CustomerPhone); COMMIT; END; /
CREATE FUNCTION AverageSalary(@dept_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @total FLOAT; SELECT @total = SUM(salary) FROM employees WHERE department_id = @dept_id; RETURN @total / COUNT(*); END; DECLARE @emp_id FLOAT; BEGIN SELECT @emp_id = employee_id FROM employees WHERE last_name = 'Smith'; IF @emp_id IS NULL BEGIN RAISERROR('-20001 Employee with last name Smith not found', 16, 1); END UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @emp_id; END; BEGIN DECLARE cur_emp CURSOR FOR SELECT * FROM employees; OPEN cur_emp; FETCH cur_emp INTO; WHILE @@FETCH_STATUS=0 BEGIN IF @cur_emp_salary > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@cur_emp_last_name, '') + ' has a salary of ' + isnull(@cur_emp_salary, '')); END FETCH cur_emp INTO; END; CLOSE cur_emp; DEALLOCATE cur_emp; END;
CREATE FUNCTION AverageSalary(dept_id NUMBER) RETURN NUMBER AS total NUMBER; BEGIN SELECT SUM(salary) INTO total FROM employees WHERE department_id = dept_id; RETURN total / COUNT(*); END; DECLARE emp_id NUMBER; BEGIN SELECT employee_id INTO emp_id FROM employees WHERE last_name = 'Smith'; IF emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee with last name Smith not found'); END IF; UPDATE employees SET salary = salary * 1.10 WHERE employee_id = emp_id; END; BEGIN FOR cur_emp IN (SELECT * FROM employees) LOOP IF cur_emp.salary > 10000 THEN DBMS_OUTPUT.PUT_LINE('Employee ' || cur_emp.last_name || ' has a salary of ' || cur_emp.salary); END IF; END LOOP; END;
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL DROP FUNCTION get_customer_orders; GO CREATE FUNCTION get_customer_orders(@p_cust_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 = p_cust_id; RETURN rc; END; BEGIN DECLARE @order_id FLOAT; order_date DATE; total_amount NUMBER; BEGIN DECLARE rec CURSOR FOR SELECT * FROM get_customer_orders(1); OPEN rec; FETCH rec INTO; WHILE @@FETCH_STATUS=0 BEGIN SET @order_id = @rec_order_id; SET @order_date = @rec_order_date; SET @total_amount = @rec_total_amount; dbms_output.put_line('Order ID: ' + isnull(@order_id, '')); dbms_output.put_line('Order Date: ' + isnull(order_date, '')); dbms_output.put_line('Total Amount: ' + isnull(total_amount, '')); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; IF @@fetch_status <> 0 BEGIN dbms_output.put_line('No orders found for customer ID 1'); END END; END;
CREATE OR REPLACE FUNCTION get_customer_orders(p_cust_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 = p_cust_id; RETURN rc; END; BEGIN DECLARE order_id NUMBER; order_date DATE; total_amount NUMBER; BEGIN FOR rec IN (SELECT * FROM get_customer_orders(1)) LOOP order_id := rec.order_id; order_date := rec.order_date; total_amount := rec.total_amount; dbms_output.put_line('Order ID: ' || order_id); dbms_output.put_line('Order Date: ' || order_date); dbms_output.put_line('Total Amount: ' || total_amount); END LOOP; EXCEPTION WHEN no_data_found THEN dbms_output.put_line('No orders found for customer ID 1'); END; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@salary IN AS NUMBER) IS BEGIN -- Up... SQLINES DEMO *** UPDATE employees SET salary = salary + salary * 0.1 WHERE salary <= salary; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found with salary less than or equal to ' + isnull(salary, '')); END END my_proc; IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS name dbo.VARCHAR2(100); BEGIN SELECT @name = name FROM employees WHERE id = id; RETURN name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END get_employee_name; BEGIN *** d procedure dbo.my_proc(10000); *** he employee with id 1 set @name = dbo.get_employee_name(1); *** the employee DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(name, '')); END;
CREATE OR REPLACE PROCEDURE my_proc(salary IN NUMBER) IS BEGIN -- Update salary UPDATE employees SET salary = salary + salary * 0.1 WHERE salary <= salary; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found with salary less than or equal to ' || salary); END my_proc; CREATE OR REPLACE FUNCTION get_employee_name(id IN NUMBER) RETURN VARCHAR2 IS name VARCHAR2(100); BEGIN SELECT name INTO name FROM employees WHERE id = id; RETURN name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END get_employee_name; BEGIN -- Execute the stored procedure my_proc(10000); -- Get the name of the employee with id 1 name := get_employee_name(1); -- Print the name of the employee DBMS_OUTPUT.PUT_LINE('Employee name: ' || name); END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN NULL; END END; BEGIN DECLARE @emp_salary FLOAT; SET @emp_salary = dbo.get_employee_salary(100); IF @emp_salary IS NULL BEGIN NULL; END ELSE BEGIN dbo.update_employee_salary(100, @emp_salary * 1.1); END END; DECLARE @salary_list SYS_REFCURSOR; BEGIN SELECT salary FROM employees; END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END; BEGIN DECLARE emp_salary NUMBER; emp_salary := get_employee_salary(100); IF emp_salary IS NULL THEN NULL; ELSE update_employee_salary(100, emp_salary * 1.1); END IF; END; DECLARE salary_list SYS_REFCURSOR; BEGIN OPEN salary_list FOR SELECT salary FROM employees; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc( @IN param1 AS VARCHAR2, OUT param2 NUMBER ) IS BEGIN SELECT @param2 = COUNT(*) FROM my_table WHERE my_column = param1; IF param2 = 0 BEGIN RAISERROR('-20001 No rows found for parameter ' + isnull(param1, ''), 16, 1); END END; / DECLARE @my_var FLOAT; BEGIN dbo.my_proc('my_value', @my_var); DBMS_OUTPUT.PUT_LINE('Number of rows: ' + isnull(@my_var, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE PROCEDURE my_proc( IN param1 VARCHAR2, OUT param2 NUMBER ) IS BEGIN SELECT COUNT(*) INTO param2 FROM my_table WHERE my_column = param1; IF param2 = 0 THEN RAISE_APPLICATION_ERROR(-20001, 'No rows found for parameter ' || param1); END IF; END; / DECLARE my_var NUMBER; BEGIN my_proc('my_value', my_var); DBMS_OUTPUT.PUT_LINE('Number of rows: ' || my_var); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL DROP PROCEDURE get_customer_orders; GO CREATE PROCEDURE get_customer_orders(@p_customer_id IN AS NUMBER) IS l_cursor SYS_REFCURSOR; BEGIN SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = p_customer_id; RETURN l_cursor; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 No orders found for the specified customer.', 16, 1); END END; IF OBJECT_ID('get_order_total', 'FN') IS NOT NULL DROP FUNCTION get_order_total; GO CREATE FUNCTION get_order_total(@p_order_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS l_total_amount NUMBER; BEGIN SELECT @l_total_amount = total_amount FROM orders WHERE order_id = p_order_id; RETURN l_total_amount; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 Order not found.', 16, 1); END END; BEGIN DECLARE c CURSOR FOR SELECT customer_id, customer_name FROM customers; OPEN c; FETCH c INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE('Customer: ' + isnull(@c_customer_name, '')); DECLARE ord CURSOR FOR dbo.get_customer_orders(@c_customer_id) LOOP DBMS_OUTPUT.PUT_LINE('tOrder ID: ' + isnull(@ord_order_id, '')); DBMS_OUTPUT.PUT_LINE('tOrder Date: ' + ISNULL(CONVERT(VARCHAR(10), @ord_order_date, 103), '')); DBMS_OUTPUT.PUT_LINE('tTotal Amount: ' + isnull(dbo.get_order_total(@ord_order_id), '')); FETCH ord INTO; END; CLOSE ord; DEALLOCATE ord; FETCH c INTO; END; CLOSE c; DEALLOCATE c; END; -- Er... SQLINES DEMO *** BEGIN BEGIN DECLARE @l_invalid_order_id FLOAT := -1; dbo.get_order_total(@l_invalid_order_id); EXCEPTION WHEN APPLICATION_ERROR THEN DBMS_OUTPUT.PUT_LINE('Invalid order ID: ' + ISNULL(SQLERRM, '')); END; END;
CREATE OR REPLACE PROCEDURE get_customer_orders(p_customer_id IN NUMBER) IS l_cursor SYS_REFCURSOR; BEGIN OPEN l_cursor FOR SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = p_customer_id; RETURN l_cursor; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'No orders found for the specified customer.'); END; CREATE OR REPLACE FUNCTION get_order_total(p_order_id IN NUMBER) RETURN NUMBER IS l_total_amount NUMBER; BEGIN SELECT total_amount INTO l_total_amount FROM orders WHERE order_id = p_order_id; RETURN l_total_amount; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'Order not found.'); END; BEGIN FOR c IN (SELECT customer_id, customer_name FROM customers) LOOP DBMS_OUTPUT.PUT_LINE('Customer: ' || c.customer_name); FOR ord IN get_customer_orders(c.customer_id) LOOP DBMS_OUTPUT.PUT_LINE('\tOrder ID: ' || ord.order_id); DBMS_OUTPUT.PUT_LINE('\tOrder Date: ' || TO_CHAR(ord.order_date, 'DD/MM/YYYY')); DBMS_OUTPUT.PUT_LINE('\tTotal Amount: ' || get_order_total(ord.order_id)); END LOOP; END LOOP; END; -- Error handling BEGIN BEGIN DECLARE l_invalid_order_id NUMBER := -1; get_order_total(l_invalid_order_id); EXCEPTION WHEN APPLICATION_ERROR THEN DBMS_OUTPUT.PUT_LINE('Invalid order ID: ' || SQLERRM); 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 = 'Smith'; IF v_salary > 10000 BEGIN UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_emp_id; COMMIT; END ELSE BEGIN RAISERROR('-20001 Salary is too low to increase', 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 = 'Smith'; IF v_salary > 10000 THEN UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_emp_id; COMMIT; ELSE RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to increase'); END IF; END; /
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc ( @p_id IN AS NUMBER, p_name IN OUT VARCHAR2 ) AS BEGIN SELECT @p_name = ename FROM emp WHERE empno = p_id; IF @@FETCH_STATUS <> 0 BEGIN set @p_name = 'Not found'; END END; BEGIN dbo.my_proc(10, @empname); DBMS_OUTPUT.PUT_LINE('Employee name: ' + Isnull(@empname, '')); END;
CREATE OR REPLACE PROCEDURE my_proc ( p_id IN NUMBER, p_name IN OUT VARCHAR2 ) AS BEGIN SELECT ename INTO p_name FROM emp WHERE empno = p_id; EXCEPTION WHEN NO_DATA_FOUND THEN p_name := 'Not found'; END; BEGIN my_proc(10, @empname); DBMS_OUTPUT.PUT_LINE('Employee name: ' || @empname); END;
CREATE PROCEDURE my_proc( @p_in IN AS VARCHAR2, p_out OUT VARCHAR2 ) AS v_temp dbo.VARCHAR2(100); BEGIN set @v_temp = 'Hello, ' + isnull(p_in, ''); IF LEN(v_temp) > 20 BEGIN RAISERROR('-20001 Value too long', 16, 1); END set @p_out = v_temp; END my_proc;
CREATE PROCEDURE my_proc( p_in IN VARCHAR2, p_out OUT VARCHAR2 ) AS v_temp VARCHAR2(100); BEGIN v_temp := 'Hello, ' || p_in; IF LENGTH(v_temp) > 20 THEN RAISE_APPLICATION_ERROR(-20001, 'Value too long'); END IF; p_out := v_temp; END my_proc;
DECLARE @v_emp_id FLOAT := 100; v_emp_name dbo.VARCHAR2(20); v_emp_salary NUMBER; BEGIN SELECT @v_emp_name = emp_name, @v_emp_salary = emp_salary FROM employees WHERE emp_id = @v_emp_id; IF v_emp_salary > 5000 BEGIN DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus.'); END UPDATE employees SET emp_salary = emp_salary * 1.10 WHERE emp_id = @v_emp_id; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred. Please contact your system administrator.'); END;
DECLARE v_emp_id NUMBER := 100; v_emp_name VARCHAR2(20); v_emp_salary NUMBER; BEGIN SELECT emp_name, emp_salary INTO v_emp_name, v_emp_salary FROM employees WHERE emp_id = v_emp_id; IF v_emp_salary > 5000 THEN DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus.'); ELSE DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus.'); END IF; UPDATE employees SET emp_salary = emp_salary * 1.10 WHERE emp_id = v_emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred. Please contact your system administrator.'); END;
CREATE TABLE users ( id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL ); CREATE PROCEDURE add_user(@IN p_name AS VARCHAR2, IN p_email VARCHAR2) AS l_id INTEGER; BEGIN SELECT @l_id = users_seq.NEXTVAL; INSERT INTO users (id, name, email) VALUES (l_id, p_name, p_email); COMMIT; END; DECLARE @v_name CONSTANT dbo.VARCHAR2(20) := 'John Doe'; v_email CONSTANT dbo.VARCHAR2(255) := 'john.doe@example.com'; BEGIN TRY dbo.add_user(@v_name, v_email); DBMS_OUTPUT.PUT_LINE('User added successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error adding user: ' + ISNULL(SQLERRM, '')); END; END; ALTER TABLE users ADD COLUMN age NUMBER DEFAULT 0; CREATE FUNCTION get_average_age() RETURNS FLOAT AS BEGIN RETURN ( SELECT AVG(age) FROM users ); END; DECLARE @v_avg_age FLOAT; BEGIN SELECT @v_avg_age = dbo.get_average_age(); DBMS_OUTPUT.PUT_LINE('Average age: ' + isnull(@v_avg_age, '')); END;
CREATE TABLE users ( id INTEGER PRIMARY KEY, name VARCHAR2(50) NOT NULL, email VARCHAR2(255) UNIQUE NOT NULL ); CREATE PROCEDURE add_user(IN p_name VARCHAR2, IN p_email VARCHAR2) AS l_id INTEGER; BEGIN SELECT users_seq.NEXTVAL INTO l_id FROM DUAL; INSERT INTO users (id, name, email) VALUES (l_id, p_name, p_email); COMMIT; END; DECLARE v_name CONSTANT VARCHAR2(20) := 'John Doe'; v_email CONSTANT VARCHAR2(255) := 'john.doe@example.com'; BEGIN TRY add_user(v_name, v_email); DBMS_OUTPUT.PUT_LINE('User added successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error adding user: ' || SQLERRM); END; END; ALTER TABLE users ADD COLUMN age NUMBER DEFAULT 0; CREATE FUNCTION get_average_age() RETURN NUMBER AS BEGIN RETURN ( SELECT AVG(age) FROM users ); END; DECLARE v_avg_age NUMBER; BEGIN SELECT get_average_age() INTO v_avg_age; DBMS_OUTPUT.PUT_LINE('Average age: ' || v_avg_age); END;
CREATE PROCEDURE TransferFunds( @IN p_from_account_id AS NUMBER, IN p_to_account_id NUMBER, IN p_amount NUMBER ) AS BEGIN UPDATE accounts SET balance = balance - p_amount WHERE account_id = p_from_account_id; UPDATE accounts SET balance = balance + p_amount WHERE account_id = p_to_account_id; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN dbo.TransferFunds(1, 2, 100); END;
CREATE PROCEDURE TransferFunds( IN p_from_account_id NUMBER, IN p_to_account_id NUMBER, IN p_amount NUMBER ) AS BEGIN UPDATE accounts SET balance = balance - p_amount WHERE account_id = p_from_account_id; UPDATE accounts SET balance = balance + p_amount WHERE account_id = p_to_account_id; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN TransferFunds(1, 2, 100); END;
CREATE TABLE customer ( customer_id BIGINT NOT NULL, customer_name VARCHAR(50), address VARCHAR(200), CONSTRAINT customer_pk PRIMARY KEY (customer_id) ); CREATE TABLE order ( order_id BIGINT NOT NULL, customer_id BIGINT NOT NULL, order_date DATE, total_amount DECIMAL(12, 2), CONSTRAINT order_pk PRIMARY KEY (order_id), CONSTRAINT order_fk FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ); INSERT INTO customer (customer_id, customer_name, address) VALUES (1, 'John Smith', '123 Main Street'); INSERT INTO customer (customer_id, customer_name, address) VALUES (2, 'Jane Doe', '456 Elm Street'); INSERT INTO customer (customer_id, customer_name, address) VALUES (3, 'Bob Johnson', '789 Oak Street'); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (1, 1, '2023-03-08', 100.00); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (2, 2, '2023-03-10', 200.00); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (3, 3, '2023-03-12', 300.00); CREATE PROCEDURE get_customer_orders ( @IN customer_id AS ROW_NUMBER(10), OUT order_cursor SYS_REFCURSOR ) IS BEGIN SELECT * FROM order WHERE customer_id = customer_id; END;
CREATE TABLE customer ( customer_id NUMBER(10) NOT NULL, customer_name VARCHAR2(50), address VARCHAR2(200), CONSTRAINT customer_pk PRIMARY KEY (customer_id) ); CREATE TABLE order ( order_id NUMBER(10) NOT NULL, customer_id NUMBER(10) NOT NULL, order_date DATE, total_amount NUMBER(12, 2), CONSTRAINT order_pk PRIMARY KEY (order_id), CONSTRAINT order_fk FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ); INSERT INTO customer (customer_id, customer_name, address) VALUES (1, 'John Smith', '123 Main Street'); INSERT INTO customer (customer_id, customer_name, address) VALUES (2, 'Jane Doe', '456 Elm Street'); INSERT INTO customer (customer_id, customer_name, address) VALUES (3, 'Bob Johnson', '789 Oak Street'); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (1, 1, '2023-03-08', 100.00); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (2, 2, '2023-03-10', 200.00); INSERT INTO order (order_id, customer_id, order_date, total_amount) VALUES (3, 3, '2023-03-12', 300.00); CREATE PROCEDURE get_customer_orders ( IN customer_id NUMBER(10), OUT order_cursor SYS_REFCURSOR ) IS BEGIN OPEN order_cursor FOR SELECT * FROM order WHERE customer_id = customer_id; END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@employee_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS emp_name dbo.VARCHAR2(20); BEGIN SELECT @emp_name = ISNULL(FIRST_NAME, '') + ' ' + ISNULL(LAST_NAME, '') FROM employees WHERE EMPLOYEE_ID = employee_id; RETURN emp_name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; / BEGIN DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(dbo.get_employee_name(10), '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END; /
CREATE OR REPLACE FUNCTION get_employee_name(employee_id IN NUMBER) RETURN VARCHAR2 IS emp_name VARCHAR2(20); BEGIN SELECT FIRST_NAME || ' ' || LAST_NAME INTO emp_name FROM employees WHERE EMPLOYEE_ID = employee_id; RETURN emp_name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; / BEGIN DBMS_OUTPUT.PUT_LINE('Employee Name: ' || get_employee_name(10)); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END; /
CREATE TABLE employee (id FLOAT, name VARCHAR(255), salary FLOAT, PRIMARY KEY (id)); INSERT INTO employee (id, name, salary) VALUES (1, 'John Doe', 10000); INSERT INTO employee (id, name, salary) VALUES (2, 'Jane Doe', 15000); CREATE PROCEDURE get_employee (@id IN AS NUMBER, OUT name OUT VARCHAR2, OUT salary OUT NUMBER) IS BEGIN SELECT @name = name, @salary = salary FROM employee WHERE id = id; END; DECLARE @emp_name VARCHAR(255); emp_salary NUMBER; BEGIN dbo.get_employee (1, @emp_name, emp_salary); DBMS_OUTPUT.PUT_LINE ('Employee name: ' + isnull(@emp_name, '')); DBMS_OUTPUT.PUT_LINE ('Employee salary: ' + isnull(convert(varchar, emp_salary), '')); IF @@fetch_status <> 0 BEGIN DBMS_OUTPUT.PUT_LINE ('No employee found with id ' + isnull(id, '')); END END;
CREATE TABLE employee (id NUMBER, name VARCHAR2(255), salary NUMBER, PRIMARY KEY (id)); INSERT INTO employee (id, name, salary) VALUES (1, 'John Doe', 10000); INSERT INTO employee (id, name, salary) VALUES (2, 'Jane Doe', 15000); CREATE PROCEDURE get_employee (id IN NUMBER, OUT name OUT VARCHAR2, OUT salary OUT NUMBER) IS BEGIN SELECT name, salary INTO name, salary FROM employee WHERE id = id; END; DECLARE emp_name VARCHAR2(255); emp_salary NUMBER; BEGIN get_employee (1, emp_name, emp_salary); DBMS_OUTPUT.PUT_LINE ('Employee name: ' || emp_name); DBMS_OUTPUT.PUT_LINE ('Employee salary: ' || to_char (emp_salary)); EXCEPTION WHEN no_data_found THEN DBMS_OUTPUT.PUT_LINE ('No employee found with id ' || id); END;
SET @SERVEROUTPUT ON; DECLARE @v_emp_id FLOAT := 100; BEGIN BEGIN DELETE FROM employees WHERE employee_id = @v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@v_emp_id, '') + ' deleted'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@v_emp_id, '') + ' not found'); END END; END;
SET SERVEROUTPUT ON; DECLARE v_emp_id NUMBER := 100; BEGIN BEGIN DELETE FROM employees WHERE employee_id = v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee with ID ' || v_emp_id || ' deleted'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee with ID ' || v_emp_id || ' not found'); END; END;
IF OBJECT_ID('usp_emp_update', 'P') IS NOT NULL DROP PROCEDURE usp_emp_update; GO CREATE PROCEDURE usp_emp_update ( @p_empno IN AS NUMBER, p_ename IN VARCHAR2, p_job IN VARCHAR2, p_sal IN NUMBER, p_deptno IN NUMBER, p_comm IN NUMBER ) IS BEGIN UPDATE emp SET ename = p_ename, job = p_job, sal = p_sal, deptno = p_deptno, comm = p_comm WHERE empno = p_empno; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 No data found for empno: ' + isnull(p_empno, ''), 16, 1); END WHEN OTHER THEN RAISE; END; DECLARE @v_empno FLOAT := 7788; v_ename dbo.VARCHAR2(10) := 'SCOTT'; v_job dbo.VARCHAR2(10) := 'PRESIDENT'; v_sal SET @NUMBER = 6000; v_deptno SET @NUMBER = 20; v_comm NUMBER; BEGIN dbo.usp_emp_update(@v_empno, v_ename, v_job, v_sal, v_deptno, v_comm); END;
CREATE OR REPLACE PROCEDURE usp_emp_update ( p_empno IN NUMBER, p_ename IN VARCHAR2, p_job IN VARCHAR2, p_sal IN NUMBER, p_deptno IN NUMBER, p_comm IN NUMBER ) IS BEGIN UPDATE emp SET ename = p_ename, job = p_job, sal = p_sal, deptno = p_deptno, comm = p_comm WHERE empno = p_empno; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'No data found for empno: ' || p_empno); WHEN OTHER THEN RAISE; END; DECLARE v_empno NUMBER := 7788; v_ename VARCHAR2(10) := 'SCOTT'; v_job VARCHAR2(10) := 'PRESIDENT'; v_sal NUMBER := 6000; v_deptno NUMBER := 20; v_comm NUMBER; BEGIN usp_emp_update(v_empno, v_ename, v_job, v_sal, v_deptno, v_comm); END;
CREATE FUNCTION Get_Employee_Name(@employee_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS emp_name dbo.VARCHAR2(20); BEGIN SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = employee_id; RETURN emp_name; END; DECLARE @emp_name VARCHAR(20); BEGIN SET @emp_name = dbo.Get_Employee_Name(100); DBMS_OUTPUT.PUT_LINE(@emp_name); END;
CREATE FUNCTION Get_Employee_Name(employee_id IN NUMBER) RETURN VARCHAR2 IS emp_name VARCHAR2(20); BEGIN SELECT first_name || ' ' || last_name INTO emp_name FROM employees WHERE employee_id = employee_id; RETURN emp_name; END; DECLARE emp_name VARCHAR2(20); BEGIN emp_name := Get_Employee_Name(100); DBMS_OUTPUT.PUT_LINE(emp_name); END;
CREATE PROCEDURE GenerateStats( @IN start_date AS DATE, IN end_date DATE, OUT total_orders INT, OUT avg_order_value FLOAT ) AS l_total_orders SET @INT = 0; l_total_value SET @FLOAT = 0; BEGIN SELECT @l_total_orders = COUNT(*) FROM orders WHERE order_date BETWEEN start_date AND end_date; SELECT @l_total_value = AVG(order_total) FROM orders WHERE order_date BETWEEN start_date AND end_date; set @total_orders = l_total_orders; set @avg_order_value = l_total_value; END; DECLARE @l_total_orders INT; l_avg_order_value FLOAT; BEGIN CALL GenerateStats( CONVERT(DATETIME, '2022-01-01'), CONVERT(DATETIME, '2022-12-31'), @l_total_orders, l_avg_order_value ); DBMS_OUTPUT.PUT_LINE('Total Orders: ' + isnull(@l_total_orders, '')); DBMS_OUTPUT.PUT_LINE('Average Order Value: ' + isnull(l_avg_order_value, '')); END; /
CREATE PROCEDURE GenerateStats( IN start_date DATE, IN end_date DATE, OUT total_orders INT, OUT avg_order_value FLOAT ) AS l_total_orders INT := 0; l_total_value FLOAT := 0; BEGIN SELECT COUNT(*) INTO l_total_orders FROM orders WHERE order_date BETWEEN start_date AND end_date; SELECT AVG(order_total) INTO l_total_value FROM orders WHERE order_date BETWEEN start_date AND end_date; total_orders := l_total_orders; avg_order_value := l_total_value; END; DECLARE l_total_orders INT; l_avg_order_value FLOAT; BEGIN CALL GenerateStats( TO_DATE('2022-01-01', 'YYYY-MM-DD'), TO_DATE('2022-12-31', 'YYYY-MM-DD'), l_total_orders, l_avg_order_value ); DBMS_OUTPUT.PUT_LINE('Total Orders: ' || l_total_orders); DBMS_OUTPUT.PUT_LINE('Average Order Value: ' || l_avg_order_value); 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, emp_name OUT VARCHAR2, emp_salary OUT NUMBER ) AS BEGIN SELECT @emp_name = first_name, @emp_salary = last_name, salary FROM employees WHERE employee_id = emp_id; END; DECLARE @emp_name VARCHAR(20); emp_salary NUMBER; BEGIN dbo.get_employee_info(100, @emp_name, emp_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with that ID.'); END END;
CREATE OR REPLACE PROCEDURE get_employee_info ( emp_id IN NUMBER, emp_name OUT VARCHAR2, emp_salary OUT NUMBER ) AS BEGIN SELECT first_name, last_name, salary INTO emp_name, emp_salary FROM employees WHERE employee_id = emp_id; END; DECLARE emp_name VARCHAR2(20); emp_salary NUMBER; BEGIN get_employee_info(100, emp_name, emp_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with that ID.'); END;
DECLARE @v_empno FLOAT; v_salary NUMBER; v_comm NUMBER; BEGIN SELECT @v_salary = salary, @v_comm = commission_pct FROM emp WHERE empno = 7369; IF v_comm IS NULL OR v_comm < 0.1 BEGIN RAISERROR('-20000 Commission pct must be >= 0.1', 16, 1); END SET @v_salary = v_salary * (1 + v_comm); UPDATE emp SET salary = v_salary WHERE empno = 7369; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_empno NUMBER; v_salary NUMBER; v_comm NUMBER; BEGIN SELECT salary, commission_pct INTO v_salary, v_comm FROM emp WHERE empno = 7369; IF v_comm IS NULL OR v_comm < 0.1 THEN RAISE_APPLICATION_ERROR(-20000, 'Commission pct must be >= 0.1'); END IF; v_salary := v_salary * (1 + v_comm); UPDATE emp SET salary = v_salary WHERE empno = 7369; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE @name VARCHAR(50); balance NUMBER; BEGIN SELECT @name = @name, @balance = balance FROM accounts; IF balance < 0 BEGIN RAISERROR('-20000 Account overdrawn', 16, 1); END END;
DECLARE name VARCHAR2(50); balance NUMBER; BEGIN SELECT name, balance INTO name, balance FROM accounts; IF balance < 0 THEN RAISE_APPLICATION_ERROR(-20000, 'Account overdrawn'); END IF; END;
BEGIN /* SQLINES DEMO *** and cursors */ DECLARE @employee_id FLOAT; employee_salary NUMBER; cursor c_employees IS SELECT @employee_id, salary FROM employees; BEGIN /* Open the cursor */ OPEN c_employees; /* SQLINES DEMO *** ow */ FETCH c_employees INTO @employee_id, @employee_salary; /* SQLINES DEMO *** ore rows */ WHILE c_employees%FOUND BEGIN /* Process the row */ DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@employee_id, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(employee_salary, '')); /* SQLINES DEMO *** w */ FETCH c_employees INTO @employee_id, @employee_salary; END; /* Close the cursor */ CLOSE c_employees; EXCEPTION /* SQLINES DEMO *** that may occur */ WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END; END;
BEGIN /* Declare variables and cursors */ DECLARE employee_id NUMBER; employee_salary NUMBER; cursor c_employees IS SELECT employee_id, salary FROM employees; BEGIN /* Open the cursor */ OPEN c_employees; /* Fetch the first row */ FETCH c_employees INTO employee_id, employee_salary; /* While there are more rows */ WHILE c_employees%FOUND LOOP /* Process the row */ DBMS_OUTPUT.PUT_LINE('Employee ID: ' || employee_id); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee_salary); /* Fetch the next row */ FETCH c_employees INTO employee_id, employee_salary; END LOOP; /* Close the cursor */ CLOSE c_employees; EXCEPTION /* Handle any errors that may occur */ WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END; END;
CREATE PROCEDURE update_department ( @deptno IN AS NUMBER, dname IN VARCHAR2, loc IN VARCHAR2 ) IS BEGIN UPDATE departments SET dname = dname, loc = loc WHERE deptno = deptno; COMMIT; END;
CREATE PROCEDURE update_department ( deptno IN NUMBER, dname IN VARCHAR2, loc IN VARCHAR2 ) IS BEGIN UPDATE departments SET dname = dname, loc = loc WHERE deptno = deptno; COMMIT; END;
IF OBJECT_ID('InsertEmployeeData', 'P') IS NOT NULL DROP PROCEDURE InsertEmployeeData; GO CREATE PROCEDURE InsertEmployeeData( @p_first_name IN AS VARCHAR2, p_last_name IN VARCHAR2, p_salary IN NUMBER, p_department_id IN NUMBER, p_error_message OUT VARCHAR2 ) AS BEGIN INSERT INTO Employees ( first_name, last_name, salary, department_id ) VALUES ( p_first_name, p_last_name, p_salary, p_department_id ); COMMIT; EXCEPTION WHEN OTHERS THEN set @p_error_message = 'An error occurred while inserting data.'; RAISE; END; /
CREATE OR REPLACE PROCEDURE InsertEmployeeData( p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_salary IN NUMBER, p_department_id IN NUMBER, p_error_message OUT VARCHAR2 ) AS BEGIN INSERT INTO Employees ( first_name, last_name, salary, department_id ) VALUES ( p_first_name, p_last_name, p_salary, p_department_id ); COMMIT; EXCEPTION WHEN OTHERS THEN p_error_message := 'An error occurred while inserting data.'; RAISE; END; /
DECLARE @x FLOAT; BEGIN SELECT @x = COUNT(*) FROM emp; IF @x > 0 BEGIN RAISERROR('-20001 No employees found', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found'); END END;
DECLARE x NUMBER; BEGIN SELECT COUNT(*) INTO x FROM emp; IF x > 0 THEN RAISE_APPLICATION_ERROR(-20001, 'No employees found'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found'); END;
DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Salary of employee 1 is ' + isnull(@v_salary, '')); IF @v_salary < 50000 BEGIN RAISERROR('-20001 Salary is less than 50000', 16, 1); END UPDATE employees SET salary = @v_salary + 10000 WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error occurred, transaction rolled back.'); END;
DECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Salary of employee 1 is ' || v_salary); IF v_salary < 50000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary is less than 50000'); END IF; UPDATE employees SET salary = v_salary + 10000 WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error occurred, transaction rolled back.'); 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; AS orders_ref CURSOR; BEGIN SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_ref; END; DECLARE @customer_id FLOAT := 100; orders_ref SYS_REFCURSOR; BEGIN SET @orders_ref = dbo.get_customer_orders(@customer_id); WHILE 1=1 BEGIN FETCH orders_ref INTO @customer_id, @order_id; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE('Customer: ' + isnull(@customer_id, '') + ' Order: ' + isnull(order_id, '')); END; CLOSE orders_ref; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No orders found for customer: ' + isnull(@customer_id, '')); END END;
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR AS orders_ref CURSOR; BEGIN OPEN orders_ref FOR SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_ref; END; DECLARE customer_id NUMBER := 100; orders_ref SYS_REFCURSOR; BEGIN orders_ref := get_customer_orders(customer_id); LOOP FETCH orders_ref INTO customer_id, order_id; EXIT WHEN orders_ref%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Customer: ' || customer_id || ' Order: ' || order_id); END LOOP; CLOSE orders_ref; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No orders found for customer: ' || customer_id); END;
CREATE PROCEDURE InsertEmployee(@name IN AS VARCHAR2, job IN VARCHAR2, salary IN NUMBER) AS BEGIN INSERT INTO employees(name, job, salary) VALUES (name, job, salary); COMMIT; END; / BEGIN DECLARE @emp_name VARCHAR(20); emp_job dbo.VARCHAR2(10); emp_salary NUMBER; BEGIN SELECT @emp_name = name, @emp_job = job, @emp_salary = salary FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@emp_name, '') + ' works as a ' + isnull(emp_job, '') + ' and earns ' + isnull(emp_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with employee_id 1'); END END; END; / CREATE FUNCTION GetEmployeeCount() RETURNS FLOAT AS BEGIN RETURN (SELECT COUNT(*) FROM employees); END; GO DECLARE @emp_count FLOAT; BEGIN SET @emp_count = dbo.GetEmployeeCount(); DBMS_OUTPUT.PUT_LINE('There are ' + isnull(@emp_count, '') + ' employees in the table'); END; / BEGIN DECLARE error_found EXCEPTION; BEGIN INSERT INTO employees(name, job, salary) VALUES ('John Doe', 'Manager', 5000); COMMIT; EXCEPTION WHEN OTHERS THEN RAISE error_found; END; IF error_found BEGIN DBMS_OUTPUT.PUT_LINE('An error occurred while inserting the employee'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee inserted successfully'); END END; /
CREATE PROCEDURE InsertEmployee(name IN VARCHAR2, job IN VARCHAR2, salary IN NUMBER) AS BEGIN INSERT INTO employees(name, job, salary) VALUES (name, job, salary); COMMIT; END; / BEGIN DECLARE emp_name VARCHAR2(20); emp_job VARCHAR2(10); emp_salary NUMBER; BEGIN SELECT name, job, salary INTO emp_name, emp_job, emp_salary FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Employee ' || emp_name || ' works as a ' || emp_job || ' and earns ' || emp_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with employee_id 1'); END; END; / FUNCTION GetEmployeeCount() RETURN NUMBER IS BEGIN RETURN (SELECT COUNT(*) FROM employees); END; / DECLARE emp_count NUMBER; BEGIN emp_count := GetEmployeeCount(); DBMS_OUTPUT.PUT_LINE('There are ' || emp_count || ' employees in the table'); END; / BEGIN DECLARE error_found EXCEPTION; BEGIN INSERT INTO employees(name, job, salary) VALUES ('John Doe', 'Manager', 5000); COMMIT; EXCEPTION WHEN OTHERS THEN RAISE error_found; END; IF error_found THEN DBMS_OUTPUT.PUT_LINE('An error occurred while inserting the employee'); ELSE DBMS_OUTPUT.PUT_LINE('Employee inserted successfully'); END IF; END; /
DECLARE @v_emp_id DECIMAL(19,4) /* Use -meta option employees.employee_id%TYPE */; v_mgr_id employees.manager_id%TYPE; v_name employees.first_name%TYPE; v_salary employees.salary%TYPE; *** riables v_sql_code NUMBER; v_sql_errm dbo.VARCHAR2(2000); BEGIN SET @v_emp_id = 100; SET @v_mgr_id = NULL; SET @v_name = 'John'; SET @v_salary = 50000; EXCEPTION WHEN OTHERS THEN SET @v_sql_code = @@ERROR; SET @v_sql_errm = SQLERRM; RAISE; END; /
DECLARE v_emp_id employees.employee_id%TYPE; v_mgr_id employees.manager_id%TYPE; v_name employees.first_name%TYPE; v_salary employees.salary%TYPE; -- Error handling variables v_sql_code NUMBER; v_sql_errm VARCHAR2(2000); BEGIN v_emp_id := 100; v_mgr_id := NULL; v_name := 'John'; v_salary := 50000; EXCEPTION WHEN OTHERS THEN v_sql_code := SQLCODE; v_sql_errm := SQLERRM; RAISE; END; /
CREATE PROCEDURE update_employee(@p_id IN AS NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER) AS BEGIN UPDATE employees SET name = p_name, salary = p_salary WHERE id = p_id; END; /
CREATE PROCEDURE update_employee(p_id IN NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER) AS BEGIN UPDATE employees SET name = p_name, salary = p_salary WHERE id = p_id; END; /
IF OBJECT_ID('calculate_bonus', 'P') IS NOT NULL DROP PROCEDURE calculate_bonus; GO CREATE PROCEDURE calculate_bonus( @employee_id IN AS NUMBER, bonus_amount OUT NUMBER ) AS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = employee_id; IF salary > 100000 BEGIN set @bonus_amount = 0.1 * salary; END ELSE IF salary > 50000 BEGIN set @bonus_amount = 0.05 * salary; END ELSE BEGIN set @bonus_amount = 0; END END;
CREATE OR REPLACE PROCEDURE calculate_bonus( employee_id IN NUMBER, bonus_amount OUT NUMBER ) AS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; IF salary > 100000 THEN bonus_amount := 0.1 * salary; ELSIF salary > 50000 THEN bonus_amount := 0.05 * salary; ELSE bonus_amount := 0; END IF; END;
BEGIN DECLARE r CURSOR FOR SELECT * FROM table1 WHERE condition = TRUE; OPEN r; FETCH r INTO; WHILE @@FETCH_STATUS=0 BEGIN UPDATE table2 SET field = @r_field WHERE id = @r_id; FETCH r INTO; END; CLOSE r; DEALLOCATE r; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END;
BEGIN FOR r IN (SELECT * FROM table1 WHERE condition = TRUE) LOOP UPDATE table2 SET field = r.field WHERE id = r.id; END LOOP; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END;
IF OBJECT_ID('process_orders', 'P') IS NOT NULL DROP PROCEDURE process_orders; GO CREATE PROCEDURE process_orders ( @p_order_id IN AS NUMBER, p_result OUT VARCHAR2 ) IS l_order_exists BOOLEAN; l_order_total NUMBER; BEGIN *** ists SELECT @l_order_exists = COUNT(*) FROM orders WHERE id = p_order_id; IF l_order_exists = FALSE BEGIN set @p_result = 'Order not found.'; RETURN; END *** otal SELECT @l_order_total = SUM(amount) FROM order_details WHERE order_id = p_order_id; -- Up... SQLINES DEMO *** UPDATE orders SET status = 'Processed' WHERE id = p_order_id; *** cessing INSERT INTO order_logs (order_id, status) VALUES (p_order_id, 'Processed'); *** rameter set @p_result = 'Order processed successfully.'; EXCEPTION WHEN OTHERS THEN set @p_result = 'An error occurred while processing the order.'; END;
CREATE OR REPLACE PROCEDURE process_orders ( p_order_id IN NUMBER, p_result OUT VARCHAR2 ) IS l_order_exists BOOLEAN; l_order_total NUMBER; BEGIN -- Check if order exists SELECT COUNT(*) INTO l_order_exists FROM orders WHERE id = p_order_id; IF l_order_exists = FALSE THEN p_result := 'Order not found.'; RETURN; END IF; -- Calculate order total SELECT SUM(amount) INTO l_order_total FROM order_details WHERE order_id = p_order_id; -- Update order status UPDATE orders SET status = 'Processed' WHERE id = p_order_id; -- Log the order processing INSERT INTO order_logs (order_id, status) VALUES (p_order_id, 'Processed'); -- Set the output parameter p_result := 'Order processed successfully.'; EXCEPTION WHEN OTHERS THEN p_result := 'An error occurred while processing the order.'; END;
CREATE PROCEDURE emp_salary_update AS emp_id NUMBER; salary NUMBER; CURSOR c1 IS SELECT employee_id, salary FROM employees WHERE job_id = 'IT_PROG'; BEGIN OPEN c1; FETCH c1 INTO @emp_id, @salary; WHILE c1%FOUND BEGIN set @salary = salary + (salary * 0.10); UPDATE employees SET salary = salary WHERE employee_id = emp_id; FETCH c1 INTO @emp_id, @salary; END; CLOSE c1; END;
CREATE PROCEDURE emp_salary_update IS emp_id NUMBER; salary NUMBER; CURSOR c1 IS SELECT employee_id, salary FROM employees WHERE job_id = 'IT_PROG'; BEGIN OPEN c1; FETCH c1 INTO emp_id, salary; WHILE c1%FOUND LOOP salary := salary + (salary * 0.10); UPDATE employees SET salary = salary WHERE employee_id = emp_id; FETCH c1 INTO emp_id, salary; END LOOP; CLOSE c1; END;
CREATE TABLE Employees ( employee_id FLOAT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, salary DECIMAL(10, 2) NOT NULL, department_id FLOAT REFERENCES Departments(department_id) ); CREATE TABLE Departments ( department_id FLOAT PRIMARY KEY, department_name VARCHAR(50) UNIQUE NOT NULL ); INSERT INTO Departments (department_id, department_name) VALUES (10, 'Sales'); INSERT INTO Departments (department_id, department_name) VALUES (20, 'Marketing'); INSERT INTO Departments (department_id, department_name) VALUES (30, 'Human Resources'); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (1, 'John', 'Doe', 'john.doe@example.com', 50000, 10); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (2, 'Jane', 'Smith', 'jane.smith@example.com', 60000, 20); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (3, 'Michael', 'Jones', 'michael.jones@example.com', 70000, 30); *** to get employee information IF OBJECT_ID('GET_EMPLOYEE_INFO', 'P') IS NOT NULL DROP PROCEDURE GET_EMPLOYEE_INFO; GO CREATE PROCEDURE GET_EMPLOYEE_INFO(@employee_id IN AS NUMBER, OUT first_name, last_name, email, salary, department_name) AS BEGIN SELECT @first_name = first_name, @last_name = last_name, @email = email, @salary = salary, @department_name = D.department_name FROM Employees E JOIN Departments D ON E.department_id = D.department_id WHERE E.employee_id = employee_id; END; *** late bonus based on salary IF OBJECT_ID('GET_BONUS', 'FN') IS NOT NULL DROP FUNCTION GET_BONUS; GO CREATE FUNCTION GET_BONUS(@salary IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN RETURN salary * 0.1; END; *** truct - IF Statement BEGIN DECLARE @bonus FLOAT; SET @bonus = dbo.GET_BONUS(60000); IF @bonus > 5000 BEGIN DBMS_OUTPUT.PUT_LINE('Eligible for high bonus'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Eligible for low bonus'); END END; *** TRY...CATCH Block BEGIN *** e a statement that may raise an error UPDATE Employees SET salary = salary * 1.10 WHERE employee_id = 4; *** that occur during execution EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE TABLE Employees ( employee_id NUMBER PRIMARY KEY, first_name VARCHAR2(50) NOT NULL, last_name VARCHAR2(50) NOT NULL, email VARCHAR2(100) UNIQUE NOT NULL, salary NUMBER(10, 2) NOT NULL, department_id NUMBER REFERENCES Departments(department_id) ); CREATE TABLE Departments ( department_id NUMBER PRIMARY KEY, department_name VARCHAR2(50) UNIQUE NOT NULL ); INSERT INTO Departments (department_id, department_name) VALUES (10, 'Sales'); INSERT INTO Departments (department_id, department_name) VALUES (20, 'Marketing'); INSERT INTO Departments (department_id, department_name) VALUES (30, 'Human Resources'); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (1, 'John', 'Doe', 'john.doe@example.com', 50000, 10); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (2, 'Jane', 'Smith', 'jane.smith@example.com', 60000, 20); INSERT INTO Employees (employee_id, first_name, last_name, email, salary, department_id) VALUES (3, 'Michael', 'Jones', 'michael.jones@example.com', 70000, 30); -- Stored Procedure to get employee information CREATE OR REPLACE PROCEDURE GET_EMPLOYEE_INFO(employee_id IN NUMBER, OUT first_name, last_name, email, salary, department_name) AS BEGIN SELECT first_name, last_name, email, salary, D.department_name INTO first_name, last_name, email, salary, department_name FROM Employees E JOIN Departments D ON E.department_id = D.department_id WHERE E.employee_id = employee_id; END; -- Function to calculate bonus based on salary CREATE OR REPLACE FUNCTION GET_BONUS(salary IN NUMBER) RETURN NUMBER AS BEGIN RETURN salary * 0.1; END; -- Control Flow Construct - IF Statement BEGIN DECLARE bonus NUMBER; bonus := GET_BONUS(60000); IF bonus > 5000 THEN DBMS_OUTPUT.PUT_LINE('Eligible for high bonus'); ELSE DBMS_OUTPUT.PUT_LINE('Eligible for low bonus'); END IF; END; -- Error Handling - TRY...CATCH Block BEGIN -- Attempt to execute a statement that may raise an error UPDATE Employees SET salary = salary * 1.10 WHERE employee_id = 4; -- Catch any errors that occur during execution EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
CREATE PROCEDURE get_employee_info (@p_emp_id IN AS NUMBER) AS v_emp_name dbo.VARCHAR2(20); v_emp_salary NUMBER; BEGIN SELECT @v_emp_name = ename, @v_emp_salary = sal FROM emp WHERE empno = p_emp_id; DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(v_emp_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.'); END END;
CREATE PROCEDURE get_employee_info (p_emp_id IN NUMBER) AS v_emp_name VARCHAR2(20); v_emp_salary NUMBER; BEGIN SELECT ename, sal INTO v_emp_name, v_emp_salary FROM emp WHERE empno = p_emp_id; DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_emp_name); DBMS_OUTPUT.PUT_LINE('Employee salary: ' || v_emp_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.'); END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(50); BEGIN *** oyee into the database INSERT INTO employees (emp_id, emp_name) VALUES (100, 'John Doe'); *** ee's name from the database SELECT @v_emp_name = emp_name FROM employees WHERE emp_id = 100; *** ee's name in the database UPDATE employees SET emp_name = 'Jane Doe' WHERE emp_id = 100; *** ee from the database DELETE FROM employees WHERE emp_id = 100; END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(50); BEGIN -- Insert a new employee into the database INSERT INTO employees (emp_id, emp_name) VALUES (100, 'John Doe'); -- Select the employee's name from the database SELECT emp_name INTO v_emp_name FROM employees WHERE emp_id = 100; -- Update the employee's name in the database UPDATE employees SET emp_name = 'Jane Doe' WHERE emp_id = 100; -- Delete the employee from the database DELETE FROM employees WHERE emp_id = 100; END;
IF OBJECT_ID('get_employee_info', 'FN') IS NOT NULL DROP FUNCTION get_employee_info; GO CREATE FUNCTION get_employee_info(@emp_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; AS emp_cur SYS_REFCURSOR; BEGIN SELECT emp_name, job_title, salary FROM employees WHERE employee_id = emp_id; RETURN emp_cur; END; /
CREATE OR REPLACE FUNCTION get_employee_info(emp_id IN NUMBER) RETURN SYS_REFCURSOR AS emp_cur SYS_REFCURSOR; BEGIN OPEN emp_cur FOR SELECT emp_name, job_title, salary FROM employees WHERE employee_id = emp_id; RETURN emp_cur; END; /
CREATE PROCEDURE create_employee ( @p_employee_id IN AS NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_phone_number IN VARCHAR2 ) AS BEGIN *** oyee into the EMPLOYEES table INSERT INTO EMPLOYEES ( EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER ) VALUES ( p_employee_id, p_first_name, p_last_name, p_email, p_phone_number ); *** ction COMMIT; END; *** mployee procedure EXECUTE dbo.create_employee ( 100, 'John', 'Doe', 'john.doe@example.com', '555-123-4567' );
CREATE PROCEDURE create_employee ( p_employee_id IN NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_phone_number IN VARCHAR2 ) AS BEGIN -- Insert a new employee into the EMPLOYEES table INSERT INTO EMPLOYEES ( EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER ) VALUES ( p_employee_id, p_first_name, p_last_name, p_email, p_phone_number ); -- Commit the transaction COMMIT; END; -- Call the create_employee procedure EXECUTE create_employee ( 100, 'John', 'Doe', 'john.doe@example.com', '555-123-4567' );
IF OBJECT_ID('get_customer_info', 'P') IS NOT NULL DROP PROCEDURE get_customer_info; GO CREATE PROCEDURE get_customer_info(@customer_id IN AS NUMBER, OUT first_name OUT VARCHAR2, OUT last_name OUT VARCHAR2) AS BEGIN SELECT @first_name = first_name, @last_name = last_name FROM customers WHERE customer_id = customer_id; IF @@FETCH_STATUS <> 0 BEGIN raiserror('-20000 Customer not found', 16, 1); END END; IF OBJECT_ID('get_order_total', 'FN') IS NOT NULL DROP FUNCTION get_order_total; GO CREATE FUNCTION get_order_total(@order_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS total NUMBER; BEGIN SELECT @total = SUM(quantity * unit_price) FROM order_details WHERE order_id = order_id; RETURN total; IF @@FETCH_STATUS <> 0 BEGIN RETURN 0; END END;
CREATE OR REPLACE PROCEDURE get_customer_info(customer_id IN NUMBER, OUT first_name OUT VARCHAR2, OUT last_name OUT VARCHAR2) AS BEGIN SELECT first_name, last_name INTO first_name, last_name FROM customers WHERE customer_id = customer_id; EXCEPTION WHEN NO_DATA_FOUND THEN raise_application_error(-20000, 'Customer not found'); END; CREATE OR REPLACE FUNCTION get_order_total(order_id IN NUMBER) RETURN NUMBER IS total NUMBER; BEGIN SELECT SUM(quantity * unit_price) INTO total FROM order_details WHERE order_id = order_id; RETURN total; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 0; END;
BEGIN DECLARE @x INT; BEGIN SET @x = 10 / 0; EXCEPTION WHEN OTHERS THEN RAISERROR('-20001 Division by zero', 16, 1); END; END;
BEGIN DECLARE x INT; BEGIN x := 10 / 0; EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR(-20001, 'Division by zero'); END; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc (@param1 IN AS NUMBER) IS v_temp NUMBER; BEGIN SELECT @v_temp = count(*) FROM my_table WHERE id = param1; IF v_temp > 0 BEGIN UPDATE my_table SET val = val + 1 WHERE id = param1; COMMIT; END ELSE BEGIN RAISERROR('-20000 Record not found.', 16, 1); END END my_proc; IF OBJECT_ID('my_func', 'FN') IS NOT NULL DROP FUNCTION my_func; GO CREATE FUNCTION my_func (@param1 IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS v_result NUMBER; BEGIN SELECT @v_result = val FROM my_table WHERE id = param1; RETURN v_result; END my_func; DECLARE @v_id FLOAT; BEGIN SET @v_id = dbo.my_func(10); dbo.my_proc(@v_id); COMMIT; END;
CREATE OR REPLACE PROCEDURE my_proc (param1 IN NUMBER) IS v_temp NUMBER; BEGIN SELECT count(*) INTO v_temp FROM my_table WHERE id = param1; IF v_temp > 0 THEN UPDATE my_table SET val = val + 1 WHERE id = param1; COMMIT; ELSE RAISE_APPLICATION_ERROR(-20000, 'Record not found.'); END IF; END my_proc; CREATE OR REPLACE FUNCTION my_func (param1 IN NUMBER) RETURN NUMBER IS v_result NUMBER; BEGIN SELECT val INTO v_result FROM my_table WHERE id = param1; RETURN v_result; END my_func; DECLARE v_id NUMBER; BEGIN v_id := my_func(10); my_proc(v_id); COMMIT; END;
DECLARE @emp_no FLOAT; sal NUMBER; BEGIN SELECT @emp_no = employee_id, @sal = salary FROM employees WHERE last_name = 'King'; IF sal IS NULL BEGIN RAISERROR('-20000 Employee not found', 16, 1); END UPDATE employees SET salary = sal * 1.10 WHERE employee_id = @emp_no; COMMIT; END;
DECLARE emp_no NUMBER; sal NUMBER; BEGIN SELECT employee_id, salary INTO emp_no, sal FROM employees WHERE last_name = 'King'; IF sal IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'Employee not found'); END IF; UPDATE employees SET salary = sal * 1.10 WHERE employee_id = emp_no; COMMIT; END;
CREATE PROCEDURE get_customer_orders(@p_customer_id IN AS NUMBER) IS v_order_id NUMBER; v_order_date DATE; v_total_cost NUMBER; BEGIN SELECT @v_order_id = order_id, @v_order_date = order_date, @v_total_cost = total_cost FROM orders WHERE customer_id = p_customer_id; IF v_order_id IS NULL BEGIN RAISERROR('-20000 No orders found for customer.', 16, 1); END DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(v_order_id, '')); DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(v_order_date, '')); DBMS_OUTPUT.PUT_LINE('Total Cost: ' + isnull(v_total_cost, '')); END get_customer_orders;
CREATE PROCEDURE get_customer_orders(p_customer_id IN NUMBER) IS v_order_id NUMBER; v_order_date DATE; v_total_cost NUMBER; BEGIN SELECT order_id, order_date, total_cost INTO v_order_id, v_order_date, v_total_cost FROM orders WHERE customer_id = p_customer_id; IF v_order_id IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'No orders found for customer.'); END IF; DBMS_OUTPUT.PUT_LINE('Order ID: ' || v_order_id); DBMS_OUTPUT.PUT_LINE('Order Date: ' || v_order_date); DBMS_OUTPUT.PUT_LINE('Total Cost: ' || v_total_cost); END get_customer_orders;
IF OBJECT_ID('get_customer_name', 'FN') IS NOT NULL DROP FUNCTION get_customer_name; GO CREATE FUNCTION get_customer_name(@customer_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; AS BEGIN RETURN ( SELECT name FROM customer WHERE customer_id = customer_id ); END; IF OBJECT_ID('update_customer_balance', 'P') IS NOT NULL DROP PROCEDURE update_customer_balance; GO CREATE PROCEDURE update_customer_balance(@customer_id IN AS NUMBER, amount IN NUMBER) AS BEGIN UPDATE customer SET balance = balance + amount WHERE customer_id = customer_id; END; BEGIN -- De... SQLINES DEMO *** l_customer_id NUMBER; l_amount NUMBER; *** nd amount from user set @l_customer_id = &customer_id; set @l_amount = &amount; *** dure to update customer balance dbo.update_customer_balance(l_customer_id, l_amount); -- Co... SQLINES DEMO *** COMMIT; *** essage DBMS_OUTPUT.PUT_LINE('Customer balance updated successfully.'); -- Er... SQLINES DEMO *** IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Customer not found.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); ROLLBACK; END;
CREATE OR REPLACE FUNCTION get_customer_name(customer_id IN NUMBER) RETURN VARCHAR2 AS BEGIN RETURN ( SELECT name FROM customer WHERE customer_id = customer_id ); END; CREATE OR REPLACE PROCEDURE update_customer_balance(customer_id IN NUMBER, amount IN NUMBER) AS BEGIN UPDATE customer SET balance = balance + amount WHERE customer_id = customer_id; END; BEGIN -- Declare variables l_customer_id NUMBER; l_amount NUMBER; -- Get customer ID and amount from user l_customer_id := &customer_id; l_amount := &amount; -- Call stored procedure to update customer balance update_customer_balance(l_customer_id, l_amount); -- Commit changes COMMIT; -- Display success message DBMS_OUTPUT.PUT_LINE('Customer balance updated successfully.'); EXCEPTION -- Error handling WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Customer not found.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); ROLLBACK; END;
DECLARE @v_count FLOAT; BEGIN SELECT @v_count = COUNT(*) FROM employees; IF @v_count > 0 BEGIN DECLARE @i INT = 1; WHILE @i <= @v_count BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(i, '')); END; END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employees found'); END END;
DECLARE v_count NUMBER; BEGIN SELECT COUNT(*) INTO v_count FROM employees; IF v_count > 0 THEN FOR i IN 1..v_count LOOP DBMS_OUTPUT.PUT_LINE('Employee ' || i); END LOOP; ELSE DBMS_OUTPUT.PUT_LINE('No employees found'); END IF; END;
CREATE PROCEDURE GetEmployeeInfo(@emp_id IN AS NUMBER) AS emp_rec RECORD(name VARCHAR(20), salary NUMBER, dept_id NUMBER); BEGIN SELECT @emp_rec = ename, sal, deptno FROM emp WHERE empno = emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_rec.name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_rec.salary, '')); DBMS_OUTPUT.PUT_LINE('Employee Department: ' + isnull(emp_rec.dept_id, '')); END; DECLARE @emp_id FLOAT := 7902; BEGIN dbo.GetEmployeeInfo(@emp_id); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(@emp_id, '')); END END;
CREATE PROCEDURE GetEmployeeInfo(emp_id IN NUMBER) AS emp_rec RECORD(name VARCHAR(20), salary NUMBER, dept_id NUMBER); BEGIN SELECT ename, sal, deptno INTO emp_rec FROM emp WHERE empno = emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_rec.name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_rec.salary); DBMS_OUTPUT.PUT_LINE('Employee Department: ' || emp_rec.dept_id); END; DECLARE emp_id NUMBER := 7902; BEGIN GetEmployeeInfo(emp_id); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || emp_id); END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN *** r the specified employee ID SELECT @salary = salary FROM employees WHERE employee_id = employee_id; -- Re... SQLINES DEMO *** RETURN salary; END; DECLARE *** e to store the employee ID @employee_id FLOAT; *** e to store the salary salary NUMBER; BEGIN *** or the employee ID DBMS_OUTPUT.PUT_LINE('Enter the employee ID: '); DBMS_INPUT.GET_LINE(@employee_id); *** oyee_salary function to get the salary for the specified employee ID SET @salary = dbo.get_employee_salary(@employee_id); -- Pr... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, '')); END;
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN -- Get the salary for the specified employee ID SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; -- Return the salary RETURN salary; END; DECLARE -- Declare a variable to store the employee ID employee_id NUMBER; -- Declare a variable to store the salary salary NUMBER; BEGIN -- Prompt the user for the employee ID DBMS_OUTPUT.PUT_LINE('Enter the employee ID: '); DBMS_INPUT.GET_LINE(employee_id); -- Call the get_employee_salary function to get the salary for the specified employee ID salary := get_employee_salary(employee_id); -- Print the salary DBMS_OUTPUT.PUT_LINE('Salary: ' || salary); END;
DECLARE @CURSOR c_employees IS SELECT * FROM employees; TYPE t_employee AS OBJECT (ename VARCHAR(30), sal ROW_NUMBER(10, 2)); TYPE t_employees IS TABLE OF t_employee; BEGIN OPEN c_employees; WHILE 1=1 BEGIN FETCH c_employees INTO @e; IF @@FETCH_STATUS <> 0 BREAK; IF e.sal > 10000 BEGIN employees_with_high_salaries.EXTEND; dbo.employees_with_high_salaries(employees_with_high_salaries.COUNT) := e; END END; CLOSE c_employees; END;
DECLARE CURSOR c_employees IS SELECT * FROM employees; TYPE t_employee AS OBJECT (ename VARCHAR(30), sal NUMBER(10, 2)); TYPE t_employees IS TABLE OF t_employee; BEGIN OPEN c_employees; LOOP FETCH c_employees INTO e; EXIT WHEN c_employees%NOTFOUND; IF e.sal > 10000 THEN employees_with_high_salaries.EXTEND; employees_with_high_salaries(employees_with_high_salaries.COUNT) := e; END IF; END LOOP; CLOSE c_employees; END;
IF OBJECT_ID('example_proc', 'P') IS NOT NULL DROP PROCEDURE example_proc; GO CREATE PROCEDURE example_proc( @p_input IN AS VARCHAR2 ) AS v_output dbo.VARCHAR2(100); BEGIN SELECT @v_output = UPPER(p_input); IF v_output IS NULL BEGIN RAISERROR('-20001 Invalid input', 16, 1); END DBMS_OUTPUT.PUT_LINE(v_output); END example_proc;
CREATE OR REPLACE PROCEDURE example_proc( p_input IN VARCHAR2 ) AS v_output VARCHAR2(100); BEGIN SELECT UPPER(p_input) INTO v_output FROM dual; IF v_output IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid input'); END IF; DBMS_OUTPUT.PUT_LINE(v_output); END example_proc;
BEGIN DECLARE @v_emp_id FLOAT; BEGIN SELECT @v_emp_id = emp_id FROM employees WHERE emp_name = 'John Doe'; IF @v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found.', 16, 1); END END; UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id; COMMIT; END;
BEGIN DECLARE v_emp_id NUMBER; BEGIN SELECT emp_id INTO v_emp_id FROM employees WHERE emp_name = 'John Doe'; IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found.'); END IF; END; UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id; COMMIT; END;
CREATE TABLE employees ( id FLOAT PRIMARY KEY, name VARCHAR(50) NOT NULL, salary FLOAT NOT NULL, department_id FLOAT, CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments (id) ); CREATE TABLE departments ( id FLOAT PRIMARY KEY, name VARCHAR(50) NOT NULL ); INSERT INTO departments (id, name) VALUES (1, 'Sales'); INSERT INTO departments (id, name) VALUES (2, 'Marketing'); INSERT INTO departments (id, name) VALUES (3, 'Engineering'); INSERT INTO employees (id, name, salary, department_id) VALUES (1, 'John Doe', 50000, 1); INSERT INTO employees (id, name, salary, department_id) VALUES (2, 'Jane Smith', 60000, 2); INSERT INTO employees (id, name, salary, department_id) VALUES (3, 'Bob Jones', 70000, 3); CREATE FUNCTION get_employee_salary (@employee_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE id = employee_id; RETURN salary; END; CREATE PROCEDURE give_raise (@employee_id IN AS NUMBER, raise_amount IN NUMBER) IS BEGIN UPDATE employees SET salary = salary + raise_amount WHERE id = employee_id; END; BEGIN DECLARE @salary FLOAT; BEGIN SET @salary = dbo.get_employee_salary(1); dbo.give_raise(1, 10000); DBMS_OUTPUT.PUT_LINE('Employee 1''s new salary is ' + isnull(@salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END END; END;
CREATE TABLE employees ( id NUMBER PRIMARY KEY, name VARCHAR(50) NOT NULL, salary NUMBER NOT NULL, department_id NUMBER, CONSTRAINT fk_department FOREIGN KEY (department_id) REFERENCES departments (id) ); CREATE TABLE departments ( id NUMBER PRIMARY KEY, name VARCHAR(50) NOT NULL ); INSERT INTO departments (id, name) VALUES (1, 'Sales'); INSERT INTO departments (id, name) VALUES (2, 'Marketing'); INSERT INTO departments (id, name) VALUES (3, 'Engineering'); INSERT INTO employees (id, name, salary, department_id) VALUES (1, 'John Doe', 50000, 1); INSERT INTO employees (id, name, salary, department_id) VALUES (2, 'Jane Smith', 60000, 2); INSERT INTO employees (id, name, salary, department_id) VALUES (3, 'Bob Jones', 70000, 3); CREATE FUNCTION get_employee_salary (employee_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE id = employee_id; RETURN salary; END; CREATE PROCEDURE give_raise (employee_id IN NUMBER, raise_amount IN NUMBER) IS BEGIN UPDATE employees SET salary = salary + raise_amount WHERE id = employee_id; END; BEGIN DECLARE salary NUMBER; BEGIN salary := get_employee_salary(1); give_raise(1, 10000); DBMS_OUTPUT.PUT_LINE('Employee 1''s new salary is ' || salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END; END;
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL DROP PROCEDURE emp_bonus; GO CREATE PROCEDURE emp_bonus(@empId FLOAT, @amt FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = salary + @amt WHERE employee_id = @empId; COMMIT; -- Er... SQLINES DEMO *** EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20000 Bonus could not be applied due to an error.', 16, 1); END; IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@empId FLOAT) RETURNS FLOAT AS BEGIN SELECT @result = salary FROM employees WHERE employee_id = @empId; RETURN result; END; DECLARE @empId FLOAT := 1; bonusAmt SET @NUMBER = 200; salary NUMBER; BEGIN SET @salary = dbo.get_employee_salary(@empId); dbo.emp_bonus(@empId, bonusAmt); SET @salary = dbo.get_employee_salary(@empId); DBMS_OUTPUT.PUT_LINE('Updated salary: ' + isnull(salary, '')); END;
CREATE OR REPLACE PROCEDURE emp_bonus(empId NUMBER, amt NUMBER) AS BEGIN UPDATE employees SET salary = salary + amt WHERE employee_id = empId; COMMIT; -- Error handling EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20000, 'Bonus could not be applied due to an error.'); END; CREATE OR REPLACE FUNCTION get_employee_salary(empId NUMBER) RETURN NUMBER AS BEGIN SELECT salary INTO result FROM employees WHERE employee_id = empId; RETURN result; END; DECLARE empId NUMBER := 1; bonusAmt NUMBER := 200; salary NUMBER; BEGIN salary := get_employee_salary(empId); emp_bonus(empId, bonusAmt); salary := get_employee_salary(empId); DBMS_OUTPUT.PUT_LINE('Updated salary: ' || salary); END;
DECLARE @v_num FLOAT; BEGIN SELECT @v_num = COUNT(*) FROM emp; IF @v_num > 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employees found!'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employees found!'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred!'); END; /
DECLARE v_num NUMBER; BEGIN SELECT COUNT(*) INTO v_num FROM emp; IF v_num > 0 THEN DBMS_OUTPUT.PUT_LINE('Employees found!'); ELSE DBMS_OUTPUT.PUT_LINE('No employees found!'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred!'); END; /
BEGIN DECLARE r CURSOR FOR SELECT * FROM t1 WHERE col1 = 10; OPEN r; FETCH r INTO; WHILE @@FETCH_STATUS=0 BEGIN UPDATE t2 SET col2 = col2 + 1 WHERE col3 = @r_col4; COMMIT; FETCH r INTO; END; CLOSE r; DEALLOCATE r; IF SQL%FOUND BEGIN DBMS_OUTPUT.PUT_LINE('Records processed: ' + ISNULL(@@ROWCOUNT, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No records found'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); ROLLBACK; END;
BEGIN FOR r IN (SELECT * FROM t1 WHERE col1 = 10) LOOP UPDATE t2 SET col2 = col2 + 1 WHERE col3 = r.col4; COMMIT; END LOOP; IF SQL%FOUND THEN DBMS_OUTPUT.PUT_LINE('Records processed: ' || SQL%ROWCOUNT); ELSE DBMS_OUTPUT.PUT_LINE('No records found'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); ROLLBACK; END;
DECLARE @v_cursor SYS_REFCURSOR; BEGIN SELECT * FROM table_name; WHILE 1=1 BEGIN FETCH v_cursor INTO @v_row; IF @@FETCH_STATUS <> 0 BREAK; /* SQLINES DEMO *** v_row */ END; CLOSE v_cursor; EXCEPTION WHEN OTHERS THEN ROLLBACK; /* Handle error */ END;
DECLARE v_cursor SYS_REFCURSOR; BEGIN OPEN v_cursor FOR SELECT * FROM table_name; LOOP FETCH v_cursor INTO v_row; EXIT WHEN v_cursor%NOTFOUND; /* Do something with v_row */ END LOOP; CLOSE v_cursor; EXCEPTION WHEN OTHERS THEN ROLLBACK; /* Handle error */ END;
IF OBJECT_ID('GetCustomerOrders', 'FN') IS NOT NULL DROP FUNCTION GetCustomerOrders; GO CREATE FUNCTION GetCustomerOrders(@customer_id FLOAT) RETURNS SYS_REFCURSOR AS BEGIN DECLARE @orders_ref CURSOR; SELECT order_id, order_date FROM orders WHERE @customer_id = @customer_id; RETURN @orders_ref; END; -- Us... SQLINES DEMO *** DECLARE @cursor_orders SYS_REFCURSOR; BEGIN SET @cursor_orders = dbo.GetCustomerOrders(12345); WHILE 1=1 BEGIN FETCH cursor_orders INTO @order_id, @order_date; IF @@FETCH_STATUS <> 0 BREAK; *** data END; CLOSE cursor_orders; END;
CREATE OR REPLACE FUNCTION GetCustomerOrders(customer_id NUMBER) RETURN SYS_REFCURSOR IS orders_ref CURSOR; BEGIN OPEN orders_ref FOR SELECT order_id, order_date FROM orders WHERE customer_id = customer_id; RETURN orders_ref; END; -- Usage example DECLARE cursor_orders SYS_REFCURSOR; BEGIN cursor_orders := GetCustomerOrders(12345); LOOP FETCH cursor_orders INTO order_id, order_date; EXIT WHEN cursor_orders%NOTFOUND; -- Process the order data END LOOP; CLOSE cursor_orders; END;
DECLARE @i FLOAT := 0; BEGIN DECLARE @i INT = 1 ; WHILE @i <= 10 BEGIN DBMS_OUTPUT.PUT_LINE('Iteration ' + isnull(@i, '')); END; RAISERROR('-20001 Error has occurred', 16, 1); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(@@ERROR, '')); END; /
DECLARE i NUMBER := 0; BEGIN FOR i IN 1 .. 10 LOOP DBMS_OUTPUT.PUT_LINE('Iteration ' || i); END LOOP; RAISE_APPLICATION_ERROR(-20001, 'Error has occurred'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLCODE); END; /
IF OBJECT_ID('calculate_bonus', 'P') IS NOT NULL DROP PROCEDURE calculate_bonus; GO CREATE PROCEDURE calculate_bonus(@employee_id IN AS NUMBER) AS emp_bonus NUMBER; BEGIN SELECT @emp_bonus = bonus FROM employees WHERE employee_id = employee_id; IF emp_bonus IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE employees SET bonus = emp_bonus * 1.10 WHERE employee_id = employee_id; END;
CREATE OR REPLACE PROCEDURE calculate_bonus(employee_id IN NUMBER) AS emp_bonus NUMBER; BEGIN SELECT bonus INTO emp_bonus FROM employees WHERE employee_id = employee_id; IF emp_bonus IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE employees SET bonus = emp_bonus * 1.10 WHERE employee_id = employee_id; END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); BEGIN SELECT @v_emp_id = employee_id, @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); IF @v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 No data found for the given query', 16, 1); END END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); BEGIN SELECT employee_id, first_name || ' ' || last_name INTO v_emp_id, v_emp_name FROM employees WHERE employee_id = 1; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'No data found for the given query'); END;
CREATE PROCEDURE GetCustomerOrders (@IN customer_id AS NUMBER) AS BEGIN DECLARE order_row CURSOR FOR SELECT * FROM orders WHERE customer_id = customer_id; OPEN order_row; FETCH order_row INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE(isnull(@order_row_order_id, '') + ' ' + isnull(@order_row_order_date, '')); FETCH order_row INTO; END; CLOSE order_row; DEALLOCATE order_row; END; DECLARE @total_orders FLOAT; BEGIN SELECT @total_orders = COUNT(*) FROM orders WHERE customer_id = 100; DBMS_OUTPUT.PUT_LINE('Total orders for customer 100: ' + isnull(@total_orders, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No orders found for customer 100'); END END; BEGIN INSERT INTO customers (customer_id, customer_name) VALUES (1234, 'New Customer'); COMMIT; DBMS_OUTPUT.PUT_LINE('Inserted new customer with ID 1234'); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Customer with ID 1234 already exists'); END; DECLARE @error_code FLOAT; error_message dbo.VARCHAR2(2000); BEGIN BEGIN *** se an error EXCEPTION WHEN OTHERS THEN SET @error_code = @@ERROR; SET @error_message = SQLERRM; DBMS_OUTPUT.PUT_LINE('Error occurred: ' + isnull(@error_code, '') + ' ' + isnull(error_message, '')); END; END;
CREATE PROCEDURE GetCustomerOrders (IN customer_id NUMBER) AS BEGIN FOR order_row IN (SELECT * FROM orders WHERE customer_id = customer_id) LOOP DBMS_OUTPUT.PUT_LINE(order_row.order_id || ' ' || order_row.order_date); END LOOP; END; DECLARE total_orders NUMBER; BEGIN SELECT COUNT(*) INTO total_orders FROM orders WHERE customer_id = 100; DBMS_OUTPUT.PUT_LINE('Total orders for customer 100: ' || total_orders); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No orders found for customer 100'); END; BEGIN INSERT INTO customers (customer_id, customer_name) VALUES (1234, 'New Customer'); COMMIT; DBMS_OUTPUT.PUT_LINE('Inserted new customer with ID 1234'); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Customer with ID 1234 already exists'); END; DECLARE error_code NUMBER; error_message VARCHAR2(2000); BEGIN BEGIN -- Code that may raise an error EXCEPTION WHEN OTHERS THEN error_code := SQLCODE; error_message := SQLERRM; DBMS_OUTPUT.PUT_LINE('Error occurred: ' || error_code || ' ' || error_message); END; END;
DECLARE @v_emp_no FLOAT := 201; BEGIN SELECT first_name FROM employees WHERE employee_id = @v_emp_no; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
DECLARE v_emp_no NUMBER := 201; BEGIN SELECT first_name FROM employees WHERE employee_id = v_emp_no; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
CREATE PROCEDURE my_sp ( @p_in IN AS NUMBER, p_out OUT NUMBER ) IS BEGIN SELECT @p_out = count(*) FROM my_table WHERE my_column = p_in; IF @@FETCH_STATUS <> 0 BEGIN set @p_out = 0; END END; DECLARE @v_count FLOAT; BEGIN dbo.my_sp(10, @v_count); DBMS_OUTPUT.PUT_LINE(@v_count); END;
CREATE PROCEDURE my_sp ( p_in IN NUMBER, p_out OUT NUMBER ) IS BEGIN SELECT count(*) INTO p_out FROM my_table WHERE my_column = p_in; EXCEPTION WHEN NO_DATA_FOUND THEN p_out := 0; END; DECLARE v_count NUMBER; BEGIN my_sp(10, v_count); DBMS_OUTPUT.PUT_LINE(v_count); END;
sql IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@p_in IN AS VARCHAR2) AS BEGIN *** iables v_out dbo.VARCHAR2(200); SELECT @v_out = UPPER(p_in); *** the input is empty IF v_out = '' BEGIN RAISERROR('-20001 Input cannot be empty', 16, 1); END -- Re... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE(v_out); END;
sql CREATE OR REPLACE PROCEDURE my_proc(p_in IN VARCHAR2) AS BEGIN -- Declare local variables v_out VARCHAR2(200); SELECT UPPER(p_in) INTO v_out FROM dual; -- Raise an error if the input is empty IF v_out = '' THEN RAISE_APPLICATION_ERROR(-20001, 'Input cannot be empty'); END IF; -- Return the output DBMS_OUTPUT.PUT_LINE(v_out); END;
IF OBJECT_ID('get_customer_count', 'FN') IS NOT NULL DROP FUNCTION get_customer_count; GO CREATE FUNCTION get_customer_count(@dept_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN DECLARE @customer_count FLOAT; BEGIN SELECT @customer_count = COUNT(*) FROM customers WHERE department_id = dept_id; RETURN @customer_count; IF @@FETCH_STATUS <> 0 BEGIN RETURN 0; END END; END / BEGIN DECLARE @dept_id FLOAT := 10; DECLARE @customer_count FLOAT; BEGIN SET @customer_count = dbo.get_customer_count(@dept_id); IF @customer_count > 0 BEGIN DBMS_OUTPUT.PUT_LINE('The number of customers in department ' + isnull(@dept_id, '') + ' is ' + isnull(@customer_count, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No customers found in department ' + isnull(@dept_id, '')); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving customer count.'); END; END /
CREATE OR REPLACE FUNCTION get_customer_count(dept_id IN NUMBER) RETURN NUMBER AS BEGIN DECLARE customer_count NUMBER; BEGIN SELECT COUNT(*) INTO customer_count FROM customers WHERE department_id = dept_id; RETURN customer_count; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 0; END; END / BEGIN DECLARE dept_id NUMBER := 10; DECLARE customer_count NUMBER; BEGIN customer_count := get_customer_count(dept_id); IF customer_count > 0 THEN DBMS_OUTPUT.PUT_LINE('The number of customers in department ' || dept_id || ' is ' || customer_count); ELSE DBMS_OUTPUT.PUT_LINE('No customers found in department ' || dept_id); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving customer count.'); END; END /