sqlserver
stringlengths
59
30k
plsql
stringlengths
54
4.67k
IF OBJECT_ID('show_employees', 'P') IS NOT NULL DROP PROCEDURE show_employees; GO CREATE PROCEDURE show_employees(@deptno IN AS NUMBER) AS BEGIN DECLARE @emp_cursor SYS_REFCURSOR; BEGIN SELECT ename, job, sal FROM emp WHERE deptno = deptno; WHILE 1=1 BEGIN FETCH emp_cursor INTO @ename, @job, @sal; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(isnull(ename, '') + ' ' + isnull(job, '') + ' ' + isnull(sal, '')); END; CLOSE emp_cursor; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found in department ' + isnull(deptno, '')); END END; END; / DECLARE @deptno FLOAT := 20; BEGIN dbo.show_employees(@deptno); END; /
CREATE OR REPLACE PROCEDURE show_employees(deptno IN NUMBER) AS BEGIN DECLARE emp_cursor SYS_REFCURSOR; BEGIN OPEN emp_cursor FOR SELECT ename, job, sal FROM emp WHERE deptno = deptno; LOOP FETCH emp_cursor INTO ename, job, sal; EXIT WHEN emp_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(ename || ' ' || job || ' ' || sal); END LOOP; CLOSE emp_cursor; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found in department ' || deptno); END; END; / DECLARE deptno NUMBER := 20; BEGIN show_employees(deptno); END; /
CREATE PROCEDURE proc_insert_emp( @p_empno IN AS NUMBER, p_ename IN VARCHAR2, p_job IN VARCHAR2, p_mgr IN NUMBER, p_sal IN NUMBER, p_comm IN NUMBER, p_deptno IN NUMBER) IS BEGIN INSERT INTO EMP(empno, ename, job, mgr, sal, comm, deptno) VALUES (p_empno, p_ename, p_job, p_mgr, p_sal, p_comm, p_deptno); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK; RAISERROR('-20001 Duplicate value on primary key.', 16, 1); END; CREATE FUNCTION func_get_emp_name(@p_empno IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS emp_name dbo.VARCHAR2(100); BEGIN SELECT @emp_name = ename FROM EMP WHERE empno = p_empno; RETURN emp_name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @v_deptno FLOAT; v_empno NUMBER; BEGIN SELECT @v_deptno = deptno FROM EMP WHERE ename = 'SCOTT'; SELECT @v_empno = empno FROM EMP WHERE mgr = @v_deptno; UPDATE EMP SET sal = sal * 1.10 WHERE empno = v_empno; COMMIT; END;
CREATE PROCEDURE proc_insert_emp( p_empno IN NUMBER, p_ename IN VARCHAR2, p_job IN VARCHAR2, p_mgr IN NUMBER, p_sal IN NUMBER, p_comm IN NUMBER, p_deptno IN NUMBER) IS BEGIN INSERT INTO EMP(empno, ename, job, mgr, sal, comm, deptno) VALUES (p_empno, p_ename, p_job, p_mgr, p_sal, p_comm, p_deptno); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Duplicate value on primary key.'); END; CREATE FUNCTION func_get_emp_name(p_empno IN NUMBER) RETURN VARCHAR2 IS emp_name VARCHAR2(100); BEGIN SELECT ename INTO emp_name FROM EMP WHERE empno = p_empno; RETURN emp_name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE v_deptno NUMBER; v_empno NUMBER; BEGIN SELECT deptno INTO v_deptno FROM EMP WHERE ename = 'SCOTT'; SELECT empno INTO v_empno FROM EMP WHERE mgr = v_deptno; UPDATE EMP SET sal = sal * 1.10 WHERE empno = v_empno; COMMIT; END;
DECLARE @x FLOAT; BEGIN SET @x = 10; IF @x > 5 BEGIN RAISERROR('-20001 x is greater than 5', 16, 1); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE x NUMBER; BEGIN x := 10; IF x > 5 THEN RAISE_APPLICATION_ERROR(-20001, 'x is greater than 5'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @l_emp_no FLOAT; l_salary NUMBER; BEGIN SELECT @l_emp_no = emp_no, @l_salary = salary FROM employees WHERE last_name = 'King'; IF l_salary > 10000 BEGIN RAISERROR('-20000 Salary too high!', 16, 1); END UPDATE employees SET salary = l_salary * 1.1 WHERE emp_no = @l_emp_no; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE l_emp_no NUMBER; l_salary NUMBER; BEGIN SELECT emp_no, salary INTO l_emp_no, l_salary FROM employees WHERE last_name = 'King'; IF l_salary > 10000 THEN RAISE_APPLICATION_ERROR(-20000, 'Salary too high!'); END IF; UPDATE employees SET salary = l_salary * 1.1 WHERE emp_no = l_emp_no; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
IF cast(SYS_CONTEXT('USERENV','CURRENT_SCHEMA') as float) <> 999 BEGIN RAISERROR('-20001 Schema error. Expected schema 999 but found ' + ISNULL(SYS_CONTEXT('USERENV','CURRENT_SCHEMA'), ''), 16, 1); END;
IF to_number(SYS_CONTEXT('USERENV','CURRENT_SCHEMA')) <> 999 THEN BEGIN RAISE_APPLICATION_ERROR(-20001,'Schema error. Expected schema 999 but found ' || SYS_CONTEXT('USERENV','CURRENT_SCHEMA')); END; END IF;
IF OBJECT_ID('update_employee', 'P') IS NOT NULL DROP PROCEDURE update_employee; GO CREATE PROCEDURE update_employee( @emp_id IN AS NUMBER, salary IN NUMBER, email IN VARCHAR2, OUT result_msg OUT VARCHAR2 ) AS BEGIN UPDATE employees SET salary = salary, email = email WHERE emp_id = emp_id; set @result_msg = 'Row Updated Successfully'; EXCEPTION WHEN OTHERS THEN set @result_msg = 'Error: ' + ISNULL(SUBSTRING(SQLERRM, 1, 100), ''); END; BEGIN dbo.update_employee(1, 50000, 'john.doe@xyzcorp.com', @result_msg); DBMS_OUTPUT.PUT_LINE(result_msg); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SUBSTRING(SQLERRM, 1, 100), '')); END;
CREATE OR REPLACE PROCEDURE update_employee( emp_id IN NUMBER, salary IN NUMBER, email IN VARCHAR2, OUT result_msg OUT VARCHAR2 ) AS BEGIN UPDATE employees SET salary = salary, email = email WHERE emp_id = emp_id; result_msg := 'Row Updated Successfully'; EXCEPTION WHEN OTHERS THEN result_msg := 'Error: ' || SUBSTR(SQLERRM, 1, 100); END; BEGIN update_employee(1, 50000, 'john.doe@xyzcorp.com', @result_msg); DBMS_OUTPUT.PUT_LINE(result_msg); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SUBSTR(SQLERRM, 1, 100)); END;
DECLARE @x FLOAT := 10; BEGIN IF @x > 5 BEGIN DBMS_OUTPUT.PUT_LINE('x is greater than 5'); END ELSE IF @x = 5 BEGIN DBMS_OUTPUT.PUT_LINE('x is equal to 5'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('x is less than 5'); END END; /
DECLARE x NUMBER := 10; BEGIN IF x > 5 THEN DBMS_OUTPUT.PUT_LINE('x is greater than 5'); ELSIF x = 5 THEN DBMS_OUTPUT.PUT_LINE('x is equal to 5'); ELSE DBMS_OUTPUT.PUT_LINE('x is less than 5'); END IF; END; /
CREATE TABLE employees ( id BIGINT PRIMARY KEY, name VARCHAR(50) NOT NULL, salary DECIMAL(10,2) NOT NULL, department VARCHAR(50) NOT NULL ); CREATE PROCEDURE hire_employee ( @p_name IN AS VARCHAR2, p_salary IN NUMBER, p_department IN VARCHAR2 ) AS BEGIN INSERT INTO employees (name, salary, department) VALUES (p_name, p_salary, p_department); END; CREATE FUNCTION get_employee_salary ( @p_id IN NUMBER DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN SELECT @p_salary = salary FROM employees WHERE id = p_id; RETURN p_salary; END; BEGIN *** oyee dbo.hire_employee('John Doe', 100000, 'Sales'); *** the new employee DECLARE @salary FLOAT; BEGIN SET @salary = dbo.get_employee_salary(1); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END; END;
CREATE TABLE employees ( id NUMBER(10) PRIMARY KEY, name VARCHAR2(50) NOT NULL, salary NUMBER(10,2) NOT NULL, department VARCHAR2(50) NOT NULL ); CREATE PROCEDURE hire_employee ( p_name IN VARCHAR2, p_salary IN NUMBER, p_department IN VARCHAR2 ) AS BEGIN INSERT INTO employees (name, salary, department) VALUES (p_name, p_salary, p_department); END; CREATE FUNCTION get_employee_salary ( p_id IN NUMBER ) RETURN NUMBER AS BEGIN SELECT salary INTO p_salary FROM employees WHERE id = p_id; RETURN p_salary; END; BEGIN -- Insert a new employee hire_employee('John Doe', 100000, 'Sales'); -- Get the salary of the new employee DECLARE salary NUMBER; BEGIN salary := get_employee_salary(1); DBMS_OUTPUT.PUT_LINE('Salary: ' || salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END; END;
CREATE PROCEDURE calculate_balances ( @employee_id IN AS NUMBER ) IS -- De... SQLINES DEMO *** salary NUMBER; *** salary. SELECT @salary = salary FROM employees WHERE employee_id = employee_id; *** loyee's gross balance. set @gross_balance = salary * 12; *** loyee's net balance. set @net_balance = gross_balance - (gross_balance * 0.20); -- Pr... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(employee_id, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, '')); DBMS_OUTPUT.PUT_LINE('Gross Balance: ' + isnull(gross_balance, '')); DBMS_OUTPUT.PUT_LINE('Net Balance: ' + isnull(net_balance, '')); EXCEPTION *** that occur. WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END;
CREATE PROCEDURE calculate_balances ( employee_id IN NUMBER ) IS -- Declare variables. salary NUMBER; -- Retrieve employee salary. SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; -- Calculate the employee's gross balance. gross_balance := salary * 12; -- Calculate the employee's net balance. net_balance := gross_balance - (gross_balance * 0.20); -- Print the results. DBMS_OUTPUT.PUT_LINE('Employee ID: ' || employee_id); DBMS_OUTPUT.PUT_LINE('Salary: ' || salary); DBMS_OUTPUT.PUT_LINE('Gross Balance: ' || gross_balance); DBMS_OUTPUT.PUT_LINE('Net Balance: ' || net_balance); EXCEPTION -- Handle any errors that occur. WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END;
IF OBJECT_ID('Get_Total_Sales', 'FN') IS NOT NULL DROP FUNCTION Get_Total_Sales; GO CREATE FUNCTION Get_Total_Sales(@customer_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS total_sales NUMBER; BEGIN SELECT @total_sales = SUM(sales) FROM sales WHERE customer_id = customer_id; RETURN total_sales; IF @@FETCH_STATUS <> 0 BEGIN RETURN 0; END END; /
CREATE OR REPLACE FUNCTION Get_Total_Sales(customer_id IN NUMBER) RETURN NUMBER IS total_sales NUMBER; BEGIN SELECT SUM(sales) INTO total_sales FROM sales WHERE customer_id = customer_id; RETURN total_sales; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 0; END; /
sql /* SQLINES DEMO *** to insert data into employees table */ IF OBJECT_ID('INSERT_EMPLOYEE', 'P') IS NOT NULL DROP PROCEDURE INSERT_EMPLOYEE; GO CREATE PROCEDURE INSERT_EMPLOYEE ( @IN EMP_ID AS NUMBER, IN EMP_NAME VARCHAR2, IN EMAIL VARCHAR2, IN SALARY NUMBER, IN DEPT_ID NUMBER ) AS BEGIN INSERT INTO employees (employee_id, employee_name, email, salary, department_id) VALUES (EMP_ID, EMP_NAME, EMAIL, SALARY, DEPT_ID); END; /* SQLINES DEMO *** late bonus for employees */ IF OBJECT_ID('GET_BONUS', 'FN') IS NOT NULL DROP FUNCTION GET_BONUS; GO CREATE FUNCTION GET_BONUS ( @IN SALARY NUMBER DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN RETURN SALARY * 0.1; END; /* SQLINES DEMO *** retrieve employee data */ SELECT * FROM employees WHERE salary > 10000; /* SQLINES DEMO *** truct using CASE expression */ SELECT CASE WHEN SALARY > 10000 THEN 'High Salary' WHEN SALARY > 5000 THEN 'Medium Salary' ELSE 'Low Salary' END AS SALARY_GRADE FROM employees; /* SQLINES DEMO *** enario using EXCEPTION block */ BEGIN /* SQLINES DEMO *** y raise an exception */ EXCEPTION /* SQLINES DEMO *** ion */ WHEN OTHERS THEN ROLLBACK; RAISERROR('-20000 An error occurred', 16, 1); END;
sql /* stored procedure to insert data into employees table */ CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE ( IN EMP_ID NUMBER, IN EMP_NAME VARCHAR2, IN EMAIL VARCHAR2, IN SALARY NUMBER, IN DEPT_ID NUMBER ) AS BEGIN INSERT INTO employees (employee_id, employee_name, email, salary, department_id) VALUES (EMP_ID, EMP_NAME, EMAIL, SALARY, DEPT_ID); END; /* function to calculate bonus for employees */ CREATE OR REPLACE FUNCTION GET_BONUS ( IN SALARY NUMBER ) RETURN NUMBER AS BEGIN RETURN SALARY * 0.1; END; /* SQL statement to retrieve employee data */ SELECT * FROM employees WHERE salary > 10000; /* control flow construct using CASE expression */ SELECT CASE WHEN SALARY > 10000 THEN 'High Salary' WHEN SALARY > 5000 THEN 'Medium Salary' ELSE 'Low Salary' END AS SALARY_GRADE FROM employees; /* error handling scenario using EXCEPTION block */ BEGIN /* some code that may raise an exception */ EXCEPTION /* handle the exception */ WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20000, 'An error occurred'); END;
CREATE PROCEDURE get_employee_info( @emp_id IN AS NUMBER, OUT fname OUT VARCHAR2, OUT lname OUT VARCHAR2, OUT salary OUT NUMBER ) AS BEGIN SELECT @fname = first_name, @lname = last_name, @salary = salary FROM employees WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
CREATE PROCEDURE get_employee_info( emp_id IN NUMBER, OUT fname OUT VARCHAR2, OUT lname OUT VARCHAR2, OUT salary OUT NUMBER ) AS BEGIN SELECT first_name, last_name, salary INTO fname, lname, salary FROM employees WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
IF OBJECT_ID('Employee_Details', 'P') IS NOT NULL DROP PROCEDURE Employee_Details; GO CREATE PROCEDURE Employee_Details ( @p_employee_id IN AS NUMBER, p_employee_name OUT VARCHAR2, p_employee_salary OUT NUMBER ) AS BEGIN SELECT @p_employee_name = employee_name, @p_employee_salary = salary FROM employees WHERE employee_id = p_employee_id; EXCEPTION WHEN OTHERS THEN -- ER... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE(SUBSTRING(SQLERRM, 1, 200)); ROLLBACK; RAISE; END; DECLARE @employee_name VARCHAR(50); employee_salary NUMBER; BEGIN dbo.Employee_Details(10, @employee_name, employee_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@employee_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(employee_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SUBSTRING(SQLERRM, 1, 200)); END;
CREATE OR REPLACE PROCEDURE Employee_Details ( p_employee_id IN NUMBER, p_employee_name OUT VARCHAR2, p_employee_salary OUT NUMBER ) AS BEGIN SELECT employee_name, salary INTO p_employee_name, p_employee_salary FROM employees WHERE employee_id = p_employee_id; EXCEPTION WHEN OTHERS THEN -- ERROR HANDLING DBMS_OUTPUT.PUT_LINE(SUBSTR(SQLERRM, 1, 200)); ROLLBACK; RAISE; END; DECLARE employee_name VARCHAR2(50); employee_salary NUMBER; BEGIN Employee_Details(10, employee_name, employee_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || employee_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SUBSTR(SQLERRM, 1, 200)); END;
IF OBJECT_ID('process_orders', 'P') IS NOT NULL DROP PROCEDURE process_orders; GO CREATE PROCEDURE process_orders ( @input_orders IN AS OUT SYS_REFCURSOR, output_errors OUT OUT SYS_REFCURSOR ) AS BEGIN *** der from the input cursor FETCH input_orders INTO @order_id, @customer_id, @product_id, @quantity; *** r is valid IF order_id IS NULL BEGIN EXIT; END -- Pr... SQLINES DEMO *** BEGIN *** into the orders table INSERT INTO orders (order_id, customer_id, product_id, quantity) VALUES (order_id, customer_id, product_id, quantity); *** t inventory UPDATE products SET quantity = quantity - quantity WHERE product_id = product_id; EXCEPTION *** insert them into the output cursor INSERT INTO output_errors (error_code, error_message) VALUES (@@ERROR, SQLERRM); END; END;
CREATE OR REPLACE PROCEDURE process_orders ( input_orders IN OUT SYS_REFCURSOR, output_errors OUT OUT SYS_REFCURSOR ) AS BEGIN -- Fetch the next order from the input cursor FETCH input_orders INTO order_id, customer_id, product_id, quantity; -- Check if the order is valid IF order_id IS NULL THEN EXIT; END IF; -- Process the order BEGIN -- Insert the order into the orders table INSERT INTO orders (order_id, customer_id, product_id, quantity) VALUES (order_id, customer_id, product_id, quantity); -- Update the product inventory UPDATE products SET quantity = quantity - quantity WHERE product_id = product_id; EXCEPTION -- Handle errors and insert them into the output cursor INSERT INTO output_errors (error_code, error_message) VALUES (SQLCODE, SQLERRM); END; END;
CREATE TABLE employees ( empno SMALLINT NOT NULL, ename VARCHAR(20) NOT NULL, job VARCHAR(20), mgr SMALLINT, hiredate DATE, sal DECIMAL(7,2), comm DECIMAL(7,2), deptno SMALLINT ); INSERT INTO employees VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20); INSERT INTO employees VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30); INSERT INTO employees VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-06-22', 1250, 500, 30); INSERT INTO employees VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-10-01', 2975, NULL, 20); INSERT INTO employees VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 400, 30); INSERT INTO employees VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30); INSERT INTO employees VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10); INSERT INTO employees VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, NULL, 20); INSERT INTO employees VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10); INSERT INTO employees VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30); INSERT INTO employees VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, NULL, 20); INSERT INTO employees VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30); INSERT INTO employees VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20); INSERT INTO employees VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10); CREATE TABLE departments ( deptno SMALLINT NOT NULL, dname VARCHAR(20) NOT NULL, loc VARCHAR(13) ); INSERT INTO departments VALUES (10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO departments VALUES (20, 'RESEARCH', 'DALLAS'); INSERT INTO departments VALUES (30, 'SALES', 'CHICAGO'); INSERT INTO departments VALUES (40, 'OPERATIONS', 'BOSTON'); IF OBJECT_ID('get_employee_by_id', 'P') IS NOT NULL DROP PROCEDURE get_employee_by_id; GO CREATE PROCEDURE get_employee_by_id ( @p_empno IN AS NUMBER, OUT p_ename VARCHAR2, OUT p_job VARCHAR2, OUT p_sal NUMBER ) AS BEGIN SELECT @p_ename = ename, @p_job = job, @p_sal = sal FROM employees WHERE empno = p_empno; END; IF OBJECT_ID('get_max_salary', 'FN') IS NOT NULL DROP FUNCTION get_max_salary; GO CREATE FUNCTION get_max_salary ( @p_deptno IN NUMBER DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN RETURN ( SELECT MAX(sal) FROM employees WHERE deptno = p_deptno ); END; BEGIN DECLARE @v_empno FLOAT := 7369; v_ename dbo.VARCHAR2(20); v_job dbo.VARCHAR2(20); v_sal NUMBER; BEGIN dbo.get_employee_by_id(@v_empno, v_ename, v_job, v_sal); DBMS_OUTPUT.PUT_LINE(isnull(@v_empno, '') + ' ' + isnull(v_ename, '') + ' ' + isnull(v_job, '') + ' ' + isnull(v_sal, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with employee number ' + isnull(@v_empno, '')); END END; DECLARE @v_deptno FLOAT := 20; v_max_salary NUMBER; BEGIN SET @v_max_salary = dbo.get_max_salary(@v_deptno); DBMS_OUTPUT.PUT_LINE('The maximum salary in department ' + isnull(@v_deptno, '') + ' is ' + isnull(v_max_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found in department ' + isnull(@v_deptno, '')); END END; END;
CREATE TABLE employees ( empno NUMBER(4) NOT NULL, ename VARCHAR2(20) NOT NULL, job VARCHAR2(20), mgr NUMBER(4), hiredate DATE, sal NUMBER(7,2), comm NUMBER(7,2), deptno NUMBER(2) ); INSERT INTO employees VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20); INSERT INTO employees VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30); INSERT INTO employees VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-06-22', 1250, 500, 30); INSERT INTO employees VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-10-01', 2975, NULL, 20); INSERT INTO employees VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 400, 30); INSERT INTO employees VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30); INSERT INTO employees VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10); INSERT INTO employees VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, NULL, 20); INSERT INTO employees VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10); INSERT INTO employees VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30); INSERT INTO employees VALUES (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, NULL, 20); INSERT INTO employees VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, NULL, 30); INSERT INTO employees VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20); INSERT INTO employees VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10); CREATE TABLE departments ( deptno NUMBER(2) NOT NULL, dname VARCHAR2(20) NOT NULL, loc VARCHAR2(13) ); INSERT INTO departments VALUES (10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO departments VALUES (20, 'RESEARCH', 'DALLAS'); INSERT INTO departments VALUES (30, 'SALES', 'CHICAGO'); INSERT INTO departments VALUES (40, 'OPERATIONS', 'BOSTON'); CREATE OR REPLACE PROCEDURE get_employee_by_id ( p_empno IN NUMBER, OUT p_ename VARCHAR2, OUT p_job VARCHAR2, OUT p_sal NUMBER ) AS BEGIN SELECT ename, job, sal INTO p_ename, p_job, p_sal FROM employees WHERE empno = p_empno; END; CREATE OR REPLACE FUNCTION get_max_salary ( p_deptno IN NUMBER ) RETURN NUMBER AS BEGIN RETURN ( SELECT MAX(sal) FROM employees WHERE deptno = p_deptno ); END; BEGIN DECLARE v_empno NUMBER := 7369; v_ename VARCHAR2(20); v_job VARCHAR2(20); v_sal NUMBER; BEGIN get_employee_by_id(v_empno, v_ename, v_job, v_sal); DBMS_OUTPUT.PUT_LINE(v_empno || ' ' || v_ename || ' ' || v_job || ' ' || v_sal); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with employee number ' || v_empno); END; DECLARE v_deptno NUMBER := 20; v_max_salary NUMBER; BEGIN v_max_salary := get_max_salary(v_deptno); DBMS_OUTPUT.PUT_LINE('The maximum salary in department ' || v_deptno || ' is ' || v_max_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found in department ' || v_deptno); END; END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@employee_id FLOAT, @new_salary FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = @new_salary WHERE @employee_id = @employee_id; COMMIT; -- Lo... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' + isnull(@employee_id, '') + ' to ' + isnull(@new_salary, '')); EXCEPTION WHEN OTHERS THEN *** and log it DBMS_OUTPUT.PUT_LINE('Error updating salary for employee ' + isnull(@employee_id, '') + ': ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id NUMBER, new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; COMMIT; -- Log the change DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' || employee_id || ' to ' || new_salary); EXCEPTION WHEN OTHERS THEN -- Handle the error and log it DBMS_OUTPUT.PUT_LINE('Error updating salary for employee ' || employee_id || ': ' || SQLERRM); END;
IF OBJECT_ID('prc_example', 'P') IS NOT NULL DROP PROCEDURE prc_example; GO CREATE PROCEDURE prc_example( @p_id IN AS NUMBER, p_name IN VARCHAR2, OUT p_count OUT NUMBER ) IS BEGIN SELECT @p_count = COUNT(*) FROM table_name WHERE id = p_id AND name = p_name; IF @@FETCH_STATUS <> 0 BEGIN set @p_count = 0; END END; /
CREATE OR REPLACE PROCEDURE prc_example( p_id IN NUMBER, p_name IN VARCHAR2, OUT p_count OUT NUMBER ) IS BEGIN SELECT COUNT(*) INTO p_count FROM table_name WHERE id = p_id AND name = p_name; EXCEPTION WHEN NO_DATA_FOUND THEN p_count := 0; END; /
DECLARE @v_deptno FLOAT; BEGIN SELECT @v_deptno = deptno FROM emp WHERE empno = 7934; UPDATE emp SET sal = sal * 1.10 WHERE deptno = @v_deptno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salaries for department ' + isnull(@v_deptno, '') + ' have been increased by 10%.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_deptno NUMBER; BEGIN SELECT deptno INTO v_deptno FROM emp WHERE empno = 7934; UPDATE emp SET sal = sal * 1.10 WHERE deptno = v_deptno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salaries for department ' || v_deptno || ' have been increased by 10%.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END;
BEGIN -- De... SQLINES DEMO *** v_deptno NUMBER; v_dname dbo.VARCHAR2(14); *** mber from user DBMS_OUTPUT.PUT_LINE('Enter department number: '); DBMS_INPUT.GET_LINE(v_deptno); *** e for department name SELECT @v_dname = dname FROM dept WHERE deptno = v_deptno; *** nt exists IF v_dname IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('Department not found.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Department name: ' + isnull(v_dname, '')); END END;
BEGIN -- Declare variables v_deptno NUMBER; v_dname VARCHAR2(14); -- Get department number from user DBMS_OUTPUT.PUT_LINE('Enter department number: '); DBMS_INPUT.GET_LINE(v_deptno); -- Query the database for department name SELECT dname INTO v_dname FROM dept WHERE deptno = v_deptno; -- Check if department exists IF v_dname IS NULL THEN DBMS_OUTPUT.PUT_LINE('Department not found.'); ELSE DBMS_OUTPUT.PUT_LINE('Department name: ' || v_dname); END IF; 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; BEGIN DECLARE @bonus FLOAT; BEGIN SET @bonus = dbo.get_employee_salary(100); IF @bonus IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('No bonus for employee 100'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Bonus for employee 100: ' + isnull(@bonus, '')); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END; DECLARE @cursor c1 IS SELECT * FROM employees; BEGIN DECLARE rec CURSOR FOR c1 LOOP DBMS_OUTPUT.PUT_LINE(isnull(@rec_employee_id, '') + ' - ' + isnull(@rec_name, '')); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; 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; BEGIN DECLARE bonus NUMBER; BEGIN bonus := get_employee_salary(100); IF bonus IS NULL THEN DBMS_OUTPUT.PUT_LINE('No bonus for employee 100'); ELSE DBMS_OUTPUT.PUT_LINE('Bonus for employee 100: ' || bonus); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; DECLARE cursor c1 IS SELECT * FROM employees; BEGIN FOR rec IN c1 LOOP DBMS_OUTPUT.PUT_LINE(rec.employee_id || ' - ' || rec.name); END LOOP; END; END;
IF OBJECT_ID('update_employee', 'P') IS NOT NULL DROP PROCEDURE update_employee; GO CREATE PROCEDURE update_employee( @emp_id IN AS NUMBER, salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = salary WHERE employee_id = emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee salary updated.'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END END; DECLARE @emp_id FLOAT := 100; salary SET @NUMBER = 10000; BEGIN dbo.update_employee(@emp_id, salary); END;
CREATE OR REPLACE PROCEDURE update_employee( emp_id IN NUMBER, salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = salary WHERE employee_id = emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee salary updated.'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END; DECLARE emp_id NUMBER := 100; salary NUMBER := 10000; BEGIN update_employee(emp_id, salary); END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc ( @p1 IN AS VARCHAR2, p2 IN OUT NUMBER ) IS BEGIN IF p1 IS NULL BEGIN RAISERROR('-20001 Parameter p1 cannot be null', 16, 1); END SELECT @p2 = COUNT(*) FROM my_table WHERE col1 = p1; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; / IF OBJECT_ID('my_func', 'FN') IS NOT NULL DROP FUNCTION my_func; GO CREATE FUNCTION my_func ( @p1 IN VARCHAR2 DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS BEGIN RETURN (SELECT COUNT(*) FROM my_table WHERE col1 = p1); END; / DECLARE @v1 FLOAT; BEGIN SELECT @v1 = dbo.my_func('value1'); INSERT INTO my_table (col1, col2) VALUES ('value2', @v1); COMMIT; END;
CREATE OR REPLACE PROCEDURE my_proc ( p1 IN VARCHAR2, p2 IN OUT NUMBER ) IS BEGIN IF p1 IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Parameter p1 cannot be null'); END IF; SELECT COUNT(*) INTO p2 FROM my_table WHERE col1 = p1; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; / CREATE OR REPLACE FUNCTION my_func ( p1 IN VARCHAR2 ) RETURN NUMBER IS BEGIN RETURN (SELECT COUNT(*) FROM my_table WHERE col1 = p1); END; / DECLARE v1 NUMBER; BEGIN SELECT my_func('value1') INTO v1 FROM dual; INSERT INTO my_table (col1, col2) VALUES ('value2', v1); COMMIT; END;
declare @l_var float; begin set @l_var = 10; while 1=1 begin if @l_var > 0 begin set @l_var = @l_var - 1; dbms_output.put_line(@l_var); end exit when l_var <= 0; end; end;
declare l_var number; begin l_var := 10; loop if l_var > 0 then l_var := l_var - 1; dbms_output.put_line(l_var); end if; exit when l_var <= 0; end loop; end;
IF OBJECT_ID('get_avg_salary', 'FN') IS NOT NULL DROP FUNCTION get_avg_salary; GO CREATE FUNCTION get_avg_salary(@dept_name VARCHAR(4000)) RETURNS FLOAT AS BEGIN DECLARE @avg_sal FLOAT; SELECT @avg_sal = AVG(salary) FROM employees WHERE department_name = @dept_name; RETURN @avg_sal; EXCEPTION WHEN OTHERS THEN RETURN -1; END; DECLARE @dept_name VARCHAR(20) := 'Sales'; avg_salary NUMBER; BEGIN SET @avg_salary = dbo.get_avg_salary(@dept_name); IF avg_salary < 0 BEGIN RAISERROR('-20001 Invalid department name.', 16, 1); END DBMS_OUTPUT.PUT_LINE('Average salary for ' + isnull(@dept_name, '') + ' is ' + isnull(avg_salary, '')); END;
CREATE OR REPLACE FUNCTION get_avg_salary(dept_name VARCHAR2) RETURN NUMBER IS avg_sal NUMBER; BEGIN SELECT AVG(salary) INTO avg_sal FROM employees WHERE department_name = dept_name; RETURN avg_sal; EXCEPTION WHEN OTHERS THEN RETURN -1; END; DECLARE dept_name VARCHAR2(20) := 'Sales'; avg_salary NUMBER; BEGIN avg_salary := get_avg_salary(dept_name); IF avg_salary < 0 THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid department name.'); END IF; DBMS_OUTPUT.PUT_LINE('Average salary for ' || dept_name || ' is ' || avg_salary); END;
DECLARE @v_empno SMALLINT; v_salary ROW_NUMBER(6,2); v_comm ROW_NUMBER(6,2); v_found SET @BOOLEAN = FALSE; BEGIN SELECT @v_ename = ename, @v_salary = sal, @v_comm = comm FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(v_ename, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); DBMS_OUTPUT.PUT_LINE('Commission: ' + isnull(v_comm, '')); SET @v_found = TRUE; IF v_found BEGIN DBMS_OUTPUT.PUT_LINE('Employee found'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END; /
DECLARE v_empno NUMBER(4); v_salary NUMBER(6,2); v_comm NUMBER(6,2); v_found BOOLEAN := FALSE; BEGIN SELECT ename, sal, comm INTO v_ename, v_salary, v_comm FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee: ' || v_ename); DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); DBMS_OUTPUT.PUT_LINE('Commission: ' || v_comm); v_found := TRUE; IF v_found THEN DBMS_OUTPUT.PUT_LINE('Employee found'); ELSE DBMS_OUTPUT.PUT_LINE('Employee not found'); END IF; END; /
CREATE PROCEDURE sp_update_employee_salary ( @employeeId FLOAT, @salary FLOAT ) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = @salary WHERE employee_id = @employeeId; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END; DECLARE @emp_id FLOAT := 100; emp_salary NUMBER; BEGIN SELECT @emp_salary = salary FROM employees WHERE employee_id = @emp_id; dbo.sp_update_employee_salary(@emp_id, emp_salary * 1.10); END;
CREATE PROCEDURE sp_update_employee_salary ( employeeId NUMBER, salary NUMBER ) AS BEGIN UPDATE employees SET salary = salary WHERE employee_id = employeeId; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END sp_update_employee_salary; DECLARE emp_id NUMBER := 100; emp_salary NUMBER; BEGIN SELECT salary INTO emp_salary FROM employees WHERE employee_id = emp_id; sp_update_employee_salary(emp_id, emp_salary * 1.10); END;
CREATE PROCEDURE get_customer_orders ( @customer_id IN AS NUMBER ) AS BEGIN DECLARE ord CURSOR FOR SELECT order_number, order_date, total_amount FROM orders WHERE customer_id = customer_id; OPEN ord; FETCH ord INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE('Order Number: ' + isnull(@ord_order_number, '')); DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@ord_order_date, '')); DBMS_OUTPUT.PUT_LINE('Total Amount:' + isnull(@ord_total_amount, '')); FETCH ord INTO; END; CLOSE ord; DEALLOCATE ord; IF @@FETCH_STATUS = 0 BEGIN DBMS_OUTPUT.PUT_LINE('No orders found for customer.'); END END; /
CREATE PROCEDURE get_customer_orders ( customer_id IN NUMBER ) AS BEGIN FOR ord IN SELECT order_number, order_date, total_amount FROM orders WHERE customer_id = customer_id LOOP DBMS_OUTPUT.PUT_LINE('Order Number: ' || ord.order_number); DBMS_OUTPUT.PUT_LINE('Order Date: ' || ord.order_date); DBMS_OUTPUT.PUT_LINE('Total Amount:' || ord.total_amount); END LOOP; IF ord%NOTFOUND THEN DBMS_OUTPUT.PUT_LINE('No orders found for customer.'); END IF; END; /
CREATE PROCEDURE my_proc (@p_num IN AS VARCHAR2) IS v_num NUMBER; BEGIN set @v_num = CAST(p_num AS FLOAT); *** v_num IF v_num IS NULL BEGIN RAISERROR('-20001 Invalid number: ' + isnull(p_num, ''), 16, 1); END END; /
CREATE PROCEDURE my_proc (p_num IN VARCHAR2) IS v_num NUMBER; BEGIN v_num := TO_NUMBER(p_num); -- do something with v_num IF v_num IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid number: ' || p_num); END IF; END; /
DECLARE @v_id FLOAT; BEGIN BEGIN SELECT @v_id = id FROM table_name WHERE name = 'John Doe'; IF @@FETCH_STATUS <> 0 BEGIN SET @v_id = -1; END END; IF @v_id = -1 BEGIN INSERT INTO table_name (name) VALUES ('John Doe'); SELECT @v_id = id FROM table_name WHERE name = 'John Doe'; END UPDATE table_name SET age = age + 1 WHERE id = @v_id; END; CREATE FUNCTION get_employee_salary (@employee_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employee_table WHERE @employee_id = @employee_id; RETURN @salary; END;
DECLARE v_id NUMBER; BEGIN BEGIN SELECT id INTO v_id FROM table_name WHERE name = 'John Doe'; EXCEPTION WHEN NO_DATA_FOUND THEN v_id := -1; END; IF v_id = -1 THEN INSERT INTO table_name (name) VALUES ('John Doe'); SELECT id INTO v_id FROM table_name WHERE name = 'John Doe'; END IF; UPDATE table_name SET age = age + 1 WHERE id = v_id; END; CREATE FUNCTION get_employee_salary (employee_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employee_table WHERE employee_id = employee_id; RETURN salary; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@p_id IN AS NUMBER) AS v_name dbo.VARCHAR2(20); BEGIN SELECT @v_name = name FROM employees WHERE id = p_id; IF v_name IS NULL BEGIN RAISERROR('-20001 Employee not found: ' + isnull(p_id, ''), 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_name, '')); END;
CREATE OR REPLACE PROCEDURE my_proc(p_id IN NUMBER) AS v_name VARCHAR2(20); BEGIN SELECT name INTO v_name FROM employees WHERE id = p_id; IF v_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found: ' || p_id); END IF; DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_name); END;
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL DROP FUNCTION get_customer_orders; GO CREATE FUNCTION get_customer_orders(@customer_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; IS orders_cursor SYS_REFCURSOR; BEGIN SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END; DECLARE @customer_id FLOAT := 10; orders_cursor SYS_REFCURSOR; BEGIN SET @orders_cursor = dbo.get_customer_orders(@customer_id); WHILE 1=1 BEGIN FETCH orders_cursor INTO @order_id, @order_date, @total_amount; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(isnull(order_id, '') + ' ' + isnull(order_date, '') + ' ' + isnull(total_amount, '')); END; CLOSE orders_cursor; 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 IS orders_cursor SYS_REFCURSOR; BEGIN OPEN orders_cursor FOR SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END; DECLARE customer_id NUMBER := 10; orders_cursor SYS_REFCURSOR; BEGIN orders_cursor := get_customer_orders(customer_id); LOOP FETCH orders_cursor INTO order_id, order_date, total_amount; EXIT WHEN orders_cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(order_id || ' ' || order_date || ' ' || total_amount); END LOOP; CLOSE orders_cursor; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || customer_id); END;
BEGIN DECLARE @dept_id SMALLINT := 10; BEGIN DECLARE @emp_name VARCHAR(30); SELECT @emp_name = ename FROM emp WHERE deptno = @dept_id; DBMS_OUTPUT.PUT_LINE('Employee name for deptno ' + isnull(@dept_id, '') + ' is ' + isnull(@emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found for deptno ' + isnull(@dept_id, '')); END END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
BEGIN DECLARE dept_id NUMBER(4) := 10; BEGIN DECLARE emp_name VARCHAR2(30); SELECT ename INTO emp_name FROM emp WHERE deptno = dept_id; DBMS_OUTPUT.PUT_LINE('Employee name for deptno ' || dept_id || ' is ' || emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found for deptno ' || dept_id); END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
IF OBJECT_ID('insert_new_customer', 'P') IS NOT NULL DROP PROCEDURE insert_new_customer; GO CREATE PROCEDURE insert_new_customer( @customer_id IN AS NUMBER, customer_name IN VARCHAR2, email IN VARCHAR2, phone_number IN VARCHAR2 ) AS BEGIN INSERT INTO customers(customer_id, customer_name, email, phone_number) VALUES(customer_id, customer_name, email, phone_number); COMMIT; END; / IF OBJECT_ID('get_customer_by_id', 'FN') IS NOT NULL DROP FUNCTION get_customer_by_id; GO CREATE FUNCTION get_customer_by_id( @customer_id IN NUMBER DECLARE ) RETURN BEGIN customers%ROWTYPE RETURN NULL; END; AS BEGIN RETURN ( SELECT * FROM customers WHERE customer_id = customer_id ); END; / BEGIN DECLARE @v_customer_name VARCHAR(100) := 'John Doe'; v_email dbo.VARCHAR2(100) := 'john.doe@example.com'; v_phone_number dbo.VARCHAR2(20) := '123-456-7890'; BEGIN dbo.insert_new_customer(100, @v_customer_name, v_email, v_phone_number); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END; / DECLARE @v_customer_id FLOAT := 100; v_customer customers%ROWTYPE; BEGIN SET @v_customer = dbo.get_customer_by_id(@v_customer_id); IF v_customer.customer_name IS NULL BEGIN RAISERROR('-20001 Customer not found', 16, 1); END DBMS_OUTPUT.PUT_LINE(v_customer.customer_name); END; /
CREATE OR REPLACE PROCEDURE insert_new_customer( customer_id IN NUMBER, customer_name IN VARCHAR2, email IN VARCHAR2, phone_number IN VARCHAR2 ) AS BEGIN INSERT INTO customers(customer_id, customer_name, email, phone_number) VALUES(customer_id, customer_name, email, phone_number); COMMIT; END; / CREATE OR REPLACE FUNCTION get_customer_by_id( customer_id IN NUMBER ) RETURN customers%ROWTYPE AS BEGIN RETURN ( SELECT * FROM customers WHERE customer_id = customer_id ); END; / BEGIN DECLARE v_customer_name VARCHAR2(100) := 'John Doe'; v_email VARCHAR2(100) := 'john.doe@example.com'; v_phone_number VARCHAR2(20) := '123-456-7890'; BEGIN insert_new_customer(100, v_customer_name, v_email, v_phone_number); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END; / DECLARE v_customer_id NUMBER := 100; v_customer customers%ROWTYPE; BEGIN v_customer := get_customer_by_id(v_customer_id); IF v_customer.customer_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Customer not found'); END IF; DBMS_OUTPUT.PUT_LINE(v_customer.customer_name); END; /
DECLARE @v_name VARCHAR(255) := 'John Doe'; BEGIN *** tement SELECT @v_name = name FROM employees WHERE id = 1; *** e to store an error message v_err_msg dbo.VARCHAR2(2000); BEGIN *** e a block of code *** s, it will be handled by the exception block INSERT INTO employees (name) VALUES (@v_name); EXCEPTION WHEN OTHERS THEN *** essage in the variable SET @v_err_msg = SQLERRM; END; *** occurred IF v_err_msg IS NOT NULL BEGIN *** n to handle the error RAISERROR(-20000, v_err_msg, 16, 1); END *** to iterate over a set of rows CURSOR c_employees IS SELECT * FROM employees; -- Op... SQLINES DEMO *** OPEN c_employees; *** ow into a record variable FETCH c_employees INTO @r_employee; *** remaining rows WHILE c_employees%FOUND BEGIN *** the row DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(r_employee.name, '')); -- Fe... SQLINES DEMO *** FETCH c_employees INTO @r_employee; END; -- Cl... SQLINES DEMO *** CLOSE c_employees; END;
DECLARE v_name VARCHAR2(255) := 'John Doe'; BEGIN -- Execute a SQL statement SELECT name INTO v_name FROM employees WHERE id = 1; -- Declare a variable to store an error message v_err_msg VARCHAR2(2000); BEGIN -- Attempt to execute a block of code -- If an error occurs, it will be handled by the exception block INSERT INTO employees (name) VALUES (v_name); EXCEPTION WHEN OTHERS THEN -- Store the error message in the variable v_err_msg := SQLERRM; END; -- Check if an error occurred IF v_err_msg IS NOT NULL THEN -- Raise an exception to handle the error RAISE_APPLICATION_ERROR(-20000, v_err_msg); END IF; -- Declare a cursor to iterate over a set of rows CURSOR c_employees IS SELECT * FROM employees; -- Open the cursor OPEN c_employees; -- Fetch the first row into a record variable FETCH c_employees INTO r_employee; -- Iterate over the remaining rows WHILE c_employees%FOUND LOOP -- Do something with the row DBMS_OUTPUT.PUT_LINE('Employee: ' || r_employee.name); -- Fetch the next row FETCH c_employees INTO r_employee; END LOOP; -- Close the cursor CLOSE c_employees; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@p_in IN AS VARCHAR2, OUT p_out OUT VARCHAR2) IS v_var dbo.VARCHAR2(100); BEGIN SELECT @v_var = ename FROM emp WHERE empno = p_in; set @p_out = v_var; IF p_out IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END; /
CREATE OR REPLACE PROCEDURE my_proc(p_in IN VARCHAR2, OUT p_out OUT VARCHAR2) IS v_var VARCHAR2(100); BEGIN SELECT ename INTO v_var FROM emp WHERE empno = p_in; p_out := v_var; IF p_out IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; 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 BEGIN RETURN (SELECT salary FROM employees WHERE employee_id = emp_id); END; BEGIN DECLARE @bonus FLOAT; emp_id NUMBER; BEGIN SELECT @emp_id = employee_id, @bonus = salary FROM employees WHERE employee_id = 100; IF @bonus IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('No bonus for employee ' + isnull(emp_id, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Bonus for employee ' + isnull(emp_id, '') + ' is ' + isnull(@bonus, '')); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END; END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS BEGIN RETURN (SELECT salary FROM employees WHERE employee_id = emp_id); END; BEGIN DECLARE bonus NUMBER; emp_id NUMBER; BEGIN SELECT employee_id, salary INTO emp_id, bonus FROM employees WHERE employee_id = 100; IF bonus IS NULL THEN DBMS_OUTPUT.PUT_LINE('No bonus for employee ' || emp_id); ELSE DBMS_OUTPUT.PUT_LINE('Bonus for employee ' || emp_id || ' is ' || bonus); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END; END;
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL DROP PROCEDURE insert_employee; GO CREATE PROCEDURE insert_employee( @p_emp_id IN AS NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER, p_dept_id IN NUMBER) IS BEGIN INSERT INTO employees(emp_id, name, salary, dept_id) VALUES(p_emp_id, p_name, p_salary, p_dept_id); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20001 Error inserting employee', 16, 1); END; /
CREATE OR REPLACE PROCEDURE insert_employee( p_emp_id IN NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER, p_dept_id IN NUMBER) IS BEGIN INSERT INTO employees(emp_id, name, salary, dept_id) VALUES(p_emp_id, p_name, p_salary, p_dept_id); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Error inserting employee'); END; /
DECLARE @v_emp_id INTEGER := 10; v_emp_name dbo.VARCHAR2(25); BEGIN SELECT @v_emp_name = ename FROM emp WHERE empno = @v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); IF v_emp_name IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee is valid'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found for ID: ' + isnull(@v_emp_id, '')); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.'); END;
DECLARE v_emp_id INTEGER := 10; v_emp_name VARCHAR2(25); BEGIN SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); IF v_emp_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; DBMS_OUTPUT.PUT_LINE('Employee is valid'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found for ID: ' || v_emp_id); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.'); END;
DECLARE @salary DECIMAL(10, 2); BEGIN SELECT @salary = @salary FROM employees WHERE employee_id = 1; IF @salary > 50000 BEGIN DBMS_OUTPUT.PUT_LINE('Salary is greater than $50,000.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to $50,000.'); END BEGIN INSERT INTO employees (employee_id, salary) VALUES (10, 60000); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Error: employee_id already exists.'); END; END;
DECLARE salary NUMBER(10, 2); BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = 1; IF salary > 50000 THEN DBMS_OUTPUT.PUT_LINE('Salary is greater than $50,000.'); ELSE DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to $50,000.'); END IF; BEGIN INSERT INTO employees (employee_id, salary) VALUES (10, 60000); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Error: employee_id already exists.'); END; END;
CREATE PROCEDURE sp_example ( @param1 IN AS VARCHAR2, param2 OUT NUMBER ) AS BEGIN SELECT @param2 = SUM(col1) FROM table1 WHERE col2 = param1; END sp_example; DECLARE @val FLOAT; BEGIN dbo.sp_example('value1', @val); IF @val IS NOT NULL BEGIN DBMS_OUTPUT.PUT_LINE('Value: ' + isnull(@val, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No matching value found'); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No matching value found'); END END;
CREATE PROCEDURE sp_example ( param1 IN VARCHAR2, param2 OUT NUMBER ) AS BEGIN SELECT SUM(col1) INTO param2 FROM table1 WHERE col2 = param1; END sp_example; DECLARE val NUMBER; BEGIN sp_example('value1', val); IF val IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('Value: ' || val); ELSE DBMS_OUTPUT.PUT_LINE('No matching value found'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No matching value found'); END;
DECLARE @v_salary DECIMAL(10, 2); v_emp_id ROW_NUMBER(6); BEGIN SELECT @v_salary = salary FROM employees WHERE emp_id = v_emp_id; IF @v_salary > 100000 BEGIN RAISERROR('-20001 Salary too high', 16, 1); END UPDATE employees SET salary = @v_salary + (@v_salary * 0.10) WHERE emp_id = v_emp_id; WHEN TOO_MANY_ROWS THEN ROLLBACK; RAISERROR('-20002 Multiple rows returned', 16, 1); IF @@FETCH_STATUS <> 0 BEGIN ROLLBACK; RAISERROR('-20003 No data found', 16, 1); END WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_salary NUMBER(10, 2); v_emp_id NUMBER(6); BEGIN SELECT salary INTO v_salary FROM employees WHERE emp_id = v_emp_id; IF v_salary > 100000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary too high'); END IF; UPDATE employees SET salary = v_salary + (v_salary * 0.10) WHERE emp_id = v_emp_id; EXCEPTION WHEN TOO_MANY_ROWS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20002, 'Multiple rows returned'); WHEN NO_DATA_FOUND THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20003, 'No data found'); WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE @v_num1 FLOAT; BEGIN SET @v_num1 = 10; IF @v_num1 > 5 BEGIN DBMS_OUTPUT.PUT_LINE('v_num1 is greater than 5'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('v_num1 is less than or equal to 5'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_num1 NUMBER; BEGIN v_num1 := 10; IF v_num1 > 5 THEN DBMS_OUTPUT.PUT_LINE('v_num1 is greater than 5'); ELSE DBMS_OUTPUT.PUT_LINE('v_num1 is less than or equal to 5'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @sal FLOAT; BEGIN SELECT @sal = salary FROM employees WHERE employee_id = 1; IF @sal > 10000 BEGIN UPDATE employees SET salary = @sal * 1.1 WHERE employee_id = 1; END ELSE IF @sal BETWEEN 5000 AND 10000 BEGIN UPDATE employees SET salary = @sal * 1.05 WHERE employee_id = 1; END ELSE BEGIN UPDATE employees SET salary = @sal * 1.02 WHERE employee_id = 1; END COMMIT; END; /
DECLARE sal NUMBER; BEGIN SELECT salary INTO sal FROM employees WHERE employee_id = 1; IF sal > 10000 THEN UPDATE employees SET salary = sal * 1.1 WHERE employee_id = 1; ELSIF sal BETWEEN 5000 AND 10000 THEN UPDATE employees SET salary = sal * 1.05 WHERE employee_id = 1; ELSE UPDATE employees SET salary = sal * 1.02 WHERE employee_id = 1; END IF; COMMIT; END; /
BEGIN -- De... SQLINES DEMO *** DECLARE @v_emp_id FLOAT; v_salary NUMBER; v_bonus NUMBER; BEGIN -- In... SQLINES DEMO *** INSERT INTO employees (emp_id, salary, bonus) VALUES (100, 2000, 100); *** alary UPDATE employees SET salary = salary + 100 WHERE emp_id = 100; *** ry and bonus SELECT @v_salary = salary, @v_bonus = bonus FROM employees WHERE emp_id = 100; *** tails DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(v_bonus, '')); EXCEPTION -- Ha... SQLINES DEMO *** WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END; END;
BEGIN -- Declare variables DECLARE v_emp_id NUMBER; v_salary NUMBER; v_bonus NUMBER; BEGIN -- Insert new employee INSERT INTO employees (emp_id, salary, bonus) VALUES (100, 2000, 100); -- Update employee salary UPDATE employees SET salary = salary + 100 WHERE emp_id = 100; -- Get employee salary and bonus SELECT salary, bonus INTO v_salary, v_bonus FROM employees WHERE emp_id = 100; -- Print employee details DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); DBMS_OUTPUT.PUT_LINE('Bonus: ' || v_bonus); EXCEPTION -- Handle errors WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END; END;
CREATE PROCEDURE DisplayEmployeesByDepartment(@deptno IN AS VARCHAR2) AS BEGIN DECLARE emp CURSOR FOR SELECT * FROM Employees WHERE department_id = deptno; OPEN emp; FETCH emp INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE(isnull(@emp_employee_id, '') + ' ' + isnull(@emp_first_name, '') + ' ' + isnull(@emp_last_name, '')); FETCH emp INTO; END; CLOSE emp; DEALLOCATE emp; END; / DECLARE @tot_sal FLOAT; BEGIN SET @tot_sal = ( SELECT SUM(salary) FROM Employees ); IF @tot_sal > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('Total salary exceeds $10,000.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to $10,000.'); END END; /
CREATE PROCEDURE DisplayEmployeesByDepartment(deptno IN VARCHAR2) AS BEGIN FOR emp IN (SELECT * FROM Employees WHERE department_id = deptno) LOOP DBMS_OUTPUT.PUT_LINE(emp.employee_id || ' ' || emp.first_name || ' ' || emp.last_name); END LOOP; END; / DECLARE tot_sal NUMBER; BEGIN tot_sal := ( SELECT SUM(salary) FROM Employees ); IF tot_sal > 10000 THEN DBMS_OUTPUT.PUT_LINE('Total salary exceeds $10,000.'); ELSE DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to $10,000.'); END IF; END; /
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc( @param1 IN AS NUMBER, param2 OUT VARCHAR2 ) IS BEGIN SELECT @param2 = name FROM my_table WHERE id = param1; DBMS_OUTPUT.PUT_LINE('Name is: ' + isnull(param2, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found for id: ' + isnull(param1, '')); END END;
CREATE OR REPLACE PROCEDURE my_proc( param1 IN NUMBER, param2 OUT VARCHAR2 ) IS BEGIN SELECT name INTO param2 FROM my_table WHERE id = param1; DBMS_OUTPUT.PUT_LINE('Name is: ' || param2); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found for id: ' || param1); END;
CREATE TABLE t (id FLOAT, name VARCHAR(20)); INSERT INTO t VALUES (1, 'John'); INSERT INTO t VALUES (2, 'Mary'); COMMIT; CREATE PROCEDURE update_name(@id_in FLOAT, @name_in VARCHAR(4000)) AS BEGIN SET NOCOUNT ON; UPDATE t SET name = @name_in WHERE id = @id_in; COMMIT; END; DECLARE @id FLOAT; name dbo.VARCHAR2(20); BEGIN SELECT @id = @id, @name = name FROM t WHERE @id = 1; CALL update_name(@id, 'Jane'); END; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE;
CREATE TABLE t (id NUMBER, name VARCHAR2(20)); INSERT INTO t VALUES (1, 'John'); INSERT INTO t VALUES (2, 'Mary'); COMMIT; CREATE PROCEDURE update_name(id_in NUMBER, name_in VARCHAR2) AS BEGIN UPDATE t SET name = name_in WHERE id = id_in; COMMIT; END; DECLARE id NUMBER; name VARCHAR2(20); BEGIN SELECT id, name INTO id, name FROM t WHERE id = 1; CALL update_name(id, 'Jane'); END; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE;
CREATE PROCEDURE get_employee_details(@employee_id IN AS NUMBER) AS BEGIN SELECT @emp_record = * FROM employees WHERE id = employee_id; IF NOT FOUND BEGIN RAISERROR('-20001 Employee not found.', 16, 1); END RETURN emp_record; END;
CREATE PROCEDURE get_employee_details(employee_id IN NUMBER) AS BEGIN SELECT * INTO emp_record FROM employees WHERE id = employee_id; IF NOT FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found.'); END IF; RETURN emp_record; END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = employee_id, @v_salary = salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 BEGIN UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = @v_emp_id; END ELSE BEGIN RAISERROR('-20001 Salary is too low to be increased', 16, 1); END END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT employee_id, salary INTO v_emp_id, v_salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 THEN UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = v_emp_id; ELSE RAISE_APPLICATION_ERROR(-20001, 'Salary is too low to be increased'); END IF; END;
IF OBJECT_ID('print_records', 'P') IS NOT NULL DROP PROCEDURE print_records; GO CREATE PROCEDURE print_records AS BEGIN SET NOCOUNT ON; DECLARE rec CURSOR FOR SELECT employee_id, first_name, salary FROM employees; OPEN rec; FETCH rec INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE(isnull(@rec_employee_id, '') + ' ' + isnull(@rec_first_name, '') + ' ' + isnull(@rec_salary, '')); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; END;
CREATE OR REPLACE PROCEDURE print_records AS BEGIN FOR rec IN (SELECT employee_id, first_name, salary FROM employees) LOOP DBMS_OUTPUT.PUT_LINE(rec.employee_id || ' ' || rec.first_name || ' ' || rec.salary); END LOOP; END print_records;
IF OBJECT_ID('get_order_details', 'P') IS NOT NULL DROP PROCEDURE get_order_details; GO CREATE PROCEDURE get_order_details( @order_id IN AS NUMBER, out_customer_name OUT VARCHAR2, out_total_amount OUT NUMBER ) AS BEGIN SELECT @out_customer_name = customer_name, @out_total_amount = SUM(quantity * unit_price) FROM orders o JOIN order_items oi ON o.order_id = oi.order_id WHERE o.order_id = order_id GROUP BY customer_name; END; BEGIN DECLARE @customer_name VARCHAR(50); total_amount NUMBER; BEGIN dbo.get_order_details(100, @customer_name, total_amount); DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(@customer_name, '')); DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(total_amount, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No order found with ID: ' + 100); END END; END;
CREATE OR REPLACE PROCEDURE get_order_details( order_id IN NUMBER, out_customer_name OUT VARCHAR2, out_total_amount OUT NUMBER ) AS BEGIN SELECT customer_name, SUM(quantity * unit_price) INTO out_customer_name, out_total_amount FROM orders o JOIN order_items oi ON o.order_id = oi.order_id WHERE o.order_id = order_id GROUP BY customer_name; END; BEGIN DECLARE customer_name VARCHAR2(50); total_amount NUMBER; BEGIN get_order_details(100, customer_name, total_amount); DBMS_OUTPUT.PUT_LINE('Customer Name: ' || customer_name); DBMS_OUTPUT.PUT_LINE('Total Amount: ' || total_amount); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No order found with ID: ' || 100); END; END;
CREATE PROCEDURE GetEmpDetails (@p_empid IN AS NUMBER) IS BEGIN DECLARE @v_empname VARCHAR(25); v_salary NUMBER; BEGIN SELECT @v_empname = ename, @v_salary = sal FROM emp WHERE empno = p_empid; DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@v_empname, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(p_empid, '')); END END; END;
CREATE PROCEDURE GetEmpDetails (p_empid IN NUMBER) IS BEGIN DECLARE v_empname VARCHAR2(25); v_salary NUMBER; BEGIN SELECT ename, sal INTO v_empname, v_salary FROM emp WHERE empno = p_empid; DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_empname); DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || p_empid); END; END;
CREATE TABLE employees ( employee_id FLOAT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, department VARCHAR(255), salary FLOAT ); INSERT INTO employees VALUES (1, 'John', 'Doe', 'Sales', 50000); INSERT INTO employees VALUES (2, 'Jane', 'Smith', 'Marketing', 60000); INSERT INTO employees VALUES (3, 'Bill', 'Jones', 'Engineering', 70000); CREATE PROCEDURE get_employee_salary ( @p_employee_id IN AS NUMBER, p_salary OUT NUMBER ) AS BEGIN SELECT @p_salary = salary FROM employees WHERE employee_id = p_employee_id; END; DECLARE @v_employee_id FLOAT := 1; v_salary NUMBER; BEGIN dbo.get_employee_salary(@v_employee_id, v_salary); DBMS_OUTPUT.PUT_LINE('Salary for employee ID ' + isnull(@v_employee_id, '') + ': ' + isnull(v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(@v_employee_id, '')); END END; CREATE FUNCTION get_average_salary () RETURNS FLOAT AS BEGIN RETURN (SELECT AVG(salary) FROM employees); END; DECLARE @v_avg_salary FLOAT := dbo.get_average_salary(); BEGIN DBMS_OUTPUT.PUT_LINE('Average salary: ' + isnull(@v_avg_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while getting the average salary'); END;
CREATE TABLE employees ( employee_id NUMBER NOT NULL, first_name VARCHAR2(255) NOT NULL, last_name VARCHAR2(255) NOT NULL, department VARCHAR2(255), salary NUMBER ); INSERT INTO employees VALUES (1, 'John', 'Doe', 'Sales', 50000); INSERT INTO employees VALUES (2, 'Jane', 'Smith', 'Marketing', 60000); INSERT INTO employees VALUES (3, 'Bill', 'Jones', 'Engineering', 70000); CREATE PROCEDURE get_employee_salary ( p_employee_id IN NUMBER, p_salary OUT NUMBER ) AS BEGIN SELECT salary INTO p_salary FROM employees WHERE employee_id = p_employee_id; END; DECLARE v_employee_id NUMBER := 1; v_salary NUMBER; BEGIN get_employee_salary(v_employee_id, v_salary); DBMS_OUTPUT.PUT_LINE('Salary for employee ID ' || v_employee_id || ': ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || v_employee_id); END; CREATE FUNCTION get_average_salary () RETURN NUMBER AS BEGIN RETURN (SELECT AVG(salary) FROM employees); END; DECLARE v_avg_salary NUMBER := get_average_salary(); BEGIN DBMS_OUTPUT.PUT_LINE('Average salary: ' || v_avg_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while getting the average salary'); END;
BEGIN BEGIN -- so... SQLINES DEMO *** IF @@FETCH_STATUS <> 0 BEGIN RETURN -1; END WHEN OTHERS THEN RAISE; END; DECLARE rec CURSOR FOR SELECT * FROM my_table; OPEN rec; FETCH rec INTO; WHILE @@FETCH_STATUS=0 BEGIN BEGIN -- so... SQLINES DEMO *** EXCEPTION WHEN OTHERS THEN RAISERROR('-20001 Error while processing record ' + isnull(@rec_id, ''), 16, 1); END; FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; END;
BEGIN BEGIN -- some code here EXCEPTION WHEN NO_DATA_FOUND THEN RETURN -1; WHEN OTHERS THEN RAISE; END; FOR rec IN (SELECT * FROM my_table) LOOP BEGIN -- some more code here EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR(-20001, 'Error while processing record ' || rec.id); END; END LOOP; END;
BEGIN DECLARE @l_emp_id FLOAT := 10; l_emp_name dbo.VARCHAR2(20); BEGIN SELECT @l_emp_name = ename FROM emp WHERE empno = @l_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(l_emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee with ID ' + isnull(@l_emp_id, '') + ' not found'); END END; END;
BEGIN DECLARE l_emp_id NUMBER := 10; l_emp_name VARCHAR2(20); BEGIN SELECT ename INTO l_emp_name FROM emp WHERE empno = l_emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee with ID ' || l_emp_id || ' not found'); END; END;
CREATE PROCEDURE UpdateEmployeeSalary( @emp_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN *** ee's salary UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; -- Co... SQLINES DEMO *** COMMIT; END; BEGIN *** iables emp_id SET @NUMBER = 100; new_salary SET @NUMBER = 20000; *** rocedure to update the employee's salary dbo.UpdateEmployeeSalary(emp_id, new_salary); *** that may occur EXCEPTION WHEN OTHERS THEN *** ges if an error occurs ROLLBACK; *** message DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE PROCEDURE UpdateEmployeeSalary( emp_id IN NUMBER, new_salary IN NUMBER) IS BEGIN -- Update the employee's salary UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; -- Commit the changes COMMIT; END; BEGIN -- Declare local variables emp_id NUMBER := 100; new_salary NUMBER := 20000; -- Call the stored procedure to update the employee's salary UpdateEmployeeSalary(emp_id, new_salary); -- Handle any errors that may occur EXCEPTION WHEN OTHERS THEN -- Rollback the changes if an error occurs ROLLBACK; -- Display the error message DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
CREATE PROCEDURE my_proc(@IN p1 AS NUMBER, INOUT p2 IN VARCHAR2) IS BEGIN SELECT @p2 = name FROM table WHERE id = p1; IF p2 IS NULL BEGIN RAISERROR('-20001 Record not found', 16, 1); END END; CREATE FUNCTION my_func(@IN p1 NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS BEGIN RETURN (SELECT name FROM table WHERE id = p1); END; BEGIN dbo.my_proc(10, @my_variable); IF @my_variable IS NOT NULL BEGIN DBMS_OUTPUT.PUT_LINE(@my_variable); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END;
CREATE PROCEDURE my_proc(IN p1 NUMBER, INOUT p2 IN VARCHAR2) IS BEGIN SELECT name INTO p2 FROM table WHERE id = p1; IF p2 IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Record not found'); END IF; END; CREATE FUNCTION my_func(IN p1 NUMBER) RETURN VARCHAR2 IS BEGIN RETURN (SELECT name FROM table WHERE id = p1); END; BEGIN my_proc(10, @my_variable); IF @my_variable IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE(@my_variable); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END;
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL DROP PROCEDURE insert_employee; GO CREATE PROCEDURE insert_employee( @in_emp_id FLOAT, @in_emp_name VARCHAR(4000), @in_salary FLOAT, @in_dept_id FLOAT ) AS BEGIN SET NOCOUNT ON; INSERT INTO employees ( emp_id, emp_name, salary, dept_id ) VALUES ( @in_emp_id, @in_emp_name, @in_salary, @in_dept_id ); COMMIT; END; GO
CREATE OR REPLACE PROCEDURE insert_employee( in_emp_id NUMBER, in_emp_name VARCHAR2, in_salary NUMBER, in_dept_id NUMBER ) AS BEGIN INSERT INTO employees ( emp_id, emp_name, salary, dept_id ) VALUES ( in_emp_id, in_emp_name, in_salary, in_dept_id ); COMMIT; END; /
IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL DROP PROCEDURE get_employee_info; GO CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER) AS BEGIN SELECT * FROM employees WHERE emp_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
CREATE OR REPLACE PROCEDURE get_employee_info(emp_id IN NUMBER) AS BEGIN SELECT * FROM employees WHERE emp_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
BEGIN INSERT INTO employee (employee_id, first_name, last_name, salary) VALUES (1, 'John', 'Doe', 10000); UPDATE employee SET salary = salary + 1000 WHERE employee_id = 1; DELETE FROM employee WHERE employee_id = 1; SELECT * FROM employee; END;
BEGIN INSERT INTO employee (employee_id, first_name, last_name, salary) VALUES (1, 'John', 'Doe', 10000); UPDATE employee SET salary = salary + 1000 WHERE employee_id = 1; DELETE FROM employee WHERE employee_id = 1; SELECT * FROM employee; END;
DECLARE @Name VARCHAR(30) := 'John Doe'; BEGIN BEGIN UPDATE Employees SET Name = @Name WHERE ID = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END; BEGIN SELECT * FROM Employees WHERE Name LIKE '%John%'; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found'); END END; END;
DECLARE @Name VARCHAR2(30) := 'John Doe'; BEGIN BEGIN UPDATE Employees SET Name = @Name WHERE ID = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END; BEGIN SELECT * FROM Employees WHERE Name LIKE '%John%'; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found'); END; END;
DECLARE @name VARCHAR(20) := 'Alice'; BEGIN *** ee's salary SELECT @name = salary FROM emp WHERE @name = 'Alice'; *** the employee does not exist IF @name IS NULL BEGIN RAISERROR('-20001 Employee not found: ' + isnull(@name, ''), 16, 1); END *** ee's salary UPDATE emp SET salary = salary * 1.10 WHERE @name = 'Alice'; -- Co... SQLINES DEMO *** COMMIT; END;
DECLARE name VARCHAR2(20) := 'Alice'; BEGIN -- Select the employee's salary SELECT salary INTO name FROM emp WHERE name = 'Alice'; -- Raise an error if the employee does not exist IF name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found: ' || name); END IF; -- Update the employee's salary UPDATE emp SET salary = salary * 1.10 WHERE name = 'Alice'; -- Commit the changes COMMIT; END;
BEGIN INSERT INTO employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id) VALUES (1, 'John', 'Doe', 'john.doe@example.com', '555-1212', GETDATE(), 'IT_PROG', 60000, NULL, 20, 20); UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20; DELETE FROM employees WHERE employee_id = 1; SELECT * FROM employees; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
BEGIN INSERT INTO employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id) VALUES (1, 'John', 'Doe', 'john.doe@example.com', '555-1212', SYSDATE, 'IT_PROG', 60000, NULL, 20, 20); UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20; DELETE FROM employees WHERE employee_id = 1; SELECT * FROM employees; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL DROP PROCEDURE insert_employee; GO CREATE PROCEDURE insert_employee( @first_name IN AS VARCHAR2, last_name IN VARCHAR2, email IN VARCHAR2, phone_number IN VARCHAR2, hire_date IN DATE ) IS BEGIN INSERT INTO employees (first_name, last_name, email, phone_number, hire_date) VALUES (first_name, last_name, email, phone_number, hire_date); COMMIT; END; / DECLARE @emp_id FLOAT; BEGIN SELECT @emp_id = employee_id FROM employees WHERE email = 'john.doe@example.com'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_id, '')); END; / BEGIN UPDATE employees SET salary = salary * 1.10 WHERE department_id = 10; COMMIT; END; / BEGIN DELETE FROM employees WHERE employee_id = 100; COMMIT; END; / IF salary > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('High salary'); END ELSE IF salary > 5000 BEGIN DBMS_OUTPUT.PUT_LINE('Medium salary'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Low salary'); END / BEGIN DECLARE @error_code FLOAT; BEGIN *** se an exception EXCEPTION WHEN OTHERS THEN SET @error_code = @@ERROR; *** ion END; END; /
CREATE OR REPLACE PROCEDURE insert_employee( first_name IN VARCHAR2, last_name IN VARCHAR2, email IN VARCHAR2, phone_number IN VARCHAR2, hire_date IN DATE ) IS BEGIN INSERT INTO employees (first_name, last_name, email, phone_number, hire_date) VALUES (first_name, last_name, email, phone_number, hire_date); COMMIT; END; / DECLARE emp_id NUMBER; BEGIN SELECT employee_id INTO emp_id FROM employees WHERE email = 'john.doe@example.com'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id); END; / BEGIN UPDATE employees SET salary = salary * 1.10 WHERE department_id = 10; COMMIT; END; / BEGIN DELETE FROM employees WHERE employee_id = 100; COMMIT; END; / IF salary > 10000 THEN DBMS_OUTPUT.PUT_LINE('High salary'); ELSIF salary > 5000 THEN DBMS_OUTPUT.PUT_LINE('Medium salary'); ELSE DBMS_OUTPUT.PUT_LINE('Low salary'); END IF; / BEGIN DECLARE error_code NUMBER; BEGIN -- Code that may raise an exception EXCEPTION WHEN OTHERS THEN error_code := SQLCODE; -- Handle the exception END; END; /
CREATE PROCEDURE Proc1(@x IN AS NUMBER, y OUT NUMBER) IS BEGIN set @y = x * 2; RAISERROR('-20001 Negative value supplied.', 16, 1); END; CREATE FUNCTION Func1(@x IN NUMBERDECLARE , y begin IN RETURN NULL; END; NUMBER) RETURN NUMBER IS BEGIN RETURN x + y; EXCEPTION WHEN OTHERS THEN RETURN -1; END; BEGIN DECLARE @x FLOAT := 10; y NUMBER; BEGIN dbo.Proc1(@x, y); DBMS_OUTPUT.PUT_LINE('y = ' + isnull(y, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error in Proc1: ' + ISNULL(@@ERROR, '') + ' ' + ISNULL(SQLERRM, '')); END; DECLARE @a FLOAT := 1; b SET @NUMBER = 2; BEGIN DBMS_OUTPUT.PUT_LINE('a + b = ' + Isnull(dbo.Func1(@a, b), '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error in Func1: ' + ISNULL(@@ERROR, '') + ' ' + ISNULL(SQLERRM, '')); END; END;
CREATE PROCEDURE Proc1(x IN NUMBER, y OUT NUMBER) IS BEGIN y := x * 2; RAISE_APPLICATION_ERROR(-20001, 'Negative value supplied.'); END; FUNCTION Func1(x IN NUMBER, y IN NUMBER) RETURN NUMBER IS BEGIN RETURN x + y; EXCEPTION WHEN OTHERS THEN RETURN -1; END; BEGIN DECLARE x NUMBER := 10; y NUMBER; BEGIN Proc1(x, y); DBMS_OUTPUT.PUT_LINE('y = ' || y); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error in Proc1: ' || SQLCODE || ' ' || SQLERRM); END; DECLARE a NUMBER := 1; b NUMBER := 2; BEGIN DBMS_OUTPUT.PUT_LINE('a + b = ' || Func1(a, b)); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error in Func1: ' || SQLCODE || ' ' || SQLERRM); END; END;
CREATE PROCEDURE Get_Product_Sales ( @start_date IN AS DATE, end_date IN DATE, sales_total OUT NUMBER ) AS BEGIN *** ales for the given period. SELECT @sales_total = SUM(sales_amount) FROM Sales WHERE sale_date BETWEEN start_date AND end_date; *** re any sales in the given period. IF sales_total IS NULL BEGIN set @sales_total = 0; END END; *** e to get total sales for the last quarter. DECLARE @total_sales FLOAT; BEGIN CALL Get_Product_Sales(CONVERT(DATETIME, '2022-01-01'), CONVERT(DATETIME, '2022-03-31'), @total_sales); IF @total_sales = 0 BEGIN DBMS_OUTPUT.PUT_LINE('No sales in the last quarter.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Total sales in the last quarter: ' + isnull(@total_sales, '')); END END;
CREATE PROCEDURE Get_Product_Sales ( start_date IN DATE, end_date IN DATE, sales_total OUT NUMBER ) AS BEGIN -- Calculate total sales for the given period. SELECT SUM(sales_amount) INTO sales_total FROM Sales WHERE sale_date BETWEEN start_date AND end_date; -- Check if there were any sales in the given period. IF sales_total IS NULL THEN sales_total := 0; END IF; END; -- Call the procedure to get total sales for the last quarter. DECLARE total_sales NUMBER; BEGIN CALL Get_Product_Sales(TO_DATE('2022-01-01', 'YYYY-MM-DD'), TO_DATE('2022-03-31', 'YYYY-MM-DD'), total_sales); IF total_sales = 0 THEN DBMS_OUTPUT.PUT_LINE('No sales in the last quarter.'); ELSE DBMS_OUTPUT.PUT_LINE('Total sales in the last quarter: ' || total_sales); END IF; END;
IF OBJECT_ID('calculate_salary', 'P') IS NOT NULL DROP PROCEDURE calculate_salary; GO CREATE PROCEDURE calculate_salary(@employee_id IN AS NUMBER, salary OUT NUMBER) AS BEGIN SELECT @salary = salary FROM employees WHERE employee_id = employee_id; IF salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END set @salary = salary * 1.10; UPDATE employees SET salary = salary WHERE employee_id = employee_id; END CREATE PROCEDURE;
CREATE OR REPLACE PROCEDURE calculate_salary(employee_id IN NUMBER, salary OUT NUMBER) AS BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; salary := salary * 1.10; UPDATE employees SET salary = salary WHERE employee_id = employee_id; END PROCEDURE;
CREATE PROCEDURE emp_details(@p_empid IN AS NUMBER) AS c_emp_name dbo.VARCHAR2(50); BEGIN SELECT @c_emp_name = ename FROM emp WHERE empno = p_empid; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(c_emp_name, '')); END; BEGIN dbo.emp_details(7934); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
CREATE PROCEDURE emp_details(p_empid IN NUMBER) AS c_emp_name VARCHAR2(50); BEGIN SELECT ename INTO c_emp_name FROM emp WHERE empno = p_empid; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || c_emp_name); END; BEGIN emp_details(7934); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;
IF OBJECT_ID('sum_salaries', 'FN') IS NOT NULL DROP FUNCTION sum_salaries; GO CREATE FUNCTION sum_salaries (@employees IN OUT DECLARE @SYS_REFCURSOR) BEGIN RETURNS RETURN NULL; END; NUMBER AS total_salary SET @NUMBER = 0; emp_rec employees%ROWTYPE; BEGIN WHILE 1=1 BEGIN FETCH employees INTO @emp_rec; IF @@FETCH_STATUS <> 0 BREAK; set @total_salary = total_salary + emp_rec.salary; END; RETURN total_salary; END;
CREATE OR REPLACE FUNCTION sum_salaries (employees IN OUT SYS_REFCURSOR) RETURNS NUMBER AS total_salary NUMBER := 0; emp_rec employees%ROWTYPE; BEGIN LOOP FETCH employees INTO emp_rec; EXIT WHEN employees%NOTFOUND; total_salary := total_salary + emp_rec.salary; END LOOP; RETURN total_salary; END;
DECLARE @v_emp_name VARCHAR(255); v_emp_salary NUMBER; BEGIN SELECT @v_emp_name = ename, @v_emp_salary = sal FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_emp_salary, '')); BEGIN UPDATE emp SET sal = sal * 1.10 WHERE empno = 7369; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary increased by 10%'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END; END;
DECLARE v_emp_name VARCHAR2(255); v_emp_salary NUMBER; BEGIN SELECT ename, sal INTO v_emp_name, v_emp_salary FROM emp WHERE empno = 7369; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_emp_salary); BEGIN UPDATE emp SET sal = sal * 1.10 WHERE empno = 7369; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary increased by 10%'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END; END;
DECLARE @v_name VARCHAR(20); v_sal NUMBER; BEGIN SELECT @v_name = name, @v_sal = sal FROM emp WHERE empno = 7839; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_sal, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
DECLARE v_name VARCHAR2(20); v_sal NUMBER; BEGIN SELECT name, sal INTO v_name, v_sal FROM emp WHERE empno = 7839; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_sal); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;
DECLARE @emp_id INT; salary ROW_NUMBER(8,2); bonus ROW_NUMBER(8,2); BEGIN SELECT @emp_id = employee_id, @salary = salary, @bonus = bonus FROM employees WHERE employee_id = 100; IF salary < 3000 BEGIN SET @bonus = bonus * 1.10; END UPDATE employees SET bonus = bonus WHERE employee_id = 100; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END WHEN OTHERS THEN RAISE; END;
DECLARE emp_id NUMBER(6); salary NUMBER(8,2); bonus NUMBER(8,2); BEGIN SELECT employee_id, salary, bonus INTO emp_id, salary, bonus FROM employees WHERE employee_id = 100; IF salary < 3000 THEN bonus := bonus * 1.10; END IF; UPDATE employees SET bonus = bonus WHERE employee_id = 100; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); WHEN OTHERS THEN RAISE; END;
IF OBJECT_ID('some_proc', 'P') IS NOT NULL DROP PROCEDURE some_proc; GO CREATE PROCEDURE some_proc ( @param1 IN AS VARCHAR2, param2 IN NUMBER ) IS v_result NUMBER; BEGIN SELECT @v_result = COUNT(*) FROM some_table WHERE col1 = param1; IF v_result > 0 BEGIN UPDATE some_table SET col2 = param2 WHERE col1 = param1; END ELSE BEGIN INSERT INTO some_table (col1, col2) VALUES (param1, param2); END END; /
CREATE OR REPLACE PROCEDURE some_proc ( param1 IN VARCHAR2, param2 IN NUMBER ) IS v_result NUMBER; BEGIN SELECT COUNT(*) INTO v_result FROM some_table WHERE col1 = param1; IF v_result > 0 THEN UPDATE some_table SET col2 = param2 WHERE col1 = param1; ELSE INSERT INTO some_table (col1, col2) VALUES (param1, param2); END IF; END; /
CREATE PROCEDURE emp_bonus(@p_empid IN AS NUMBER, p_bonus IN NUMBER) IS l_sal NUMBER; BEGIN SELECT @l_sal = salary FROM emp WHERE empid = p_empid; UPDATE emp SET salary = l_sal + p_bonus WHERE empid = p_empid; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END WHEN OTHERS THEN ROLLBACK; RAISE; END;
PROCEDURE emp_bonus(p_empid IN NUMBER, p_bonus IN NUMBER) IS l_sal NUMBER; BEGIN SELECT salary INTO l_sal FROM emp WHERE empid = p_empid; UPDATE emp SET salary = l_sal + p_bonus WHERE empid = p_empid; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = 1; IF @v_salary > 10000 BEGIN RAISERROR('-20000 Salary too high', 16, 1); END UPDATE employees SET salary = @v_salary + 1000 WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 1; IF v_salary > 10000 THEN RAISE_APPLICATION_ERROR(-20000, 'Salary too high'); END IF; UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
*** with Error Handling IF OBJECT_ID('my_procedure', 'P') IS NOT NULL DROP PROCEDURE my_procedure; GO CREATE PROCEDURE my_procedure(@id IN AS NUMBER) IS emp_name dbo.VARCHAR2(20); BEGIN -- Fe... SQLINES DEMO *** SELECT @emp_name = ename FROM emp WHERE empno = id; *** found error IF SQL%NOTFOUND BEGIN RAISERROR('-20001 Employee ID not found: ' + isnull(id, ''), 16, 1); END -- Ot... SQLINES DEMO *** END;
-- Stored Procedure with Error Handling CREATE OR REPLACE PROCEDURE my_procedure(id IN NUMBER) IS emp_name VARCHAR2(20); BEGIN -- Fetch employee name SELECT ename INTO emp_name FROM emp WHERE empno = id; -- Check for no data found error IF SQL%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee ID not found: ' || id); END IF; -- Other operations... END;
BEGIN DECLARE @v_empno FLOAT; v_ename dbo.VARCHAR2(10); BEGIN SELECT @v_empno = empno, @v_ename = ename FROM emp WHERE empno = &empno; DBMS_OUTPUT.PUT_LINE('Employee Number: ' + isnull(@v_empno, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found with the given employee number.'); END END; END;
BEGIN DECLARE v_empno NUMBER; v_ename VARCHAR2(10); BEGIN SELECT empno, ename INTO v_empno, v_ename FROM emp WHERE empno = &empno; DBMS_OUTPUT.PUT_LINE('Employee Number: ' || v_empno); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found with the given employee number.'); END; END;
CREATE PROCEDURE get_customer_orders ( @customer_id IN AS NUMBER, out_orders OUT SYS_REFCURSOR ) AS BEGIN SELECT * FROM orders WHERE customer_id = customer_id; END; DECLARE @v_cursor SYS_REFCURSOR; BEGIN dbo.get_customer_orders(1, @v_cursor); DECLARE rec CURSOR FOR @v_cursor LOOP DBMS_OUTPUT.PUT_LINE(@rec_order_id); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; CLOSE v_cursor; END;
CREATE PROCEDURE get_customer_orders ( customer_id IN NUMBER, out_orders OUT SYS_REFCURSOR ) AS BEGIN OPEN out_orders FOR SELECT * FROM orders WHERE customer_id = customer_id; END; DECLARE v_cursor SYS_REFCURSOR; BEGIN get_customer_orders(1, v_cursor); FOR rec IN v_cursor LOOP DBMS_OUTPUT.PUT_LINE(rec.order_id); END LOOP; CLOSE v_cursor; END;
DECLARE @v_name VARCHAR(20) := 'John'; v_salary SET @NUMBER = 1000; BEGIN INSERT INTO employees (name, salary) VALUES (@v_name, v_salary); COMMIT; DBMS_OUTPUT.PUT_LINE('New employee added!'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_name VARCHAR2(20) := 'John'; v_salary NUMBER := 1000; BEGIN INSERT INTO employees (name, salary) VALUES (v_name, v_salary); COMMIT; DBMS_OUTPUT.PUT_LINE('New employee added!'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
DECLARE @v_num FLOAT := 10; BEGIN DECLARE @i INT = 1; WHILE @i <= @v_num BEGIN DBMS_OUTPUT.PUT_LINE('Loop Iteration: ' + isnull(i, '')); END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(@@ERROR, '') + ' - ' + ISNULL(SQLERRM, '')); END;
DECLARE v_num NUMBER := 10; BEGIN FOR i IN 1..v_num LOOP DBMS_OUTPUT.PUT_LINE('Loop Iteration: ' || i); END LOOP; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLCODE || ' - ' || SQLERRM); END;
BEGIN *** oyee into the database INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (100, 'John Doe', 1000); *** g employee's salary UPDATE employees SET emp_salary = 1500 WHERE emp_id = 100; *** e from the database DELETE FROM employees WHERE emp_id = 101; *** cedure to generate a report CALL generate_report; -- Ha... SQLINES DEMO *** BEGIN *** query that will fail SELECT * FROM non_existent_table; IF @@FETCH_STATUS <> 0 BEGIN *** gracefully DBMS_OUTPUT.PUT_LINE('No data found in the table.'); END END; END;
BEGIN -- Insert a new employee into the database INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (100, 'John Doe', 1000); -- Update an existing employee's salary UPDATE employees SET emp_salary = 1500 WHERE emp_id = 100; -- Delete an employee from the database DELETE FROM employees WHERE emp_id = 101; -- Call a stored procedure to generate a report CALL generate_report; -- Handle an error BEGIN -- Try to execute a query that will fail SELECT * FROM non_existent_table; EXCEPTION WHEN NO_DATA_FOUND THEN -- Handle the error gracefully DBMS_OUTPUT.PUT_LINE('No data found in the table.'); END; END;
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL DROP PROCEDURE my_procedure; GO CREATE PROCEDURE my_procedure( @param1 IN AS NUMBER, param2 IN OUT VARCHAR2 ) IS v_temp dbo.VARCHAR2(100); BEGIN SELECT @v_temp = name FROM my_table WHERE id = param1; set @param2 = isnull(v_temp, '') + '_updated'; UPDATE my_table SET name = param2 WHERE id = param1; EXCEPTION WHEN OTHERS THEN RAISE; END;
CREATE OR REPLACE PROCEDURE my_procedure( param1 IN NUMBER, param2 IN OUT VARCHAR2 ) IS v_temp VARCHAR2(100); BEGIN SELECT name INTO v_temp FROM my_table WHERE id = param1; param2 := v_temp || '_updated'; UPDATE my_table SET name = param2 WHERE id = param1; EXCEPTION WHEN OTHERS THEN RAISE; END;
DECLARE @v_emp_id FLOAT := 100; v_emp_name dbo.VARCHAR2(100); v_salary NUMBER; BEGIN SELECT @v_emp_name = ename, @v_salary = sal FROM emp WHERE empno = @v_emp_id; IF v_emp_name IS NULL BEGIN RAISERROR('-20001 Employee does not exist', 16, 1); END UPDATE emp SET sal = v_salary * 1.10 WHERE empno = @v_emp_id; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 No data found for Employee ID: ' + isnull(@v_emp_id, ''), 16, 1); END WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_emp_id NUMBER := 100; v_emp_name VARCHAR2(100); v_salary NUMBER; BEGIN SELECT ename, sal INTO v_emp_name, v_salary FROM emp WHERE empno = v_emp_id; IF v_emp_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee does not exist'); END IF; UPDATE emp SET sal = v_salary * 1.10 WHERE empno = v_emp_id; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'No data found for Employee ID: ' || v_emp_id); WHEN OTHERS THEN ROLLBACK; RAISE; END;
CREATE PROCEDURE get_customer_details(@customer_id IN AS NUMBER) AS l_customer_name dbo.VARCHAR2(100); BEGIN SELECT @l_customer_name = name FROM customers WHERE customer_id = customer_id; DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(l_customer_name, '')); END; BEGIN dbo.get_customer_details(10); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Customer not found.'); END END;
CREATE PROCEDURE get_customer_details(customer_id IN NUMBER) AS l_customer_name VARCHAR2(100); BEGIN SELECT name INTO l_customer_name FROM customers WHERE customer_id = customer_id; DBMS_OUTPUT.PUT_LINE('Customer Name: ' || l_customer_name); END; BEGIN get_customer_details(10); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Customer not found.'); END;
DECLARE @v_salary DECIMAL(10, 2); BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = 1; IF @v_salary IS NULL BEGIN RAISERROR('-20001 Salary not found for employee ID 1', 16, 1); END UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = 1; COMMIT; END;
DECLARE v_salary NUMBER(10, 2); BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 1; IF v_salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Salary not found for employee ID 1'); END IF; UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = 1; COMMIT; END;
DECLARE @v_empno FLOAT; v_sal NUMBER; BEGIN SELECT @v_empno = empno, @v_sal = sal FROM emp WHERE deptno = 10; IF v_sal > 10000 BEGIN UPDATE emp SET sal = v_sal + 1000 WHERE empno = @v_empno; END ELSE BEGIN RAISERROR('-20001 Salary too low', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN NULL; END END;
DECLARE v_empno NUMBER; v_sal NUMBER; BEGIN SELECT empno, sal INTO v_empno, v_sal FROM emp WHERE deptno = 10; IF v_sal > 10000 THEN UPDATE emp SET sal = v_sal + 1000 WHERE empno = v_empno; ELSE RAISE_APPLICATION_ERROR(-20001, 'Salary too low'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END;
CREATE PROCEDURE get_customer_orders (@p_customer_id IN AS NUMBER) AS CURSOR c_orders IS SELECT * FROM orders WHERE customer_id = p_customer_id; BEGIN DECLARE r_order CURSOR FOR c_orders LOOP DBMS_OUTPUT.PUT_LINE(isnull(@r_order_order_id, '') + ' - ' + isnull(@r_order_product_id, '')); FETCH r_order INTO; END; CLOSE r_order; DEALLOCATE r_order; END; CREATE FUNCTION get_order_total (@p_order_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN RETURN ( SELECT SUM(quantity * unit_price) FROM order_items WHERE order_id = p_order_id ); END; BEGIN DECLARE @v_total FLOAT; BEGIN SET @v_total = dbo.get_order_total(100); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No order found with ID ' + 100); END END; DBMS_OUTPUT.PUT_LINE('Total: ' + isnull(@v_total, '')); END;
CREATE PROCEDURE get_customer_orders (p_customer_id IN NUMBER) AS CURSOR c_orders IS SELECT * FROM orders WHERE customer_id = p_customer_id; BEGIN FOR r_order IN c_orders LOOP DBMS_OUTPUT.PUT_LINE(r_order.order_id || ' - ' || r_order.product_id); END LOOP; END; CREATE FUNCTION get_order_total (p_order_id IN NUMBER) RETURN NUMBER AS BEGIN RETURN ( SELECT SUM(quantity * unit_price) FROM order_items WHERE order_id = p_order_id ); END; BEGIN DECLARE v_total NUMBER; BEGIN v_total := get_order_total(100); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No order found with ID ' || 100); END; DBMS_OUTPUT.PUT_LINE('Total: ' || v_total); END;
CREATE PROCEDURE Emp_Details( @p_emp_id IN AS NUMBER, p_emp_name OUT VARCHAR2, p_emp_salary OUT NUMBER ) IS emp_record EMP%ROWTYPE; BEGIN SELECT @p_emp_name = emp_name, @p_emp_salary = emp_salary FROM emp WHERE emp_id = p_emp_id; IF SQL%NOTFOUND BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END Emp_Details;
PROCEDURE Emp_Details( p_emp_id IN NUMBER, p_emp_name OUT VARCHAR2, p_emp_salary OUT NUMBER ) IS emp_record EMP%ROWTYPE; BEGIN SELECT emp_name, emp_salary INTO p_emp_name, p_emp_salary FROM emp WHERE emp_id = p_emp_id; IF SQL%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; END Emp_Details;
CREATE PROCEDURE AddProduct( @product_name IN AS VARCHAR2, product_price IN NUMBER, product_quantity IN NUMBER) IS BEGIN INSERT INTO products( product_name, product_price, product_quantity) VALUES( product_name, product_price, product_quantity); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; CREATE FUNCTION GetProductPrice( @product_name IN VARCHAR2DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS product_price NUMBER; BEGIN SELECT @product_price = product_price FROM products WHERE product_name = product_name; RETURN product_price; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @product_name VARCHAR(30) := 'Apple'; product_price NUMBER; BEGIN SET @product_price = dbo.GetProductPrice(@product_name); IF product_price IS NULL BEGIN RAISERROR('-20001 Product not found', 16, 1); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Product price: ' + isnull(product_price, '')); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE PROCEDURE AddProduct( product_name IN VARCHAR2, product_price IN NUMBER, product_quantity IN NUMBER) IS BEGIN INSERT INTO products( product_name, product_price, product_quantity) VALUES( product_name, product_price, product_quantity); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; CREATE FUNCTION GetProductPrice( product_name IN VARCHAR2) RETURN NUMBER IS product_price NUMBER; BEGIN SELECT product_price INTO product_price FROM products WHERE product_name = product_name; RETURN product_price; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE product_name VARCHAR2(30) := 'Apple'; product_price NUMBER; BEGIN product_price := GetProductPrice(product_name); IF product_price IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Product not found'); ELSE DBMS_OUTPUT.PUT_LINE('Product price: ' || product_price); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL DROP PROCEDURE get_employee_salary; GO CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER, OUT salary OUT NUMBER) AS BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN set @salary = NULL; END END;
CREATE OR REPLACE PROCEDURE get_employee_salary(emp_id IN NUMBER, OUT salary OUT NUMBER) AS BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN salary := NULL; END;
CREATE PROCEDURE InsertCustomer ( @customer_name IN AS VARCHAR2, customer_email IN VARCHAR2, customer_phone IN VARCHAR2, customer_address IN VARCHAR2 ) AS BEGIN *** customer table INSERT INTO customers (customer_name, customer_email, customer_phone, customer_address) VALUES (customer_name, customer_email, customer_phone, customer_address); END; / CREATE FUNCTION GetCustomerByEmail ( @customer_email IN VARCHAR2 DECLARE ) RETURN BEGIN customers%ROWTYPE RETURN NULL; END; AS customer CURSOR; BEGIN *** retrieve customer data SELECT * FROM customers WHERE customer_email = customer_email; *** ow from the cursor FETCH customer INTO @customer; -- Cl... SQLINES DEMO *** CLOSE customer; *** er data RETURN customer; END; / DECLARE @customer_email VARCHAR(255) := 'customer@email.com'; BEGIN *** to get customer data SELECT * FROM TABLE(dbo.GetCustomerByEmail(@customer_email)); *** u can assign the returned value to a variable SET @customer = dbo.GetCustomerByEmail(@customer_email); END; / BEGIN -- St... SQLINES DEMO *** BEGIN TRANSACTION; *** ata UPDATE customers SET customer_name = 'New Customer Name' WHERE @customer_email = 'customer@email.com'; -- Co... SQLINES DEMO *** COMMIT; EXCEPTION WHEN OTHERS THEN *** ges if an error occurs ROLLBACK; -- Ha... SQLINES DEMO *** RAISERROR('-20000 Error occurred while updating customer data.', 16, 1); END; /
CREATE PROCEDURE InsertCustomer ( customer_name IN VARCHAR2, customer_email IN VARCHAR2, customer_phone IN VARCHAR2, customer_address IN VARCHAR2 ) AS BEGIN -- Insert data into customer table INSERT INTO customers (customer_name, customer_email, customer_phone, customer_address) VALUES (customer_name, customer_email, customer_phone, customer_address); END; / CREATE FUNCTION GetCustomerByEmail ( customer_email IN VARCHAR2 ) RETURN customers%ROWTYPE AS customer CURSOR; BEGIN -- Open a cursor to retrieve customer data OPEN customer FOR SELECT * FROM customers WHERE customer_email = customer_email; -- Fetch the first row from the cursor FETCH customer INTO customer; -- Close the cursor CLOSE customer; -- Return the customer data RETURN customer; END; / DECLARE customer_email VARCHAR2(255) := 'customer@email.com'; BEGIN -- Call the function to get customer data SELECT * FROM TABLE(GetCustomerByEmail(customer_email)); -- Alternatively, you can assign the returned value to a variable customer := GetCustomerByEmail(customer_email); END; / BEGIN -- Start a transaction BEGIN TRANSACTION; -- Update customer data UPDATE customers SET customer_name = 'New Customer Name' WHERE customer_email = 'customer@email.com'; -- Commit the changes COMMIT; EXCEPTION WHEN OTHERS THEN -- Rollback the changes if an error occurs ROLLBACK; -- Handle the error RAISE_APPLICATION_ERROR(-20000, 'Error occurred while updating customer data.'); END; /
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); BEGIN SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_id = 10; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with ID 10'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); BEGIN SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_id = 10; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with ID 10'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END;
CREATE PROCEDURE get_customer_orders( @p_custid IN AS NUMBER, p_orders OUT SYS_REFCURSOR ) AS BEGIN SELECT orderid, orderdate, totalamount FROM orders WHERE custid = p_custid; END; -- Ex... SQLINES DEMO *** DECLARE @l_orders SYS_REFCURSOR; BEGIN dbo.get_customer_orders(p_custid => 100, p_orders => @l_orders); WHILE 1=1 BEGIN FETCH l_orders INTO @l_orderid, @l_orderdate, @l_totalamount; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(isnull(l_orderid, '') + ' ' + isnull(l_orderdate, '') + ' ' + isnull(l_totalamount, '')); END; CLOSE l_orders; END;
CREATE PROCEDURE get_customer_orders( p_custid IN NUMBER, p_orders OUT SYS_REFCURSOR ) AS BEGIN OPEN p_orders FOR SELECT orderid, orderdate, totalamount FROM orders WHERE custid = p_custid; END; -- Example usage DECLARE l_orders SYS_REFCURSOR; BEGIN get_customer_orders(p_custid => 100, p_orders => l_orders); LOOP FETCH l_orders INTO l_orderid, l_orderdate, l_totalamount; EXIT WHEN l_orders%NOTFOUND; DBMS_OUTPUT.PUT_LINE(l_orderid || ' ' || l_orderdate || ' ' || l_totalamount); END LOOP; CLOSE l_orders; END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE emp_id = 10; IF v_salary > 10000 BEGIN UPDATE employees SET salary = v_salary * 1.1 WHERE emp_id = @v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; ELSE IF v_salary <= 10000 BEGIN UPDATE employees SET salary = v_salary * 0.9 WHERE emp_id = @v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE emp_id = 10; IF v_salary > 10000 THEN BEGIN UPDATE employees SET salary = v_salary * 1.1 WHERE emp_id = v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; ELSIF v_salary <= 10000 THEN BEGIN UPDATE employees SET salary = v_salary * 0.9 WHERE emp_id = v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END IF; END;
DECLARE @v_empno float := 100; v_ename dbo.varchar2(20) := 'John Doe'; v_deptno SET @number = 20; BEGIN *** oyee INSERT INTO employees (empno, ename, deptno) VALUES (@v_empno, v_ename, v_deptno); *** e's department UPDATE employees SET deptno = 30 WHERE empno = @v_empno; -- De... SQLINES DEMO *** DELETE FROM employees WHERE empno = @v_empno; *** ees from a department SELECT * FROM employees WHERE deptno = v_deptno; -- Ha... SQLINES DEMO *** BEGIN *** a duplicate employee INSERT INTO employees (empno, ename, deptno) VALUES (@v_empno, v_ename, v_deptno); EXCEPTION WHEN dup_val_on_index THEN -- Er... SQLINES DEMO *** DBMS_OUTPUT.put_line('Error: Duplicate employee number.'); END; END;
DECLARE v_empno number := 100; v_ename varchar2(20) := 'John Doe'; v_deptno number := 20; BEGIN -- Insert a new employee INSERT INTO employees (empno, ename, deptno) VALUES (v_empno, v_ename, v_deptno); -- Update an employee's department UPDATE employees SET deptno = 30 WHERE empno = v_empno; -- Delete an employee DELETE FROM employees WHERE empno = v_empno; -- Select all employees from a department SELECT * FROM employees WHERE deptno = v_deptno; -- Handle an error BEGIN -- Attempt to insert a duplicate employee INSERT INTO employees (empno, ename, deptno) VALUES (v_empno, v_ename, v_deptno); EXCEPTION WHEN dup_val_on_index THEN -- Error handling code DBMS_OUTPUT.put_line('Error: Duplicate employee number.'); END; END;
IF OBJECT_ID('calculate_bonus', 'FN') IS NOT NULL DROP FUNCTION calculate_bonus; GO CREATE FUNCTION calculate_bonus(@salary FLOAT) RETURNS FLOAT AS BEGIN DECLARE @bonus FLOAT; IF @salary > 50000 BEGIN SET @bonus = @salary * 0.10; END ELSE BEGIN SET @bonus = @salary * 0.05; END RETURN @bonus; END; DECLARE @employee_salary FLOAT; employee_bonus NUMBER; BEGIN SET @employee_salary = 45000; SET @employee_bonus = dbo.calculate_bonus(@employee_salary); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(@employee_salary, '')); DBMS_OUTPUT.PUT_LINE('Employee Bonus: ' + isnull(employee_bonus, '')); EXCEPTION WHEN others THEN DBMS_OUTPUT.PUT_LINE('An error occurred:' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE FUNCTION calculate_bonus(salary NUMBER) RETURN NUMBER IS bonus NUMBER; BEGIN IF salary > 50000 THEN bonus := salary * 0.10; ELSE bonus := salary * 0.05; END IF; RETURN bonus; END; DECLARE employee_salary NUMBER; employee_bonus NUMBER; BEGIN employee_salary := 45000; employee_bonus := calculate_bonus(employee_salary); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee_salary); DBMS_OUTPUT.PUT_LINE('Employee Bonus: ' || employee_bonus); EXCEPTION WHEN others THEN DBMS_OUTPUT.PUT_LINE('An error occurred:' || SQLERRM); END;
CREATE PROCEDURE MyProcedure(@IN Param1 AS NUMBER, INOUT Param2 VARCHAR2) AS BEGIN *** Param1 and Param2 Set @Param2 = Isnull(Param2, '') + ' - Modified'; EXCEPTION WHEN OTHERS THEN *** that occur DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END; BEGIN -- De... SQLINES DEMO *** Var1 NUMBER; Var2 VARCHAR(50); -- Ca... SQLINES DEMO *** dbo.MyProcedure(10, Var2); -- Pr... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE('Var1: ' + Isnull(Var1, '')); DBMS_OUTPUT.PUT_LINE('Var2: ' + Isnull(Var2, '')); END;
CREATE PROCEDURE MyProcedure(IN Param1 NUMBER, INOUT Param2 VARCHAR2) AS BEGIN -- Do something with Param1 and Param2 Param2 := Param2 || ' - Modified'; EXCEPTION WHEN OTHERS THEN -- Handle any errors that occur DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END; BEGIN -- Declare variables Var1 NUMBER; Var2 VARCHAR(50); -- Call the procedure MyProcedure(10, Var2); -- Print the results DBMS_OUTPUT.PUT_LINE('Var1: ' || Var1); DBMS_OUTPUT.PUT_LINE('Var2: ' || Var2); END;
IF OBJECT_ID('update_employees', 'P') IS NOT NULL DROP PROCEDURE update_employees; GO CREATE PROCEDURE update_employees(@emp_id IN AS NUMBER, new_salary IN NUMBER) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee with ID ' + isnull(emp_id, '') + ' not found', 16, 1); END WHEN OTHERS THEN RAISE; END;
CREATE OR REPLACE PROCEDURE update_employees(emp_id IN NUMBER, new_salary IN NUMBER) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' not found'); WHEN OTHERS THEN RAISE; END;
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL DROP PROCEDURE sp_update_employee; GO CREATE PROCEDURE sp_update_employee ( @p_employee_id IN AS NUMBER, p_salary IN NUMBER, p_dept_id IN NUMBER ) IS BEGIN UPDATE employees SET salary = p_salary, dept_id = p_dept_id WHERE employee_id = p_employee_id; COMMIT; END; /
CREATE OR REPLACE PROCEDURE sp_update_employee ( p_employee_id IN NUMBER, p_salary IN NUMBER, p_dept_id IN NUMBER ) IS BEGIN UPDATE employees SET salary = p_salary, dept_id = p_dept_id WHERE employee_id = p_employee_id; COMMIT; END; /
CREATE PROCEDURE get_employee_info( @employee_id IN AS NUMBER, OUT OUT_employee_name VARCHAR2, OUT OUT_salary NUMBER) AS BEGIN SELECT @OUT_employee_name = name, @OUT_salary = salary FROM employees WHERE id = employee_id; IF @@FETCH_STATUS <> 0 BEGIN NULL; END END;
CREATE PROCEDURE get_employee_info( employee_id IN NUMBER, OUT OUT_employee_name VARCHAR2, OUT OUT_salary NUMBER) AS BEGIN SELECT name, salary INTO OUT_employee_name, OUT_salary FROM employees WHERE id = employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END;