sqlserver
stringlengths
59
30k
plsql
stringlengths
54
4.67k
IF OBJECT_ID('find_employees', 'P') IS NOT NULL DROP PROCEDURE find_employees; GO CREATE PROCEDURE find_employees( @p_dept_id IN AS NUMBER, OUT emp_cursor OUT SYS_REFCURSOR ) IS BEGIN SELECT employee_id, first_name, last_name FROM employees WHERE department_id = p_dept_id; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN DECLARE @dept_id FLOAT := 20; emp_cur SYS_REFCURSOR; emp_id NUMBER; emp_first_name dbo.VARCHAR2(20); emp_last_name dbo.VARCHAR2(20); BEGIN dbo.find_employees(@dept_id, emp_cur); WHILE 1=1 BEGIN FETCH emp_cur INTO @emp_id, @emp_first_name, @emp_last_name; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE( isnull(emp_id, '') + ' ' + isnull(emp_first_name, '') + ' ' + isnull(emp_last_name, '') ); END; CLOSE emp_cur; EXCEPTION WHEN OTHERS THEN CLOSE emp_cur; ROLLBACK; RAISE; END; END;
CREATE OR REPLACE PROCEDURE find_employees( p_dept_id IN NUMBER, OUT emp_cursor OUT SYS_REFCURSOR ) IS BEGIN OPEN emp_cursor FOR SELECT employee_id, first_name, last_name FROM employees WHERE department_id = p_dept_id; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN DECLARE dept_id NUMBER := 20; emp_cur SYS_REFCURSOR; emp_id NUMBER; emp_first_name VARCHAR2(20); emp_last_name VARCHAR2(20); BEGIN find_employees(dept_id, emp_cur); LOOP FETCH emp_cur INTO emp_id, emp_first_name, emp_last_name; EXIT WHEN emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE( emp_id || ' ' || emp_first_name || ' ' || emp_last_name ); END LOOP; CLOSE emp_cur; EXCEPTION WHEN OTHERS THEN CLOSE emp_cur; ROLLBACK; RAISE; END; END;
CREATE PROCEDURE compute_average(@IN department_name AS VARCHAR2) AS total_salary NUMBER; avg_salary NUMBER; BEGIN SELECT @total_salary = SUM(salary) FROM employees WHERE department_name = department_name; IF total_salary IS NOT NULL BEGIN SELECT @avg_salary = COUNT(*) FROM employees WHERE department_name = department_name; set @avg_salary = total_salary / avg_salary; END ELSE BEGIN NULL; END END;
PROCEDURE compute_average(IN department_name VARCHAR2) AS total_salary NUMBER; avg_salary NUMBER; BEGIN SELECT SUM(salary) INTO total_salary FROM employees WHERE department_name = department_name; IF total_salary IS NOT NULL THEN SELECT COUNT(*) INTO avg_salary FROM employees WHERE department_name = department_name; avg_salary := total_salary / avg_salary; ELSE NULL; END IF; END;
CREATE PROCEDURE get_employee_details( @employee_id IN AS NUMBER, employee_name OUT VARCHAR2, employee_salary OUT NUMBER ) IS BEGIN SELECT @employee_name = ename, @employee_salary = sal FROM emp WHERE empno = employee_id; END;
CREATE PROCEDURE get_employee_details( employee_id IN NUMBER, employee_name OUT VARCHAR2, employee_salary OUT NUMBER ) IS BEGIN SELECT ename, sal INTO employee_name, employee_salary FROM emp WHERE empno = employee_id; END;
WITH EmployeeData AS ( SELECT * FROM Employee WHERE Salary > 10000 ) SELECT * FROM EmployeeData WHERE Department = 'Marketing'; UPDATE Employee SET Salary = Salary * 1.1 WHERE Department = 'Marketing'; DECLARE @emp_id INTEGER; BEGIN SELECT TOP 1 @emp_id = EmployeeID FROM Employee ORDER BY Salary DESC; IF @emp_id IS NOT NULL BEGIN DELETE FROM Employee WHERE EmployeeID = @emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee with highest salary deleted successfully'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employee found to delete'); END END;
WITH EmployeeData AS ( SELECT * FROM Employee WHERE Salary > 10000 ) SELECT * FROM EmployeeData WHERE Department = 'Marketing'; UPDATE Employee SET Salary = Salary * 1.1 WHERE Department = 'Marketing'; DECLARE emp_id INTEGER; BEGIN SELECT EmployeeID INTO emp_id FROM Employee ORDER BY Salary DESC LIMIT 1; IF emp_id IS NOT NULL THEN DELETE FROM Employee WHERE EmployeeID = emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee with highest salary deleted successfully'); ELSE DBMS_OUTPUT.PUT_LINE('No employee found to delete'); END IF; END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@employee_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS employee_name dbo.VARCHAR2(50); BEGIN SELECT @employee_name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = employee_id; RETURN employee_name; IF @@fetch_status <> 0 BEGIN RETURN NULL; END END;
CREATE OR REPLACE FUNCTION get_employee_name(employee_id IN NUMBER) RETURN VARCHAR2 IS employee_name VARCHAR2(50); BEGIN SELECT first_name || ' ' || last_name INTO employee_name FROM employees WHERE employee_id = employee_id; RETURN employee_name; EXCEPTION WHEN no_data_found THEN RETURN NULL; END;
DECLARE @v_emp_id FLOAT := 100; BEGIN DECLARE @i INT = 1; WHILE @i <= 10 BEGIN INSERT INTO employees (emp_id, name, salary) VALUES (@v_emp_id, 'Employee ' + isnull(i, ''), i * 1000); SET @v_emp_id = @v_emp_id + 1; END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + isnull(sqlerrm, '')); END;
DECLARE v_emp_id NUMBER := 100; BEGIN FOR i IN 1..10 LOOP INSERT INTO employees (emp_id, name, salary) VALUES (v_emp_id, 'Employee ' || i, i * 1000); v_emp_id := v_emp_id + 1; END LOOP; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || sqlerrm); END;
*** with input and output parameters IF OBJECT_ID('get_employee_info', 'P') IS NOT NULL DROP PROCEDURE get_employee_info; GO CREATE PROCEDURE get_employee_info( @p_emp_id IN AS NUMBER, p_emp_name OUT VARCHAR2, p_emp_salary OUT NUMBER ) IS BEGIN SELECT @p_emp_name = ename, @p_emp_salary = sal FROM emp WHERE empno = p_emp_id; END; *** urns a cursor IF OBJECT_ID('get_employees_by_dept', 'FN') IS NOT NULL DROP FUNCTION get_employees_by_dept; GO CREATE FUNCTION get_employees_by_dept( @p_dept_id IN NUMBER DECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; IS l_emp_cur SYS_REFCURSOR; BEGIN SELECT * FROM emp WHERE deptno = p_dept_id; RETURN l_emp_cur; END; *** h a subquery SELECT empno, ename, (SELECT dname FROM dept WHERE deptno = emp.deptno) AS dept_name FROM emp; *** THEN-ELSE) DECLARE @l_emp_count FLOAT; BEGIN SELECT @l_emp_count = COUNT(*) FROM emp; IF @l_emp_count > 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employees found'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employees found'); END END; *** XCEPTION) BEGIN *** ght raise an exception INSERT INTO emp (empno, ename, sal) VALUES (999, 'New Employee', 10000); COMMIT; EXCEPTION WHEN OTHERS THEN *** ion DBMS_OUTPUT.PUT_LINE('An error occurred'); ROLLBACK; END;
-- Stored procedure with input and output parameters CREATE OR REPLACE PROCEDURE get_employee_info( p_emp_id IN NUMBER, p_emp_name OUT VARCHAR2, p_emp_salary OUT NUMBER ) IS BEGIN SELECT ename, sal INTO p_emp_name, p_emp_salary FROM emp WHERE empno = p_emp_id; END; -- Function that returns a cursor CREATE OR REPLACE FUNCTION get_employees_by_dept( p_dept_id IN NUMBER ) RETURN SYS_REFCURSOR IS l_emp_cur SYS_REFCURSOR; BEGIN OPEN l_emp_cur FOR SELECT * FROM emp WHERE deptno = p_dept_id; RETURN l_emp_cur; END; -- SQL statement with a subquery SELECT empno, ename, (SELECT dname FROM dept WHERE deptno = emp.deptno) AS dept_name FROM emp; -- Control flow (IF-THEN-ELSE) DECLARE l_emp_count NUMBER; BEGIN SELECT COUNT(*) INTO l_emp_count FROM emp; IF l_emp_count > 0 THEN DBMS_OUTPUT.PUT_LINE('Employees found'); ELSE DBMS_OUTPUT.PUT_LINE('No employees found'); END IF; END; -- Error handling (EXCEPTION) BEGIN -- Some code that might raise an exception INSERT INTO emp (empno, ename, sal) VALUES (999, 'New Employee', 10000); COMMIT; EXCEPTION WHEN OTHERS THEN -- Handle the exception DBMS_OUTPUT.PUT_LINE('An error occurred'); ROLLBACK; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id; IF @salary IS NULL BEGIN RAISERROR('-20000 Employee not found', 16, 1); END RETURN @salary; END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id; COMMIT; END; DECLARE @emp_id FLOAT := 100; salary NUMBER; BEGIN SET @salary = dbo.get_employee_salary(@emp_id); dbo.UPDATE_EMPLOYEE_SALARY(@emp_id, salary + 1000); END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'Employee not found'); END IF; RETURN salary; END; CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; DECLARE emp_id NUMBER := 100; salary NUMBER; BEGIN salary := get_employee_salary(emp_id); UPDATE_EMPLOYEE_SALARY(emp_id, salary + 1000); END;
*** rt data into a table CREATE PROCEDURE insert_data ( @IN p_id AS NUMBER, IN p_name VARCHAR2, IN p_age NUMBER ) AS BEGIN INSERT INTO employee (id, name, age) VALUES (p_id, p_name, p_age); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN *** value on index error WHEN TOO_MANY_ROWS THEN *** ows error WHEN OTHERS THEN -- Ha... SQLINES DEMO *** END; / *** tenate two strings CREATE FUNCTION concat_strings ( @IN p_str1 VARCHAR2DECLARE , IN BEGIN p_str2 return null; end; VARCHAR2 ) RETURN VARCHAR2 AS BEGIN RETURN isnull(p_str1, '') + ' ' + isnull(p_str2, ''); END; / *** select data from a table SELECT * FROM employee WHERE age > 30; *** truct to check if a value is greater than 10 IF age > 10 BEGIN -- Do... SQLINES DEMO *** END ELSE BEGIN -- Do... SQLINES DEMO *** END *** enario to catch an invalid value error BEGIN *** y throw an invalid value error EXCEPTION WHEN invalid_value THEN *** lue error END;
-- Procedure to insert data into a table CREATE PROCEDURE insert_data ( IN p_id NUMBER, IN p_name VARCHAR2, IN p_age NUMBER ) AS BEGIN INSERT INTO employee (id, name, age) VALUES (p_id, p_name, p_age); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN -- Handle duplicate value on index error WHEN TOO_MANY_ROWS THEN -- Handle too many rows error WHEN OTHERS THEN -- Handle other errors END; / -- Function to concatenate two strings CREATE FUNCTION concat_strings ( IN p_str1 VARCHAR2, IN p_str2 VARCHAR2 ) RETURN VARCHAR2 AS BEGIN RETURN p_str1 || ' ' || p_str2; END; / -- SQL statement to select data from a table SELECT * FROM employee WHERE age > 30; -- Control flow construct to check if a value is greater than 10 IF age > 10 THEN -- Do something ELSE -- Do something else END IF; -- Error handling scenario to catch an invalid value error BEGIN -- Some code that may throw an invalid value error EXCEPTION WHEN invalid_value THEN -- Handle invalid value error END;
CREATE TABLE Employees ( EmployeeID FLOAT NOT NULL, FirstName VARCHAR(50) NOT NULL, LastName VARCHAR(50), Salary FLOAT, Department VARCHAR(50) ); CREATE PROCEDURE HireEmployee ( @EmpID IN AS NUMBER, FName IN VARCHAR2, LName IN VARCHAR2, Sal IN NUMBER, Dept IN VARCHAR2 ) AS BEGIN INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary, Department) VALUES (EmpID, FName, LName, Sal, Dept); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; CREATE FUNCTION GetEmployeeName ( @EmpID IN NUMBER DECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; AS EmpName dbo.VARCHAR2(50); BEGIN SELECT @EmpName = ISNULL(FirstName, '') + ' ' + ISNULL(LastName, '') FROM Employees WHERE EmployeeID = EmpID; RETURN EmpName; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @EmpID FLOAT := 1001; FName dbo.VARCHAR2(50) := 'John'; LName dbo.VARCHAR2(50) := 'Doe'; Sal SET @NUMBER = 50000; Dept dbo.VARCHAR2(50) := 'Sales'; BEGIN dbo.HireEmployee(@EmpID, FName, LName, Sal, Dept); DBMS_OUTPUT.Put_Line('Employee ' + ISNULL(dbo.GetEmployeeName(@EmpID), '') + ' hired successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.Put_Line('Error hiring employee.'); END;
CREATE TABLE Employees ( EmployeeID NUMBER NOT NULL, FirstName VARCHAR2(50) NOT NULL, LastName VARCHAR2(50), Salary NUMBER, Department VARCHAR2(50) ); CREATE PROCEDURE HireEmployee ( EmpID IN NUMBER, FName IN VARCHAR2, LName IN VARCHAR2, Sal IN NUMBER, Dept IN VARCHAR2 ) AS BEGIN INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary, Department) VALUES (EmpID, FName, LName, Sal, Dept); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; CREATE FUNCTION GetEmployeeName ( EmpID IN NUMBER ) RETURN VARCHAR2 AS EmpName VARCHAR2(50); BEGIN SELECT FirstName || ' ' || LastName INTO EmpName FROM Employees WHERE EmployeeID = EmpID; RETURN EmpName; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE EmpID NUMBER := 1001; FName VARCHAR2(50) := 'John'; LName VARCHAR2(50) := 'Doe'; Sal NUMBER := 50000; Dept VARCHAR2(50) := 'Sales'; BEGIN HireEmployee(EmpID, FName, LName, Sal, Dept); DBMS_OUTPUT.Put_Line('Employee ' || GetEmployeeName(EmpID) || ' hired successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.Put_Line('Error hiring employee.'); END;
DECLARE @v_empno FLOAT; v_ename dbo.VARCHAR2(20); BEGIN SELECT @v_ename = ename FROM emp WHERE empno = 7788; IF v_ename IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE emp SET sal = sal * 1.10 WHERE ename = v_ename; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END; /
DECLARE v_empno NUMBER; v_ename VARCHAR2(20); BEGIN SELECT ename INTO v_ename FROM emp WHERE empno = 7788; IF v_ename IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE emp SET sal = sal * 1.10 WHERE ename = v_ename; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; /
IF OBJECT_ID('increment_order_status', 'P') IS NOT NULL DROP PROCEDURE increment_order_status; GO CREATE PROCEDURE increment_order_status ( @order_id IN AS NUMBER ) IS BEGIN UPDATE orders SET status = status + 1 WHERE order_id = order_id; IF status = 5 BEGIN RAISERROR('-20000 Order has been completed', 16, 1); END END;
CREATE OR REPLACE PROCEDURE increment_order_status ( order_id IN NUMBER ) IS BEGIN UPDATE orders SET status = status + 1 WHERE order_id = order_id; IF status = 5 THEN RAISE_APPLICATION_ERROR(-20000, 'Order has been completed'); END IF; END;
DECLARE @x FLOAT := 10; y SET @NUMBER = 5; BEGIN IF @x > y BEGIN DBMS_OUTPUT.PUT_LINE('x is greater than y'); END ELSE IF @x = y BEGIN DBMS_OUTPUT.PUT_LINE('x is equal to y'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('x is less than y'); END BEGIN SET @x = @x + 1; SET @y = y + 1; DBMS_OUTPUT.PUT_LINE('x + 1 = ' + isnull(@x, '')); DBMS_OUTPUT.PUT_LINE('y + 1 = ' + isnull(y, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred!'); END; END;
DECLARE x NUMBER := 10; y NUMBER := 5; BEGIN IF x > y THEN DBMS_OUTPUT.PUT_LINE('x is greater than y'); ELSIF x = y THEN DBMS_OUTPUT.PUT_LINE('x is equal to y'); ELSE DBMS_OUTPUT.PUT_LINE('x is less than y'); END IF; BEGIN x := x + 1; y := y + 1; DBMS_OUTPUT.PUT_LINE('x + 1 = ' || x); DBMS_OUTPUT.PUT_LINE('y + 1 = ' || y); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred!'); END; END;
DECLARE @v_id FLOAT; v_name dbo.VARCHAR2(50); BEGIN SELECT @v_id = id, @v_name = name FROM employees WHERE id = 10; IF @v_id IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE employees SET salary = salary * 1.1 WHERE id = @v_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_id NUMBER; v_name VARCHAR2(50); BEGIN SELECT id, name INTO v_id, v_name FROM employees WHERE id = 10; IF v_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE employees SET salary = salary * 1.1 WHERE id = v_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
CREATE PROCEDURE get_employee_data(@emp_id IN AS NUMBER, OUT emp_name OUT VARCHAR2, OUT emp_salary OUT NUMBER) AS BEGIN SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, ''), @emp_salary = salary FROM employees WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee data not found', 16, 1); END END;
CREATE PROCEDURE get_employee_data(emp_id IN NUMBER, OUT emp_name OUT VARCHAR2, OUT emp_salary OUT NUMBER) AS BEGIN SELECT first_name || ' ' || last_name, salary INTO emp_name, emp_salary FROM employees WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee data not found'); END;
BEGIN DECLARE @c_id FLOAT; c_name dbo.VARCHAR2(20); c_location dbo.VARCHAR2(20); c_revenue NUMBER; BEGIN DECLARE rec CURSOR FOR SELECT * FROM customers; OPEN rec; FETCH rec INTO; WHILE @@FETCH_STATUS=0 BEGIN SET @c_id = @rec_customer_id; SET @c_name = @rec_customer_name; SET @c_location = @rec_customer_location; SET @c_revenue = @rec_customer_revenue; DBMS_OUTPUT.PUT_LINE(isnull(@c_id, '') + ' ' + isnull(c_name, '') + ' ' + isnull(c_location, '') + ' ' + isnull(c_revenue, '')); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No customers found'); END END; END;
BEGIN DECLARE c_id NUMBER; c_name VARCHAR2(20); c_location VARCHAR2(20); c_revenue NUMBER; BEGIN FOR rec IN (SELECT * FROM customers) LOOP c_id := rec.customer_id; c_name := rec.customer_name; c_location := rec.customer_location; c_revenue := rec.customer_revenue; DBMS_OUTPUT.PUT_LINE(c_id || ' ' || c_name || ' ' || c_location || ' ' || c_revenue); END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No customers found'); END; END;
DECLARE @v_empno SMALLINT; v_salary ROW_NUMBER(8,2); BEGIN SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 7369; IF v_salary > 10000 BEGIN RAISERROR('-20001 Salary is too high', 16, 1); END UPDATE emp SET salary = salary * 1.10 WHERE empno = @v_empno; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;
DECLARE v_empno NUMBER(4); v_salary NUMBER(8,2); BEGIN SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 7369; IF v_salary > 10000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary is too high'); END IF; UPDATE emp SET salary = salary * 1.10 WHERE empno = v_empno; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;
DECLARE @v_max_id FLOAT; BEGIN SELECT @v_max_id = MAX(id) FROM users; IF @v_max_id IS NULL BEGIN RAISE VALUE_ERROR; END INSERT INTO users (id, name) VALUES (@v_max_id + 1, 'John Doe'); COMMIT; END;
DECLARE v_max_id NUMBER; BEGIN SELECT MAX(id) INTO v_max_id FROM users; IF v_max_id IS NULL THEN RAISE VALUE_ERROR; END IF; INSERT INTO users (id, name) VALUES (v_max_id + 1, 'John Doe'); COMMIT; END;
DECLARE @v_salary DECIMAL(10,2); v_employee_id ROW_NUMBER(6); BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = v_employee_id; IF @v_salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE employees SET salary = @v_salary + 1000 WHERE employee_id = v_employee_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 No data found for employee', 16, 1); END END;
DECLARE v_salary NUMBER(10,2); v_employee_id NUMBER(6); BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = v_employee_id; IF v_salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = v_employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'No data found for employee'); END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); v_salary NUMBER; v_bonus NUMBER; BEGIN SELECT @v_emp_id = employee_id, @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '') AS employee_name, @v_salary = salary, @v_bonus = bonus FROM employees WHERE employee_id = 100; IF v_bonus IS NULL BEGIN SET @v_bonus = 0; END UPDATE employees SET salary = v_salary + (v_bonus * 0.1), bonus = NULL WHERE employee_id = @v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while updating the employee: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); v_salary NUMBER; v_bonus NUMBER; BEGIN SELECT employee_id, first_name || ' ' || last_name AS employee_name, salary, bonus INTO v_emp_id, v_emp_name, v_salary, v_bonus FROM employees WHERE employee_id = 100; IF v_bonus IS NULL THEN v_bonus := 0; END IF; UPDATE employees SET salary = v_salary + (v_bonus * 0.1), bonus = NULL WHERE employee_id = v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while updating the employee: ' || SQLERRM); END;
CREATE FUNCTION GetCustomerInfo(@CustomerID FLOAT) RETURNS SYS_REFCURSOR AS BEGIN DECLARE @Customers_Ref CURSOR FOR SELECT RETURN NULL; END; CustomerID, CustomerName, Phone, Email FROM Customers WHERE CustomerID = CustomerID; BEGIN OPEN Customers_Ref; RETURN Customers_Ref; END; BEGIN DECLARE @Customers_Cursor SYS_REFCURSOR; BEGIN SET @Customers_Cursor = dbo.GetCustomerInfo(10); WHILE 1=1 BEGIN FETCH Customers_Cursor INTO @CustomerID, @CustomerName, @Phone, @Email; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(ISNULL(CustomerID, '') + ' - ' + ISNULL(CustomerName, '') + ' - ' + Isnull(Phone, '') + ' - ' + Isnull(Email, '')); END; CLOSE Customers_Cursor; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END; END;
CREATE FUNCTION GetCustomerInfo(CustomerID NUMBER) RETURN SYS_REFCURSOR IS Customers_Ref CURSOR FOR SELECT CustomerID, CustomerName, Phone, Email FROM Customers WHERE CustomerID = CustomerID; BEGIN OPEN Customers_Ref; RETURN Customers_Ref; END; BEGIN DECLARE Customers_Cursor SYS_REFCURSOR; BEGIN Customers_Cursor := GetCustomerInfo(10); LOOP FETCH Customers_Cursor INTO CustomerID, CustomerName, Phone, Email; EXIT WHEN Customers_Cursor%NOTFOUND; DBMS_OUTPUT.PUT_LINE(CustomerID || ' - ' || CustomerName || ' - ' || Phone || ' - ' || Email); END LOOP; CLOSE Customers_Cursor; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; END;
CREATE PROCEDURE GetCustomerOrders( @IN customer_id AS NUMBER) RETURN SYS_REFCURSOR IS orders_cursor SYS_REFCURSOR; BEGIN SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END;
CREATE PROCEDURE GetCustomerOrders( IN customer_id NUMBER) RETURN SYS_REFCURSOR IS orders_cursor SYS_REFCURSOR; BEGIN OPEN orders_cursor FOR SELECT * FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END;
DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = 1; IF @v_salary > 10000 BEGIN RAISERROR('-20101 Salary too high', 16, 1); END UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = 1; COMMIT; 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(-20101, 'Salary too high'); END IF; UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = 1; COMMIT; END;
DECLARE @v_empno FLOAT; BEGIN SELECT @v_empno = empno FROM emp WHERE ename = 'SCOTT'; IF @v_empno IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno; END;
DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE ename = 'SCOTT'; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno; END;
IF OBJECT_ID('get_employee_info', 'FN') IS NOT NULL DROP FUNCTION get_employee_info; GO CREATE FUNCTION get_employee_info(@emp_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; IS CURSOR c_emp_info IS SELECT * FROM employees WHERE employee_id = emp_id; BEGIN OPEN c_emp_info; RETURN c_emp_info; END;
CREATE OR REPLACE FUNCTION get_employee_info(emp_id IN NUMBER) RETURN SYS_REFCURSOR IS CURSOR c_emp_info IS SELECT * FROM employees WHERE employee_id = emp_id; BEGIN OPEN c_emp_info; RETURN c_emp_info; END;
IF OBJECT_ID('pro_update_employee', 'P') IS NOT NULL DROP PROCEDURE pro_update_employee; GO CREATE PROCEDURE pro_update_employee( @p_emp_id IN AS NUMBER, p_new_salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = p_new_salary WHERE employee_id = p_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred while updating employee salary.'); ROLLBACK; END;
CREATE OR REPLACE PROCEDURE pro_update_employee( p_emp_id IN NUMBER, p_new_salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = p_new_salary WHERE employee_id = p_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred while updating employee salary.'); ROLLBACK; END;
DECLARE @v_emp_id FLOAT; BEGIN BEGIN SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'King'; IF @@FETCH_STATUS <> 0 BEGIN raiserror('-20001 No such employee found', 16, 1); END END; UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; raiserror('-20002 An unexpected error occurred', 16, 1); END;
DECLARE v_emp_id NUMBER; BEGIN BEGIN SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'King'; EXCEPTION WHEN NO_DATA_FOUND THEN raise_application_error(-20001, 'No such employee found'); END; UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; raise_application_error(-20002, 'An unexpected error occurred'); END;
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL DROP PROCEDURE sample_proc; GO CREATE PROCEDURE sample_proc(@IN param1 AS NUMBER, IN param2 VARCHAR2) AS BEGIN UPDATE table1 SET col1 = param1 WHERE col2 = param2; IF @@ERROR != 0 BEGIN RAISERROR('-20001 Error while updating table1', 16, 1); END COMMIT; END sample_proc;
CREATE OR REPLACE PROCEDURE sample_proc(IN param1 NUMBER, IN param2 VARCHAR2) AS BEGIN UPDATE table1 SET col1 = param1 WHERE col2 = param2; IF SQLCODE != 0 THEN RAISE_APPLICATION_ERROR(-20001, 'Error while updating table1'); END IF; COMMIT; END sample_proc;
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, salary DECIMAL(10,2) DEFAULT 0, dept_id SMALLINT REFERENCES departments(dept_id) ); CREATE TABLE departments ( dept_id SMALLINT PRIMARY KEY, dept_name VARCHAR(50) NOT NULL ); CREATE PROCEDURE add_employee ( @p_id IN AS NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER, p_dept_id IN NUMBER ) AS BEGIN INSERT INTO employees (id, name, salary, dept_id) VALUES (p_id, p_name, p_salary, p_dept_id); END; CREATE FUNCTION get_employee_salary ( @p_id IN NUMBER DECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS BEGIN RETURN (SELECT salary FROM employees WHERE id = p_id); END; BEGIN *** oyee CALL add_employee(10, 'John Doe', 10000, 20); *** an employee DECLARE @l_salary FLOAT; BEGIN SET @l_salary = dbo.get_employee_salary(10); DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@l_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; END;
CREATE TABLE employees ( id NUMBER(6) PRIMARY KEY, name VARCHAR2(50) NOT NULL, salary NUMBER(10,2) DEFAULT 0, dept_id NUMBER(4) REFERENCES departments(dept_id) ); CREATE TABLE departments ( dept_id NUMBER(4) PRIMARY KEY, dept_name VARCHAR2(50) NOT NULL ); CREATE PROCEDURE add_employee ( p_id IN NUMBER, p_name IN VARCHAR2, p_salary IN NUMBER, p_dept_id IN NUMBER ) AS BEGIN INSERT INTO employees (id, name, salary, dept_id) VALUES (p_id, p_name, p_salary, p_dept_id); END; CREATE FUNCTION get_employee_salary ( p_id IN NUMBER ) RETURN NUMBER AS BEGIN RETURN (SELECT salary FROM employees WHERE id = p_id); END; BEGIN -- Insert a new employee CALL add_employee(10, 'John Doe', 10000, 20); -- Get the salary of an employee DECLARE l_salary NUMBER; BEGIN l_salary := get_employee_salary(10); DBMS_OUTPUT.PUT_LINE('Employee salary: ' || l_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; END;
BEGIN *** into the table INSERT INTO my_table (id, name, age) VALUES (1, 'John', 25); *** he table UPDATE my_table SET name = 'Jane' WHERE id = 1; *** the table DELETE FROM my_table WHERE id = 1; *** the table SELECT * FROM my_table; *** variable DECLARE @my_var INT; SET @my_var = 10; *** w construct (IF statement) IF @my_var > 0 BEGIN -- Do... SQLINES DEMO *** END *** g (exception block) BEGIN *** me code EXCEPTION *** that occur END; END;
BEGIN -- Insert a new row into the table INSERT INTO my_table (id, name, age) VALUES (1, 'John', 25); -- Update a row in the table UPDATE my_table SET name = 'Jane' WHERE id = 1; -- Delete a row from the table DELETE FROM my_table WHERE id = 1; -- Select rows from the table SELECT * FROM my_table; -- Declare and use a variable DECLARE my_var INT; SET my_var = 10; -- Use a control flow construct (IF statement) IF my_var > 0 THEN -- Do something END IF; -- Use error handling (exception block) BEGIN -- Try to execute some code EXCEPTION -- Handle any errors that occur END; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@p_name IN AS VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello ' + isnull(p_name, '') + '!'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE PROCEDURE my_proc(p_name IN VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello ' || p_name || '!'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @v_result FLOAT; v_error_msg dbo.VARCHAR2(500); BEGIN SELECT @v_result = SUM(salary) FROM employees; IF @v_result IS NULL BEGIN SET @v_error_msg = 'No employee data found'; RAISERROR(-20001, v_error_msg, 16, 1); END END; /
DECLARE v_result NUMBER; v_error_msg VARCHAR2(500); BEGIN SELECT SUM(salary) INTO v_result FROM employees; IF v_result IS NULL THEN v_error_msg := 'No employee data found'; RAISE_APPLICATION_ERROR(-20001, v_error_msg); END IF; END; /
IF OBJECT_ID('sp_insert_employee', 'P') IS NOT NULL DROP PROCEDURE sp_insert_employee; GO CREATE PROCEDURE sp_insert_employee ( @p_emp_id IN AS NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_salary IN NUMBER ) IS BEGIN INSERT INTO employees ( emp_id, first_name, last_name, email, salary ) VALUES ( p_emp_id, p_first_name, p_last_name, p_email, p_salary ); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN dbo.sp_insert_employee( p_emp_id => 1001, p_first_name => 'John', p_last_name => 'Doe', p_email => 'john.doe@company.com', p_salary => 50000 ); END;
CREATE OR REPLACE PROCEDURE sp_insert_employee ( p_emp_id IN NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_salary IN NUMBER ) IS BEGIN INSERT INTO employees ( emp_id, first_name, last_name, email, salary ) VALUES ( p_emp_id, p_first_name, p_last_name, p_email, p_salary ); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; BEGIN sp_insert_employee( p_emp_id => 1001, p_first_name => 'John', p_last_name => 'Doe', p_email => 'john.doe@company.com', p_salary => 50000 ); END;
DECLARE @v_emp_id FLOAT := 100; v_salary NUMBER; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = @v_emp_id; IF v_salary IS NULL BEGIN RAISE NO_DATA_FOUND; END ELSE IF v_salary < 1000 BEGIN UPDATE employees SET salary = 1000 WHERE employee_id = @v_emp_id; END END;
DECLARE v_emp_id NUMBER := 100; v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id; IF v_salary IS NULL THEN RAISE NO_DATA_FOUND; ELSIF v_salary < 1000 THEN UPDATE employees SET salary = 1000 WHERE employee_id = v_emp_id; END IF; END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary( @emp_id IN AS NUMBER, new_salary IN NUMBER ) AS *** iables salary_diff NUMBER; BEGIN *** ary difference set @salary_diff = new_salary - (SELECT salary FROM employees WHERE employee_id = emp_id); *** ee's salary UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; *** ange INSERT INTO salary_changes (employee_id, salary_diff, change_date) VALUES (emp_id, salary_diff, GETDATE()); -- Co... SQLINES DEMO *** COMMIT; END;
CREATE OR REPLACE PROCEDURE update_employee_salary( emp_id IN NUMBER, new_salary IN NUMBER ) AS -- Declare local variables salary_diff NUMBER; BEGIN -- Calculate the salary difference salary_diff := new_salary - (SELECT salary FROM employees WHERE employee_id = emp_id); -- Update the employee's salary UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; -- Log the salary change INSERT INTO salary_changes (employee_id, salary_diff, change_date) VALUES (emp_id, salary_diff, SYSDATE); -- Commit the changes COMMIT; END;
DECLARE @v_code FLOAT; BEGIN SELECT @v_code = COUNT(*) FROM employees WHERE salary > 50000; IF @v_code > 0 BEGIN DBMS_OUTPUT.PUT_LINE('Found ' + isnull(@v_code, '') + ' employees with salary greater than 50000.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No employees found with salary greater than 50000.'); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found in the table.'); END END; /
DECLARE v_code NUMBER; BEGIN SELECT COUNT(*) INTO v_code FROM employees WHERE salary > 50000; IF v_code > 0 THEN DBMS_OUTPUT.PUT_LINE('Found ' || v_code || ' employees with salary greater than 50000.'); ELSE DBMS_OUTPUT.PUT_LINE('No employees found with salary greater than 50000.'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found in the table.'); END; /
IF OBJECT_ID('ExampleProc', 'P') IS NOT NULL DROP PROCEDURE ExampleProc; GO CREATE PROCEDURE ExampleProc ( @p_in_val IN AS VARCHAR2, p_out_val OUT VARCHAR2 ) BEGIN -- Er... SQLINES DEMO *** BEGIN *** may raise an error SELECT * FROM non_existing_table; -- Ha... SQLINES DEMO *** IF @@FETCH_STATUS <> 0 BEGIN set @p_out_val = 'No data found'; END END; -- Co... SQLINES DEMO *** IF p_in_val IS NULL BEGIN set @p_out_val = 'NULL'; END ELSE BEGIN set @p_out_val = p_in_val; END -- SQ... SQLINES DEMO *** INSERT INTO ExampleTable (col1, col2) VALUES (p_in_val, p_out_val); END; DECLARE @v_in_val VARCHAR(20) := 'Value In'; v_out_val dbo.VARCHAR2(20); BEGIN *** dure dbo.ExampleProc(@v_in_val, v_out_val); -- Di... SQLINES DEMO *** DBMS_OUTPUT.PUT_LINE('Value Out: ' + isnull(v_out_val, '')); END;
CREATE OR REPLACE PROCEDURE ExampleProc ( p_in_val IN VARCHAR2, p_out_val OUT VARCHAR2 ) BEGIN -- Error handling BEGIN -- Some action that may raise an error SELECT * FROM non_existing_table; -- Handle error EXCEPTION WHEN NO_DATA_FOUND THEN p_out_val := 'No data found'; END; -- Control flow IF p_in_val IS NULL THEN p_out_val := 'NULL'; ELSE p_out_val := p_in_val; END IF; -- SQL statement INSERT INTO ExampleTable (col1, col2) VALUES (p_in_val, p_out_val); END; DECLARE v_in_val VARCHAR2(20) := 'Value In'; v_out_val VARCHAR2(20); BEGIN -- Call stored procedure ExampleProc(v_in_val, v_out_val); -- Display output DBMS_OUTPUT.PUT_LINE('Value Out: ' || v_out_val); END;
CREATE PROCEDURE Check_Salary( @emp_id IN AS number, salary IN out number ) AS BEGIN SELECT @salary = salary FROM employees WHERE emp_id = emp_id; IF salary < 10000 BEGIN RAISERROR('-20001 Salary must be atleast 10000.00', 16, 1); END END Check_Salary; BEGIN dbo.Check_Salary(100, :salary_var); DBMS_OUTPUT.PUT_LINE('Salary is :' + ISNULL(CONVERT(VARCHAR, salary_var), '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END;
CREATE PROCEDURE Check_Salary( emp_id IN number, salary IN out number ) AS BEGIN SELECT salary INTO salary FROM employees WHERE emp_id = emp_id; IF salary < 10000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary must be atleast 10000.00'); END IF; END Check_Salary; BEGIN Check_Salary(100, :salary_var); DBMS_OUTPUT.PUT_LINE('Salary is :' || TO_CHAR(salary_var)); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM); END;
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL DROP FUNCTION get_customer_orders; GO CREATE FUNCTION get_customer_orders(@customer_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; IS orders_cursor SYS_REFCURSOR; BEGIN SELECT order_id, order_date, total_price FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END get_customer_orders;
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_price FROM orders WHERE customer_id = customer_id; RETURN orders_cursor; END get_customer_orders;
DECLARE @l_emp_id DECIMAL(19,4) /* Use -meta option employee.employee_id%TYPE */; BEGIN DELETE FROM employee WHERE department_id = 20; INSERT INTO log (event_time, event_type, employee_id) VALUES (GETDATE(), 'DELETE', :l_emp_id); END;
DECLARE l_emp_id employee.employee_id%TYPE; BEGIN DELETE FROM employee WHERE department_id = 20; INSERT INTO log (event_time, event_type, employee_id) VALUES (SYSTIMESTAMP, 'DELETE', :l_emp_id); END;
DECLARE @l_max_sal FLOAT; BEGIN SELECT @l_max_sal = MAX(salary) FROM employees; IF @l_max_sal > 10000 BEGIN INSERT INTO bonuses (employee_id, bonus_amount, bonus_date) VALUES ((SELECT employee_id FROM employees WHERE salary = @l_max_sal), 500, GETDATE()); END ELSE IF @l_max_sal > 5000 BEGIN UPDATE employees SET salary = salary * (1 + (@l_max_sal - 5000) / 20000) WHERE salary > 5000; END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE l_max_sal NUMBER; BEGIN SELECT MAX(salary) INTO l_max_sal FROM employees; IF l_max_sal > 10000 THEN INSERT INTO bonuses (employee_id, bonus_amount, bonus_date) VALUES ((SELECT employee_id FROM employees WHERE salary = l_max_sal), 500, SYSDATE); ELSIF l_max_sal > 5000 THEN UPDATE employees SET salary = salary * (1 + (l_max_sal - 5000) / 20000) WHERE salary > 5000; END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); v_salary NUMBER; BEGIN SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = 1; IF v_salary > 10000 BEGIN RAISERROR('-20001 Salary is too high', 16, 1); END UPDATE employees SET salary = salary * 1.1 WHERE emp_id = @v_emp_id; END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); v_salary NUMBER; BEGIN SELECT emp_id, emp_name, salary INTO v_emp_id, v_emp_name, v_salary FROM employees WHERE emp_id = 1; IF v_salary > 10000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary is too high'); END IF; UPDATE employees SET salary = salary * 1.1 WHERE emp_id = v_emp_id; END;
DECLARE @v_cursor SYS_REFCURSOR; BEGIN SELECT * FROM table_name WHERE field_name = 'value'; WHILE 1=1 BEGIN FETCH v_cursor INTO @v_fieldname; IF @@FETCH_STATUS <> 0 BREAK; *** ata END; CLOSE v_cursor; EXIT; IF @@FETCH_STATUS <> 0 BEGIN NULL; END *** und scenario WHEN OTHERS THEN RAISE; -- Ha... SQLINES DEMO *** END;
DECLARE v_cursor SYS_REFCURSOR; BEGIN OPEN v_cursor FOR SELECT * FROM table_name WHERE field_name = 'value'; LOOP FETCH v_cursor INTO v_fieldname; EXIT WHEN v_cursor%NOTFOUND; -- Code to process data END LOOP; CLOSE v_cursor; EXIT; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; -- Handle no data found scenario WHEN OTHERS THEN RAISE; -- Handle other errors END;
CREATE TABLE employee(id FLOAT, name VARCHAR(20), salary FLOAT); INSERT INTO employee VALUES(1, 'John', 1000); INSERT INTO employee VALUES(2, 'Mary', 2000); SELECT * FROM employee; UPDATE employee SET salary = salary * 1.1 WHERE name = 'John'; COMMIT; BEGIN DELETE FROM employee WHERE name = 'Mary'; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN NULL; END END;
CREATE TABLE employee(id NUMBER, name VARCHAR2(20), salary NUMBER); INSERT INTO employee VALUES(1, 'John', 1000); INSERT INTO employee VALUES(2, 'Mary', 2000); SELECT * FROM employee; UPDATE employee SET salary = salary * 1.1 WHERE name = 'John'; COMMIT; BEGIN DELETE FROM employee WHERE name = 'Mary'; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END;
DECLARE @v_name VARCHAR(50) := 'John Doe'; v_balance SET @NUMBER = 10000; BEGIN UPDATE accounts SET balance = balance + v_balance WHERE name = @v_name; IF @@ROWCOUNT = 0 BEGIN INSERT INTO accounts (name, balance) VALUES (@v_name, v_balance); END COMMIT; END;
DECLARE v_name VARCHAR2(50) := 'John Doe'; v_balance NUMBER := 10000; BEGIN UPDATE accounts SET balance = balance + v_balance WHERE name = v_name; IF SQL%ROWCOUNT = 0 THEN INSERT INTO accounts (name, balance) VALUES (v_name, v_balance); END IF; COMMIT; END;
IF OBJECT_ID('get_emp_salary', 'FN') IS NOT NULL DROP FUNCTION get_emp_salary; GO CREATE FUNCTION get_emp_salary(@emp_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id; IF @salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END RETURN @salary; END; DECLARE @emp_salary FLOAT; BEGIN SET @emp_salary = dbo.get_emp_salary(100); DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END;
CREATE OR REPLACE FUNCTION get_emp_salary(emp_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; RETURN salary; END; DECLARE emp_salary NUMBER; BEGIN emp_salary := get_emp_salary(100); DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END;
DECLARE @v_empno FLOAT := 2341; v_empname dbo.VARCHAR2(25); BEGIN SELECT @v_empname = ename FROM emp WHERE empno = @v_empno; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_empname, '')); IF v_empname IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_empno NUMBER := 2341; v_empname VARCHAR2(25); BEGIN SELECT ename INTO v_empname FROM emp WHERE empno = v_empno; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_empname); IF v_empname IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
BEGIN -- De... SQLINES DEMO *** v_empno NUMBER; v_ename dbo.VARCHAR2(30); v_sal NUMBER; *** rmation SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal FROM emp WHERE empno = 7839; *** nformation UPDATE emp SET sal = v_sal + 1000 WHERE empno = v_empno; -- Co... SQLINES DEMO *** COMMIT; *** lary DBMS_OUTPUT.PUT_LINE('Updated salary: ' + isnull(v_sal + 1000, '')); -- Ha... SQLINES DEMO *** EXCEPTION WHEN OTHERS THEN -- Ro... SQLINES DEMO *** ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
BEGIN -- Declare variables v_empno NUMBER; v_ename VARCHAR2(30); v_sal NUMBER; -- Get employee information SELECT empno, ename, sal INTO v_empno, v_ename, v_sal FROM emp WHERE empno = 7839; -- Update employee information UPDATE emp SET sal = v_sal + 1000 WHERE empno = v_empno; -- Commit changes COMMIT; -- Output updated salary DBMS_OUTPUT.PUT_LINE('Updated salary: ' || v_sal + 1000); -- Handle errors EXCEPTION WHEN OTHERS THEN -- Rollback changes ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @emp_id SMALLINT; emp_name dbo.VARCHAR2(30); salary ROW_NUMBER(8, 2); BEGIN SELECT @emp_id = employee_id, @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, ''), @salary = salary FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, salary), '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END END;
DECLARE emp_id NUMBER(4); emp_name VARCHAR2(30); salary NUMBER(8, 2); BEGIN SELECT employee_id, first_name || ' ' || last_name, salary INTO emp_id, emp_name, salary FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(salary)); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END;
IF OBJECT_ID('get_employee_info', 'FN') IS NOT NULL DROP FUNCTION get_employee_info; GO CREATE FUNCTION get_employee_info(@emp_id FLOAT) RETURNS SYS_REFCURSOR AS BEGIN DECLARE @emp_ref CURSOR; SELECT * FROM employees WHERE employee_id = @emp_id; RETURN @emp_ref; END; GO DECLARE @emp_info SYS_REFCURSOR; BEGIN SET @emp_info = dbo.get_employee_info(10); WHILE 1=1 BEGIN FETCH emp_info INTO @emp_id, @first_name, @last_name; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, '')); DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(first_name, '')); DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, '')); END; CLOSE emp_info; END; /
CREATE OR REPLACE FUNCTION get_employee_info(emp_id NUMBER) RETURN SYS_REFCURSOR IS emp_ref CURSOR; BEGIN OPEN emp_ref FOR SELECT * FROM employees WHERE employee_id = emp_id; RETURN emp_ref; END; / DECLARE emp_info SYS_REFCURSOR; BEGIN emp_info := get_employee_info(10); LOOP FETCH emp_info INTO emp_id, first_name, last_name; EXIT WHEN emp_info%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id); DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name); DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name); END LOOP; CLOSE emp_info; END; /
IF OBJECT_ID('sp_customer_update', 'P') IS NOT NULL DROP PROCEDURE sp_customer_update; GO CREATE PROCEDURE sp_customer_update( @p_customer_id IN AS NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2 ) AS BEGIN UPDATE customers SET first_name = p_first_name, last_name = p_last_name WHERE customer_id = p_customer_id; COMMIT; END; BEGIN DECLARE @v_customer_id FLOAT := 1; v_first_name dbo.VARCHAR2(20) := 'John'; v_last_name dbo.VARCHAR2(20) := 'Doe'; BEGIN dbo.sp_customer_update(p_customer_id, v_first_name, v_last_name); EXCEPTION WHEN OTHERS THEN RAISE; END; END;
CREATE OR REPLACE PROCEDURE sp_customer_update( p_customer_id IN NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2 ) AS BEGIN UPDATE customers SET first_name = p_first_name, last_name = p_last_name WHERE customer_id = p_customer_id; COMMIT; END; BEGIN DECLARE v_customer_id NUMBER := 1; v_first_name VARCHAR2(20) := 'John'; v_last_name VARCHAR2(20) := 'Doe'; BEGIN sp_customer_update(p_customer_id, v_first_name, v_last_name); EXCEPTION WHEN OTHERS THEN RAISE; END; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@employee_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE @employee_id = @employee_id; RETURN @salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; 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; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN RAISERROR('-20001 Duplicate employee ID', 16, 1); END; BEGIN DECLARE @employee_id FLOAT := 100; DECLARE @salary FLOAT; BEGIN SET @salary = dbo.get_employee_salary(@employee_id); IF @salary IS NULL BEGIN RAISERROR('-20002 Employee not found', 16, 1); END SET @salary = @salary + 1000; dbo.update_employee_salary(@employee_id, @salary); END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); ROLLBACK; END;
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = employee_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id NUMBER, new_salary NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN RAISE_APPLICATION_ERROR(-20001, 'Duplicate employee ID'); END; BEGIN DECLARE employee_id NUMBER := 100; DECLARE salary NUMBER; BEGIN salary := get_employee_salary(employee_id); IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20002, 'Employee not found'); END IF; salary := salary + 1000; update_employee_salary(employee_id, salary); END; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); ROLLBACK; END;
CREATE PROCEDURE sp_Example (@p_Input VARCHAR(4000)) AS v_Output VARCHAR2; BEGIN SET @v_Output = 'Hello, ' + ISNULL(p_Input, ''); EXCEPTION WHEN OTHERS THEN RAISERROR('-20000 Error occurred', 16, 1); END;
CREATE PROCEDURE sp_Example (p_Input VARCHAR2) AS v_Output VARCHAR2; BEGIN v_Output := 'Hello, ' || p_Input; EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR(-20000, 'Error occurred'); END;
DECLARE @name VARCHAR(50); salary ROW_NUMBER(10, 2); BEGIN SELECT @name = ename, @salary = sal FROM emp WHERE empno = 7934; IF salary > 10000 BEGIN DBMS_OUTPUT.PUT_LINE('High salary'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Low salary'); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
DECLARE name VARCHAR2(50); salary NUMBER(10, 2); BEGIN SELECT ename, sal INTO name, salary FROM emp WHERE empno = 7934; IF salary > 10000 THEN DBMS_OUTPUT.PUT_LINE('High salary'); ELSE DBMS_OUTPUT.PUT_LINE('Low salary'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;
CREATE FUNCTION get_employee_info (@employee_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; AS rc SYS_REFCURSOR; BEGIN SELECT * FROM employees WHERE employee_id = employee_id; RETURN rc; END; / DECLARE @emp_info SYS_REFCURSOR; employee_id NUMBER; BEGIN SET @employee_id = 100; SET @emp_info = dbo.get_employee_info(employee_id); WHILE 1=1 BEGIN IF @@FETCH_STATUS <> 0 BREAK; FETCH emp_info INTO @employee_id; dbms_output.put_line(employee_id); END; CLOSE emp_info; END; /
CREATE FUNCTION get_employee_info (employee_id IN NUMBER) RETURN SYS_REFCURSOR AS rc SYS_REFCURSOR; BEGIN OPEN rc FOR SELECT * FROM employees WHERE employee_id = employee_id; RETURN rc; END; / DECLARE emp_info SYS_REFCURSOR; employee_id NUMBER; BEGIN employee_id := 100; emp_info := get_employee_info(employee_id); LOOP EXIT WHEN emp_info%NOTFOUND; FETCH emp_info INTO employee_id; dbms_output.put_line(employee_id); END LOOP; CLOSE emp_info; END; /
CREATE FUNCTION random_word(@n FLOAT) RETURNS VARCHAR(4000) AS BEGIN DECLARE ( SELECT SUBSTRING( 'abcdefghijklmnopqrstuvwxyz', 1 + ROUND(DBMS_RANDOM.VALUE * 25, 0), 1 ) RETURN NULL; END; FROM DUAL CONNECT BY LEVEL <= n ); DECLARE @name VARCHAR(10); BEGIN SET @name = dbo.random_word(6); DBMS_OUTPUT.PUT_LINE('Random Name: ' + isnull(@name, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END;
CREATE FUNCTION random_word(n NUMBER) RETURN VARCHAR2 AS ( SELECT SUBSTR( 'abcdefghijklmnopqrstuvwxyz', 1 + ROUND(DBMS_RANDOM.VALUE * 25), 1 ) FROM DUAL CONNECT BY LEVEL <= n ); DECLARE name VARCHAR2(10); BEGIN name := random_word(6); DBMS_OUTPUT.PUT_LINE('Random Name: ' || name); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END;
IF OBJECT_ID('generate_employee_report', 'P') IS NOT NULL DROP PROCEDURE generate_employee_report; GO CREATE PROCEDURE generate_employee_report AS CURSOR emp_cur IS SELECT employee_id, last_name, first_name, salary FROM employees; BEGIN -- De... SQLINES DEMO *** TYPE emp_rec IS RECORD ( employee_id NUMBER, last_name dbo.VARCHAR2(25), first_name dbo.VARCHAR2(25), salary NUMBER ); emp emp_rec; -- Op... SQLINES DEMO *** OPEN emp_cur; *** ocess WHILE 1=1 BEGIN FETCH emp_cur INTO @emp; IF @@FETCH_STATUS <> 0 BREAK; DBMS_OUTPUT.PUT_LINE(isnull(emp.employee_id, '') + ' ' + isnull(emp.last_name, '') + ', ' + isnull(emp.first_name, '') + ' ' + isnull(emp.salary, '')); END; -- Cl... SQLINES DEMO *** CLOSE emp_cur; END; / -- Ca... SQLINES DEMO *** EXEC generate_employee_report;
CREATE OR REPLACE PROCEDURE generate_employee_report AS CURSOR emp_cur IS SELECT employee_id, last_name, first_name, salary FROM employees; BEGIN -- Declare variables TYPE emp_rec IS RECORD ( employee_id NUMBER, last_name VARCHAR2(25), first_name VARCHAR2(25), salary NUMBER ); emp emp_rec; -- Open cursor OPEN emp_cur; -- Fetch data and process LOOP FETCH emp_cur INTO emp; EXIT WHEN emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE(emp.employee_id || ' ' || emp.last_name || ', ' || emp.first_name || ' ' || emp.salary); END LOOP; -- Close cursor CLOSE emp_cur; END; / -- Call the procedure EXEC generate_employee_report;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; END;
IF OBJECT_ID('GetAverageSalary', 'FN') IS NOT NULL DROP FUNCTION GetAverageSalary; GO CREATE FUNCTION GetAverageSalary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS avg_salary NUMBER; BEGIN SELECT @avg_salary = AVG(salary) FROM emp WHERE emp_id = emp_id; RETURN avg_salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END;
CREATE OR REPLACE FUNCTION GetAverageSalary(emp_id IN NUMBER) RETURN NUMBER IS avg_salary NUMBER; BEGIN SELECT AVG(salary) INTO avg_salary FROM emp WHERE emp_id = emp_id; RETURN avg_salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END;
IF OBJECT_ID('get_product_details', 'FN') IS NOT NULL DROP FUNCTION get_product_details; GO CREATE FUNCTION get_product_details(@product_id IN NUMBERDECLARE ) RETURN BEGIN PRODUCT_DETAILS_TYPE RETURN NULL; END; AS BEGIN RETURN (SELECT * FROM products WHERE product_id = product_id); END; / IF OBJECT_ID('update_product_name', 'P') IS NOT NULL DROP PROCEDURE update_product_name; GO CREATE PROCEDURE update_product_name(@product_id IN AS NUMBER, new_name IN VARCHAR2) AS BEGIN UPDATE products SET product_name = new_name WHERE product_id = product_id; COMMIT; END; / BEGIN DECLARE @i INT = 1; WHILE @i <= 10 BEGIN INSERT INTO orders (order_date, customer_id, product_id, quantity) VALUES (GETDATE(), 1, i, 1); END; COMMIT; END; / DECLARE @error_code FLOAT; error_message dbo.VARCHAR2(200); BEGIN BEGIN RAISERROR('-20000 An unexpected error occurred.', 16, 1); EXCEPTION WHEN OTHERS THEN SET @error_code = @@ERROR; SET @error_message = SQLERRM; END; DBMS_OUTPUT.PUT_LINE('Error code: ' + isnull(@error_code, '')); DBMS_OUTPUT.PUT_LINE('Error message: ' + isnull(error_message, '')); END; /
CREATE OR REPLACE FUNCTION get_product_details(product_id IN NUMBER) RETURN PRODUCT_DETAILS_TYPE AS BEGIN RETURN (SELECT * FROM products WHERE product_id = product_id); END; / CREATE OR REPLACE PROCEDURE update_product_name(product_id IN NUMBER, new_name IN VARCHAR2) AS BEGIN UPDATE products SET product_name = new_name WHERE product_id = product_id; COMMIT; END; / BEGIN FOR i IN 1..10 LOOP INSERT INTO orders (order_date, customer_id, product_id, quantity) VALUES (SYSDATE, 1, i, 1); END LOOP; COMMIT; END; / DECLARE error_code NUMBER; error_message VARCHAR2(200); BEGIN BEGIN RAISE_APPLICATION_ERROR(-20000, 'An unexpected error occurred.'); EXCEPTION WHEN OTHERS THEN error_code := SQLCODE; error_message := SQLERRM; END; DBMS_OUTPUT.PUT_LINE('Error code: ' || error_code); DBMS_OUTPUT.PUT_LINE('Error message: ' || error_message); END; /
CREATE FUNCTION get_employee_name(@emp_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS emp_name dbo.VARCHAR2(50); BEGIN SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = emp_id; RETURN emp_name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END get_employee_name; BEGIN DECLARE @emp_name VARCHAR(50); SET @emp_name = dbo.get_employee_name(100); IF @emp_name IS NOT NULL BEGIN DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@emp_name, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
CREATE FUNCTION get_employee_name(emp_id IN NUMBER) RETURN VARCHAR2 IS emp_name VARCHAR2(50); BEGIN SELECT first_name || ' ' || last_name INTO emp_name FROM employees WHERE employee_id = emp_id; RETURN emp_name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END get_employee_name; BEGIN DECLARE emp_name VARCHAR2(50); emp_name := get_employee_name(100); IF emp_name IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name); ELSE DBMS_OUTPUT.PUT_LINE('Employee not found'); END IF; END;
DECLARE @v_empno FLOAT := 10; v_salary NUMBER; BEGIN SELECT @v_salary = salary FROM emp WHERE empno = @v_empno; IF v_salary > 10000 BEGIN UPDATE emp SET salary = salary * 1.1 WHERE empno = @v_empno; END ELSE IF v_salary < 5000 BEGIN RAISERROR('-20001 Salary too low', 16, 1); END COMMIT; END;
DECLARE v_empno NUMBER := 10; v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM emp WHERE empno = v_empno; IF v_salary > 10000 THEN UPDATE emp SET salary = salary * 1.1 WHERE empno = v_empno; ELSIF v_salary < 5000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary too low'); END IF; COMMIT; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc ( @in_param1 IN AS VARCHAR2, out_param2 OUT VARCHAR2 ) AS BEGIN SELECT @out_param2 = name FROM my_table WHERE id = in_param1; EXCEPTION WHEN OTHERS THEN ROLLBACK; raiserror('-20000 Error in my_proc: ' + ISNULL(SQLERRM, ''), 16, 1); END;
CREATE OR REPLACE PROCEDURE my_proc ( in_param1 IN VARCHAR2, out_param2 OUT VARCHAR2 ) AS BEGIN SELECT name INTO out_param2 FROM my_table WHERE id = in_param1; EXCEPTION WHEN OTHERS THEN ROLLBACK; raise_application_error(-20000, 'Error in my_proc: ' || SQLERRM); END;
BEGIN -- De... SQLINES DEMO *** v_emp_name dbo.VARCHAR2(20); v_salary ROW_NUMBER(10,2); *** data SELECT @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = 1; *** s greater than $50,000 IF v_salary > 50000 BEGIN *** by 10% UPDATE employees SET salary = salary * 1.10 WHERE emp_id = 1; -- Co... SQLINES DEMO *** COMMIT; END ELSE BEGIN *** sage RAISERROR('-20001 Salary is not greater than $50,000', 16, 1); END END;
BEGIN -- Declare variables v_emp_name VARCHAR2(20); v_salary NUMBER(10,2); -- Retrieve employee data SELECT emp_name, salary INTO v_emp_name, v_salary FROM employees WHERE emp_id = 1; -- Check if salary is greater than $50,000 IF v_salary > 50000 THEN -- Increment salary by 10% UPDATE employees SET salary = salary * 1.10 WHERE emp_id = 1; -- Commit changes COMMIT; ELSE -- Display error message RAISE_APPLICATION_ERROR(-20001, 'Salary is not greater than $50,000'); END IF; END;
DECLARE @v_empno FLOAT; BEGIN SELECT @v_empno = empno FROM emp WHERE last_name = 'Jones'; IF @v_empno IS NULL BEGIN INSERT INTO emp (empno, last_name) VALUES (9999, 'Jones'); END END;
DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE last_name = 'Jones'; IF v_empno IS NULL THEN INSERT INTO emp (empno, last_name) VALUES (9999, 'Jones'); END IF; END;
DECLARE @v_custno FLOAT; v_balance NUMBER; BEGIN SELECT @v_custno = custno, @v_balance = balance FROM customers WHERE custno = 100; IF v_balance < 0 BEGIN raiserror('-20000 Negative balance', 16, 1); END UPDATE customers SET balance = balance - 100 WHERE custno = @v_custno; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_custno NUMBER; v_balance NUMBER; BEGIN SELECT custno, balance INTO v_custno, v_balance FROM customers WHERE custno = 100; IF v_balance < 0 THEN raise_application_error(-20000, 'Negative balance'); END IF; UPDATE customers SET balance = balance - 100 WHERE custno = v_custno; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; / DECLARE @emp_id FLOAT := 100; new_salary SET @NUMBER = 15000; BEGIN dbo.UPDATE_EMPLOYEE_SALARY(@emp_id, new_salary); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20000 Error occurred while updating employee salary', 16, 1); END; /
CREATE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; / DECLARE emp_id NUMBER := 100; new_salary NUMBER := 15000; BEGIN UPDATE_EMPLOYEE_SALARY(emp_id, new_salary); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20000, 'Error occurred while updating employee salary'); END; /
IF OBJECT_ID('increment_employee_salary', 'P') IS NOT NULL DROP PROCEDURE increment_employee_salary; GO CREATE PROCEDURE increment_employee_salary( @p_empid IN AS NUMBER, p_inc IN NUMBER) IS v_old_salary NUMBER; BEGIN SELECT @v_old_salary = salary FROM employees WHERE empid = p_empid; UPDATE employees SET salary = v_old_salary + p_inc WHERE empid = p_empid; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
CREATE OR REPLACE PROCEDURE increment_employee_salary( p_empid IN NUMBER, p_inc IN NUMBER) IS v_old_salary NUMBER; BEGIN SELECT salary INTO v_old_salary FROM employees WHERE empid = p_empid; UPDATE employees SET salary = v_old_salary + p_inc WHERE empid = p_empid; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
DECLARE @v_empno FLOAT; BEGIN SELECT @v_empno = empno FROM emp WHERE ename = 'JOHN'; IF @v_empno IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE emp SET sal = sal * 1.1 WHERE empno = @v_empno; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20002 No data found for employee', 16, 1); END WHEN DUP_VAL_ON_INDEX THEN RAISERROR('-20003 Duplicate value on unique index', 16, 1); END;
DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE ename = 'JOHN'; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE emp SET sal = sal * 1.1 WHERE empno = v_empno; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20002, 'No data found for employee'); WHEN DUP_VAL_ON_INDEX THEN RAISE_APPLICATION_ERROR(-20003, 'Duplicate value on unique index'); END;
IF OBJECT_ID('generate_sales_report', 'P') IS NOT NULL DROP PROCEDURE generate_sales_report; GO CREATE PROCEDURE generate_sales_report( @start_date DATE, @end_date DATE, @sales_target FLOAT ) AS sales_total NUMBER; BEGIN SELECT @sales_total = SUM(sales_amount) FROM sales WHERE sale_date BETWEEN start_date AND end_date; IF sales_total < sales_target BEGIN RAISERROR('-20001 Sales target not met', 16, 1); END DBMS_OUTPUT.PUT_LINE('Sales report generated successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while generating the sales report'); END;
CREATE OR REPLACE PROCEDURE generate_sales_report( start_date DATE, end_date DATE, sales_target NUMBER ) AS sales_total NUMBER; BEGIN SELECT SUM(sales_amount) INTO sales_total FROM sales WHERE sale_date BETWEEN start_date AND end_date; IF sales_total < sales_target THEN RAISE_APPLICATION_ERROR(-20001, 'Sales target not met'); END IF; DBMS_OUTPUT.PUT_LINE('Sales report generated successfully'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while generating the sales report'); END;
DECLARE @v_empno FLOAT; v_ename dbo.VARCHAR2(20); BEGIN SELECT @v_ename = ename, @v_empno = empno FROM emp WHERE empno = 7369; IF @v_empno IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_ename, '') + ' not found'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_ename, '') + ' found with empno ' + isnull(@v_empno, '')); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found for employee with empno ' + isnull(@v_empno, '')); END END; /
DECLARE v_empno NUMBER; v_ename VARCHAR2(20); BEGIN SELECT ename, empno INTO v_ename, v_empno FROM emp WHERE empno = 7369; IF v_empno IS NULL THEN DBMS_OUTPUT.PUT_LINE('Employee ' || v_ename || ' not found'); ELSE DBMS_OUTPUT.PUT_LINE('Employee ' || v_ename || ' found with empno ' || v_empno); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found for employee with empno ' || v_empno); END; /
declare @v_emp_id float; begin select @v_emp_id = employee_id from employees where last_name = 'King'; if @v_emp_id is null begin raiserror('-20001 Employee not found', 16, 1); end update employees set salary = salary * 1.1 where employee_id = @v_emp_id; commit; end;
declare v_emp_id number; begin select employee_id into v_emp_id from employees where last_name = 'King'; if v_emp_id is null then raise_application_error(-20001, 'Employee not found'); end if; update employees set salary = salary * 1.1 where employee_id = v_emp_id; commit; end;
CREATE PROCEDURE UpdateEmployeeSalary ( @emp_id FLOAT, @new_salary FLOAT ) AS *** iables old_salary NUMBER; BEGIN -- Ge... SQLINES DEMO *** SELECT @old_salary = salary FROM employees WHERE employee_id = emp_id; -- Up... SQLINES DEMO *** UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; *** ry was updated successfully IF old_salary IS NULL OR old_salary <> new_salary BEGIN RAISERROR('-20001 Salary update failed.', 16, 1); END END UpdateEmployeeSalary;
CREATE PROCEDURE UpdateEmployeeSalary ( emp_id NUMBER, new_salary NUMBER ) IS -- Declare local variables old_salary NUMBER; BEGIN -- Get the old salary SELECT salary INTO old_salary FROM employees WHERE employee_id = emp_id; -- Update the salary UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; -- Check if the salary was updated successfully IF old_salary IS NULL OR old_salary <> new_salary THEN RAISE_APPLICATION_ERROR(-20001, 'Salary update failed.'); END IF; END UpdateEmployeeSalary;
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL DROP PROCEDURE get_employee_salary; GO CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER) AS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; IF salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, salary), '')); END END;
CREATE OR REPLACE PROCEDURE get_employee_salary(emp_id IN NUMBER) AS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); ELSE DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(salary)); END IF; END;
CREATE PROCEDURE get_product_details(@product_id IN AS NUMBER, OUT product_name VARCHAR2, OUT unit_price NUMBER) AS BEGIN SELECT @product_name = product_name, @unit_price = unit_price FROM products WHERE product_id = product_id; END; CREATE FUNCTION get_product_count() RETURNS FLOAT AS BEGIN DECLARE @cnt FLOAT; SELECT @cnt = COUNT(*) FROM products; RETURN @cnt; END; BEGIN DECLARE @product_name VARCHAR(100); unit_price NUMBER; BEGIN dbo.get_product_details(12345, @product_name, unit_price); DBMS_OUTPUT.PUT_LINE(isnull(@product_name, '') + ' costs ' + isnull(unit_price, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error getting product details.'); END; DECLARE @product_count FLOAT := dbo.get_product_count(); BEGIN DBMS_OUTPUT.PUT_LINE('There are ' + isnull(@product_count, '') + ' products in the database.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error getting product count.'); END; END;
CREATE PROCEDURE get_product_details(product_id IN NUMBER, OUT product_name VARCHAR2, OUT unit_price NUMBER) AS BEGIN SELECT product_name, unit_price INTO product_name, unit_price FROM products WHERE product_id = product_id; END; CREATE FUNCTION get_product_count() RETURN NUMBER AS cnt NUMBER; BEGIN SELECT COUNT(*) INTO cnt FROM products; RETURN cnt; END; BEGIN DECLARE product_name VARCHAR2(100); unit_price NUMBER; BEGIN get_product_details(12345, product_name, unit_price); DBMS_OUTPUT.PUT_LINE(product_name || ' costs ' || unit_price); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error getting product details.'); END; DECLARE product_count NUMBER := get_product_count(); BEGIN DBMS_OUTPUT.PUT_LINE('There are ' || product_count || ' products in the database.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error getting product count.'); END; END;
IF OBJECT_ID('inc_emp_salary', 'P') IS NOT NULL DROP PROCEDURE inc_emp_salary; GO CREATE PROCEDURE inc_emp_salary(@emp_id IN AS NUMBER, amt IN NUMBER) AS BEGIN *** e salary by the specified amount UPDATE emp SET salary = salary + amt WHERE empno = emp_id; *** n if the salary would be negative after the increment IF salary < 0 BEGIN RAISERROR('-20000 Salary cannot be negative', 16, 1); END END inc_emp_salary; BEGIN *** crement the salary of employee with ID 10 by 100 dbo.inc_emp_salary(10, 100); *** that may have occurred during the procedure execution EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE PROCEDURE inc_emp_salary(emp_id IN NUMBER, amt IN NUMBER) AS BEGIN -- Increment employee salary by the specified amount UPDATE emp SET salary = salary + amt WHERE empno = emp_id; -- Raise an exception if the salary would be negative after the increment IF salary < 0 THEN RAISE_APPLICATION_ERROR(-20000, 'Salary cannot be negative'); END IF; END inc_emp_salary; BEGIN -- Example usage: Increment the salary of employee with ID 10 by 100 inc_emp_salary(10, 100); -- Catch any errors that may have occurred during the procedure execution EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
DECLARE @v_deptno FLOAT:=20; v_salary NUMBER; BEGIN SELECT @v_salary = salary FROM emp WHERE deptno=@v_deptno; IF v_salary>2000 BEGIN UPDATE emp SET salary=v_salary*1.10 WHERE deptno=@v_deptno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salaries updated successfully for department '+isnull(@v_deptno, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No salaries updated for department '+isnull(@v_deptno, '')); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found in department '+isnull(@v_deptno, '')); END END;
DECLARE v_deptno NUMBER:=20; v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM emp WHERE deptno=v_deptno; IF v_salary>2000 THEN UPDATE emp SET salary=v_salary*1.10 WHERE deptno=v_deptno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salaries updated successfully for department '||v_deptno); ELSE DBMS_OUTPUT.PUT_LINE('No salaries updated for department '||v_deptno); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found in department '||v_deptno); END;
BEGIN BEGIN INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, PhoneNumber, HireDate, JobID, DepartmentID) VALUES (201, 'John', 'Doe', 'john.doe@mycompany.com', '555-1212', CONVERT(DATETIME, CONVERT(DATE, GETDATE())), 'IT_PROG', 20); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE @v_employeeID FLOAT; BEGIN SELECT @v_employeeID = EmployeeID FROM Employees WHERE LastName = 'Doe'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + ISNULL(@v_employeeID, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the last name Doe.'); END END; UPDATE Employees SET Salary = Salary * 1.10 WHERE JobID = 'IT_PROG'; DELETE FROM Employees WHERE EmployeeID = 201; END;
BEGIN BEGIN INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, PhoneNumber, HireDate, JobID, DepartmentID) VALUES (201, 'John', 'Doe', 'john.doe@mycompany.com', '555-1212', TRUNC(SYSDATE), 'IT_PROG', 20); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE v_employeeID NUMBER; BEGIN SELECT EmployeeID INTO v_employeeID FROM Employees WHERE LastName = 'Doe'; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employeeID); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the last name Doe.'); END; UPDATE Employees SET Salary = Salary * 1.10 WHERE JobID = 'IT_PROG'; DELETE FROM Employees WHERE EmployeeID = 201; END;
DECLARE @v_empno FLOAT := 10; BEGIN *** nformation SELECT @emp_record = * FROM employees WHERE employee_id = @v_empno; *** alary UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_empno; -- In... SQLINES DEMO *** INSERT INTO employees (employee_id, first_name, last_name, email) VALUES (20, 'John', 'Doe', 'john.doe@example.com'); -- De... SQLINES DEMO *** DELETE FROM employees WHERE employee_id = 30; *** call CALL my_stored_procedure(@v_empno); -- Fu... SQLINES DEMO *** SELECT dbo.my_function(@v_empno); -- Co... SQLINES DEMO *** IF @v_empno > 15 BEGIN DBMS_OUTPUT.put_line('Employee is eligible for promotion'); END ELSE BEGIN DBMS_OUTPUT.put_line('Employee is not eligible for promotion'); END -- Er... SQLINES DEMO *** BEGIN *** se an error RAISERROR('-20000 Custom error message', 16, 1); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('An error occurred: ' + ISNULL(SQLERRM, '')); END; END;
DECLARE v_empno NUMBER := 10; BEGIN -- Select employee information SELECT * INTO emp_record FROM employees WHERE employee_id = v_empno; -- Update employee salary UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_empno; -- Insert new employee INSERT INTO employees (employee_id, first_name, last_name, email) VALUES (20, 'John', 'Doe', 'john.doe@example.com'); -- Delete employee DELETE FROM employees WHERE employee_id = 30; -- Stored procedure call CALL my_stored_procedure(v_empno); -- Function call SELECT my_function(v_empno) FROM dual; -- Control flow IF v_empno > 15 THEN DBMS_OUTPUT.put_line('Employee is eligible for promotion'); ELSE DBMS_OUTPUT.put_line('Employee is not eligible for promotion'); END IF; -- Error handling BEGIN -- Code that may raise an error RAISE_APPLICATION_ERROR(-20000, 'Custom error message'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('An error occurred: ' || SQLERRM); END; END;
sql DECLARE @v_empno FLOAT; v_ename dbo.VARCHAR2(30); BEGIN SELECT @v_empno = empno, @v_ename = ename FROM employees WHERE empno = 7788; IF v_ename IS NULL BEGIN RAISERROR('-20001 Employee not found.', 16, 1); END ELSE BEGIN UPDATE employees SET ename = 'SCOTT' WHERE empno = @v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee updated successfully.'); END END; /
sql DECLARE v_empno NUMBER; v_ename VARCHAR2(30); BEGIN SELECT empno, ename INTO v_empno, v_ename FROM employees WHERE empno = 7788; IF v_ename IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found.'); ELSE UPDATE employees SET ename = 'SCOTT' WHERE empno = v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee updated successfully.'); END IF; END; /
DECLARE @emp_id INTEGER; BEGIN SELECT @emp_id = employee_id FROM employees WHERE last_name = 'King'; IF @emp_id IS NULL BEGIN RAISERROR('-20001 Employee ''King'' not found.', 16, 1); END UPDATE employees SET last_name = 'King' + ' (updated)' WHERE employee_id = @emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee King updated'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee with that name exists'); END END;
DECLARE emp_id INTEGER; BEGIN SELECT employee_id INTO emp_id FROM employees WHERE last_name = 'King'; IF emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee ''King'' not found.'); END IF; UPDATE employees SET last_name = 'King' || ' (updated)' WHERE employee_id = emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Employee King updated'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee with that name exists'); 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 BEGIN *** iables v_name dbo.VARCHAR2(50); v_salary NUMBER; *** atabase SELECT @v_name = name, @v_salary = salary FROM employees WHERE id = p_id; *** tails DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_name, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); *** n if salary is less than 1000 IF v_salary < 1000 BEGIN RAISERROR('-20001 Salary cannot be less than 1000', 16, 1); END END;
CREATE OR REPLACE PROCEDURE my_proc(p_id IN NUMBER) AS BEGIN -- Declare local variables v_name VARCHAR2(50); v_salary NUMBER; -- Fetch data from database SELECT name, salary INTO v_name, v_salary FROM employees WHERE id = p_id; -- Print employee details DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name); DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); -- Raise an exception if salary is less than 1000 IF v_salary < 1000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be less than 1000'); END IF; END;
*** rocedure with an input parameter CREATE PROCEDURE GetEmployeeInfo(@emp_id IN AS NUMBER) AS *** iables emp_name dbo.VARCHAR2(100); emp_salary NUMBER; BEGIN *** e for the employee information SELECT @emp_name = ename, @emp_salary = sal FROM emp WHERE empno = emp_id; *** ee information DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, '')); END; *** to calculate the total salary of all employees CREATE FUNCTION GetTotalSalary() RETURNS FLOAT AS BEGIN DECLARE @total_salary FLOAT; *** e for the total salary SELECT @total_salary = SUM(sal) FROM emp; *** salary RETURN @total_salary; END; *** ement to insert a new employee INSERT INTO emp(empno, ename, sal) VALUES (100, 'John Doe', 50000); *** flow construct to check if an employee exists DECLARE @emp_exists BIT; BEGIN *** e to check if the employee exists SELECT @emp_exists = COUNT(*) FROM emp WHERE empno = 100; *** xists, output a message IF @emp_exists BEGIN DBMS_OUTPUT.PUT_LINE('Employee exists'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee does not exist'); END END; *** andling scenario to catch an exception when inserting a duplicate employee BEGIN *** uplicate employee INSERT INTO emp(empno, ename, sal) VALUES (100, 'John Doe', 50000); EXCEPTION *** on and output an error message WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate employee error'); END;
-- Create a stored procedure with an input parameter CREATE PROCEDURE GetEmployeeInfo(emp_id IN NUMBER) AS -- Declare local variables emp_name VARCHAR2(100); emp_salary NUMBER; BEGIN -- Query the database for the employee information SELECT ename, sal INTO emp_name, emp_salary FROM emp WHERE empno = emp_id; -- Output the employee information DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary); END; -- Create a function to calculate the total salary of all employees CREATE FUNCTION GetTotalSalary() RETURN NUMBER AS total_salary NUMBER; BEGIN -- Query the database for the total salary SELECT SUM(sal) INTO total_salary FROM emp; -- Return the total salary RETURN total_salary; END; -- Create a SQL statement to insert a new employee INSERT INTO emp(empno, ename, sal) VALUES (100, 'John Doe', 50000); -- Create a control flow construct to check if an employee exists DECLARE emp_exists BOOLEAN; BEGIN -- Query the database to check if the employee exists SELECT COUNT(*) INTO emp_exists FROM emp WHERE empno = 100; -- If the employee exists, output a message IF emp_exists THEN DBMS_OUTPUT.PUT_LINE('Employee exists'); ELSE DBMS_OUTPUT.PUT_LINE('Employee does not exist'); END IF; END; -- Create an error handling scenario to catch an exception when inserting a duplicate employee BEGIN -- Try to insert a duplicate employee INSERT INTO emp(empno, ename, sal) VALUES (100, 'John Doe', 50000); EXCEPTION -- Catch the exception and output an error message WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate employee error'); END;
BEGIN -- De... SQLINES DEMO *** v_emp_id NUMBER; v_emp_name dbo.VARCHAR2(50); v_salary NUMBER; *** variables set @v_emp_id = 100; set @v_emp_name = 'John Doe'; set @v_salary = 10000; *** table INSERT INTO employees (emp_id, emp_name, salary) VALUES (v_emp_id, v_emp_name, v_salary); -- Co... SQLINES DEMO *** COMMIT; END;
BEGIN -- Declare variables v_emp_id NUMBER; v_emp_name VARCHAR2(50); v_salary NUMBER; -- Assign values to variables v_emp_id := 100; v_emp_name := 'John Doe'; v_salary := 10000; -- Insert data into table INSERT INTO employees (emp_id, emp_name, salary) VALUES (v_emp_id, v_emp_name, v_salary); -- Commit changes COMMIT; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc ( @p_id IN AS NUMBER, p_name IN VARCHAR2 ) AS BEGIN IF p_id IS NULL BEGIN RAISERROR('-20001 ID cannot be null', 16, 1); END UPDATE employees SET name = p_name WHERE id = p_id; END;
CREATE OR REPLACE PROCEDURE my_proc ( p_id IN NUMBER, p_name IN VARCHAR2 ) AS BEGIN IF p_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'ID cannot be null'); END IF; UPDATE employees SET name = p_name WHERE id = p_id; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = employee_id; IF salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END RETURN salary; END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, salary IN NUMBER) IS BEGIN UPDATE employees SET salary = salary WHERE employee_id = employee_id; IF @@ROWCOUNT = 0 BEGIN RAISERROR('-20002 Employee not found', 16, 1); 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; IF salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; RETURN salary; END; CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, salary IN NUMBER) IS BEGIN UPDATE employees SET salary = salary WHERE employee_id = employee_id; IF SQL%ROWCOUNT = 0 THEN RAISE_APPLICATION_ERROR(-20002, 'Employee not found'); END IF; END;
IF OBJECT_ID('update_customer', 'P') IS NOT NULL DROP PROCEDURE update_customer; GO CREATE PROCEDURE update_customer( @p_customer_id IN AS NUMBER, p_new_address IN VARCHAR2 ) IS l_old_address dbo.VARCHAR2(255); BEGIN SELECT @l_old_address = address FROM customers WHERE customer_id = p_customer_id; IF l_old_address = p_new_address BEGIN RETURN; END UPDATE customers SET address = p_new_address WHERE customer_id = p_customer_id; DBMS_OUTPUT.PUT_LINE('Customer address updated.'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No customer found with that ID.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END update_customer;
CREATE OR REPLACE PROCEDURE update_customer( p_customer_id IN NUMBER, p_new_address IN VARCHAR2 ) IS l_old_address VARCHAR2(255); BEGIN SELECT address INTO l_old_address FROM customers WHERE customer_id = p_customer_id; IF l_old_address = p_new_address THEN RETURN; END IF; UPDATE customers SET address = p_new_address WHERE customer_id = p_customer_id; DBMS_OUTPUT.PUT_LINE('Customer address updated.'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No customer found with that ID.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END update_customer;
CREATE PROCEDURE GetCustomerInfo(@cust_id FLOAT) AS name dbo.VARCHAR2(256); address dbo.VARCHAR2(256); phone dbo.VARCHAR2(256); email dbo.VARCHAR2(256); BEGIN SELECT @name = name, @address = address, @phone = phone, @email = email FROM customers WHERE customer_id = cust_id; DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(name, '')); DBMS_OUTPUT.PUT_LINE('Address: ' + isnull(address, '')); DBMS_OUTPUT.PUT_LINE('Phone: ' + isnull(phone, '')); DBMS_OUTPUT.PUT_LINE('Email: ' + isnull(email, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Customer not found'); END END; /
CREATE PROCEDURE GetCustomerInfo(cust_id NUMBER) IS name VARCHAR2(256); address VARCHAR2(256); phone VARCHAR2(256); email VARCHAR2(256); BEGIN SELECT name, address, phone, email INTO name, address, phone, email FROM customers WHERE customer_id = cust_id; DBMS_OUTPUT.PUT_LINE('Name: ' || name); DBMS_OUTPUT.PUT_LINE('Address: ' || address); DBMS_OUTPUT.PUT_LINE('Phone: ' || phone); DBMS_OUTPUT.PUT_LINE('Email: ' || email); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Customer not found'); END; /
DECLARE @emp_id FLOAT := 100; emp_name dbo.VARCHAR2(50) := 'John Doe'; BEGIN INSERT INTO employees (emp_id, emp_name) VALUES (@emp_id, emp_name); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20001 Employee with ID ' + isnull(@emp_id, '') + ' could not be added.', 16, 1); END;
DECLARE emp_id NUMBER := 100; emp_name VARCHAR2(50) := 'John Doe'; BEGIN INSERT INTO employees (emp_id, emp_name) VALUES (emp_id, emp_name); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || emp_id || ' could not be added.'); END;
sql CREATE PROCEDURE GetCustomerInfo( @IN customer_id AS NUMBER ) AS CURSOR c_cursor IS SELECT * FROM Customers WHERE CustomerID = customer_id; BEGIN OPEN c_cursor; IF c_cursor%FOUND BEGIN *** the customer information END CLOSE c_cursor; END;
sql CREATE PROCEDURE GetCustomerInfo( IN customer_id NUMBER ) AS CURSOR c_cursor IS SELECT * FROM Customers WHERE CustomerID = customer_id; BEGIN OPEN c_cursor; IF c_cursor%FOUND THEN -- Do something with the customer information END IF; CLOSE c_cursor; END;
declare @v_emp_id integer; v_ename dbo.varchar2(20); begin select @v_emp_id = emp_id, @v_ename = ename from emp where emp_id = 10; dbms_output.put_line('Emp_id: ' + isnull(@v_emp_id, '')); dbms_output.put_line('Ename: ' + isnull(v_ename, '')); if @@fetch_status <> 0 begin dbms_output.put_line('No data found for emp_id: ' + isnull(@v_emp_id, '')); end end;
declare v_emp_id integer; v_ename varchar2(20); begin select emp_id, ename into v_emp_id, v_ename from emp where emp_id = 10; dbms_output.put_line('Emp_id: ' || v_emp_id); dbms_output.put_line('Ename: ' || v_ename); exception when no_data_found then dbms_output.put_line('No data found for emp_id: ' || v_emp_id); end;
BEGIN DECLARE r CURSOR FOR SELECT * FROM employees WHERE department_id = 10; OPEN r; FETCH r INTO; WHILE @@FETCH_STATUS=0 BEGIN *** the row r FETCH r INTO; END; CLOSE r; DEALLOCATE r; IF @@FETCH_STATUS <> 0 BEGIN END *** ion END;
BEGIN FOR r IN ( SELECT * FROM employees WHERE department_id = 10 ) LOOP -- do something with the row r END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN -- handle the exception END;
BEGIN set @exc_check = 0; BEGIN BEGIN INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (201305, 'John Doe', 'Trainee', 300, CONVERT(DATETIME, '18-SEP-21', 'DD-MON-RR'), 3000, NULL, 10); WHEN DUP_VAL_ON_INDEX THEN set @exc_check = 1; DBMS_OUTPUT.PUT_LINE('Duplicate value on empno index'); IF @@FETCH_STATUS <> 0 BEGIN set @exc_check = 1; DBMS_OUTPUT.PUT_LINE('No data found for the given criteria'); END WHEN OTHERS THEN set @exc_check = 1; DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END; IF exc_check = 0 BEGIN DBMS_OUTPUT.PUT_LINE('Record inserted successfully'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Rolling back transaction'); ROLLBACK; END; COMMIT; END;
BEGIN exc_check := 0; BEGIN BEGIN INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (201305, 'John Doe', 'Trainee', 300, TO_DATE('18-SEP-21', 'DD-MON-RR'), 3000, NULL, 10); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN exc_check := 1; DBMS_OUTPUT.PUT_LINE('Duplicate value on empno index'); WHEN NO_DATA_FOUND THEN exc_check := 1; DBMS_OUTPUT.PUT_LINE('No data found for the given criteria'); WHEN OTHERS THEN exc_check := 1; DBMS_OUTPUT.PUT_LINE('An unexpected error occurred'); END; IF exc_check = 0 THEN DBMS_OUTPUT.PUT_LINE('Record inserted successfully'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Rolling back transaction'); ROLLBACK; END; COMMIT; END;
DECLARE @v_empno FLOAT; v_salary NUMBER; BEGIN SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 10; IF v_salary > 50000 BEGIN UPDATE emp SET deptno = 20 WHERE empno = @v_empno; END COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_empno NUMBER; v_salary NUMBER; BEGIN SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 10; IF v_salary > 50000 THEN UPDATE emp SET deptno = 20 WHERE empno = v_empno; END IF; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END;
DECLARE @v_empno FLOAT; v_salary NUMBER; v_comm NUMBER; BEGIN SELECT @v_salary = salary, @v_comm = comm FROM emp WHERE empno = 7788; IF v_comm IS NULL BEGIN SET @v_comm = 0; END SET @v_salary = v_salary + v_comm; UPDATE emp SET salary = v_salary WHERE empno = 7788; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END WHEN OTHERS THEN RAISE; END;
DECLARE v_empno NUMBER; v_salary NUMBER; v_comm NUMBER; BEGIN SELECT salary, comm INTO v_salary, v_comm FROM emp WHERE empno = 7788; IF v_comm IS NULL THEN v_comm := 0; END IF; v_salary := v_salary + v_comm; UPDATE emp SET salary = v_salary WHERE empno = 7788; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); WHEN OTHERS THEN RAISE; END;
BEGIN BEGIN INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, MANAGER_ID, DEPARTMENT_ID) VALUES (1002, 'Elwood', 'Blues', 'elwood.blues@bluesbrothers.com', '555-1212', GETDATE(), 'IT_PROG', 10000, 1001, 20); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN NULL; WHEN OTHERS THEN RAISE; END; BEGIN UPDATE EMPLOYEES SET SALARY = SALARY * 1.10 WHERE DEPARTMENT_ID = 20; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN NULL; END WHEN OTHERS THEN RAISE; END; BEGIN DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = 1002; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN NULL; END WHEN OTHERS THEN RAISE; END; DECLARE @emp_id FLOAT; emp_name dbo.VARCHAR2(50); BEGIN SELECT @emp_id = EMPLOYEE_ID, @emp_name = ISNULL(FIRST_NAME, '') + ' ' + ISNULL(LAST_NAME, '') FROM EMPLOYEES WHERE DEPARTMENT_ID = 20; DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_id, '') + ', Employee Name: ' + isnull(emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found in department 20.'); END WHEN OTHERS THEN RAISE; END; END;
BEGIN BEGIN INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, MANAGER_ID, DEPARTMENT_ID) VALUES (1002, 'Elwood', 'Blues', 'elwood.blues@bluesbrothers.com', '555-1212', SYSDATE, 'IT_PROG', 10000, 1001, 20); COMMIT; EXCEPTION WHEN DUP_VAL_ON_INDEX THEN NULL; WHEN OTHERS THEN RAISE; END; BEGIN UPDATE EMPLOYEES SET SALARY = SALARY * 1.10 WHERE DEPARTMENT_ID = 20; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN OTHERS THEN RAISE; END; BEGIN DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = 1002; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; WHEN OTHERS THEN RAISE; END; DECLARE emp_id NUMBER; emp_name VARCHAR2(50); BEGIN SELECT EMPLOYEE_ID, FIRST_NAME || ' ' || LAST_NAME INTO emp_id, emp_name FROM EMPLOYEES WHERE DEPARTMENT_ID = 20; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id || ', Employee Name: ' || emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found in department 20.'); WHEN OTHERS THEN RAISE; END; END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@id FLOAT) RETURNS VARCHAR(4000) AS BEGIN DECLARE @name VARCHAR(50); SELECT @name = ename FROM emp WHERE empno = @id; RETURN @name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END WHEN OTHERS THEN RAISE; END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@id FLOAT, @new_salary FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE emp SET sal = @new_salary WHERE empno = @id; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END WHEN OTHERS THEN RAISE; END; BEGIN DECLARE @emp_name VARCHAR(50); DECLARE @new_salary FLOAT; BEGIN SET @emp_name = dbo.get_employee_name(100); SET @new_salary = 10000; dbo.update_employee_salary(100, @new_salary); DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@emp_name, '') + ' salary updated to ' + isnull(@new_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating employee salary: ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE FUNCTION get_employee_name(id NUMBER) RETURN VARCHAR2 IS name VARCHAR2(50); BEGIN SELECT ename INTO name FROM emp WHERE empno = id; RETURN name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN OTHERS THEN RAISE; END; CREATE OR REPLACE PROCEDURE update_employee_salary(id NUMBER, new_salary NUMBER) IS BEGIN UPDATE emp SET sal = new_salary WHERE empno = id; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); WHEN OTHERS THEN RAISE; END; BEGIN DECLARE emp_name VARCHAR2(50); DECLARE new_salary NUMBER; BEGIN emp_name := get_employee_name(100); new_salary := 10000; update_employee_salary(100, new_salary); DBMS_OUTPUT.PUT_LINE('Employee ' || emp_name || ' salary updated to ' || new_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error updating employee salary: ' || SQLERRM); END;
BEGIN DECLARE @v_emp_id FLOAT := 7839; v_salary NUMBER; BEGIN SELECT @v_salary = salary FROM emp WHERE empno = @v_emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END; DBMS_OUTPUT.PUT_LINE('Salary of employee ' + isnull(@v_emp_id, '') + ' is ' + isnull(v_salary, '')); END;
BEGIN DECLARE v_emp_id NUMBER := 7839; v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM emp WHERE empno = v_emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END; DBMS_OUTPUT.PUT_LINE('Salary of employee ' || v_emp_id || ' is ' || v_salary); END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; v_dept_name dbo.VARCHAR2(30); v_error_msg dbo.VARCHAR2(200); BEGIN BEGIN SELECT @v_salary = salary, @v_dept_name = department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id) WHERE employee_id = @v_emp_id; IF @@FETCH_STATUS <> 0 BEGIN SET @v_error_msg = 'Employee not found with ID: ' + isnull(@v_emp_id, ''); RAISERROR(-20000, v_error_msg, 16, 1); END END; DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, '')); DBMS_OUTPUT.PUT_LINE('Department: ' + isnull(v_dept_name, '')); END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; v_dept_name VARCHAR2(30); v_error_msg VARCHAR2(200); BEGIN BEGIN SELECT salary, department_name INTO v_salary, v_dept_name FROM employees e JOIN departments d ON (e.department_id = d.department_id) WHERE employee_id = v_emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN v_error_msg := 'Employee not found with ID: ' || v_emp_id; RAISE_APPLICATION_ERROR(-20000, v_error_msg); END; DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); DBMS_OUTPUT.PUT_LINE('Department: ' || v_dept_name); END;
BEGIN BEGIN INSERT INTO employees (emp_id, name, salary) VALUES (100, 'John Doe', 1000); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK; END; SELECT * FROM employees WHERE emp_id = 100; DECLARE emp CURSOR FOR SELECT emp_id, salary FROM employees; OPEN emp; FETCH emp INTO; WHILE @@FETCH_STATUS=0 BEGIN BEGIN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = @emp_emp_id; IF @@FETCH_STATUS <> 0 BEGIN CONTINUE; END END; FETCH emp INTO; END; CLOSE emp; DEALLOCATE emp; DELETE FROM employees WHERE emp_id = 100; END; /
BEGIN BEGIN INSERT INTO employees (emp_id, name, salary) VALUES (100, 'John Doe', 1000); EXCEPTION WHEN DUP_VAL_ON_INDEX THEN ROLLBACK; END; SELECT * FROM employees WHERE emp_id = 100; FOR emp IN (SELECT emp_id, salary FROM employees) LOOP BEGIN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = emp.emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN CONTINUE; END; END LOOP; DELETE FROM employees WHERE emp_id = 100; END; /