sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@a IN AS NUMBER, b IN OUT NUMBER) IS
BEGIN
set @b = a + 1;
IF b > 10 BEGIN
RAISERROR('-20001 b is greater than 10', 16, 1);
END
END;
BEGIN
dbo.my_proc(5, :my_var);
DBMS_OUTPUT.PUT_LINE('my_var = ' + isnull(my_var, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(@@ERROR, '') + ' - ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_proc (a IN NUMBER, b IN OUT NUMBER) IS
BEGIN
b := a + 1;
IF b > 10 THEN
RAISE_APPLICATION_ERROR(-20001, 'b is greater than 10');
END IF;
END;
BEGIN
my_proc(5, :my_var);
DBMS_OUTPUT.PUT_LINE('my_var = ' || my_var);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLCODE || ' - ' || SQLERRM);
END;
|
IF OBJECT_ID('MyProcedure', 'P') IS NOT NULL
DROP PROCEDURE MyProcedure;
GO
CREATE PROCEDURE MyProcedure (
@p1 IN AS VARCHAR2,
p2 IN OUT NUMBER
) AS
BEGIN
IF p1 IS NULL BEGIN
RAISERROR('-20001 Parameter p1 cannot be NULL', 16, 1);
END
SELECT @p2 = COUNT(*) FROM MyTable WHERE Name = p1;
IF p2 IS NULL BEGIN
RAISERROR('-20002 No rows found for parameter p1', 16, 1);
END
END MyProcedure;
|
CREATE OR REPLACE PROCEDURE MyProcedure (
p1 IN VARCHAR2,
p2 IN OUT NUMBER
) AS
BEGIN
IF p1 IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Parameter p1 cannot be NULL');
END IF;
SELECT COUNT(*) INTO p2 FROM MyTable WHERE Name = p1;
IF p2 IS NULL THEN
RAISE_APPLICATION_ERROR(-20002, 'No rows found for parameter p1');
END IF;
END MyProcedure;
|
CREATE PROCEDURE update_salary(@salary_change IN AS NUMBER, emp_id IN NUMBER)
IS
BEGIN
*** ee's salary
UPDATE employees SET salary = salary + salary_change
WHERE employee_id = emp_id;
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
-- Lo... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
*** to calculate total sales for a given product
CREATE FUNCTION total_sales(@product_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
IS
sales_total NUMBER;
BEGIN
*** al sales for the product
SELECT @sales_total = SUM(sales)
FROM sales
WHERE product_id = product_id;
*** sales
RETURN sales_total;
END;
*** rocedure to handle customer orders
CREATE PROCEDURE process_order(@order_id IN AS NUMBER)
IS
*** e order information
customer_id NUMBER;
product_id NUMBER;
quantity NUMBER;
BEGIN
*** ails
SELECT @customer_id = customer_id, @product_id = product_id, @quantity = quantity
FROM orders
WHERE order_id = order_id;
-- Pr... SQLINES DEMO ***
IF quantity > 0 BEGIN
*** ty from stock
UPDATE products SET stock = stock - quantity
WHERE product_id = product_id;
END
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
-- Lo... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
PROCEDURE update_salary(salary_change IN NUMBER, emp_id IN NUMBER)
IS
BEGIN
-- Update the employee's salary
UPDATE employees SET salary = salary + salary_change
WHERE employee_id = emp_id;
-- Handle any errors
EXCEPTION
WHEN OTHERS THEN
-- Log the error
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
-- Create a function to calculate total sales for a given product
FUNCTION total_sales(product_id IN NUMBER)
RETURN NUMBER
IS
sales_total NUMBER;
BEGIN
-- Calculate the total sales for the product
SELECT SUM(sales) INTO sales_total
FROM sales
WHERE product_id = product_id;
-- Return the total sales
RETURN sales_total;
END;
-- Create a stored procedure to handle customer orders
PROCEDURE process_order(order_id IN NUMBER)
IS
-- Variables to store order information
customer_id NUMBER;
product_id NUMBER;
quantity NUMBER;
BEGIN
-- Get the order details
SELECT customer_id, product_id, quantity
INTO customer_id, product_id, quantity
FROM orders
WHERE order_id = order_id;
-- Process the order
IF quantity > 0 THEN
-- Deduct the quantity from stock
UPDATE products SET stock = stock - quantity
WHERE product_id = product_id;
END IF;
-- Handle any errors
EXCEPTION
WHEN OTHERS THEN
-- Log the error
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
|
DECLARE
@name VARCHAR(30);
salary NUMBER;
BEGIN
SELECT @name = @name, @salary = salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
IF salary > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('You are rich!');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('You need to work harder.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
DECLARE
name VARCHAR2(30);
salary NUMBER;
BEGIN
SELECT name, salary INTO name, salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
IF salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('You are rich!');
ELSE
DBMS_OUTPUT.PUT_LINE('You need to work harder.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
DECLARE
@x FLOAT;
y NUMBER;
BEGIN
BEGIN
SELECT @x = COUNT(*) FROM emp;
IF @@FETCH_STATUS <> 0 BEGIN
SET @x = 0;
END
END;
IF @x > 0 BEGIN
UPDATE hr.employees SET salary = salary * 1.10
WHERE department_id = 10;
SET @y = @@ROWCOUNT;
COMMIT;
END
ELSE BEGIN
SET @y = 0;
END
DBMS_OUTPUT.PUT_LINE('Updated ' + isnull(y, '') + ' salaries.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
x NUMBER;
y NUMBER;
BEGIN
BEGIN
SELECT COUNT(*) INTO x FROM emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
x := 0;
END;
IF x > 0 THEN
UPDATE hr.employees SET salary = salary * 1.10
WHERE department_id = 10;
y := SQL%ROWCOUNT;
COMMIT;
ELSE
y := 0;
END IF;
DBMS_OUTPUT.PUT_LINE('Updated ' || y || ' salaries.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER) AS
salary OUT NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
EXEC dbo.get_employee_salary(100);
|
CREATE OR REPLACE PROCEDURE get_employee_salary(emp_id IN NUMBER) AS
salary OUT NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
EXEC get_employee_salary(100);
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM employees WHERE last_name = 'Smith';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
ELSE BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id INTO v_emp_id FROM employees WHERE last_name = 'Smith';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
ELSE
UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@p_emp_id IN AS NUMBER,
p_salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = p_salary WHERE employee_id = p_emp_id;
COMMIT;
END;
/
DECLARE
@emp_id FLOAT := 100;
salary SET @NUMBER = 20000;
BEGIN
dbo.update_employee(@emp_id, salary);
SELECT * FROM employees WHERE employee_id = @emp_id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE update_employee(
p_emp_id IN NUMBER,
p_salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = p_salary WHERE employee_id = p_emp_id;
COMMIT;
END;
/
DECLARE
emp_id NUMBER := 100;
salary NUMBER := 20000;
BEGIN
update_employee(emp_id, salary);
SELECT * FROM employees WHERE employee_id = emp_id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
DECLARE
@v_emp_id DECIMAL(19,4) /* Use -meta option EMPLOYEES.EMPLOYEE_ID%TYPE */;
v_job_id EMPLOYEES.JOB_ID%TYPE;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_job_id = job_id, @v_salary = salary
FROM employees
WHERE employee_id = 1;
UPDATE employees
SET job_id = 'CLERK',
salary = v_salary + 1000
WHERE employee_id = @v_emp_id;
IF v_job_id = 'MANAGER' BEGIN
RAISERROR('-20001 Managers cannot be modified.', 16, 1);
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No employee found with employee_id = ' + isnull(@v_emp_id, ''), 16, 1);
END
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20003 Duplicate value found on index.', 16, 1);
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
v_emp_id EMPLOYEES.EMPLOYEE_ID%TYPE;
v_job_id EMPLOYEES.JOB_ID%TYPE;
v_salary NUMBER;
BEGIN
SELECT employee_id, job_id, salary
INTO v_emp_id, v_job_id, v_salary
FROM employees
WHERE employee_id = 1;
UPDATE employees
SET job_id = 'CLERK',
salary = v_salary + 1000
WHERE employee_id = v_emp_id;
IF v_job_id = 'MANAGER' THEN
RAISE_APPLICATION_ERROR(-20001, 'Managers cannot be modified.');
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No employee found with employee_id = ' || v_emp_id);
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20003, 'Duplicate value found on index.');
WHEN OTHERS THEN
RAISE;
END;
|
sql
IF OBJECT_ID('get_dept_salary', 'FN') IS NOT NULL
DROP FUNCTION get_dept_salary;
GO
CREATE FUNCTION get_dept_salary(@dept_id FLOAT)
RETURNS FLOAT
AS
BEGIN
DECLARE @avg_sal FLOAT;
SELECT @avg_sal = AVG(salary) FROM emp WHERE @dept_id = @dept_id;
RETURN @avg_sal;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN -1;
END
END;
| sql
CREATE OR REPLACE FUNCTION get_dept_salary(dept_id NUMBER)
RETURN NUMBER
IS
avg_sal NUMBER;
BEGIN
SELECT AVG(salary) INTO avg_sal FROM emp WHERE dept_id = dept_id;
RETURN avg_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
END;
|
DECLARE
@v_emp_id INTEGER;
v_emp_name dbo.VARCHAR2(100);
v_dept_id INTEGER;
BEGIN
*** ils
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_dept_id = dept_id
FROM employees
WHERE emp_id = 100;
*** exists
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee does not exist', 16, 1);
END
*** epartment
UPDATE employees
SET dept_id = 20
WHERE emp_id = @v_emp_id;
-- Co... SQLINES DEMO ***
COMMIT;
*** sage
DBMS_OUTPUT.PUT_LINE('Employee updated successfully');
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id INTEGER;
v_emp_name VARCHAR2(100);
v_dept_id INTEGER;
BEGIN
-- Get employee details
SELECT emp_id, emp_name, dept_id
INTO v_emp_id, v_emp_name, v_dept_id
FROM employees
WHERE emp_id = 100;
-- Check if employee exists
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee does not exist');
END IF;
-- Update employee department
UPDATE employees
SET dept_id = 20
WHERE emp_id = v_emp_id;
-- Commit changes
COMMIT;
-- Print success message
DBMS_OUTPUT.PUT_LINE('Employee updated successfully');
EXCEPTION
-- Handle errors
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('sp_Employee_Insert', 'P') IS NOT NULL
DROP PROCEDURE sp_Employee_Insert;
GO
CREATE PROCEDURE sp_Employee_Insert(
@p_EmpID IN AS NUMBER,
p_EmpName IN VARCHAR2,
p_EmpSalary IN NUMBER,
p_EmpDept IN VARCHAR2)
IS
BEGIN
*** oyee record into the Employee table
INSERT INTO Employee (EmpID, EmpName, EmpSalary, EmpDept)
VALUES (p_EmpID, p_EmpName, p_EmpSalary, p_EmpDept);
*** s to the database
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE sp_Employee_Insert(
p_EmpID IN NUMBER,
p_EmpName IN VARCHAR2,
p_EmpSalary IN NUMBER,
p_EmpDept IN VARCHAR2)
IS
BEGIN
-- Insert a new employee record into the Employee table
INSERT INTO Employee (EmpID, EmpName, EmpSalary, EmpDept)
VALUES (p_EmpID, p_EmpName, p_EmpSalary, p_EmpDept);
-- Commit the changes to the database
COMMIT;
END;
|
IF OBJECT_ID('InsertEmployee', 'P') IS NOT NULL
DROP PROCEDURE InsertEmployee;
GO
CREATE PROCEDURE InsertEmployee(
@EmployeeID IN AS NUMBER,
FirstName IN VARCHAR2,
LastName IN VARCHAR2,
Salary IN NUMBER)
IS
BEGIN
INSERT INTO Employees(EmployeeID, FirstName, LastName, Salary)
VALUES(EmployeeID, FirstName, LastName, Salary);
END;
IF OBJECT_ID('GetEmployeeName', 'FN') IS NOT NULL
DROP FUNCTION GetEmployeeName;
GO
CREATE FUNCTION GetEmployeeName(
@EmployeeID IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
EmployeeName dbo.VARCHAR2(50);
BEGIN
SELECT @EmployeeName = ISNULL(FirstName, '') + ' ' + ISNULL(LastName, '') FROM Employees WHERE EmployeeID = EmployeeID;
RETURN EmployeeName;
END;
DECLARE
@EmpID FLOAT := 100;
EmpName dbo.VARCHAR2(50);
BEGIN
SET @EmpName = dbo.GetEmployeeName(@EmpID);
IF EmpName IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + ISNULL(EmpName, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for EmployeeID: ' + ISNULL(@EmpID, ''));
END
END;
|
CREATE OR REPLACE PROCEDURE InsertEmployee(
EmployeeID IN NUMBER,
FirstName IN VARCHAR2,
LastName IN VARCHAR2,
Salary IN NUMBER)
IS
BEGIN
INSERT INTO Employees(EmployeeID, FirstName, LastName, Salary)
VALUES(EmployeeID, FirstName, LastName, Salary);
END;
CREATE OR REPLACE FUNCTION GetEmployeeName(
EmployeeID IN NUMBER)
RETURN VARCHAR2
IS
EmployeeName VARCHAR2(50);
BEGIN
SELECT FirstName || ' ' || LastName INTO EmployeeName FROM Employees WHERE EmployeeID = EmployeeID;
RETURN EmployeeName;
END;
DECLARE
EmpID NUMBER := 100;
EmpName VARCHAR2(50);
BEGIN
EmpName := GetEmployeeName(EmpID);
IF EmpName IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || EmpName);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for EmployeeID: ' || EmpID);
END;
|
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_employee(
@p_empno IN AS NUMBER,
p_ename IN VARCHAR2,
p_job IN VARCHAR2,
p_mgr IN NUMBER,
p_hiredate IN DATE,
p_sal IN NUMBER,
p_comm IN NUMBER,
p_deptno IN NUMBER)
IS
BEGIN
INSERT INTO employees (empno, ename, job, mgr, hiredate, sal, comm, deptno)
VALUES (p_empno, p_ename, p_job, p_mgr, p_hiredate, p_sal, p_comm, p_deptno);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE insert_employee(
p_empno IN NUMBER,
p_ename IN VARCHAR2,
p_job IN VARCHAR2,
p_mgr IN NUMBER,
p_hiredate IN DATE,
p_sal IN NUMBER,
p_comm IN NUMBER,
p_deptno IN NUMBER)
IS
BEGIN
INSERT INTO employees (empno, ename, job, mgr, hiredate, sal, comm, deptno)
VALUES (p_empno, p_ename, p_job, p_mgr, p_hiredate, p_sal, p_comm, p_deptno);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE @emp_id INT = 1;
WHILE @emp_id <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary for employee ' + isnull(emp_id, '') + ': ' + isnull(dbo.get_employee_salary(emp_id), ''));
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
FOR emp_id IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('Salary for employee ' || emp_id || ': ' || get_employee_salary(emp_id));
END LOOP;
END;
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure (
@p_id IN AS NUMBER
) AS
BEGIN
-- De... SQLINES DEMO ***
v_name dbo.VARCHAR2(100);
-- Qu... SQLINES DEMO ***
SELECT @v_name = name FROM employees WHERE id = p_id;
-- Ch... SQLINES DEMO ***
IF v_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
*** the data
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_name, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_procedure (
p_id IN NUMBER
) AS
BEGIN
-- Declare variables
v_name VARCHAR2(100);
-- Query the database
SELECT name INTO v_name FROM employees WHERE id = p_id;
-- Check for errors
IF v_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
-- Do something with the data
DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_name);
END;
|
CREATE PROCEDURE get_emp_details(@p_emp_id IN AS NUMBER) AS
v_emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_emp_name = ename FROM emp WHERE empno = p_emp_id;
DBMS_OUTPUT.PUT_LINE(v_emp_name);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END get_emp_details;
DECLARE
@v_emp_id FLOAT := 100;
BEGIN
dbo.get_emp_details(@v_emp_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.');
END;
|
CREATE PROCEDURE get_emp_details(p_emp_id IN NUMBER) AS
v_emp_name VARCHAR2(50);
BEGIN
SELECT ename INTO v_emp_name FROM emp WHERE empno = p_emp_id;
DBMS_OUTPUT.PUT_LINE(v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END get_emp_details;
DECLARE
v_emp_id NUMBER := 100;
BEGIN
get_emp_details(v_emp_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error occurred.');
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
*** ID and salary
SELECT @v_emp_id = emp_id, @v_salary = salary
FROM employees
WHERE last_name = 'Smith';
-- Up... SQLINES DEMO ***
UPDATE employees
SET salary = v_salary * 1.10
WHERE emp_id = @v_emp_id;
-- Co... SQLINES DEMO ***
COMMIT;
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
*** ges
ROLLBACK;
-- Lo... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
-- Get the employee ID and salary
SELECT emp_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'Smith';
-- Update the salary
UPDATE employees
SET salary = v_salary * 1.10
WHERE emp_id = v_emp_id;
-- Commit the changes
COMMIT;
-- Handle errors
EXCEPTION
WHEN OTHERS THEN
-- Rollback the changes
ROLLBACK;
-- Log the error
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
CREATE PROCEDURE get_emp_salary(@p_emp_id IN AS NUMBER, OUT p_salary OUT NUMBER) IS
BEGIN
SELECT @p_salary = salary FROM employees WHERE employee_id = p_emp_id;
IF p_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
DECLARE
@v_emp_id FLOAT := 100;
v_salary NUMBER;
BEGIN
dbo.get_emp_salary(@v_emp_id, v_salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
CREATE PROCEDURE get_emp_salary(p_emp_id IN NUMBER, OUT p_salary OUT NUMBER) IS
BEGIN
SELECT salary INTO p_salary FROM employees WHERE employee_id = p_emp_id;
IF p_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
DECLARE
v_emp_id NUMBER := 100;
v_salary NUMBER;
BEGIN
get_emp_salary(v_emp_id, v_salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
IF OBJECT_ID('GetCustomerBalance', 'FN') IS NOT NULL
DROP FUNCTION GetCustomerBalance;
GO
CREATE FUNCTION GetCustomerBalance(@customerId FLOAT)
RETURNS FLOAT AS
BEGIN
DECLARE @balance FLOAT;
SELECT @balance = @balance FROM CustomerBalances WHERE customer_id = @customerId;
RETURN @balance;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('UpdateCustomerBalance', 'P') IS NOT NULL
DROP PROCEDURE UpdateCustomerBalance;
GO
CREATE PROCEDURE UpdateCustomerBalance(@customerId FLOAT, @amount FLOAT)
AS
balance NUMBER;
BEGIN
SELECT @balance = balance FROM CustomerBalances WHERE customer_id = customerId;
set @balance = balance + amount;
UPDATE CustomerBalances SET balance = balance WHERE customer_id = customerId;
IF @@FETCH_STATUS <> 0 BEGIN
INSERT INTO CustomerBalances (customer_id, balance) VALUES (customerId, amount);
END
END;
|
CREATE OR REPLACE FUNCTION GetCustomerBalance(customerId NUMBER)
RETURN NUMBER IS
balance NUMBER;
BEGIN
SELECT balance INTO balance FROM CustomerBalances WHERE customer_id = customerId;
RETURN balance;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE UpdateCustomerBalance(customerId NUMBER, amount NUMBER)
IS
balance NUMBER;
BEGIN
SELECT balance INTO balance FROM CustomerBalances WHERE customer_id = customerId;
balance := balance + amount;
UPDATE CustomerBalances SET balance = balance WHERE customer_id = customerId;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO CustomerBalances (customer_id, balance) VALUES (customerId, amount);
END;
|
CREATE FUNCTION get_customer_name(@id FLOAT)
RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @cname VARCHAR(100);
SELECT @cname = name FROM customers WHERE @id = @id;
RETURN @cname;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
CREATE PROCEDURE update_customer_balance(@id FLOAT, @amount FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE customers SET balance = balance + @amount WHERE @id = @id;
END;
DECLARE
@c_id FLOAT := 10;
c_balance NUMBER;
BEGIN
SET @c_balance = dbo.get_customer_balance(@c_id);
IF c_balance < 100 BEGIN
dbo.update_customer_balance(@c_id, 50);
END
END;
|
CREATE FUNCTION get_customer_name(id NUMBER)
RETURN VARCHAR2 IS
cname VARCHAR2(100);
BEGIN
SELECT name INTO cname FROM customers WHERE id = id;
RETURN cname;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE PROCEDURE update_customer_balance(id NUMBER, amount NUMBER) IS
BEGIN
UPDATE customers SET balance = balance + amount WHERE id = id;
END;
DECLARE
c_id NUMBER := 10;
c_balance NUMBER;
BEGIN
c_balance := get_customer_balance(c_id);
IF c_balance < 100 THEN
update_customer_balance(c_id, 50);
END IF;
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE employee_id = 1;
IF @v_salary > 10000 BEGIN
dbo.RAISE_SALARY(employee_id => 1, percentage => 10);
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary
FROM employees
WHERE employee_id = 1;
IF v_salary > 10000 THEN
RAISE_SALARY(employee_id => 1, percentage => 10);
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('emp_details', 'P') IS NOT NULL
DROP PROCEDURE emp_details;
GO
CREATE PROCEDURE emp_details(
@p_emp_id IN AS NUMBER)
IS
v_empname dbo.VARCHAR2(100);
BEGIN
SELECT @v_empname = ename FROM emp WHERE empno = p_emp_id;
IF v_empname IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE(v_empname);
END;
|
CREATE OR REPLACE PROCEDURE emp_details(
p_emp_id IN NUMBER)
IS
v_empname VARCHAR2(100);
BEGIN
SELECT ename INTO v_empname FROM emp WHERE empno = p_emp_id;
IF v_empname IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE(v_empname);
END;
|
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
INSERT INTO my_table VALUES (i, 'Value ' + isnull(i, ''));
END;
COMMIT;
END;
|
BEGIN
FOR i IN 1 .. 10 LOOP
INSERT INTO my_table VALUES (i, 'Value ' || i);
END LOOP;
COMMIT;
END;
|
CREATE FUNCTION GetTotalSales (@cust_id VARCHAR(4000))
RETURNS FLOAT AS
BEGIN
DECLARE @total FLOAT;
BEGIN
SELECT @total = SUM(sales)
FROM sales
WHERE customer_id = @cust_id;
RETURN @total;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN -1;
END
END;
/
DECLARE
@total FLOAT;
BEGIN
SET @total = dbo.GetTotalSales('A001');
IF @total = -1 BEGIN
DBMS_OUTPUT.PUT_LINE('Customer not found.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Total sales for customer A001: ' + isnull(@total, ''));
END
END;
/
RETURN NULL;
|
CREATE FUNCTION GetTotalSales (cust_id VARCHAR2)
RETURN NUMBER IS
BEGIN
DECLARE total NUMBER;
BEGIN
SELECT SUM(sales) INTO total
FROM sales
WHERE customer_id = cust_id;
RETURN total;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
END;
/
DECLARE
total NUMBER;
BEGIN
total := GetTotalSales('A001');
IF total = -1 THEN
DBMS_OUTPUT.PUT_LINE('Customer not found.');
ELSE
DBMS_OUTPUT.PUT_LINE('Total sales for customer A001: ' || total);
END IF;
END;
/
|
CREATE PROCEDURE proc_example(@p_input IN AS VARCHAR2)
AS
v_output dbo.VARCHAR2(255);
BEGIN
SELECT @v_output = 'Output: ' + isnull(p_input, '');
DBMS_OUTPUT.PUT_LINE(v_output);
END proc_example;
/
BEGIN
dbo.proc_example('Example Input');
END;
/
|
CREATE PROCEDURE proc_example(p_input IN VARCHAR2)
AS
v_output VARCHAR2(255);
BEGIN
SELECT 'Output: ' || p_input INTO v_output FROM dual;
DBMS_OUTPUT.PUT_LINE(v_output);
END proc_example;
/
BEGIN
proc_example('Example Input');
END;
/
|
DECLARE
@price FLOAT;
BEGIN
SELECT @price = @price
FROM sales
WHERE product_id = 1;
IF @price IS NULL BEGIN
RAISE NO_DATA_FOUND;
END
UPDATE sales SET price = @price * 1.1
WHERE product_id = 1;
END;
|
DECLARE
price NUMBER;
BEGIN
SELECT price
INTO price
FROM sales
WHERE product_id = 1;
IF price IS NULL THEN
RAISE NO_DATA_FOUND;
END IF;
UPDATE sales SET price = price * 1.1
WHERE product_id = 1;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE
@salary FLOAT;
BEGIN
SET @salary = dbo.get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting salary: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE
salary NUMBER;
BEGIN
salary := get_employee_salary(1);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting salary: ' || SQLERRM);
END;
END;
|
BEGIN
DECLARE
@v_deptno FLOAT;
v_dname dbo.VARCHAR2(30);
v_loc dbo.VARCHAR2(30);
BEGIN
SELECT @v_deptno = deptno, @v_dname = dname, @v_loc = loc FROM dept WHERE deptno = 10;
DBMS_OUTPUT.PUT_LINE('Department Number: ' + isnull(@v_deptno, ''));
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(v_dname, ''));
DBMS_OUTPUT.PUT_LINE('Department Location: ' + isnull(v_loc, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No department found with the specified deptno.');
END
END;
END;
/ | BEGIN
DECLARE
v_deptno NUMBER;
v_dname VARCHAR2(30);
v_loc VARCHAR2(30);
BEGIN
SELECT deptno, dname, loc INTO v_deptno, v_dname, v_loc FROM dept WHERE deptno = 10;
DBMS_OUTPUT.PUT_LINE('Department Number: ' || v_deptno);
DBMS_OUTPUT.PUT_LINE('Department Name: ' || v_dname);
DBMS_OUTPUT.PUT_LINE('Department Location: ' || v_loc);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No department found with the specified deptno.');
END;
END;
/ |
CREATE TABLE Persons (
PersonID INTEGER PRIMARY KEY,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (1, 'Smith', 'John', '123 Main Street', 'Anytown');
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (2, 'Jones', 'Mary', '456 Elm Street', 'Anytown');
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (3, 'Williams', 'Bob', '789 Oak Street', 'Anytown');
SELECT PersonID, LastName, FirstName, Address, City FROM Persons;
UPDATE Persons SET LastName = 'Johnson' WHERE PersonID = 1;
DELETE FROM Persons WHERE PersonID = 3;
CREATE PROCEDURE GetPerson (
@IN PersonID AS INTEGER,
OUT LastName VARCHAR(255),
OUT FirstName VARCHAR(255),
OUT Address VARCHAR(255),
OUT City VARCHAR(255)
)
AS
BEGIN
SELECT @LastName = LastName, @FirstName = FirstName, @Address = Address, @City = City FROM Persons WHERE PersonID = PersonID;
END;
BEGIN
DECLARE
@LastName VARCHAR(255);
FirstName VARCHAR(255);
Address VARCHAR(255);
City VARCHAR(255);
BEGIN
dbo.GetPerson(1, @LastName, FirstName, Address, City);
DBMS_OUTPUT.Put_Line('PersonID: ' + 1);
DBMS_OUTPUT.Put_Line('LastName: ' + ISNULL(@LastName, ''));
DBMS_OUTPUT.Put_Line('FirstName: ' + ISNULL(FirstName, ''));
DBMS_OUTPUT.Put_Line('Address: ' + Isnull(Address, ''));
DBMS_OUTPUT.Put_Line('City: ' + Isnull(City, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.Put_Line('No person found with PersonID ' + 1);
END
END;
END; | CREATE TABLE Persons (
PersonID INTEGER PRIMARY KEY,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (1, 'Smith', 'John', '123 Main Street', 'Anytown');
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (2, 'Jones', 'Mary', '456 Elm Street', 'Anytown');
INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) VALUES (3, 'Williams', 'Bob', '789 Oak Street', 'Anytown');
SELECT PersonID, LastName, FirstName, Address, City FROM Persons;
UPDATE Persons SET LastName = 'Johnson' WHERE PersonID = 1;
DELETE FROM Persons WHERE PersonID = 3;
CREATE PROCEDURE GetPerson (
IN PersonID INTEGER,
OUT LastName VARCHAR(255),
OUT FirstName VARCHAR(255),
OUT Address VARCHAR(255),
OUT City VARCHAR(255)
)
AS
BEGIN
SELECT LastName, FirstName, Address, City INTO LastName, FirstName, Address, City FROM Persons WHERE PersonID = PersonID;
END;
BEGIN
DECLARE
LastName VARCHAR(255);
FirstName VARCHAR(255);
Address VARCHAR(255);
City VARCHAR(255);
BEGIN
GetPerson(1, LastName, FirstName, Address, City);
DBMS_OUTPUT.Put_Line('PersonID: ' || 1);
DBMS_OUTPUT.Put_Line('LastName: ' || LastName);
DBMS_OUTPUT.Put_Line('FirstName: ' || FirstName);
DBMS_OUTPUT.Put_Line('Address: ' || Address);
DBMS_OUTPUT.Put_Line('City: ' || City);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.Put_Line('No person found with PersonID ' || 1);
END;
END; |
BEGIN
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_emp_id = employee_id, @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100.');
END
END;
END; | BEGIN
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
BEGIN
SELECT employee_id, first_name || ' ' || last_name
INTO v_emp_id, v_emp_name
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100.');
END;
END; |
CREATE PROCEDURE update_customer(
@customer_id IN AS NUMBER,
new_email IN VARCHAR2,
OUT error_msg OUT VARCHAR2
)
AS
BEGIN
*** exists
IF customer_id NOT IN (SELECT customer_id FROM customers) BEGIN
set @error_msg = 'Customer not found';
RETURN;
END
*** mail
UPDATE customers
SET email = new_email
WHERE customer_id = customer_id;
set @error_msg = 'Success';
END;
|
CREATE PROCEDURE update_customer(
customer_id IN NUMBER,
new_email IN VARCHAR2,
OUT error_msg OUT VARCHAR2
)
AS
BEGIN
-- Check if customer exists
IF customer_id NOT IN (SELECT customer_id FROM customers) THEN
error_msg := 'Customer not found';
RETURN;
END IF;
-- Update customer email
UPDATE customers
SET email = new_email
WHERE customer_id = customer_id;
error_msg := 'Success';
END;
|
CREATE PROCEDURE Create_Employee(
@p_emp_id IN AS NUMBER,
p_name IN VARCHAR2,
p_dept_id IN NUMBER,
p_salary IN NUMBER
)
IS
BEGIN
*** oyee into the Employees table
INSERT INTO Employees (emp_id, name, dept_id, salary)
VALUES (p_emp_id, p_name, p_dept_id, p_salary);
*** ction
COMMIT;
END Create_Employee;
|
PROCEDURE Create_Employee(
p_emp_id IN NUMBER,
p_name IN VARCHAR2,
p_dept_id IN NUMBER,
p_salary IN NUMBER
)
IS
BEGIN
-- Insert a new employee into the Employees table
INSERT INTO Employees (emp_id, name, dept_id, salary)
VALUES (p_emp_id, p_name, p_dept_id, p_salary);
-- Commit the transaction
COMMIT;
END Create_Employee;
|
BEGIN
DECLARE
@l_emp_id INT;
l_salary ROW_NUMBER(8,2);
BEGIN
SELECT @l_emp_id = employee_id, @l_salary = salary
FROM employees
WHERE last_name = 'King';
UPDATE employees
SET salary = l_salary * 1.10
WHERE employee_id = @l_emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
END; | BEGIN
DECLARE
l_emp_id NUMBER(6);
l_salary NUMBER(8,2);
BEGIN
SELECT employee_id, salary
INTO l_emp_id, l_salary
FROM employees
WHERE last_name = 'King';
UPDATE employees
SET salary = l_salary * 1.10
WHERE employee_id = l_emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN OTHERS THEN
RAISE;
END;
END; |
BEGIN
DECLARE @v_my_var FLOAT;
SET @v_my_var = 10;
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
SET @v_my_var = @v_my_var + i;
DBMS_OUTPUT.PUT_LINE('Value of v_my_var: ' + isnull(@v_my_var, ''));
END;
END;
|
BEGIN
DECLARE v_my_var NUMBER;
v_my_var := 10;
FOR i IN 1..10 LOOP
v_my_var := v_my_var + i;
DBMS_OUTPUT.PUT_LINE('Value of v_my_var: ' || v_my_var);
END LOOP;
END;
|
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR(50) UNIQUE,
password VARCHAR(50)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT REFERENCES users(id),
product_id INT,
quantity INT
);
CREATE PROCEDURE create_user (
@p_username IN AS VARCHAR2,
p_password IN VARCHAR2
)
IS
BEGIN
INSERT INTO users (username, password)
VALUES (p_username, p_password);
END;
CREATE FUNCTION get_user_by_username (
@p_username IN VARCHAR2
DECLARE ) RETURN
BEGIN users%ROWTYPE
RETURN NULL;
END;
IS
l_user users%ROWTYPE;
BEGIN
SELECT @l_user = *
FROM users
WHERE username = p_username;
RETURN l_user;
END;
CREATE PROCEDURE delete_user (
@p_id IN AS INT
)
IS
BEGIN
DELETE FROM users
WHERE id = p_id;
END;
BEGIN
DECLARE
@l_user_id INT;
BEGIN
SET @l_user_id = DBMS_RANDOM.RANDOM_INT(1, 100);
DBMS_OUTPUT.PUT_LINE('User ID: ' + isnull(@l_user_id, ''));
dbo.CREATE_USER('test_user_' + isnull(@l_user_id, ''), 'test_password_' + isnull(@l_user_id, ''));
dbo.DELETE_USER(@l_user_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE TABLE users (
id INT PRIMARY KEY,
username VARCHAR2(50) UNIQUE,
password VARCHAR2(50)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT REFERENCES users(id),
product_id INT,
quantity INT
);
CREATE PROCEDURE create_user (
p_username IN VARCHAR2,
p_password IN VARCHAR2
)
IS
BEGIN
INSERT INTO users (username, password)
VALUES (p_username, p_password);
END;
CREATE FUNCTION get_user_by_username (
p_username IN VARCHAR2
) RETURN users%ROWTYPE
IS
l_user users%ROWTYPE;
BEGIN
SELECT * INTO l_user
FROM users
WHERE username = p_username;
RETURN l_user;
END;
CREATE PROCEDURE delete_user (
p_id IN INT
)
IS
BEGIN
DELETE FROM users
WHERE id = p_id;
END;
BEGIN
DECLARE
l_user_id INT;
BEGIN
l_user_id := DBMS_RANDOM.RANDOM_INT(1, 100);
DBMS_OUTPUT.PUT_LINE('User ID: ' || l_user_id);
CREATE_USER('test_user_' || l_user_id, 'test_password_' || l_user_id);
DELETE_USER(l_user_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
|
DECLARE
@emp_id INT;
BEGIN
SELECT @emp_id = employee_id FROM employees WHERE last_name = 'King';
IF @emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
DECLARE
emp_id NUMBER(6);
BEGIN
SELECT employee_id INTO emp_id FROM employees WHERE last_name = 'King';
IF emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
IF OBJECT_ID('update_emp_salary', 'P') IS NOT NULL
DROP PROCEDURE update_emp_salary;
GO
CREATE PROCEDURE update_emp_salary(
@emp_id IN AS NUMBER,
salary_increase IN NUMBER,
OUT error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET salary = salary + salary_increase
WHERE employee_id = emp_id;
IF @@fetch_status <> 0 BEGIN
set @error_code = 'EMP_NOT_FOUND';
END
WHEN invalid_salary_increase THEN
set @error_code = 'INVALID_SALARY_INCREASE';
ELSE
set @error_code = 'UNKNOWN_ERROR';
END;
END;
|
CREATE OR REPLACE PROCEDURE update_emp_salary(
emp_id IN NUMBER,
salary_increase IN NUMBER,
OUT error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET salary = salary + salary_increase
WHERE employee_id = emp_id;
EXCEPTION
WHEN no_data_found THEN
error_code := 'EMP_NOT_FOUND';
WHEN invalid_salary_increase THEN
error_code := 'INVALID_SALARY_INCREASE';
ELSE
error_code := 'UNKNOWN_ERROR';
END;
END;
|
IF OBJECT_ID('get_customer', 'P') IS NOT NULL
DROP PROCEDURE get_customer;
GO
CREATE PROCEDURE get_customer(@customer_id IN AS NUMBER) IS
customer_record customer%ROWTYPE;
BEGIN
SELECT @customer_record = * FROM customers WHERE customer_id = customer_id;
IF customer_record.customer_id IS NULL BEGIN
RAISERROR('-20001 Customer not found.', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Customer Name: ' + isnull(customer_record.customer_name, ''));
DBMS_OUTPUT.PUT_LINE('Customer Address: ' + isnull(customer_record.customer_address, ''));
END;
DECLARE
@customer_name VARCHAR(50);
BEGIN
dbo.get_customer(100);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.');
END
WHEN OTHERS THEN
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE get_customer(customer_id IN NUMBER) IS
customer_record customer%ROWTYPE;
BEGIN
SELECT * INTO customer_record FROM customers WHERE customer_id = customer_id;
IF customer_record.customer_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer not found.');
END IF;
DBMS_OUTPUT.PUT_LINE('Customer Name: ' || customer_record.customer_name);
DBMS_OUTPUT.PUT_LINE('Customer Address: ' || customer_record.customer_address);
END;
DECLARE
customer_name VARCHAR2(50);
BEGIN
get_customer(100);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No customer found with the specified ID.');
WHEN OTHERS THEN
RAISE;
END;
|
CREATE PROCEDURE update_employee_salary (
@employee_id IN AS NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
*** of the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
*** te was successful
IF @@ROWCOUNT = 1 BEGIN
*** ccessful
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN
*** t successful
DBMS_OUTPUT.PUT_LINE('Error updating salary.');
END
END;
BEGIN
*** rocedure to update the salary of employee with ID 10
dbo.update_employee_salary(10, 50000);
END;
|
CREATE PROCEDURE update_employee_salary (
employee_id IN NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
-- Update the salary of the specified employee
UPDATE employees SET salary = new_salary
WHERE employee_id = employee_id;
-- Check if the update was successful
IF SQL%ROWCOUNT = 1 THEN
-- The update was successful
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE
-- The update was not successful
DBMS_OUTPUT.PUT_LINE('Error updating salary.');
END IF;
END;
BEGIN
-- Call the stored procedure to update the salary of employee with ID 10
update_employee_salary(10, 50000);
END;
|
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL
DROP FUNCTION get_employee_details;
GO
CREATE FUNCTION get_employee_details(@emp_id IN NUMBERDECLARE )
RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END;
IS
rc SYS_REFCURSOR;
BEGIN
SELECT * FROM employees WHERE employee_id = emp_id;
RETURN rc;
END;
/
BEGIN
DECLARE rec CURSOR FOR dbo.get_employee_details(100)
LOOP
DBMS_OUTPUT.PUT_LINE(isnull(@rec_first_name, '') + ' ' + isnull(@rec_last_name, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/
|
CREATE OR REPLACE FUNCTION get_employee_details(emp_id IN NUMBER)
RETURN SYS_REFCURSOR
IS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR
SELECT * FROM employees WHERE employee_id = emp_id;
RETURN rc;
END;
/
BEGIN
FOR rec IN get_employee_details(100)
LOOP
DBMS_OUTPUT.PUT_LINE(rec.first_name || ' ' || rec.last_name);
END LOOP;
END;
/
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@fetch_status <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE
@salary FLOAT;
BEGIN
SET @salary = dbo.get_employee_salary(100);
IF @salary IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Salary for employee 100: ' + isnull(@salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No salary data found for employee 100.');
END
IF @@fetch_status <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving the salary.');
END
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
END;
BEGIN
DECLARE
salary NUMBER;
BEGIN
salary := get_employee_salary(100);
IF salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Salary for employee 100: ' || salary);
ELSE
DBMS_OUTPUT.PUT_LINE('No salary data found for employee 100.');
END IF;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving the salary.');
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 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
RAISE NO_DATA_FOUND;
END
RETURN salary;
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 NO_DATA_FOUND;
END IF;
RETURN salary;
END;
|
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE get_customer_orders;
GO
CREATE PROCEDURE get_customer_orders (@customer_id IN AS NUMBER) IS
CURSOR c1 IS
SELECT order_id, product_id, quantity
FROM orders
WHERE customer_id = customer_id;
BEGIN
DECLARE order_rec CURSOR FOR c1 LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@order_rec_order_id, ''));
DBMS_OUTPUT.PUT_LINE('Product ID: ' + isnull(@order_rec_product_id, ''));
DBMS_OUTPUT.PUT_LINE('Quantity: ' + isnull(@order_rec_quantity, ''));
FETCH order_rec INTO;
END;
CLOSE order_rec;
DEALLOCATE order_rec;
END;
/
|
CREATE OR REPLACE PROCEDURE get_customer_orders (customer_id IN NUMBER) IS
CURSOR c1 IS
SELECT order_id, product_id, quantity
FROM orders
WHERE customer_id = customer_id;
BEGIN
FOR order_rec IN c1 LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_rec.order_id);
DBMS_OUTPUT.PUT_LINE('Product ID: ' || order_rec.product_id);
DBMS_OUTPUT.PUT_LINE('Quantity: ' || order_rec.quantity);
END LOOP;
END;
/
|
DECLARE
@v_name VARCHAR(20);
v_age NUMBER;
BEGIN
*** a table
INSERT INTO employees (name, age)
VALUES ('John Smith', 25);
*** a table
SELECT @v_name = name, @v_age = age
FROM employees
WHERE id = 1;
*** table
UPDATE employees
SET age = v_age + 1
WHERE id = 1;
*** a table
DELETE FROM employees
WHERE id = 2;
*** IF-THEN-ELSE
IF v_age > 30 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is over 30.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is 30 or under.');
END
*** th EXCEPTION
BEGIN
*** se an error
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found.');
END
END;
END;
|
DECLARE
v_name VARCHAR2(20);
v_age NUMBER;
BEGIN
-- Insert data into a table
INSERT INTO employees (name, age)
VALUES ('John Smith', 25);
-- Select data from a table
SELECT name, age
INTO v_name, v_age
FROM employees
WHERE id = 1;
-- Update data in a table
UPDATE employees
SET age = v_age + 1
WHERE id = 1;
-- Delete data from a table
DELETE FROM employees
WHERE id = 2;
-- Control flow with IF-THEN-ELSE
IF v_age > 30 THEN
DBMS_OUTPUT.PUT_LINE('Employee is over 30.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee is 30 or under.');
END IF;
-- Error handling with EXCEPTION
BEGIN
-- Code that may raise an error
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found.');
END;
END;
|
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE get_customer_orders;
GO
CREATE PROCEDURE get_customer_orders(@customer_id IN AS NUMBER)
AS
orders_cursor SYS_REFCURSOR;
BEGIN
SELECT *
FROM orders
WHERE customer_id = customer_id;
RETURN orders_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No orders found for customer ' + isnull(customer_id, '') + '.', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE get_customer_orders(customer_id IN NUMBER)
AS
orders_cursor SYS_REFCURSOR;
BEGIN
OPEN orders_cursor FOR
SELECT *
FROM orders
WHERE customer_id = customer_id;
RETURN orders_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No orders found for customer ' || customer_id || '.');
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(100);
IF @emp_salary IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, ''));
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(100);
IF emp_salary IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 100');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary);
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(20);
v_emp_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_emp_salary = emp_salary
FROM emp
WHERE emp_id = 10;
IF @v_emp_id IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' has a salary of ' + isnull(v_emp_salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee with ID 10 not found');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(20);
v_emp_salary NUMBER;
BEGIN
SELECT emp_id, emp_name, emp_salary
INTO v_emp_id, v_emp_name, v_emp_salary
FROM emp
WHERE emp_id = 10;
IF v_emp_id IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' has a salary of ' || v_emp_salary);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee with ID 10 not found');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
CREATE TABLE employees(
id BIGINT NOT NULL,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE PROCEDURE raise_salary(
@id IN AS NUMBER,
amount IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = salary + amount
WHERE id = id;
END;
BEGIN
DECLARE
@id FLOAT := 100;
amount SET @NUMBER = 1000;
BEGIN
dbo.raise_salary(@id, amount);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.PUT_LINE('Error occurred');
END;
END;
SELECT * FROM employees; | CREATE TABLE employees(
id NUMBER(10) NOT NULL,
name VARCHAR2(50) NOT NULL,
salary NUMBER(10,2) NOT NULL,
PRIMARY KEY (id)
);
CREATE PROCEDURE raise_salary(
id IN NUMBER,
amount IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = salary + amount
WHERE id = id;
END;
BEGIN
DECLARE
id NUMBER := 100;
amount NUMBER := 1000;
BEGIN
raise_salary(id, amount);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.PUT_LINE('Error occurred');
END;
END;
SELECT * FROM employees; |
BEGIN
BEGIN
-- Ex... SQLINES DEMO ***
SELECT * FROM table1;
EXCEPTION
WHEN OTHERS THEN
-- Ha... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
-- De... SQLINES DEMO ***
v_var1 SET @NUMBER = 1;
*** ment
IF v_var1 > 0 BEGIN
-- Do... SQLINES DEMO ***
END
ELSE IF v_var1 < 0 BEGIN
-- Do... SQLINES DEMO ***
END
ELSE BEGIN
-- Do... SQLINES DEMO ***
END
*** ble
DECLARE r CURSOR FOR SELECT * FROM table2; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** each row
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
-- Ra... SQLINES DEMO ***
IF v_var1 = 0 BEGIN
RAISERROR('-20001 v_var1 cannot be zero.', 16, 1);
END
END;
|
BEGIN
BEGIN
-- Execute some SQL
SELECT * FROM table1;
EXCEPTION
WHEN OTHERS THEN
-- Handle errors
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
-- Declare a variable
v_var1 NUMBER := 1;
-- Conditional statement
IF v_var1 > 0 THEN
-- Do something
ELSIF v_var1 < 0 THEN
-- Do something else
ELSE
-- Do something else
END IF;
-- Loop through a table
FOR r IN (SELECT * FROM table2) LOOP
-- Do something with each row
END LOOP;
-- Raise an exception
IF v_var1 = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'v_var1 cannot be zero.');
END IF;
END;
|
BEGIN
-- De... SQLINES DEMO ***
v_deptno SET @NUMBER = 20;
v_sal SET @NUMBER = 30000;
v_empno NUMBER;
v_ename dbo.VARCHAR2(20);
*** tails
SELECT @v_empno = empno, @v_ename = ename FROM emp WHERE deptno = v_deptno AND sal > v_sal;
*** etails
DBMS_OUTPUT.PUT_LINE('Employee Number: ' + isnull(v_empno, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
END;
/
|
BEGIN
-- Declare variables
v_deptno NUMBER := 20;
v_sal NUMBER := 30000;
v_empno NUMBER;
v_ename VARCHAR2(20);
-- Fetch employee details
SELECT empno, ename INTO v_empno, v_ename FROM emp WHERE deptno = v_deptno AND sal > v_sal;
-- Output employee details
DBMS_OUTPUT.PUT_LINE('Employee Number: ' || v_empno);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
END;
/
|
BEGIN
-- De... SQLINES DEMO ***
v_empno NUMBER;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
*** formation
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal, @v_comm = comm, @v_deptno = deptno
FROM emp
WHERE empno = 7839;
*** department 20, give 10% raise
IF v_deptno = 20 BEGIN
UPDATE emp
SET sal = sal * 1.10
WHERE empno = v_empno;
-- Ou... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Salary updated for employee ' + isnull(v_ename, ''));
END
ELSE BEGIN
*** age
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_ename, '') + ' is not in department 20');
END
END; | BEGIN
-- Declare variables
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
-- Fetch employee information
SELECT empno, ename, sal, comm, deptno
INTO v_empno, v_ename, v_sal, v_comm, v_deptno
FROM emp
WHERE empno = 7839;
-- If employee is in department 20, give 10% raise
IF v_deptno = 20 THEN
UPDATE emp
SET sal = sal * 1.10
WHERE empno = v_empno;
-- Output message
DBMS_OUTPUT.PUT_LINE('Salary updated for employee ' || v_ename);
ELSE
-- Output error message
DBMS_OUTPUT.PUT_LINE('Employee ' || v_ename || ' is not in department 20');
END IF;
END; |
CREATE TABLE emps (
id FLOAT,
name VARCHAR(100),
salary FLOAT,
dept_id FLOAT
);
CREATE TABLE depts (
id FLOAT,
name VARCHAR(100)
);
INSERT INTO emps VALUES (1, 'John', 1000, 1);
INSERT INTO emps VALUES (2, 'Jane', 2000, 2);
INSERT INTO depts VALUES (1, 'Sales');
INSERT INTO depts VALUES (2, 'Marketing');
CREATE PROCEDURE update_salary (@emp_id IN AS NUMBER, new_salary IN NUMBER)
AS
BEGIN
UPDATE emps SET salary = new_salary WHERE id = emp_id;
COMMIT;
END;
CREATE FUNCTION get_employee_name (@emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
emp_name dbo.VARCHAR2(100);
BEGIN
SELECT @emp_name = name FROM emps WHERE id = emp_id;
RETURN emp_name;
END;
BEGIN
DECLARE
@emp_id FLOAT;
new_salary NUMBER;
BEGIN
SET @emp_id = 1;
SET @new_salary = 1500;
dbo.update_salary(@emp_id, new_salary);
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20001 Error updating salary', 16, 1);
END;
END;
|
CREATE TABLE emps (
id NUMBER,
name VARCHAR2(100),
salary NUMBER,
dept_id NUMBER
);
CREATE TABLE depts (
id NUMBER,
name VARCHAR2(100)
);
INSERT INTO emps VALUES (1, 'John', 1000, 1);
INSERT INTO emps VALUES (2, 'Jane', 2000, 2);
INSERT INTO depts VALUES (1, 'Sales');
INSERT INTO depts VALUES (2, 'Marketing');
CREATE PROCEDURE update_salary (emp_id IN NUMBER, new_salary IN NUMBER)
AS
BEGIN
UPDATE emps SET salary = new_salary WHERE id = emp_id;
COMMIT;
END;
CREATE FUNCTION get_employee_name (emp_id IN NUMBER) RETURN VARCHAR2
AS
emp_name VARCHAR2(100);
BEGIN
SELECT name INTO emp_name FROM emps WHERE id = emp_id;
RETURN emp_name;
END;
BEGIN
DECLARE
emp_id NUMBER;
new_salary NUMBER;
BEGIN
emp_id := 1;
new_salary := 1500;
update_salary(emp_id, new_salary);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Error updating salary');
END;
END;
|
IF OBJECT_ID('sp_sample', 'P') IS NOT NULL
DROP PROCEDURE sp_sample;
GO
CREATE PROCEDURE sp_sample(@p_in IN AS NUMBER) AS
BEGIN
IF p_in > 0 BEGIN
UPDATE table SET column = column + 1 WHERE id = p_in;
END
ELSE IF p_in < 0 BEGIN
DELETE FROM table WHERE id = p_in;
END
ELSE BEGIN
INSERT INTO table (id, [column]) VALUES (p_in, 1);
END
COMMIT;
END sp_sample;
|
CREATE OR REPLACE PROCEDURE sp_sample(p_in IN NUMBER) AS
BEGIN
IF p_in > 0 THEN
UPDATE table SET column = column + 1 WHERE id = p_in;
ELSIF p_in < 0 THEN
DELETE FROM table WHERE id = p_in;
ELSE
INSERT INTO table (id, column) VALUES (p_in, 1);
END IF;
COMMIT;
END sp_sample;
|
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL
DROP PROCEDURE sample_proc;
GO
CREATE PROCEDURE sample_proc(
@p_id IN AS NUMBER
)
IS
v_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_name = name FROM table_name WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Hello, ' + isnull(v_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
CREATE FUNCTION sample_func(@p_value FLOAT) RETURNS FLOAT AS
BEGIN
IF @p_value > 0 BEGIN
RETURN @p_value + 1;
END
ELSE BEGIN
RETURN @p_value - 1;
END
RETURN NULL;
END;
DECLARE
@v_count FLOAT;
BEGIN
SELECT @v_count = COUNT(*) FROM table_name;
IF @v_count > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Table is not empty');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Table is empty');
END
END;
BEGIN
/* SQLINES DEMO *** enario */
DECLARE
@v_num FLOAT;
BEGIN
SET @v_num = 1 / 0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Division by zero error');
END;
END;
|
CREATE OR REPLACE PROCEDURE sample_proc(
p_id IN NUMBER
)
IS
v_name VARCHAR2(100);
BEGIN
SELECT name INTO v_name FROM table_name WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Hello, ' || v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
FUNCTION sample_func(p_value NUMBER) RETURN NUMBER IS
BEGIN
IF p_value > 0 THEN
RETURN p_value + 1;
ELSE
RETURN p_value - 1;
END IF;
END;
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM table_name;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Table is not empty');
ELSE
DBMS_OUTPUT.PUT_LINE('Table is empty');
END IF;
END;
BEGIN
/* Error handling scenario */
DECLARE
v_num NUMBER;
BEGIN
v_num := 1 / 0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Division by zero error');
END;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
v_error_msg dbo.VARCHAR2(255);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM emp WHERE emp_id = :emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_error_msg = 'Employee not found with id: ' + isnull(:emp_id, '');
DBMS_OUTPUT.PUT_LINE(v_error_msg);
END
END; | DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_error_msg VARCHAR2(255);
BEGIN
SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM emp WHERE emp_id = :emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_error_msg := 'Employee not found with id: ' || :emp_id;
DBMS_OUTPUT.PUT_LINE(v_error_msg);
END; |
IF OBJECT_ID('calculate_sales_total', 'P') IS NOT NULL
DROP PROCEDURE calculate_sales_total;
GO
CREATE PROCEDURE calculate_sales_total(
@customer_id IN AS NUMBER,
total_sales OUT NUMBER
)
IS
CURSOR c_orders IS
SELECT order_id, order_total
FROM orders
WHERE customer_id = customer_id;
BEGIN
set @total_sales = 0;
DECLARE ord CURSOR FOR c_orders LOOP
set @total_sales = total_sales + @ord_order_total;
FETCH ord INTO;
END;
CLOSE ord;
DEALLOCATE ord;
END;
DECLARE
@total_sales FLOAT;
BEGIN
dbo.calculate_sales_total(3, @total_sales);
END;
|
CREATE OR REPLACE PROCEDURE calculate_sales_total(
customer_id IN NUMBER,
total_sales OUT NUMBER
)
IS
CURSOR c_orders IS
SELECT order_id, order_total
FROM orders
WHERE customer_id = customer_id;
BEGIN
total_sales := 0;
FOR ord IN c_orders LOOP
total_sales := total_sales + ord.order_total;
END LOOP;
END;
DECLARE
total_sales NUMBER;
BEGIN
calculate_sales_total(3, total_sales);
END;
|
IF OBJECT_ID('get_customer_name', 'FN') IS NOT NULL
DROP FUNCTION get_customer_name;
GO
CREATE FUNCTION get_customer_name(@id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
name dbo.VARCHAR2(100);
BEGIN
SELECT @name = customer_name FROM customers WHERE customer_id = id;
RETURN name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END get_customer_name;
IF OBJECT_ID('update_customer_balance', 'P') IS NOT NULL
DROP PROCEDURE update_customer_balance;
GO
CREATE PROCEDURE update_customer_balance(@id IN AS NUMBER, amount IN NUMBER)
IS
BEGIN
UPDATE customers SET balance = balance + amount WHERE customer_id = id;
COMMIT;
END update_customer_balance;
DECLARE
@customer_id FLOAT := 1001;
amount SET @NUMBER = 100;
BEGIN
DECLARE
@cust_name VARCHAR(100);
BEGIN
SET @cust_name = dbo.get_customer_name(@customer_id);
IF @cust_name IS NULL BEGIN
RAISERROR('-20000 Customer not found', 16, 1);
END
dbo.update_customer_balance(@customer_id, amount);
DBMS_OUTPUT.PUT_LINE('Customer ' + isnull(@cust_name, '') + ' updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE OR REPLACE FUNCTION get_customer_name(id IN NUMBER)
RETURN VARCHAR2
IS
name VARCHAR2(100);
BEGIN
SELECT customer_name INTO name FROM customers WHERE customer_id = id;
RETURN name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END get_customer_name;
CREATE OR REPLACE PROCEDURE update_customer_balance(id IN NUMBER, amount IN NUMBER)
IS
BEGIN
UPDATE customers SET balance = balance + amount WHERE customer_id = id;
COMMIT;
END update_customer_balance;
DECLARE
customer_id NUMBER := 1001;
amount NUMBER := 100;
BEGIN
DECLARE
cust_name VARCHAR2(100);
BEGIN
cust_name := get_customer_name(customer_id);
IF cust_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Customer not found');
END IF;
update_customer_balance(customer_id, amount);
DBMS_OUTPUT.PUT_LINE('Customer ' || cust_name || ' updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
END;
|
CREATE PROCEDURE get_employees
(
@in_deptno IN AS NUMBER
)
IS
CURSOR c_employees
IS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = in_deptno;
emp employee%ROWTYPE;
BEGIN
OPEN c_employees;
WHILE 1=1 BEGIN
FETCH c_employees INTO @emp;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(emp.employee_id, '') + ' ' + isnull(emp.first_name, '') + ' ' + isnull(emp.last_name, '') + ' ' + isnull(emp.salary, ''));
END;
CLOSE c_employees;
END;
|
CREATE PROCEDURE get_employees
(
in_deptno IN NUMBER
)
IS
CURSOR c_employees
IS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = in_deptno;
emp employee%ROWTYPE;
BEGIN
OPEN c_employees;
LOOP
FETCH c_employees INTO emp;
EXIT WHEN c_employees%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp.employee_id || ' ' || emp.first_name || ' ' || emp.last_name || ' ' || emp.salary);
END LOOP;
CLOSE c_employees;
END;
|
CREATE PROCEDURE emp_summary(@emp_id FLOAT, @OUT summary AS VARCHAR2)
AS
BEGIN
SELECT @summary = isnull(ename, '') + ' earns ' + ISNULL(FORMAT(salary, 'N0'), '') + ' per year'
FROM emp
WHERE empno = emp_id;
END;
BEGIN
DECLARE @summary VARCHAR(100);
BEGIN
dbo.emp_summary(7369, @summary);
DBMS_OUTPUT.PUT_LINE(@summary);
END;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE PROCEDURE emp_summary(emp_id NUMBER, OUT summary VARCHAR2)
AS
BEGIN
SELECT ename || ' earns ' || TO_CHAR(salary, '999,999') || ' per year' INTO summary
FROM emp
WHERE empno = emp_id;
END;
BEGIN
DECLARE summary VARCHAR2(100);
BEGIN
emp_summary(7369, summary);
DBMS_OUTPUT.PUT_LINE(summary);
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
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
emp_cur SYS_REFCURSOR;
BEGIN
SELECT emp_id, ename, sal, deptno
FROM emp
WHERE emp_id = emp_id;
RETURN emp_cur;
END get_employee_info;
BEGIN
DECLARE
@emp_cur SYS_REFCURSOR;
emp_id SET @NUMBER = 100;
ename dbo.VARCHAR2(10);
sal NUMBER;
deptno NUMBER;
BEGIN
SET @emp_cur = dbo.get_employee_info(emp_id);
WHILE 1=1 BEGIN
FETCH emp_cur INTO @ename, @sal, @deptno;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(ename, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(sal, ''));
DBMS_OUTPUT.PUT_LINE('Department Number: ' + isnull(deptno, ''));
END;
CLOSE emp_cur;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(emp_id, ''));
END
END;
COMMIT;
END;
|
CREATE OR REPLACE FUNCTION get_employee_info(emp_id IN NUMBER)
RETURN SYS_REFCURSOR IS
emp_cur SYS_REFCURSOR;
BEGIN
OPEN emp_cur FOR
SELECT emp_id, ename, sal, deptno
FROM emp
WHERE emp_id = emp_id;
RETURN emp_cur;
END get_employee_info;
BEGIN
DECLARE
emp_cur SYS_REFCURSOR;
emp_id NUMBER := 100;
ename VARCHAR2(10);
sal NUMBER;
deptno NUMBER;
BEGIN
emp_cur := get_employee_info(emp_id);
LOOP
FETCH emp_cur INTO ename, sal, deptno;
EXIT WHEN emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('Name: ' || ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || sal);
DBMS_OUTPUT.PUT_LINE('Department Number: ' || deptno);
END LOOP;
CLOSE emp_cur;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || emp_id);
END;
COMMIT;
END;
|
CREATE PROCEDURE get_customer_orders
(
@customer_id IN AS VARCHAR2
)
AS
BEGIN
SELECT o.order_id,
o.order_date,
o.total_amount
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id = customer_id;
END;
|
CREATE PROCEDURE get_customer_orders
(
customer_id IN VARCHAR2
)
AS
BEGIN
SELECT o.order_id,
o.order_date,
o.total_amount
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id = customer_id;
END;
|
IF OBJECT_ID('example_proc', 'P') IS NOT NULL
DROP PROCEDURE example_proc;
GO
CREATE PROCEDURE example_proc(@p_in IN AS NUMBER) IS
l_var NUMBER;
BEGIN
SELECT @l_var = COUNT(*) FROM example_table WHERE id = p_in;
IF l_var = 0 BEGIN
RAISERROR('-20001 No rows found for id ' + isnull(p_in, ''), 16, 1);
END
UPDATE example_table SET value = value + 1 WHERE id = p_in;
END;
|
CREATE OR REPLACE PROCEDURE example_proc(p_in IN NUMBER) IS
l_var NUMBER;
BEGIN
SELECT COUNT(*) INTO l_var FROM example_table WHERE id = p_in;
IF l_var = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found for id ' || p_in);
END IF;
UPDATE example_table SET value = value + 1 WHERE id = p_in;
END;
|
CREATE PROCEDURE usp_insert_customer (
@p_customer_id IN AS NUMBER,
p_customer_name IN VARCHAR2,
p_customer_address IN VARCHAR2,
p_customer_city IN VARCHAR2,
p_customer_state IN VARCHAR2,
p_customer_zip IN VARCHAR2,
p_customer_phone IN VARCHAR2,
p_customer_email IN VARCHAR2
)
AS
v_customer_rowcount NUMBER;
BEGIN
INSERT INTO customers (
customer_id,
customer_name,
customer_address,
customer_city,
customer_state,
customer_zip,
customer_phone,
customer_email
)
VALUES (
p_customer_id,
p_customer_name,
p_customer_address,
p_customer_city,
p_customer_state,
p_customer_zip,
p_customer_phone,
p_customer_email
);
COMMIT;
SELECT @v_customer_rowcount = COUNT(*) FROM customers WHERE customer_id = p_customer_id;
IF v_customer_rowcount = 0 BEGIN
RAISERROR('-20001 Customer not inserted', 16, 1);
END
END usp_insert_customer;
|
CREATE PROCEDURE usp_insert_customer (
p_customer_id IN NUMBER,
p_customer_name IN VARCHAR2,
p_customer_address IN VARCHAR2,
p_customer_city IN VARCHAR2,
p_customer_state IN VARCHAR2,
p_customer_zip IN VARCHAR2,
p_customer_phone IN VARCHAR2,
p_customer_email IN VARCHAR2
)
AS
v_customer_rowcount NUMBER;
BEGIN
INSERT INTO customers (
customer_id,
customer_name,
customer_address,
customer_city,
customer_state,
customer_zip,
customer_phone,
customer_email
)
VALUES (
p_customer_id,
p_customer_name,
p_customer_address,
p_customer_city,
p_customer_state,
p_customer_zip,
p_customer_phone,
p_customer_email
);
COMMIT;
SELECT COUNT(*) INTO v_customer_rowcount FROM customers WHERE customer_id = p_customer_id;
IF v_customer_rowcount = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Customer not inserted');
END IF;
END usp_insert_customer;
|
declare
@p_name VARCHAR(255);
p_id NUMBER;
v_salary CONSTANT set @NUMBER = 1000;
begin
select @p_id = * @p_name from emp where empno = 7788;
if @p_name is null begin
raiserror('-20001 Employee Not Found', 16, 1);
end
update emp set sal = v_salary * 1.1 where empno = p_id;
commit;
exception
when others then
rollback;
DBMS_OUTPUT.put_line('Exception Raised: ' + ISNULL(SQLERRM, ''));
end;
|
declare
p_name VARCHAR2(255);
p_id NUMBER;
v_salary CONSTANT NUMBER := 1000;
begin
select * into p_id, p_name from emp where empno = 7788;
if p_name is null then
raise_application_error(-20001, 'Employee Not Found');
end if;
update emp set sal = v_salary * 1.1 where empno = p_id;
commit;
exception
when others then
rollback;
DBMS_OUTPUT.put_line('Exception Raised: ' || SQLERRM);
end;
|
IF OBJECT_ID('emp_name_salary', 'FN') IS NOT NULL
DROP FUNCTION emp_name_salary;
GO
CREATE FUNCTION emp_name_salary(@emp_id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN (
'Name: '
+ ISNULL((SELECT isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = emp_id), '')
+ ', Salary: '
+ ISNULL((SELECT salary FROM employees WHERE employee_id = emp_id), '')
);
END;
BEGIN
DECLARE @emp_name VARCHAR(50);
BEGIN
SET @emp_name = dbo.emp_name_salary(100);
DBMS_OUTPUT.PUT_LINE(@emp_name);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
END;
|
CREATE OR REPLACE FUNCTION emp_name_salary(emp_id IN NUMBER)
RETURN VARCHAR2
AS
BEGIN
RETURN (
'Name: '
|| (SELECT first_name || ' ' || last_name FROM employees WHERE employee_id = emp_id)
|| ', Salary: '
|| (SELECT salary FROM employees WHERE employee_id = emp_id)
);
END;
BEGIN
DECLARE emp_name VARCHAR2(50);
BEGIN
emp_name := emp_name_salary(100);
DBMS_OUTPUT.PUT_LINE(emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
END;
|
BEGIN
DECLARE @v_empid INT;
DECLARE @v_salary DECIMAL(8,2);
BEGIN
SELECT @v_empid = employee_id, @v_salary = salary FROM employees WHERE last_name = 'King';
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = @v_empid;
END
ELSE BEGIN
UPDATE employees SET salary = @v_salary * 1.05 WHERE employee_id = @v_empid;
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END; | BEGIN
DECLARE v_empid NUMBER(6);
DECLARE v_salary NUMBER(8,2);
BEGIN
SELECT employee_id, salary INTO v_empid, v_salary FROM employees WHERE last_name = 'King';
IF v_salary > 10000 THEN
UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = v_empid;
ELSE
UPDATE employees SET salary = v_salary * 1.05 WHERE employee_id = v_empid;
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END;
END; |
CREATE TABLE employees (
employee_id INT NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
department_id SMALLINT NOT NULL,
PRIMARY KEY (employee_id),
FOREIGN KEY (department_id) REFERENCES departments (department_id)
);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (1, 'Alice', 'Smith', 10);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (2, 'Bob', 'Jones', 20);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (3, 'Carol', 'Davis', 30);
COMMIT;
SELECT * FROM employees;
IF OBJECT_ID('get_employee_by_id', 'P') IS NOT NULL
DROP PROCEDURE get_employee_by_id;
GO
CREATE PROCEDURE get_employee_by_id (
@employee_id IN AS NUMBER,
OUT first_name OUT VARCHAR2,
OUT last_name OUT VARCHAR2
)
AS
BEGIN
SELECT @first_name = first_name, @last_name = last_name
FROM employees
WHERE employee_id = employee_id;
END;
DECLARE
@emp_id INT := 1;
f_name dbo.VARCHAR2(20);
l_name dbo.VARCHAR2(20);
BEGIN
dbo.get_employee_by_id (@emp_id, f_name, l_name);
DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(f_name, '') + ' ' + isnull(l_name, ''));
END;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found with ID: ' + isnull(@emp_id, ''));
END
END;
/
|
CREATE TABLE employees (
employee_id NUMBER(6) NOT NULL,
first_name VARCHAR2(20) NOT NULL,
last_name VARCHAR2(20) NOT NULL,
department_id NUMBER(4) NOT NULL,
PRIMARY KEY (employee_id),
FOREIGN KEY (department_id) REFERENCES departments (department_id)
);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (1, 'Alice', 'Smith', 10);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (2, 'Bob', 'Jones', 20);
INSERT INTO employees (employee_id, first_name, last_name, department_id) VALUES (3, 'Carol', 'Davis', 30);
COMMIT;
SELECT * FROM employees;
CREATE OR REPLACE PROCEDURE get_employee_by_id (
employee_id IN NUMBER,
OUT first_name OUT VARCHAR2,
OUT last_name OUT VARCHAR2
)
AS
BEGIN
SELECT first_name, last_name
INTO first_name, last_name
FROM employees
WHERE employee_id = employee_id;
END;
DECLARE
emp_id NUMBER(6) := 1;
f_name VARCHAR2(20);
l_name VARCHAR2(20);
BEGIN
get_employee_by_id (emp_id, f_name, l_name);
DBMS_OUTPUT.PUT_LINE('Employee: ' || f_name || ' ' || l_name);
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found with ID: ' || emp_id);
END;
/
|
CREATE FUNCTION employee_salary_average (@department_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary_sum NUMBER;
employee_count NUMBER;
BEGIN
SELECT @salary_sum = SUM(salary), @employee_count = COUNT(*)
FROM employees
WHERE department_id = department_id;
IF employee_count = 0 BEGIN
RETURN NULL;
END
ELSE BEGIN
RETURN salary_sum / employee_count;
END
END;
|
CREATE FUNCTION employee_salary_average (department_id IN NUMBER)
RETURN NUMBER IS
salary_sum NUMBER;
employee_count NUMBER;
BEGIN
SELECT SUM(salary), COUNT(*)
INTO salary_sum, employee_count
FROM employees
WHERE department_id = department_id;
IF employee_count = 0 THEN
RETURN NULL;
ELSE
RETURN salary_sum / employee_count;
END IF;
END;
|
IF OBJECT_ID('add_employee', 'P') IS NOT NULL
DROP PROCEDURE add_employee;
GO
CREATE PROCEDURE add_employee(
@p_first_name IN AS VARCHAR2,
p_last_name IN VARCHAR2,
p_salary IN NUMBER
) AS
v_empid NUMBER;
BEGIN
SELECT @v_empid = ISNULL(MAX(empid), 0) + 1
FROM employees;
INSERT INTO employees (empid, first_name, last_name, salary)
VALUES (v_empid, p_first_name, p_last_name, p_salary);
COMMIT;
END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(
@p_empid IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE empid = p_empid;
RETURN v_salary;
END;
BEGIN
DECLARE
@v_empid FLOAT;
v_salary NUMBER;
BEGIN
SET @v_empid = 100;
SET @v_salary = dbo.get_employee_salary(@v_empid);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
END;
|
CREATE OR REPLACE PROCEDURE add_employee(
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_salary IN NUMBER
) AS
v_empid NUMBER;
BEGIN
SELECT NVL(MAX(empid), 0) + 1
INTO v_empid
FROM employees;
INSERT INTO employees (empid, first_name, last_name, salary)
VALUES (v_empid, p_first_name, p_last_name, p_salary);
COMMIT;
END;
CREATE OR REPLACE FUNCTION get_employee_salary(
p_empid IN NUMBER
) RETURN NUMBER AS
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM employees
WHERE empid = p_empid;
RETURN v_salary;
END;
BEGIN
DECLARE
v_empid NUMBER;
v_salary NUMBER;
BEGIN
v_empid := 100;
v_salary := get_employee_salary(v_empid);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
END;
|
DECLARE
@num1 FLOAT := 10;
num2 SET @NUMBER = 20;
BEGIN
IF @num1 > num2 BEGIN
DBMS_OUTPUT.PUT_LINE('Num1 is greater than Num2');
END
ELSE IF @num1 < num2 BEGIN
DBMS_OUTPUT.PUT_LINE('Num1 is less than Num2');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Num1 is equal to Num2');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
BEGIN
IF num1 > num2 THEN
DBMS_OUTPUT.PUT_LINE('Num1 is greater than Num2');
ELSIF num1 < num2 THEN
DBMS_OUTPUT.PUT_LINE('Num1 is less than Num2');
ELSE
DBMS_OUTPUT.PUT_LINE('Num1 is equal to Num2');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || 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
cursor_out SYS_REFCURSOR;
BEGIN
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN cursor_out;
END;
DECLARE
@customer_id FLOAT := 10;
orders_cursor SYS_REFCURSOR;
BEGIN
SET @orders_cursor = dbo.get_customer_orders(@customer_id);
WHILE 1=1 BEGIN
FETCH orders_cursor INTO @order_id, @order_date, @total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
*** the order data
END;
CLOSE orders_cursor;
END;
|
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR IS
cursor_out SYS_REFCURSOR;
BEGIN
OPEN cursor_out FOR
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN cursor_out;
END;
DECLARE
customer_id NUMBER := 10;
orders_cursor SYS_REFCURSOR;
BEGIN
orders_cursor := get_customer_orders(customer_id);
LOOP
FETCH orders_cursor INTO order_id, order_date, total_amount;
EXIT WHEN orders_cursor%NOTFOUND;
-- Do something with the order data
END LOOP;
CLOSE orders_cursor;
END;
|
CREATE FUNCTION GetEmployeeInfo(@id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
emp_rec SYS_REFCURSOR;
BEGIN
SELECT * FROM employees WHERE id = id;
RETURN emp_rec;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 Error retrieving employee info', 16, 1);
END;
CREATE PROCEDURE UpdateEmployeeInfo(@id IN AS NUMBER, name IN VARCHAR2, salary IN NUMBER) IS
BEGIN
UPDATE employees SET name = name, salary = salary WHERE id = id;
COMMIT;
END;
BEGIN
DECLARE
@result SYS_REFCURSOR;
emp_id SET @NUMBER = 10;
emp_name dbo.VARCHAR2(50);
emp_salary NUMBER;
BEGIN
SET @result = dbo.GetEmployeeInfo(emp_id);
FETCH result INTO @emp_name, @emp_salary;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with id ' + isnull(emp_id, ''));
END
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE FUNCTION GetEmployeeInfo(id IN NUMBER) RETURN SYS_REFCURSOR IS
emp_rec SYS_REFCURSOR;
BEGIN
OPEN emp_rec FOR
SELECT * FROM employees WHERE id = id;
RETURN emp_rec;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'Error retrieving employee info');
END;
CREATE PROCEDURE UpdateEmployeeInfo(id IN NUMBER, name IN VARCHAR2, salary IN NUMBER) IS
BEGIN
UPDATE employees SET name = name, salary = salary WHERE id = id;
COMMIT;
END;
BEGIN
DECLARE
result SYS_REFCURSOR;
emp_id NUMBER := 10;
emp_name VARCHAR2(50);
emp_salary NUMBER;
BEGIN
result := GetEmployeeInfo(emp_id);
FETCH result INTO emp_name, emp_salary;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with id ' || emp_id);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(@p_id IN AS NUMBER) IS
v_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_name = name FROM employees WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(v_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
CREATE OR REPLACE PROCEDURE my_proc(p_id IN NUMBER) IS
v_name VARCHAR2(100);
BEGIN
SELECT name INTO v_name FROM employees WHERE id = p_id;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
CREATE PROCEDURE my_proc (@id IN AS NUMBER, name IN VARCHAR2) IS
BEGIN
-- So... SQLINES DEMO ***
UPDATE my_table SET name = name WHERE id = id;
-- Ha... SQLINES DEMO ***
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Row not found', 16, 1);
END
END;
|
CREATE PROCEDURE my_proc (id IN NUMBER, name IN VARCHAR2) IS
BEGIN
-- Some processing
UPDATE my_table SET name = name WHERE id = id;
-- Handle errors
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Row not found');
END IF;
END;
|
IF OBJECT_ID('get_employee_count', 'FN') IS NOT NULL
DROP FUNCTION get_employee_count;
GO
CREATE FUNCTION get_employee_count() RETURNS FLOAT AS
BEGIN
DECLARE @emp_count FLOAT;
SELECT @emp_count = COUNT(*) FROM employees;
RETURN @emp_count;
END;
DECLARE
@emp_count FLOAT;
BEGIN
SET @emp_count = dbo.get_employee_count();
IF @emp_count > 10 BEGIN
raiserror('-20001 Too many employees!', 16, 1);
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = id;
COMMIT;
END;
BEGIN
dbo.update_employee_salary(10, 50000);
END;
|
CREATE OR REPLACE FUNCTION get_employee_count() RETURN NUMBER IS
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees;
RETURN emp_count;
END;
DECLARE
emp_count NUMBER;
BEGIN
emp_count := get_employee_count();
IF emp_count > 10 THEN
raise_application_error(-20001, 'Too many employees!');
END IF;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = id;
COMMIT;
END;
BEGIN
update_employee_salary(10, 50000);
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT @v_ename = ename, @v_salary = salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
END
ELSE IF v_salary > 5000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT ename, salary
INTO v_ename, v_salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 10000');
ELSIF v_salary > 5000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 5000');
END IF;
END;
|
declare
@v_empno float;
v_ename dbo.varchar2(20);
v_job dbo.varchar2(20);
v_mgr number;
v_sal number;
v_comm number;
v_deptno number;
begin
select @v_empno = empno, @v_ename = ename, @v_job = job, @v_mgr = mgr, @v_sal = sal, @v_comm = comm, @v_deptno = deptno
from emp
where empno = 7788;
dbms_output.put_line('Employee Details:');
dbms_output.put_line('-----------------');
dbms_output.put_line('Employee Number: ' + isnull(@v_empno, ''));
dbms_output.put_line('Employee Name: ' + isnull(v_ename, ''));
dbms_output.put_line('Employee Job: ' + isnull(v_job, ''));
dbms_output.put_line('Manager Number: ' + isnull(v_mgr, ''));
dbms_output.put_line('Salary: ' + isnull(v_sal, ''));
dbms_output.put_line('Commission: ' + isnull(v_comm, ''));
dbms_output.put_line('Department Number: ' + isnull(v_deptno, ''));
end;
/
|
declare
v_empno number;
v_ename varchar2(20);
v_job varchar2(20);
v_mgr number;
v_sal number;
v_comm number;
v_deptno number;
begin
select empno, ename, job, mgr, sal, comm, deptno
into v_empno, v_ename, v_job, v_mgr, v_sal, v_comm, v_deptno
from emp
where empno = 7788;
dbms_output.put_line('Employee Details:');
dbms_output.put_line('-----------------');
dbms_output.put_line('Employee Number: ' || v_empno);
dbms_output.put_line('Employee Name: ' || v_ename);
dbms_output.put_line('Employee Job: ' || v_job);
dbms_output.put_line('Manager Number: ' || v_mgr);
dbms_output.put_line('Salary: ' || v_sal);
dbms_output.put_line('Commission: ' || v_comm);
dbms_output.put_line('Department Number: ' || v_deptno);
end;
/
|
BEGIN
DECLARE @v_emp_id FLOAT := 10;
DECLARE @v_emp_name VARCHAR(20);
DECLARE @v_dept_id FLOAT;
SELECT @v_dept_id = dept_id FROM employees WHERE employee_id = @v_emp_id;
SELECT @v_emp_name = department_name FROM departments WHERE department_id = @v_dept_id;
DBMS_OUTPUT.PUT_LINE('Employee name is ' + isnull(@v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found for ID ' + isnull(@v_emp_id, ''));
END
END; | BEGIN
DECLARE v_emp_id NUMBER := 10;
DECLARE v_emp_name VARCHAR2(20);
DECLARE v_dept_id NUMBER;
SELECT dept_id INTO v_dept_id FROM employees WHERE employee_id = v_emp_id;
SELECT department_name INTO v_emp_name FROM departments WHERE department_id = v_dept_id;
DBMS_OUTPUT.PUT_LINE('Employee name is ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found for ID ' || v_emp_id);
END; |
CREATE PROCEDURE get_employee_details(
@IN employee_id AS NUMBER,
OUT emp_name VARCHAR2
) AS
BEGIN
SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No employee found with that ID', 16, 1);
END
END;
DECLARE
@employee_name VARCHAR(50);
BEGIN
dbo.get_employee_details(10, @employee_name);
DBMS_OUTPUT.PUT_LINE(@employee_name);
END;
|
CREATE PROCEDURE get_employee_details(
IN employee_id NUMBER,
OUT emp_name VARCHAR2
) AS
BEGIN
SELECT first_name || ' ' || last_name INTO emp_name
FROM employees
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with that ID');
END;
DECLARE
employee_name VARCHAR2(50);
BEGIN
get_employee_details(10, employee_name);
DBMS_OUTPUT.PUT_LINE(employee_name);
END;
|
BEGIN
-- De... SQLINES DEMO ***
v_name dbo.VARCHAR2(30);
v_age NUMBER;
-- Ge... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Enter your name: ');
DBMS_INPUT.GET_LINE(v_name);
DBMS_OUTPUT.PUT_LINE('Enter your age: ');
DBMS_INPUT.GET_LINE(v_age);
*** is over 18
IF v_age > 18
BEGIN
*** nto the database
INSERT INTO users (name, age) VALUES (v_name, v_age);
COMMIT;
*** message
DBMS_OUTPUT.PUT_LINE('User ' + isnull(v_name, '') + ' added to the database.');
END;
ELSE
BEGIN
*** message
DBMS_OUTPUT.PUT_LINE('Error: User must be over 18 years old.');
END;
EXCEPTION
WHEN OTHERS THEN
BEGIN
*** message
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
BEGIN
-- Declare variables
v_name VARCHAR2(30);
v_age NUMBER;
-- Get user input
DBMS_OUTPUT.PUT_LINE('Enter your name: ');
DBMS_INPUT.GET_LINE(v_name);
DBMS_OUTPUT.PUT_LINE('Enter your age: ');
DBMS_INPUT.GET_LINE(v_age);
-- Check if the user is over 18
IF v_age > 18 THEN
BEGIN
-- Insert the user into the database
INSERT INTO users (name, age) VALUES (v_name, v_age);
COMMIT;
-- Display a success message
DBMS_OUTPUT.PUT_LINE('User ' || v_name || ' added to the database.');
END;
ELSE
BEGIN
-- Display an error message
DBMS_OUTPUT.PUT_LINE('Error: User must be over 18 years old.');
END;
END IF;
EXCEPTION
WHEN OTHERS THEN
BEGIN
-- Display an error message
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|
BEGIN
DECLARE
@emp_id FLOAT := 7839;
empname dbo.VARCHAR2(20);
BEGIN
SELECT @empname = ename FROM emp WHERE empno = @emp_id;
IF @@fetch_status <> 0 BEGIN
SET @empname = '<Not Found>';
END
END;
BEGIN
UPDATE emp SET sal = sal * 1.1 WHERE empno = @emp_id;
EXCEPTION
WHEN duplicate_value_on_index THEN
RAISERROR(
'-20001 Duplicate value found on unique index for empno'
, 16, 1);
END;
END;
|
BEGIN
DECLARE
emp_id NUMBER := 7839;
empname VARCHAR2(20);
BEGIN
SELECT ename INTO empname FROM emp WHERE empno = emp_id;
EXCEPTION
WHEN no_data_found THEN
empname := '<Not Found>';
END;
BEGIN
UPDATE emp SET sal = sal * 1.1 WHERE empno = emp_id;
EXCEPTION
WHEN duplicate_value_on_index THEN
RAISE_APPLICATION_ERROR(
-20001,
'Duplicate value found on unique index for empno'
);
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 IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
DECLARE
@employee_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@employee_id);
IF salary IS NOT NULL BEGIN
dbo.update_employee_salary(@employee_id, salary * 1.1);
COMMIT;
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
DECLARE
employee_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(employee_id);
IF salary IS NOT NULL THEN
update_employee_salary(employee_id, salary * 1.1);
COMMIT;
END IF;
END;
|
CREATE PROCEDURE InsertCustomer (
@IN name AS VARCHAR2,
IN address VARCHAR2,
IN phone VARCHAR2
)
AS
BEGIN
INSERT INTO Customers (name, address, phone) VALUES (name, address, phone);
COMMIT;
END;
CREATE FUNCTION GetCustomerByName (
@IN name VARCHAR2
DECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
SELECT @VARCHAR2 = name FROM Customers WHERE name = name;
RETURN name;
END;
BEGIN
DECLARE
@cust_name VARCHAR(20);
BEGIN
SELECT @cust_name = name FROM Customers WHERE id = 1;
dbms_output.put_line('Customer Name: ' + isnull(@cust_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
dbms_output.put_line('No customer found with that ID.');
END
END;
END;
|
CREATE PROCEDURE InsertCustomer (
IN name VARCHAR2,
IN address VARCHAR2,
IN phone VARCHAR2
)
AS
BEGIN
INSERT INTO Customers (name, address, phone) VALUES (name, address, phone);
COMMIT;
END;
CREATE FUNCTION GetCustomerByName (
IN name VARCHAR2
)
RETURN VARCHAR2
AS
BEGIN
SELECT name INTO VARCHAR2 FROM Customers WHERE name = name;
RETURN name;
END;
BEGIN
DECLARE
cust_name VARCHAR2(20);
BEGIN
SELECT name INTO cust_name FROM Customers WHERE id = 1;
dbms_output.put_line('Customer Name: ' || cust_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No customer found with that ID.');
END;
END;
|
IF OBJECT_ID('employee_salary_update', 'P') IS NOT NULL
DROP PROCEDURE employee_salary_update;
GO
CREATE PROCEDURE employee_salary_update (
@emp_id IN AS NUMBER,
new_salary IN NUMBER
) IS
l_old_salary NUMBER;
BEGIN
SELECT @l_old_salary = salary FROM employees WHERE employee_id = emp_id;
IF l_old_salary IS NOT NULL BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
/
DECLARE
@emp_id FLOAT := 100;
new_salary SET @NUMBER = 50000;
BEGIN
dbo.employee_salary_update(@emp_id, new_salary);
END;
/
|
CREATE OR REPLACE PROCEDURE employee_salary_update (
emp_id IN NUMBER,
new_salary IN NUMBER
) IS
l_old_salary NUMBER;
BEGIN
SELECT salary INTO l_old_salary FROM employees WHERE employee_id = emp_id;
IF l_old_salary IS NOT NULL THEN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
/
DECLARE
emp_id NUMBER := 100;
new_salary NUMBER := 50000;
BEGIN
employee_salary_update(emp_id, new_salary);
END;
/
|
CREATE PROCEDURE sp_example(@i_param1 IN AS NUMBER, i_param2 IN VARCHAR2) IS
BEGIN
*** clarations
l_var1 NUMBER;
l_var2 dbo.VARCHAR2(20);
*** dure
SELECT @l_var1 = COUNT(*) FROM my_table WHERE id = i_param1;
IF l_var1 > 0 BEGIN
set @l_var2 = 'Record found';
END
ELSE BEGIN
set @l_var2 = 'Record not found';
END
dbms_output.put_line(l_var2);
END;
|
CREATE PROCEDURE sp_example(i_param1 IN NUMBER, i_param2 IN VARCHAR2) IS
BEGIN
-- Local variable declarations
l_var1 NUMBER;
l_var2 VARCHAR2(20);
-- Body of the procedure
SELECT COUNT(*) INTO l_var1 FROM my_table WHERE id = i_param1;
IF l_var1 > 0 THEN
l_var2 := 'Record found';
ELSE
l_var2 := 'Record not found';
END IF;
dbms_output.put_line(l_var2);
END;
|
sql
BEGIN
DECLARE r CURSOR FOR SELECT * FROM Employees; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE Employees SET salary = salary * 1.1 WHERE employee_id = @r_employee_id;
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END
END;
| sql
BEGIN
FOR r IN (SELECT * FROM Employees) LOOP
UPDATE Employees SET salary = salary * 1.1 WHERE employee_id = r.employee_id;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END;
|
CREATE TABLE customers (
customer_id FLOAT PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL,
customer_email VARCHAR(50) UNIQUE
);
CREATE TABLE orders (
order_id FLOAT PRIMARY KEY,
customer_id FLOAT REFERENCES customers(customer_id),
order_date DATE,
order_total DECIMAL(10,2)
);
CREATE PROCEDURE get_customer_orders (
@p_customer_id IN AS NUMBER
)
AS
CURSOR c_orders IS
SELECT *
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
DECLARE r_orders CURSOR FOR c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@r_orders_order_id, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@r_orders_order_date, ''));
DBMS_OUTPUT.PUT_LINE('Order Total: ' + isnull(@r_orders_order_total, ''));
FETCH r_orders INTO;
END;
CLOSE r_orders;
DEALLOCATE r_orders;
END;
BEGIN
dbo.get_customer_orders(10);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE customers (
customer_id NUMBER PRIMARY KEY,
customer_name VARCHAR2(50) NOT NULL,
customer_email VARCHAR2(50) UNIQUE
);
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER REFERENCES customers(customer_id),
order_date DATE,
order_total NUMBER(10,2)
);
CREATE PROCEDURE get_customer_orders (
p_customer_id IN NUMBER
)
AS
CURSOR c_orders IS
SELECT *
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
FOR r_orders IN c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' || r_orders.order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || r_orders.order_date);
DBMS_OUTPUT.PUT_LINE('Order Total: ' || r_orders.order_total);
END LOOP;
END;
BEGIN
get_customer_orders(10);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
*** rocedure to insert data into a table
IF OBJECT_ID('insert_data', 'P') IS NOT NULL
DROP PROCEDURE insert_data;
GO
CREATE PROCEDURE insert_data(@i_id IN AS NUMBER, i_name IN VARCHAR2, i_age IN NUMBER) IS
BEGIN
*** the table
INSERT INTO employees(id, name, age) VALUES(i_id, i_name, i_age);
END;
/
*** to calculate the average age of employees
IF OBJECT_ID('get_avg_age', 'FN') IS NOT NULL
DROP FUNCTION get_avg_age;
GO
CREATE FUNCTION get_avg_age() RETURNS FLOAT AS
BEGIN
DECLARE @l_avg_age FLOAT;
*** rage age
SELECT @l_avg_age = AVG(age) FROM employees;
*** e age
RETURN @l_avg_age;
END;
GO
*** ement to select data from a table
SELECT * FROM employees;
*** flow construct to handle different scenarios
IF l_avg_age > 30 BEGIN
*** he average age is greater than 30
DBMS_OUTPUT.PUT_LINE('The average age is greater than 30.');
END
ELSE BEGIN
*** if the average age is less than or equal to 30
DBMS_OUTPUT.PUT_LINE('The average age is less than or equal to 30.');
END
*** andling scenario to handle exceptions
BEGIN
*** statement
SELECT * FROM non_existing_table;
*** ion
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found in the table.');
END
END;
|
-- Create a stored procedure to insert data into a table
CREATE OR REPLACE PROCEDURE insert_data(i_id IN NUMBER, i_name IN VARCHAR2, i_age IN NUMBER) IS
BEGIN
-- Insert data into the table
INSERT INTO employees(id, name, age) VALUES(i_id, i_name, i_age);
END;
/
-- Create a function to calculate the average age of employees
CREATE OR REPLACE FUNCTION get_avg_age() RETURN NUMBER IS
l_avg_age NUMBER;
BEGIN
-- Calculate the average age
SELECT AVG(age) INTO l_avg_age FROM employees;
-- Return the average age
RETURN l_avg_age;
END;
/
-- Create a SQL statement to select data from a table
SELECT * FROM employees;
-- Create a control flow construct to handle different scenarios
IF l_avg_age > 30 THEN
-- Do something if the average age is greater than 30
DBMS_OUTPUT.PUT_LINE('The average age is greater than 30.');
ELSE
-- Do something else if the average age is less than or equal to 30
DBMS_OUTPUT.PUT_LINE('The average age is less than or equal to 30.');
END IF;
-- Create an error handling scenario to handle exceptions
BEGIN
-- Try to execute a statement
SELECT * FROM non_existing_table;
EXCEPTION
-- Handle the exception
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found in the table.');
END;
|
IF OBJECT_ID('sp_update_employee', 'P') IS NOT NULL
DROP PROCEDURE sp_update_employee;
GO
CREATE PROCEDURE sp_update_employee
(@p_employee_id IN AS NUMBER
,p_employee_name IN VARCHAR2
,p_salary IN NUMBER
,p_error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET employee_name = p_employee_name, salary = p_salary WHERE employee_id = p_employee_id;
set @p_error_code = '00';
IF @@FETCH_STATUS <> 0 BEGIN
set @p_error_code = '200';
END
WHEN TOO_MANY_ROWS THEN
set @p_error_code = '300';
WHEN VALUE_ERROR THEN
set @p_error_code = '400';
END sp_update_employee;
|
CREATE OR REPLACE PROCEDURE sp_update_employee
(p_employee_id IN NUMBER
,p_employee_name IN VARCHAR2
,p_salary IN NUMBER
,p_error_code OUT VARCHAR2)
AS
BEGIN
UPDATE employees SET employee_name = p_employee_name, salary = p_salary WHERE employee_id = p_employee_id;
p_error_code := '00';
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_error_code := '200';
WHEN TOO_MANY_ROWS THEN
p_error_code := '300';
WHEN VALUE_ERROR THEN
p_error_code := '400';
END sp_update_employee;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
/
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
/
|
IF OBJECT_ID('test_stored_proc', 'P') IS NOT NULL
DROP PROCEDURE test_stored_proc;
GO
CREATE PROCEDURE test_stored_proc (
@IN p_in_num AS PLS_INTEGER,
OUT p_out_num PLS_INTEGER
)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE ( 'Input number: ' + isnull(p_in_num, '') );
set @p_out_num = p_in_num * 2;
DBMS_OUTPUT.PUT_LINE ( 'Output number: ' + isnull(p_out_num, '') );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ( 'Error: ' + ISNULL(@@ERROR, '') + ' - ' + ISNULL(SQLERRM, '') );
set @p_out_num = -1;
END test_stored_proc;
/
|
CREATE OR REPLACE PROCEDURE test_stored_proc (
IN p_in_num PLS_INTEGER,
OUT p_out_num PLS_INTEGER
)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE ( 'Input number: ' || p_in_num );
p_out_num := p_in_num * 2;
DBMS_OUTPUT.PUT_LINE ( 'Output number: ' || p_out_num );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ( 'Error: ' || SQLCODE || ' - ' || SQLERRM );
p_out_num := -1;
END test_stored_proc;
/
|
CREATE PROCEDURE show_sales
(
@start_date DATE,
@end_date DATE
)
AS
BEGIN
SET NOCOUNT ON;
SELECT
name,
SUM(sales)
FROM sales
WHERE
date BETWEEN @start_date AND @end_date
GROUP BY
name;
END;
CALL show_sales('2023-01-01', '2023-01-31');
BEGIN
DECLARE
@sales_total FLOAT;
BEGIN
SELECT
@sales_total = SUM(sales)
FROM sales;
IF @@FETCH_STATUS <> 0 BEGIN
SET @sales_total = 0;
END
END;
DBMS_OUTPUT.PUT_LINE('Total sales: ' + isnull(@sales_total, ''));
END;
|
CREATE PROCEDURE show_sales
(
start_date DATE,
end_date DATE
)
AS
BEGIN
SELECT
name,
SUM(sales)
FROM sales
WHERE
date BETWEEN start_date AND end_date
GROUP BY
name;
END;
CALL show_sales('2023-01-01', '2023-01-31');
BEGIN
DECLARE
sales_total NUMBER;
BEGIN
SELECT
SUM(sales)
INTO sales_total
FROM sales;
EXCEPTION
WHEN NO_DATA_FOUND THEN
sales_total := 0;
END;
DBMS_OUTPUT.PUT_LINE('Total sales: ' || sales_total);
END;
|
IF OBJECT_ID('test_procedure', 'P') IS NOT NULL
DROP PROCEDURE test_procedure;
GO
CREATE PROCEDURE test_procedure(
@p_id IN AS NUMBER
) AS
DECLARE
@v_name VARCHAR(100);
BEGIN
SELECT @v_name = name FROM test_table WHERE id = p_id;
IF @v_name IS NULL BEGIN
RAISERROR('-20001 Record not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@v_name, ''));
END test_procedure;
|
CREATE OR REPLACE PROCEDURE test_procedure(
p_id IN NUMBER
) AS
DECLARE
v_name VARCHAR(100);
BEGIN
SELECT name INTO v_name FROM test_table WHERE id = p_id;
IF v_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Record not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
END test_procedure;
|
CREATE TABLE employees (
employee_id FLOAT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
salary DECIMAL(10, 2) NOT NULL,
hire_date DATE NOT NULL
);
INSERT INTO employees
VALUES (101, 'John', 'Smith', 50000, '2023-01-01');
INSERT INTO employees
VALUES (102, 'Jane', 'Doe', 60000, '2023-02-01');
DECLARE
@total_salary FLOAT;
BEGIN
SELECT @total_salary = SUM(salary) FROM employees;
DBMS_OUTPUT.PUT_LINE('Total salary: ' + isnull(@total_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found');
END
END;
IF OBJECT_ID('get_employee_by_id', 'P') IS NOT NULL
DROP PROCEDURE get_employee_by_id;
GO
CREATE PROCEDURE get_employee_by_id (
@employee_id IN AS NUMBER,
first_name OUT VARCHAR2,
last_name OUT VARCHAR2
) AS
BEGIN
SELECT @first_name = first_name, @last_name = last_name
FROM employees
WHERE employee_id = employee_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 NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = employee_id
);
END;
|
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
salary NUMBER(10, 2) NOT NULL,
hire_date DATE NOT NULL
);
INSERT INTO employees
VALUES (101, 'John', 'Smith', 50000, '2023-01-01');
INSERT INTO employees
VALUES (102, 'Jane', 'Doe', 60000, '2023-02-01');
DECLARE
total_salary NUMBER;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees;
DBMS_OUTPUT.PUT_LINE('Total salary: ' || total_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found');
END;
CREATE OR REPLACE PROCEDURE get_employee_by_id (
employee_id IN NUMBER,
first_name OUT VARCHAR2,
last_name OUT VARCHAR2
) AS
BEGIN
SELECT first_name, last_name
INTO first_name, last_name
FROM employees
WHERE employee_id = employee_id;
END;
CREATE OR REPLACE FUNCTION get_employee_salary (
employee_id IN NUMBER
) RETURN NUMBER AS
BEGIN
RETURN (
SELECT salary
FROM employees
WHERE employee_id = employee_id
);
END;
|
CREATE PROCEDURE get_customer_orders(
@p_customer_id IN AS NUMBER)
IS
CURSOR cur_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
OPEN cur_orders;
WHILE 1=1 BEGIN
FETCH cur_orders
INTO @v_order_id, @v_order_date, @v_total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(v_order_id, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(v_order_date, ''));
DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(v_total_amount, ''));
END;
CLOSE cur_orders;
END;
/
|
CREATE PROCEDURE get_customer_orders(
p_customer_id IN NUMBER)
IS
CURSOR cur_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = p_customer_id;
BEGIN
OPEN cur_orders;
LOOP
FETCH cur_orders
INTO v_order_id, v_order_date, v_total_amount;
EXIT WHEN cur_orders%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || v_order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || v_order_date);
DBMS_OUTPUT.PUT_LINE('Total Amount: ' || v_total_amount);
END LOOP;
CLOSE cur_orders;
END;
/
|
DECLARE
@v_emp_id INT;
v_salary ROW_NUMBER(10,2);
v_bonus ROW_NUMBER(10,2) := 0;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = @v_emp_id;
IF v_salary > 5000 BEGIN
SET @v_bonus = v_salary * 0.1;
END
ELSE BEGIN
SET @v_bonus = v_salary * 0.05;
END
UPDATE employees SET bonus = v_bonus WHERE employee_id = @v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER(6);
v_salary NUMBER(10,2);
v_bonus NUMBER(10,2) := 0;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id;
IF v_salary > 5000 THEN
v_bonus := v_salary * 0.1;
ELSE
v_bonus := v_salary * 0.05;
END IF;
UPDATE employees SET bonus = v_bonus WHERE employee_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
BEGIN
-- De... SQLINES DEMO ***
v_empno NUMBER;
v_salary NUMBER;
v_comm NUMBER;
*** er and salary
SELECT @v_empno = empno, @v_salary = salary, @v_comm = comm FROM emp WHERE empno = 7839;
*** on is NULL
IF v_comm IS NULL BEGIN
-- Se... SQLINES DEMO ***
set @v_comm = 0;
END
-- Ca... SQLINES DEMO ***
set @v_bonus = v_salary * 0.1;
*** able with bonus
UPDATE emp SET bonus = v_bonus WHERE empno = v_empno;
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
*** ges
ROLLBACK;
-- Ra... SQLINES DEMO ***
RAISE;
END;
|
BEGIN
-- Declare variables
v_empno NUMBER;
v_salary NUMBER;
v_comm NUMBER;
-- Get employee number and salary
SELECT empno, salary, comm INTO v_empno, v_salary, v_comm FROM emp WHERE empno = 7839;
-- Check if commission is NULL
IF v_comm IS NULL THEN
-- Set commission to 0
v_comm := 0;
END IF;
-- Calculate bonus
v_bonus := v_salary * 0.1;
-- Update employee table with bonus
UPDATE emp SET bonus = v_bonus WHERE empno = v_empno;
EXCEPTION
-- Handle any errors
WHEN OTHERS THEN
-- Rollback any changes
ROLLBACK;
-- Raise exception
RAISE;
END;
|
DECLARE
*** and cursors
@my_cursor SYS_REFCURSOR;
my_var dbo.VARCHAR2(20);
BEGIN
*** nd store results in a cursor
SELECT name FROM employees WHERE department = 'Engineering';
*** cursor and process each row
WHILE 1=1 BEGIN
FETCH my_cursor INTO @my_var;
IF @@FETCH_STATUS <> 0 BREAK;
*** the data in my_var
END;
-- Cl... SQLINES DEMO ***
CLOSE my_cursor;
*** on that might occur during the execution
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/ | DECLARE
-- Define variables and cursors
my_cursor SYS_REFCURSOR;
my_var VARCHAR2(20);
BEGIN
-- Execute a query and store results in a cursor
OPEN my_cursor FOR
SELECT name FROM employees WHERE department = 'Engineering';
-- Iterate over the cursor and process each row
LOOP
FETCH my_cursor INTO my_var;
EXIT WHEN my_cursor%NOTFOUND;
-- Do something with the data in my_var
END LOOP;
-- Close the cursor
CLOSE my_cursor;
-- Handle an exception that might occur during the execution
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || 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_amount
FROM orders
WHERE customer_id = customer_id;
RETURN orders_cursor;
END;
DECLARE
@customer_id FLOAT := 100;
orders_cursor SYS_REFCURSOR;
order_id NUMBER;
order_date DATE;
total_amount NUMBER;
BEGIN
SET @orders_cursor = dbo.get_customer_orders(@customer_id);
WHILE 1=1 BEGIN
FETCH orders_cursor INTO @order_id, @order_date, @total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(order_id, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(order_date, ''));
DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(total_amount, ''));
END;
CLOSE orders_cursor;
END;
|
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR
IS
orders_cursor SYS_REFCURSOR;
BEGIN
OPEN orders_cursor FOR
SELECT order_id,
order_date,
total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN orders_cursor;
END;
DECLARE
customer_id NUMBER := 100;
orders_cursor SYS_REFCURSOR;
order_id NUMBER;
order_date DATE;
total_amount NUMBER;
BEGIN
orders_cursor := get_customer_orders(customer_id);
LOOP
FETCH orders_cursor INTO order_id, order_date, total_amount;
EXIT WHEN orders_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || order_date);
DBMS_OUTPUT.PUT_LINE('Total Amount: ' || total_amount);
END LOOP;
CLOSE orders_cursor;
END;
|