sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('GetCustomerOrders', 'FN') IS NOT NULL
DROP FUNCTION GetCustomerOrders;
GO
CREATE FUNCTION GetCustomerOrders(@customer_id FLOAT) RETURNS CURSOR AS
BEGIN
DECLARE @orders CURSOR;
SELECT order_id, order_date, total_price
FROM orders
WHERE @customer_id = @customer_id;
RETURN @orders;
END;
DECLARE
order_cursor CURSOR;
order_id NUMBER;
order_date DATE;
total_price NUMBER;
BEGIN
SET @order_cursor = dbo.GetCustomerOrders(10);
WHILE 1=1 BEGIN
FETCH order_cursor INTO @order_id, @order_date, @total_price;
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 Price: ' + isnull(total_price, ''));
END;
CLOSE order_cursor;
END;
|
CREATE OR REPLACE FUNCTION GetCustomerOrders(customer_id NUMBER) RETURN CURSOR AS
orders CURSOR;
BEGIN
OPEN orders FOR
SELECT order_id, order_date, total_price
FROM orders
WHERE customer_id = customer_id;
RETURN orders;
END;
DECLARE
order_cursor CURSOR;
order_id NUMBER;
order_date DATE;
total_price NUMBER;
BEGIN
order_cursor := GetCustomerOrders(10);
LOOP
FETCH order_cursor INTO order_id, order_date, total_price;
EXIT WHEN order_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || order_date);
DBMS_OUTPUT.PUT_LINE('Total Price: ' || total_price);
END LOOP;
CLOSE order_cursor;
END;
|
BEGIN
DECLARE @i FLOAT;
DECLARE @j FLOAT;
DECLARE @c_id FLOAT;
CURSOR c1 IS SELECT customer_id, SUM(amount) FROM orders GROUP BY customer_id HAVING SUM(amount) > 1000;
OPEN c1;
FETCH c1 INTO @i, @j;
CLOSE c1;
IF @i IS NOT NULL
BEGIN
UPDATE customers SET credit_limit = credit_limit + 100 WHERE customer_id = @i;
SET @c_id = @i;
END;
COMMIT;
SELECT @c_id, SUM(amount) FROM orders WHERE customer_id = @c_id GROUP BY @c_id;
END; | BEGIN
DECLARE i NUMBER;
DECLARE j NUMBER;
DECLARE c_id NUMBER;
CURSOR c1 IS SELECT customer_id, SUM(amount) FROM orders GROUP BY customer_id HAVING SUM(amount) > 1000;
OPEN c1;
FETCH c1 INTO i, j;
CLOSE c1;
IF i IS NOT NULL THEN
BEGIN
UPDATE customers SET credit_limit = credit_limit + 100 WHERE customer_id = i;
c_id := i;
END;
END IF;
COMMIT;
SELECT c_id, SUM(amount) FROM orders WHERE customer_id = c_id GROUP BY c_id;
END; |
DECLARE
@num1 DECIMAL(5, 2) := 10.50;
num2 ROW_NUMBER(5, 2) := 20.25;
result ROW_NUMBER(10, 4);
BEGIN
SET @result = (@num1 + num2) / 2;
DBMS_OUTPUT.PUT_LINE('Average of ' + isnull(@num1, '') + ' and ' + isnull(num2, '') + ' is ' + isnull(result, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
DECLARE
num1 NUMBER(5, 2) := 10.50;
num2 NUMBER(5, 2) := 20.25;
result NUMBER(10, 4);
BEGIN
result := (num1 + num2) / 2;
DBMS_OUTPUT.PUT_LINE('Average of ' || num1 || ' and ' || num2 || ' is ' || result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
DECLARE
@salary DECIMAL(10, 2);
empno ROW_NUMBER(4);
BEGIN
SELECT @salary = @salary FROM employees WHERE empno = 7788;
IF @salary < 10000 BEGIN
UPDATE employees SET salary = @salary * 1.1 WHERE empno = 7788;
END
ELSE BEGIN
RAISERROR('-20000 Salary too high for raise', 16, 1);
END
END;
|
DECLARE
salary NUMBER(10, 2);
empno NUMBER(4);
BEGIN
SELECT salary INTO salary FROM employees WHERE empno = 7788;
IF salary < 10000 THEN
UPDATE employees SET salary = salary * 1.1 WHERE empno = 7788;
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Salary too high for raise');
END IF;
END;
|
CREATE PROCEDURE proc_example (
@in_param IN AS NUMBER
) IS
out_param OUT NUMBER;
BEGIN
SELECT @out_param = COUNT(*)
FROM table_example
WHERE column1 = in_param;
IF @@FETCH_STATUS <> 0 BEGIN
set @out_param = null;
END
END;
DECLARE
@v_result FLOAT;
BEGIN
dbo.proc_example(2, @v_result);
IF @v_result IS NULL BEGIN
RAISERROR('-20001 No data found for input parameter', 16, 1);
END
END;
|
CREATE PROCEDURE proc_example (
in_param IN NUMBER
) IS
out_param OUT NUMBER;
BEGIN
SELECT COUNT(*)
INTO out_param
FROM table_example
WHERE column1 = in_param;
EXCEPTION
WHEN NO_DATA_FOUND THEN
out_param := null;
END;
DECLARE
v_result NUMBER;
BEGIN
proc_example(2, v_result);
IF v_result IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No data found for input parameter');
END IF;
END;
|
DECLARE
@x FLOAT := 10;
y SET @NUMBER = 20;
BEGIN
IF @x > y BEGIN
DBMS_OUTPUT.PUT_LINE('x is greater than y');
END
ELSE IF @x < y BEGIN
DBMS_OUTPUT.PUT_LINE('x is less than y');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('x is equal to y');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
DECLARE
x NUMBER := 10;
y NUMBER := 20;
BEGIN
IF x > y THEN
DBMS_OUTPUT.PUT_LINE('x is greater than y');
ELSIF x < y THEN
DBMS_OUTPUT.PUT_LINE('x is less than y');
ELSE
DBMS_OUTPUT.PUT_LINE('x is equal to y');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
|
CREATE PROCEDURE emp_salary_raise(@emp_id FLOAT, @raise_percent FLOAT) AS
BEGIN
SET NOCOUNT ON;
/* SQLINES DEMO *** alary with given percentage raise */
UPDATE employees
SET salary = salary * (1 + @raise_percent / 100)
WHERE employee_id = @emp_id;
/* SQLINES DEMO *** the given employee ID does not exist */
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 Employee ID not found', 16, 1);
END
COMMIT;
END;
CREATE FUNCTION avg_salary(@dept_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @avg_sal FLOAT;
/* SQLINES DEMO *** salary for a given department */
SELECT @avg_sal = AVG(salary)
FROM employees
WHERE department_id = @dept_id;
/* SQLINES DEMO *** e salary or NULL if no employees in the department */
RETURN COALESCE(@avg_sal, NULL);
END;
SQL> BEGIN
dbo.emp_salary_raise(20, 10);
/* SQLINES DEMO *** handling by calling the function with an invalid department ID */
dbo.avg_salary(100);
END;
/
|
PROCEDURE emp_salary_raise(emp_id NUMBER, raise_percent NUMBER) IS
BEGIN
/* Update employee salary with given percentage raise */
UPDATE employees
SET salary = salary * (1 + raise_percent / 100)
WHERE employee_id = emp_id;
/* Raise an error if the given employee ID does not exist */
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee ID not found');
END IF;
COMMIT;
END;
FUNCTION avg_salary(dept_id NUMBER) RETURN NUMBER IS
avg_sal NUMBER;
BEGIN
/* Calculate average salary for a given department */
SELECT AVG(salary)
INTO avg_sal
FROM employees
WHERE department_id = dept_id;
/* Return the average salary or NULL if no employees in the department */
RETURN COALESCE(avg_sal, NULL);
END;
SQL> BEGIN
emp_salary_raise(20, 10);
/* Demonstrate error handling by calling the function with an invalid department ID */
avg_salary(100);
END;
/
|
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
INSERT INTO employees(id, name, salary) VALUES (i, 'Employee' + isnull(i, ''), i * 1000);
END;
DECLARE
@v_name VARCHAR(20);
v_salary NUMBER;
BEGIN
SELECT @v_name = name, @v_salary = salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@v_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_salary, ''));
END;
DECLARE
@v_error_msg VARCHAR(200);
BEGIN
BEGIN
INSERT INTO employees(id, name, salary) VALUES (11, 'Employee11', 11000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
SET @v_error_msg = SQLERRM;
END;
IF @v_error_msg IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + isnull(@v_error_msg, ''));
END
END;
END; | BEGIN
FOR i IN 1..10 LOOP
INSERT INTO employees(id, name, salary) VALUES (i, 'Employee' || i, i * 1000);
END LOOP;
DECLARE
v_name VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT name, salary INTO v_name, v_salary FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
END;
DECLARE
v_error_msg VARCHAR2(200);
BEGIN
BEGIN
INSERT INTO employees(id, name, salary) VALUES (11, 'Employee11', 11000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
v_error_msg := SQLERRM;
END;
IF v_error_msg IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee inserted successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || v_error_msg);
END IF;
END;
END; |
CREATE PROCEDURE update_employee_salary(
@p_employee_id IN AS NUMBER,
p_salary_increase IN NUMBER
)
IS
l_current_salary NUMBER;
BEGIN
SELECT @l_current_salary = salary
FROM employees
WHERE employee_id = p_employee_id;
IF l_current_salary IS NULL BEGIN
RAISERROR('-20001 Employee not found for the employee_id: ' + isnull(p_employee_id, ''), 16, 1);
END
UPDATE employees
SET salary = l_current_salary + p_salary_increase
WHERE employee_id = p_employee_id;
END;
|
CREATE PROCEDURE update_employee_salary(
p_employee_id IN NUMBER,
p_salary_increase IN NUMBER
)
IS
l_current_salary NUMBER;
BEGIN
SELECT salary INTO l_current_salary
FROM employees
WHERE employee_id = p_employee_id;
IF l_current_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found for the employee_id: ' || p_employee_id);
END IF;
UPDATE employees
SET salary = l_current_salary + p_salary_increase
WHERE employee_id = p_employee_id;
END;
|
IF OBJECT_ID('insert_emp', 'P') IS NOT NULL
DROP PROCEDURE insert_emp;
GO
CREATE PROCEDURE insert_emp(@p_emp_id IN AS NUMBER, p_name IN VARCHAR2, p_job IN VARCHAR2) AS
l_empid NUMBER;
BEGIN
SELECT @l_empid = emp_id FROM emp WHERE emp_id = p_emp_id;
IF l_empid IS NULL BEGIN
INSERT INTO emp (emp_id, ename, job) VALUES (p_emp_id, p_name, p_job);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee inserted');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee already exists');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting employee: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE OR REPLACE PROCEDURE insert_emp(p_emp_id IN NUMBER, p_name IN VARCHAR2, p_job IN VARCHAR2) AS
l_empid NUMBER;
BEGIN
SELECT emp_id INTO l_empid FROM emp WHERE emp_id = p_emp_id;
IF l_empid IS NULL THEN
INSERT INTO emp (emp_id, ename, job) VALUES (p_emp_id, p_name, p_job);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee inserted');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee already exists');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting employee: ' || SQLERRM);
END;
/
|
IF OBJECT_ID('PrintEmpSalaries', 'P') IS NOT NULL
DROP PROCEDURE PrintEmpSalaries;
GO
CREATE PROCEDURE PrintEmpSalaries
AS
BEGIN
SET NOCOUNT ON;
DECLARE emp_row CURSOR FOR
SELECT emp_id, first_name, last_name, salary
FROM employees;
OPEN emp_row;
FETCH emp_row INTO;
WHILE @@FETCH_STATUS=0
BEGIN
dbms_output.put_line(isnull(@emp_row_emp_id, '') + ' ' + isnull(@emp_row_first_name, '') + ' ' + isnull(@emp_row_last_name, '') + ' ' + isnull(@emp_row_salary, ''));
FETCH emp_row INTO;
END;
CLOSE emp_row;
DEALLOCATE emp_row;
END;
GO
BEGIN
PrintEmpSalaries;
END;
/
|
CREATE OR REPLACE PROCEDURE PrintEmpSalaries
AS
BEGIN
FOR emp_row IN (
SELECT emp_id, first_name, last_name, salary
FROM employees
)
LOOP
dbms_output.put_line(emp_row.emp_id || ' ' || emp_row.first_name || ' ' || emp_row.last_name || ' ' || emp_row.salary);
END LOOP;
END;
/
BEGIN
PrintEmpSalaries;
END;
/
|
IF OBJECT_ID('get_total_sales', 'FN') IS NOT NULL
DROP FUNCTION get_total_sales;
GO
CREATE FUNCTION get_total_sales(@region_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
total_sales NUMBER;
BEGIN
SELECT @total_sales = SUM(sales)
FROM sales
WHERE region_id = region_id;
RETURN total_sales;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN 0;
END
END;
|
CREATE OR REPLACE FUNCTION get_total_sales(region_id IN NUMBER) RETURN NUMBER AS
total_sales NUMBER;
BEGIN
SELECT SUM(sales) INTO total_sales
FROM sales
WHERE region_id = region_id;
RETURN total_sales;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
|
BEGIN
DECLARE @v_salary FLOAT := 1000;
DECLARE @v_bonus FLOAT := 500;
DECLARE @v_total_salary FLOAT;
BEGIN
SET @v_total_salary = @v_salary + @v_bonus;
IF @v_total_salary > 1500 BEGIN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 1500');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to 1500');
END
END;
END; | BEGIN
DECLARE v_salary NUMBER := 1000;
DECLARE v_bonus NUMBER := 500;
DECLARE v_total_salary NUMBER;
BEGIN
v_total_salary := v_salary + v_bonus;
IF v_total_salary > 1500 THEN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 1500');
ELSE
DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to 1500');
END IF;
END;
END; |
CREATE PROCEDURE UpdateEmployeeSalary(@IN employeeId AS INTEGER, IN salary INTEGER) AS
BEGIN
DECLARE @oldSalary INTEGER;
*** d salary
SELECT @oldSalary = salary FROM employees WHERE employeeId = employeeId;
*** salary is different
IF @oldSalary <> salary BEGIN
*** ee's salary
UPDATE employees SET salary = salary WHERE employeeId = employeeId;
*** a table
INSERT INTO salary_history (employeeId, oldSalary, newSalary) VALUES (employeeId, @oldSalary, salary);
END
END; | CREATE PROCEDURE UpdateEmployeeSalary(IN employeeId INTEGER, IN salary INTEGER) AS
BEGIN
DECLARE oldSalary INTEGER;
-- Get employee's old salary
SELECT salary INTO oldSalary FROM employees WHERE employeeId = employeeId;
-- Check if the new salary is different
IF oldSalary <> salary THEN
-- Update the employee's salary
UPDATE employees SET salary = salary WHERE employeeId = employeeId;
-- Log the change in a table
INSERT INTO salary_history (employeeId, oldSalary, newSalary) VALUES (employeeId, oldSalary, salary);
END IF;
END; |
IF OBJECT_ID('calc_bonus', 'FN') IS NOT NULL
DROP FUNCTION calc_bonus;
GO
CREATE FUNCTION calc_bonus(@salary IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
bonus NUMBER;
BEGIN
IF salary > 5000 BEGIN
set @bonus = salary * 0.1;
END
ELSE BEGIN
set @bonus = 0;
END
RETURN bonus;
END;
/
DECLARE
@emp_salary FLOAT;
BEGIN
SET @emp_salary = 6000;
DBMS_OUTPUT.PUT_LINE('Bonus for salary ' + isnull(@emp_salary, '') + ' is ' + isnull(dbo.calc_bonus(@emp_salary), ''));
SET @emp_salary = 4500;
DBMS_OUTPUT.PUT_LINE('Bonus for salary ' + isnull(@emp_salary, '') + ' is ' + isnull(dbo.calc_bonus(@emp_salary), ''));
END;
/
|
CREATE OR REPLACE FUNCTION calc_bonus(salary IN NUMBER) RETURN NUMBER IS
bonus NUMBER;
BEGIN
IF salary > 5000 THEN
bonus := salary * 0.1;
ELSE
bonus := 0;
END IF;
RETURN bonus;
END;
/
DECLARE
emp_salary NUMBER;
BEGIN
emp_salary := 6000;
DBMS_OUTPUT.PUT_LINE('Bonus for salary ' || emp_salary || ' is ' || calc_bonus(emp_salary));
emp_salary := 4500;
DBMS_OUTPUT.PUT_LINE('Bonus for salary ' || emp_salary || ' is ' || calc_bonus(emp_salary));
END;
/
|
BEGIN
DECLARE rec CURSOR FOR SELECT * FROM emp; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = @rec_empno;
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
COMMIT;
END;
/ | BEGIN
FOR rec IN (SELECT * FROM emp) LOOP
UPDATE emp SET sal = sal * 1.10 WHERE empno = rec.empno;
END LOOP;
COMMIT;
END;
/ |
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE emp_id = 10;
IF v_salary > 100000 BEGIN
raiserror('-20001 Salary too high!', 16, 1);
END
UPDATE employees SET salary = v_salary + 1000 WHERE emp_id = @v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary: ' + ISNULL(SQLERRM, ''));
END;
/
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE emp_id = 10;
IF v_salary > 100000 THEN
raise_application_error(-20001, 'Salary too high!');
END IF;
UPDATE employees SET salary = v_salary + 1000 WHERE emp_id = v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error updating salary: ' || SQLERRM);
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;
IF salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
RETURN salary;
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
DBMS_OUTPUT.PUT_LINE('Current salary: ' + isnull(salary, ''));
dbo.update_employee_salary(@emp_id, salary + 1000);
SET @salary = dbo.get_employee_salary(@emp_id);
DBMS_OUTPUT.PUT_LINE('New salary: ' + isnull(salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
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;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
RETURN salary;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END IF;
END;
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
DBMS_OUTPUT.PUT_LINE('Current salary: ' || salary);
update_employee_salary(emp_id, salary + 1000);
salary := get_employee_salary(emp_id);
DBMS_OUTPUT.PUT_LINE('New salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc(
@in_param IN AS VARCHAR2,
out_param OUT NUMBER)
AS
BEGIN
SELECT @out_param = COUNT(*)
FROM my_table
WHERE my_column = in_param;
IF out_param = 0 BEGIN
RAISERROR('-20001 No rows found for ' + isnull(in_param, ''), 16, 1);
END
END;
/
BEGIN
dbo.my_proc('my_value', :out_param);
IF :out_param > 0 BEGIN
*** the out parameter...
END
END;
|
CREATE OR REPLACE PROCEDURE my_proc(
in_param IN VARCHAR2,
out_param OUT NUMBER)
AS
BEGIN
SELECT COUNT(*) INTO out_param
FROM my_table
WHERE my_column = in_param;
IF out_param = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found for ' || in_param);
END IF;
END;
/
BEGIN
my_proc('my_value', :out_param);
IF :out_param > 0 THEN
-- Do something with the out parameter...
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary
FROM emp
WHERE emp_id = 10;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary is too high', 16, 1);
END
UPDATE emp
SET salary = v_salary + 1000
WHERE emp_id = @v_emp_id;
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary
INTO v_emp_id, v_salary
FROM emp
WHERE emp_id = 10;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary is too high');
END IF;
UPDATE emp
SET salary = v_salary + 1000
WHERE emp_id = v_emp_id;
COMMIT;
END;
|
BEGIN
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'Scott';
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the last name Scott.');
END
END;
END; | BEGIN
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'Scott';
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the last name Scott.');
END;
END; |
CREATE TABLE t(id FLOAT, name VARCHAR(20));
INSERT INTO t VALUES(1,'John');
INSERT INTO t VALUES(2,'Mary');
COMMIT;
DECLARE
@cursor c IS
SELECT * FROM t;
BEGIN
DECLARE rec CURSOR FOR c LOOP
dbms_output.put_line(isnull(@rec_id, '') + ' ' + isnull(@rec_name, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@fetch_status <> 0 BEGIN
dbms_output.put_line('No data found in table');
END
END;
|
CREATE TABLE t(id NUMBER, name VARCHAR2(20));
INSERT INTO t VALUES(1,'John');
INSERT INTO t VALUES(2,'Mary');
COMMIT;
DECLARE
cursor c IS
SELECT * FROM t;
BEGIN
FOR rec IN c LOOP
dbms_output.put_line(rec.id || ' ' || rec.name);
END LOOP;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No data found in table');
END;
|
CREATE PROCEDURE my_sp (@param1 IN AS NUMBER, param2 OUT NUMBER) IS
BEGIN
set @param2 = param1 * 2;
IF param2 > 10 BEGIN
RAISERROR('-20000 Result is too large', 16, 1);
END
END;
|
CREATE PROCEDURE my_sp (param1 IN NUMBER, param2 OUT NUMBER) IS
BEGIN
param2 := param1 * 2;
IF param2 > 10 THEN
RAISE_APPLICATION_ERROR(-20000, 'Result is too large');
END IF;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
IF salary IS NOT NULL BEGIN
dbo.update_employee_salary(@emp_id, salary * 1.10);
END
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
IF salary IS NOT NULL THEN
update_employee_salary(emp_id, salary * 1.10);
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
|
DECLARE
@emp_id FLOAT := 1234;
BEGIN
DECLARE emp_row CURSOR FOR
SELECT * FROM employees WHERE employee_id = @emp_id;
OPEN emp_row;
FETCH emp_row INTO;
WHILE @@FETCH_STATUS=0
BEGIN
IF @emp_row_salary > 5000 BEGIN
UPDATE employees SET department_id = 20 WHERE employee_id = @emp_id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary must be greater than 5000', 16, 1);
END
FETCH emp_row INTO;
END;
CLOSE emp_row;
DEALLOCATE emp_row;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 No employee found with employee_id ' + isnull(@emp_id, ''), 16, 1);
END
END;
|
DECLARE
emp_id NUMBER := 1234;
BEGIN
FOR emp_row IN (
SELECT * FROM employees WHERE employee_id = emp_id
) LOOP
IF emp_row.salary > 5000 THEN
UPDATE employees SET department_id = 20 WHERE employee_id = emp_id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary must be greater than 5000');
END IF;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'No employee found with employee_id ' || emp_id);
END;
|
DECLARE
@v_x BIGINT := 10;
BEGIN
BEGIN
SET @v_x = @v_x + 10;
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_x = 1;
END
END;
IF @v_x > 10 BEGIN
UPDATE table SET column_name = @v_x;
COMMIT;
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_x NUMBER(10) := 10;
BEGIN
BEGIN
v_x := v_x + 10;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_x := 1;
END;
IF v_x > 10 THEN
UPDATE table SET column_name = v_x;
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@salary FLOAT := 10000;
bonus SET @NUMBER = 0;
BEGIN
IF @salary > 15000 BEGIN
SET @bonus = @salary * 0.15;
END
ELSE IF @salary > 10000 BEGIN
SET @bonus = @salary * 0.10;
END
UPDATE employees
SET bonus = bonus + :BONUS
WHERE employee_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 An error occurred during the salary calculation.', 16, 1);
END;
|
DECLARE
salary NUMBER := 10000;
bonus NUMBER := 0;
BEGIN
IF salary > 15000 THEN
bonus := salary * 0.15;
ELSIF salary > 10000 THEN
bonus := salary * 0.10;
END IF;
UPDATE employees
SET bonus = bonus + :BONUS
WHERE employee_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'An error occurred during the salary calculation.');
END;
|
DECLARE
@cursor c_emp IS
SELECT empno, empname, deptno
FROM emp;
BEGIN
DECLARE rec CURSOR FOR c_emp LOOP
DBMS_OUTPUT.put_line(@rec_empname);
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/
|
DECLARE
cursor c_emp IS
SELECT empno, empname, deptno
FROM emp;
BEGIN
FOR rec IN c_emp LOOP
DBMS_OUTPUT.put_line(rec.empname);
END LOOP;
END;
/
|
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_employee(
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
salary IN NUMBER
)
IS
BEGIN
INSERT INTO employees (first_name, last_name, email, salary)
VALUES (first_name, last_name, email, salary);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE insert_employee(
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
salary IN NUMBER
)
IS
BEGIN
INSERT INTO employees (first_name, last_name, email, salary)
VALUES (first_name, last_name, email, salary);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('get_customer_balance', 'FN') IS NOT NULL
DROP FUNCTION get_customer_balance;
GO
CREATE FUNCTION get_customer_balance(@customer_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
DECLARE @balance FLOAT;
BEGIN
SELECT @balance = @balance FROM accounts WHERE customer_id = customer_id;
RETURN @balance;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
END $$
| DELIMITER $$
CREATE OR REPLACE FUNCTION get_customer_balance(customer_id IN NUMBER)
RETURN NUMBER AS
BEGIN
DECLARE balance NUMBER;
BEGIN
SELECT balance INTO balance FROM accounts WHERE customer_id = customer_id;
RETURN balance;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
END $$
DELIMITER ; |
IF OBJECT_ID('get_total_sales', 'FN') IS NOT NULL
DROP FUNCTION get_total_sales;
GO
CREATE FUNCTION get_total_sales(@cust_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
total_sales NUMBER;
BEGIN
SELECT @total_sales = SUM(sales)
FROM sales
WHERE customer_id = cust_id;
RETURN total_sales;
END;
|
CREATE OR REPLACE FUNCTION get_total_sales(cust_id IN NUMBER) RETURN NUMBER IS
total_sales NUMBER;
BEGIN
SELECT SUM(sales) INTO total_sales
FROM sales
WHERE customer_id = cust_id;
RETURN total_sales;
END;
|
DECLARE
@v_emp_id INT;
v_salary ROW_NUMBER(10,2);
BEGIN
UPDATE employees SET salary = salary * 1.1
WHERE job_id = 'SA_REP';
SELECT @v_emp_id = emp_id, @v_salary = salary
FROM employees
WHERE emp_id = 100;
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_emp_id, '') + ' has salary ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee ' + isnull(@v_emp_id, ''));
END
END;
|
DECLARE
v_emp_id NUMBER(6);
v_salary NUMBER(10,2);
BEGIN
UPDATE employees SET salary = salary * 1.1
WHERE job_id = 'SA_REP';
SELECT emp_id, salary INTO v_emp_id, v_salary
FROM employees
WHERE emp_id = 100;
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_id || ' has salary ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee ' || v_emp_id);
END;
|
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
END;
CREATE PROCEDURE calc_order_total(@order_id IN AS NUMBER)
AS
total NUMBER;
BEGIN
SELECT @total = SUM(quantity * unit_price) FROM order_items WHERE order_id = order_id;
UPDATE orders SET total = total WHERE order_id = order_id;
END;
CREATE FUNCTION get_customer_name(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
SELECT @name = name FROM customers WHERE customer_id = customer_id;
RETURN name;
END;
WHILE 1=1 BEGIN
SELECT customer_id FROM customers INTO customer_id;
DBMS_OUTPUT.PUT_LINE('Processing order for customer: ' + isnull(dbo.get_customer_name(customer_id), ''));
EXIT WHEN customer_id IS NULL;
END;
BEGIN
*** n exception
SELECT * FROM non_existent_table;
IF @@fetch_status <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END;
|
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
END;
CREATE PROCEDURE calc_order_total(order_id IN NUMBER)
AS
total NUMBER;
BEGIN
SELECT SUM(quantity * unit_price) INTO total FROM order_items WHERE order_id = order_id;
UPDATE orders SET total = total WHERE order_id = order_id;
END;
FUNCTION get_customer_name(customer_id IN NUMBER) RETURN VARCHAR2
AS
BEGIN
SELECT name INTO name FROM customers WHERE customer_id = customer_id;
RETURN name;
END;
LOOP
SELECT customer_id FROM customers INTO customer_id;
DBMS_OUTPUT.PUT_LINE('Processing order for customer: ' || get_customer_name(customer_id));
EXIT WHEN customer_id IS NULL;
END LOOP;
BEGIN
-- This will raise an exception
SELECT * FROM non_existent_table;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END;
|
BEGIN
-- De... SQLINES DEMO ***
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
BEGIN
*** tails
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 100;
*** alary
UPDATE employees
SET salary = v_salary + 1000
WHERE emp_id = @v_emp_id;
-- Co... SQLINES DEMO ***
COMMIT;
*** mployee details
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(@v_emp_id, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END; | BEGIN
-- Declare variables
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
BEGIN
-- Fetch employee details
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 100;
-- Update employee salary
UPDATE employees
SET salary = v_salary + 1000
WHERE emp_id = v_emp_id;
-- Commit changes
COMMIT;
-- Display updated employee details
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || v_emp_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END; |
CREATE TABLE orders (orderid FLOAT, orderdate DATE, customerid FLOAT);
CREATE PROCEDURE insert_order (@i_orderid IN AS NUMBER, i_orderdate IN DATE, i_customerid IN NUMBER) IS
BEGIN
INSERT INTO orders (orderid, orderdate, customerid)
VALUES (i_orderid, i_orderdate, i_customerid);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE FUNCTION get_order (@i_orderid IN NUMBERDECLARE ) RETURN
BEGIN orders%ROWTYPE
RETURN NULL;
END; IS
BEGIN
RETURN (SELECT * FROM orders WHERE orderid = i_orderid);
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
-- In... SQLINES DEMO ***
dbo.insert_order(1001, GETDATE(), 101);
*** ails
DECLARE rec CURSOR FOR SELECT * FROM orders; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.Put_Line('Order ID: ' + isnull(@rec_orderid, ''));
DBMS_OUTPUT.Put_Line('Order Date: ' + isnull(@rec_orderdate, ''));
DBMS_OUTPUT.Put_Line('Customer ID: ' + isnull(@rec_customerid, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE TABLE orders (orderid NUMBER, orderdate DATE, customerid NUMBER);
CREATE PROCEDURE insert_order (i_orderid IN NUMBER, i_orderdate IN DATE, i_customerid IN NUMBER) IS
BEGIN
INSERT INTO orders (orderid, orderdate, customerid)
VALUES (i_orderid, i_orderdate, i_customerid);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE FUNCTION get_order (i_orderid IN NUMBER) RETURN orders%ROWTYPE IS
BEGIN
RETURN (SELECT * FROM orders WHERE orderid = i_orderid);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
-- Insert a new order
insert_order(1001, SYSDATE, 101);
-- Get the order details
FOR rec IN (SELECT * FROM orders) LOOP
DBMS_OUTPUT.Put_Line('Order ID: ' || rec.orderid);
DBMS_OUTPUT.Put_Line('Order Date: ' || rec.orderdate);
DBMS_OUTPUT.Put_Line('Customer ID: ' || rec.customerid);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@employee_id FLOAT)
RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @name VARCHAR(100);
SELECT @name = isnull(last_name, '') + ', ' + isnull(first_name, '')
FROM employees
WHERE @employee_id = @employee_id;
RETURN @name;
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(employee_id NUMBER)
RETURN VARCHAR2 IS
name VARCHAR2(100);
BEGIN
SELECT last_name || ', ' || first_name
INTO name
FROM employees
WHERE employee_id = employee_id;
RETURN name;
END;
|
DECLARE
@name VARCHAR(255);
BEGIN
SELECT @name = @name FROM employees WHERE employee_id = 1;
<<outer_block>>
IF @name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
BEGIN
<<inner_block>>
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in inner block: ' + ISNULL(DBMS_ERROR_TEXT, ''));
GOTO outer_block;
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in outer block: ' + ISNULL(DBMS_ERROR_TEXT, ''));
END;
END;
|
DECLARE
name VARCHAR2(255);
BEGIN
SELECT name INTO name FROM employees WHERE employee_id = 1;
<<outer_block>>
IF name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
BEGIN
<<inner_block>>
DBMS_OUTPUT.PUT_LINE('Employee name: ' || name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in inner block: ' || DBMS_ERROR_TEXT);
GOTO outer_block;
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in outer block: ' || DBMS_ERROR_TEXT);
END;
END;
|
BEGIN
BEGIN
DECLARE r CURSOR FOR SELECT * FROM employees; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@r_first_name, '') + ' ' + isnull(@r_last_name, ''));
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found');
END
END;
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;
DECLARE
@x INTEGER := 1;
BEGIN
WHILE @x < 10 BEGIN
DBMS_OUTPUT.PUT_LINE(@x);
SET @x = @x + 1;
END;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows returned');
END;
END;
|
BEGIN
BEGIN
FOR r IN (SELECT * FROM employees) LOOP
DBMS_OUTPUT.PUT_LINE(r.first_name || ' ' || r.last_name);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found');
END;
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;
DECLARE
x INTEGER := 1;
BEGIN
WHILE x < 10 LOOP
DBMS_OUTPUT.PUT_LINE(x);
x := x + 1;
END LOOP;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many rows returned');
END;
END;
|
DECLARE
@v_emp_id FLOAT := 100;
v_emp_name dbo.VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT @v_emp_name = ename, @v_salary = sal FROM emp WHERE empno = @v_emp_id;
IF v_emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found.', 16, 1);
END
ELSE IF v_salary >= 10000 BEGIN
UPDATE emp SET comm = 0.1 * sal WHERE empno = @v_emp_id;
END
DECLARE c CURSOR FOR SELECT * FROM COMPANY; OPEN c;
FETCH c INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@c_cname, '') + ', ' + isnull(@c_city, ''));
FETCH c INTO;
END;
CLOSE c;
DEALLOCATE c;
END;
|
DECLARE
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT ename, sal INTO v_emp_name, v_salary FROM emp WHERE empno = v_emp_id;
IF v_emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found.');
ELSIF v_salary >= 10000 THEN
UPDATE emp SET comm = 0.1 * sal WHERE empno = v_emp_id;
END IF;
FOR c IN (SELECT * FROM COMPANY) LOOP
DBMS_OUTPUT.PUT_LINE(c.cname || ', ' || c.city);
END LOOP;
END;
|
DECLARE @name VARCHAR(50);
BEGIN
SELECT @name = name WHERE id = 1;
IF @name IS NULL BEGIN
PRINT 'No record found with id = 1';
END
ELSE BEGIN
PRINT 'Name = ' + @name;
END
UPDATE table SET name = 'New Name' WHERE id = 1;
PRINT 'Updated record with id = 1';
PRINT 'New name = ' + (SELECT name from table WHERE id = 1);
END;
|
DECLARE @name VARCHAR(50);
BEGIN
SELECT name INTO @name WHERE id = 1;
IF @name IS NULL THEN
PRINT 'No record found with id = 1';
ELSE
PRINT 'Name = ' + @name;
END IF;
UPDATE table SET name = 'New Name' WHERE id = 1;
PRINT 'Updated record with id = 1';
PRINT 'New name = ' + (SELECT name from table WHERE id = 1);
END;
|
CREATE TABLE employees (
emp_id FLOAT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary DECIMAL(10,2)
);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (101, 'John', 'Doe', 50000);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (102, 'Jane', 'Smith', 60000);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (103, 'Michael', 'Jones', 70000);
COMMIT;
CREATE PROCEDURE get_employee (
@emp_id IN AS NUMBER,
OUT first_name VARCHAR2,
OUT last_name VARCHAR2,
OUT salary NUMBER
)
AS
BEGIN
SELECT @first_name = first_name, @last_name = last_name, @salary = salary
FROM employees
WHERE emp_id = emp_id;
END;
DECLARE
@first_name VARCHAR(50);
last_name dbo.VARCHAR2(50);
salary NUMBER;
BEGIN
dbo.get_employee(101, @first_name, last_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(@first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END
END;
|
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
salary NUMBER(10,2)
);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (101, 'John', 'Doe', 50000);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (102, 'Jane', 'Smith', 60000);
INSERT INTO employees (emp_id, first_name, last_name, salary)
VALUES (103, 'Michael', 'Jones', 70000);
COMMIT;
CREATE PROCEDURE get_employee (
emp_id IN NUMBER,
OUT first_name VARCHAR2,
OUT last_name VARCHAR2,
OUT salary NUMBER
)
AS
BEGIN
SELECT first_name, last_name, salary
INTO first_name, last_name, salary
FROM employees
WHERE emp_id = emp_id;
END;
DECLARE
first_name VARCHAR2(50);
last_name VARCHAR2(50);
salary NUMBER;
BEGIN
get_employee(101, first_name, last_name, salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@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 -1;
END
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
SET @salary = dbo.get_employee_salary(@emp_id);
IF salary > 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is highly paid.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is not highly paid.');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing.');
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 -1;
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
salary := get_employee_salary(emp_id);
IF salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('Employee is highly paid.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee is not highly paid.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing.');
END;
END;
|
IF OBJECT_ID('update_salary', 'P') IS NOT NULL
DROP PROCEDURE update_salary;
GO
CREATE PROCEDURE update_salary
(
@emp_id IN AS NUMBER,
new_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE PROCEDURE update_salary
(
emp_id IN NUMBER,
new_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE PROCEDURE get_employee_salary
(
@emp_id IN AS NUMBER,
salary OUT NUMBER
)
AS
BEGIN
SELECT @salary = salary
FROM employees
WHERE emp_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @salary = 0;
END
END;
|
CREATE PROCEDURE get_employee_salary
(
emp_id IN NUMBER,
salary OUT NUMBER
)
AS
BEGIN
SELECT salary
INTO salary
FROM employees
WHERE emp_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
salary := 0;
END;
|
DECLARE
@v_emp_name VARCHAR(40);
BEGIN
SELECT @v_emp_name = name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee name is: ' + isnull(@v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END; | DECLARE
v_emp_name VARCHAR2(40);
BEGIN
SELECT name INTO v_emp_name FROM employees WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Employee name is: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END; |
IF OBJECT_ID('UpdateEmployee', 'P') IS NOT NULL
DROP PROCEDURE UpdateEmployee;
GO
CREATE PROCEDURE UpdateEmployee(
@p_emp_id IN AS NUMBER,
p_last_name IN VARCHAR2)
IS
BEGIN
UPDATE employees SET last_name = p_last_name
WHERE emp_id = p_emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee with ID ' + isnull(p_emp_id, '') + ' not found', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE UpdateEmployee(
p_emp_id IN NUMBER,
p_last_name IN VARCHAR2)
IS
BEGIN
UPDATE employees SET last_name = p_last_name
WHERE emp_id = p_emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee with ID ' || p_emp_id || ' not found');
END;
|
CREATE PROCEDURE my_proc(@p_in IN AS NUMBER, p_out OUT NUMBER) AS
BEGIN
SELECT @p_out = COUNT(*) FROM my_table WHERE col1 = p_in;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_out = 0;
END
END;
|
CREATE PROCEDURE my_proc(p_in IN NUMBER, p_out OUT NUMBER) AS
BEGIN
SELECT COUNT(*) INTO p_out FROM my_table WHERE col1 = p_in;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_out := 0;
END;
|
DECLARE
@v_emp_id SMALLINT;
v_emp_name dbo.VARCHAR2(20);
v_salary ROW_NUMBER(10,2);
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM emp
WHERE emp_id = 10;
IF v_salary > 10000
BEGIN
UPDATE emp
SET salary = salary * 1.1
WHERE emp_id = @v_emp_id;
END
ELSE BEGIN
RAISERROR('-20001 Salary is too low for increment', 16, 1);
END
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER(4);
v_emp_name VARCHAR2(20);
v_salary NUMBER(10,2);
BEGIN
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM emp
WHERE emp_id = 10;
IF v_salary > 10000
THEN
UPDATE emp
SET salary = salary * 1.1
WHERE emp_id = v_emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary is too low for increment');
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_name VARCHAR(100);
BEGIN
SELECT @v_name = name FROM employees WHERE id = 1;
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE(@v_name);
END;
IF @@fetch_status <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 1.');
END
END; | DECLARE
v_name VARCHAR2(100);
BEGIN
SELECT name INTO v_name FROM employees WHERE id = 1;
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(v_name);
END LOOP;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 1.');
END; |
IF OBJECT_ID('get_average', 'FN') IS NOT NULL
DROP FUNCTION get_average;
GO
CREATE FUNCTION get_average(@salary FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @avg FLOAT;
SELECT @avg = AVG(@salary) FROM employees WHERE @salary >= @salary;
RETURN @avg;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
|
CREATE OR REPLACE FUNCTION get_average(salary NUMBER) RETURN NUMBER IS
avg NUMBER;
BEGIN
SELECT AVG(salary) INTO avg FROM employees WHERE salary >= salary;
RETURN avg;
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
|
IF OBJECT_ID('p1', 'P') IS NOT NULL
DROP PROCEDURE p1;
GO
CREATE PROCEDURE p1(@x IN AS VARCHAR2) IS
BEGIN
INSERT INTO t1 (x) VALUES (x);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
IF OBJECT_ID('f1', 'FN') IS NOT NULL
DROP FUNCTION f1;
GO
CREATE FUNCTION f1(@x IN VARCHAR2DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
BEGIN
RETURN UPPER(x);
END;
DECLARE
@v1 VARCHAR(20);
BEGIN
SET @v1 = dbo.f1('hello');
DBMS_OUTPUT.PUT_LINE(@v1);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred');
END;
|
CREATE OR REPLACE PROCEDURE p1(x IN VARCHAR2) IS
BEGIN
INSERT INTO t1 (x) VALUES (x);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE OR REPLACE FUNCTION f1(x IN VARCHAR2) RETURN VARCHAR2 IS
BEGIN
RETURN UPPER(x);
END;
DECLARE
v1 VARCHAR2(20);
BEGIN
v1 := f1('hello');
DBMS_OUTPUT.PUT_LINE(v1);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred');
END;
|
CREATE TABLE t1 (a FLOAT NOT NULL /* SQLines: Added for PK */, b VARCHAR(10));
ALTER TABLE t1 ADD CONSTRAINT t1_pk PRIMARY KEY (a);
INSERT INTO t1 (a, b) VALUES (1, 'a'), (2, 'b'), (3, 'c');
CREATE TYPE t_employees AS OBJECT
(
id NUMBER,
name VARCHAR(255),
address VARCHAR(255)
);
CREATE TABLE employees OF t_employees;
INSERT INTO employees (id, name, address) VALUES (1, 'John Doe', '123 Main Street');
CREATE PROCEDURE get_employee (@in_id FLOAT, @out_employee OUT AS t_employees)
AS
BEGIN
SELECT @out_employee = * FROM employees WHERE id = in_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @out_employee = NULL;
END
END;
CREATE FUNCTION avg_salary (@in_dept_id FLOAT) RETURNS FLOAT
AS
BEGIN
RETURN (SELECT AVG(salary) FROM employees WHERE dept_id = @in_dept_id);
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE
@emp t_employees;
sal NUMBER;
BEGIN
dbo.get_employee (1, @emp);
IF emp.id IS NOT NULL BEGIN
SET @sal = dbo.avg_salary (emp.dept_id);
IF sal IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(emp.name, '') + ' has an average salary of ' + isnull(sal, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No salary data found for employee ' + isnull(emp.name, ''));
END
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END; | CREATE TABLE t1 (a NUMBER, b VARCHAR(10));
ALTER TABLE t1 ADD CONSTRAINT t1_pk PRIMARY KEY (a);
INSERT INTO t1 (a, b) VALUES (1, 'a'), (2, 'b'), (3, 'c');
CREATE TYPE t_employees AS OBJECT
(
id NUMBER,
name VARCHAR(255),
address VARCHAR(255)
);
CREATE TABLE employees OF t_employees;
INSERT INTO employees (id, name, address) VALUES (1, 'John Doe', '123 Main Street');
CREATE PROCEDURE get_employee (in_id NUMBER, out_employee OUT t_employees)
AS
BEGIN
SELECT * INTO out_employee FROM employees WHERE id = in_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
out_employee := NULL;
END;
CREATE FUNCTION avg_salary (in_dept_id NUMBER) RETURN NUMBER
AS
BEGIN
RETURN (SELECT AVG(salary) FROM employees WHERE dept_id = in_dept_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE
emp t_employees;
sal NUMBER;
BEGIN
get_employee (1, emp);
IF emp.id IS NOT NULL THEN
sal := avg_salary (emp.dept_id);
IF sal IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || emp.name || ' has an average salary of ' || sal);
ELSE
DBMS_OUTPUT.PUT_LINE('No salary data found for employee ' || emp.name);
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END; |
CREATE TABLE emp (empno smallint, ename varchar(20), sal decimal(10,2), job varchar(20), deptno smallint, PRIMARY KEY (empno));
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (1, 'Alice', 1000, 'Clerk', 10);
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (2, 'Bob', 1500, 'Manager', 20);
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (3, 'Carol', 2000, 'Analyst', 30);
ALTER TABLE emp ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno);
CREATE TABLE dept (deptno smallint, dname varchar(20), PRIMARY KEY (deptno));
INSERT INTO dept (deptno, dname) VALUES (10, 'Sales');
INSERT INTO dept (deptno, dname) VALUES (20, 'Management');
INSERT INTO dept (deptno, dname) VALUES (30, 'Finance');
CREATE PROCEDURE give_raise (@empno float, @percent float)
AS
new_sal row_number(10,2);
BEGIN
SELECT @new_sal = sal FROM emp WHERE empno = empno;
set @new_sal = new_sal * (1 + percent / 100);
UPDATE emp SET sal = new_sal WHERE empno = empno;
IF @@fetch_status <> 0 BEGIN
raiserror('-20001 Employee not found', 16, 1);
END
END give_raise;
CREATE FUNCTION get_dept_name (@deptno float) RETURNS varchar(4000)
AS
BEGIN
DECLARE @dname varchar(20);
SELECT @dname = @dname FROM dept WHERE @deptno = @deptno;
IF @@fetch_status <> 0 BEGIN
raiserror('-20002 Department not found', 16, 1);
END
RETURN NULL;
END;
BEGIN
DECLARE
@l_empno float := 1;
l_percent SET @number = 10;
BEGIN
dbo.give_raise(@l_empno, l_percent);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(dbo.get_dept_name(20), ''));
END;
|
CREATE TABLE emp (empno number(4), ename varchar2(20), sal number(10,2), job varchar2(20), deptno number(2), PRIMARY KEY (empno));
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (1, 'Alice', 1000, 'Clerk', 10);
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (2, 'Bob', 1500, 'Manager', 20);
INSERT INTO emp (empno, ename, sal, job, deptno) VALUES (3, 'Carol', 2000, 'Analyst', 30);
ALTER TABLE emp ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept(deptno);
CREATE TABLE dept (deptno number(2), dname varchar2(20), PRIMARY KEY (deptno));
INSERT INTO dept (deptno, dname) VALUES (10, 'Sales');
INSERT INTO dept (deptno, dname) VALUES (20, 'Management');
INSERT INTO dept (deptno, dname) VALUES (30, 'Finance');
PROCEDURE give_raise (empno number, percent number)
AS
new_sal number(10,2);
BEGIN
SELECT sal INTO new_sal FROM emp WHERE empno = empno;
new_sal := new_sal * (1 + percent / 100);
UPDATE emp SET sal = new_sal WHERE empno = empno;
EXCEPTION
WHEN no_data_found THEN
raise_application_error(-20001, 'Employee not found');
END give_raise;
FUNCTION get_dept_name (deptno number) RETURN varchar2
AS
dname varchar2(20);
BEGIN
SELECT dname INTO dname FROM dept WHERE deptno = deptno;
EXCEPTION
WHEN no_data_found THEN
raise_application_error(-20002, 'Department not found');
END get_dept_name;
BEGIN
DECLARE
l_empno number := 1;
l_percent number := 10;
BEGIN
give_raise(l_empno, l_percent);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
DBMS_OUTPUT.PUT_LINE('Department Name: ' || get_dept_name(20));
END;
|
CREATE TABLE employees (
id BIGINT PRIMARY KEY,
name VARCHAR(200) NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
IF OBJECT_ID('update_salary', 'P') IS NOT NULL
DROP PROCEDURE update_salary;
GO
CREATE PROCEDURE update_salary (
@employee_id IN AS NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE id = employee_id;
COMMIT;
END;
BEGIN
DECLARE
@emp_id BIGINT;
emp_salary ROW_NUMBER(10,2);
BEGIN
SELECT @emp_id = id, @emp_salary = salary FROM employees WHERE name = 'John Doe';
dbo.update_salary(@emp_id, emp_salary * 1.1);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
END;
|
CREATE TABLE employees (
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(200) NOT NULL,
salary NUMBER(10,2) NOT NULL
);
CREATE OR REPLACE PROCEDURE update_salary (
employee_id IN NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE id = employee_id;
COMMIT;
END;
BEGIN
DECLARE
emp_id NUMBER(10);
emp_salary NUMBER(10,2);
BEGIN
SELECT id, salary INTO emp_id, emp_salary FROM employees WHERE name = 'John Doe';
update_salary(emp_id, emp_salary * 1.1);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
END;
|
DECLARE
@x INT := 10;
@y SET @INT = 20;
@z SET @INT = 0;
BEGIN
IF @x > @y BEGIN
SET @z = @x - @y;
END
ELSE BEGIN
SET @z = @y - @x;
END
WHILE @z > 0 BEGIN
DBMS_OUTPUT.PUT_LINE(@z);
SET @z = @z - 1;
END;
END;
/ | DECLARE
@x INT := 10;
@y INT := 20;
@z INT := 0;
BEGIN
IF @x > @y THEN
@z := @x - @y;
ELSE
@z := @y - @x;
END IF;
WHILE @z > 0 DO
DBMS_OUTPUT.PUT_LINE(@z);
@z := @z - 1;
END WHILE;
END;
/ |
CREATE TABLE customers (
id FLOAT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) UNIQUE
);
CREATE TABLE orders (
id FLOAT PRIMARY KEY,
customer_id FLOAT REFERENCES customers(id),
product_id FLOAT,
quantity FLOAT
);
IF OBJECT_ID('place_order', 'P') IS NOT NULL
DROP PROCEDURE place_order;
GO
CREATE PROCEDURE place_order (
@p_customer_id IN AS NUMBER,
p_product_id IN NUMBER,
p_quantity IN NUMBER
)
AS
BEGIN
INSERT INTO orders (customer_id, product_id, quantity)
VALUES (p_customer_id, p_product_id, p_quantity);
COMMIT;
END;
BEGIN
DECLARE
@order_id FLOAT;
BEGIN
dbo.place_order(1, 1, 2);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value on unique index');
END;
SELECT @order_id = id FROM orders WHERE customer_id = 1 AND product_id = 1;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@order_id, ''));
END;
|
CREATE TABLE customers (
id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
email VARCHAR2(50) UNIQUE
);
CREATE TABLE orders (
id NUMBER PRIMARY KEY,
customer_id NUMBER REFERENCES customers(id),
product_id NUMBER,
quantity NUMBER
);
CREATE OR REPLACE PROCEDURE place_order (
p_customer_id IN NUMBER,
p_product_id IN NUMBER,
p_quantity IN NUMBER
)
AS
BEGIN
INSERT INTO orders (customer_id, product_id, quantity)
VALUES (p_customer_id, p_product_id, p_quantity);
COMMIT;
END;
BEGIN
DECLARE
order_id NUMBER;
BEGIN
place_order(1, 1, 2);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Duplicate value on unique index');
END;
SELECT id INTO order_id FROM orders WHERE customer_id = 1 AND product_id = 1;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_id);
END;
|
CREATE FUNCTION get_total_sales(@cust_id in numberDECLARE ) RETURN
BEGIN number
return null;
end; IS
total_sales number;
BEGIN
SELECT @total_sales = sum(amount)
FROM sales
WHERE customer_id = cust_id;
RETURN total_sales;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN 0;
END
END;
-- Ex... SQLINES DEMO ***
BEGIN
DBMS_OUTPUT.put_line('Total sales for customer 1: ' + isnull(dbo.get_total_sales(1), ''));
END;
|
CREATE FUNCTION get_total_sales(cust_id in number) RETURN number IS
total_sales number;
BEGIN
SELECT sum(amount)
INTO total_sales
FROM sales
WHERE customer_id = cust_id;
RETURN total_sales;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
-- Example usage
BEGIN
DBMS_OUTPUT.put_line('Total sales for customer 1: ' || get_total_sales(1));
END;
|
CREATE FUNCTION get_customer_name(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; IS
customer_name dbo.VARCHAR2(255);
BEGIN
SELECT @customer_name = name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
CREATE PROCEDURE insert_order(@customer_id IN AS NUMBER, product_id IN NUMBER, quantity IN NUMBER) IS
BEGIN
INSERT INTO orders (customer_id, product_id, quantity) VALUES (customer_id, product_id, quantity);
COMMIT;
END;
BEGIN
DECLARE
@customer_name VARCHAR(255);
order_id NUMBER;
BEGIN
SET @customer_name = dbo.get_customer_name(1);
DBMS_OUTPUT.PUT_LINE('Customer: ' + isnull(@customer_name, ''));
dbo.insert_order(1, 1, 10);
SELECT @order_id = order_id FROM orders WHERE customer_id = 1 ORDER BY order_date DESC;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(order_id, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE FUNCTION get_customer_name(customer_id IN NUMBER) RETURN VARCHAR2 IS
customer_name VARCHAR2(255);
BEGIN
SELECT name INTO customer_name FROM customers WHERE customer_id = customer_id;
RETURN customer_name;
END;
CREATE PROCEDURE insert_order(customer_id IN NUMBER, product_id IN NUMBER, quantity IN NUMBER) IS
BEGIN
INSERT INTO orders (customer_id, product_id, quantity) VALUES (customer_id, product_id, quantity);
COMMIT;
END;
BEGIN
DECLARE
customer_name VARCHAR2(255);
order_id NUMBER;
BEGIN
customer_name := get_customer_name(1);
DBMS_OUTPUT.PUT_LINE('Customer: ' || customer_name);
insert_order(1, 1, 10);
SELECT order_id INTO order_id FROM orders WHERE customer_id = 1 ORDER BY order_date DESC;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || order_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 1;
IF (@v_salary IS NULL) BEGIN
RAISERROR('-20100 Employee with ID 1 not found', 16, 1);
END
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 1;
IF (v_salary IS NULL) THEN
RAISE_APPLICATION_ERROR(-20100, 'Employee with ID 1 not found');
END IF;
END;
|
DECLARE
@l_empno FLOAT;
BEGIN
SELECT TOP 1 @l_empno = empno
FROM emp
;
IF @l_empno IS NULL BEGIN
RAISERROR('-20001 No employees found', 16, 1);
END
UPDATE emp
SET sal = sal * 1.10
WHERE empno = @l_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated for employee ' + isnull(@l_empno, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
l_empno NUMBER;
BEGIN
SELECT empno
INTO l_empno
FROM emp
WHERE rownum = 1;
IF l_empno IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employees found');
END IF;
UPDATE emp
SET sal = sal * 1.10
WHERE empno = l_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated for employee ' || l_empno);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
CREATE OR REPLACE PACKAGE my_pkg AS
PROCEDURE my_proc( @arg1 IN DECLARE @VARCHAR2, arg2 OUT NUMBER );
CREATE FUNCTION my_func( @arg1 IN NUMBER DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;;
END my_pkg;
/
/* PACKAGE BODY my_pkg */
IF OBJECT_ID('my_pkg_my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_pkg_my_proc;
GO
CREATE PROCEDURE my_pkg_my_proc( @arg1 IN AS VARCHAR2DECLARE , arg2 DECLARE @OUT FLOAT DECLARE ) AS
BEGIN
SET @arg2 = arg1.Length;
my_proc;
CREATE FUNCTION my_func( @arg1 IN NUMBER DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN arg1.ToString;
END my_func;
END my_pkg;
/
DECLARE
@v_arg2 FLOAT;
BEGIN
my_pkg.my_proc( 'Hello World', @v_arg2 );
DBMS_OUTPUT.Put_Line( @v_arg2 );
DBMS_OUTPUT.Put_Line( my_pkg.my_func( 10 ) );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.Put_Line( SQLERRM );
END;
/
|
CREATE OR REPLACE PACKAGE my_pkg AS
PROCEDURE my_proc( arg1 IN VARCHAR2, arg2 OUT NUMBER );
FUNCTION my_func( arg1 IN NUMBER ) RETURN VARCHAR2;
END my_pkg;
/
CREATE OR REPLACE PACKAGE BODY my_pkg AS
PROCEDURE my_proc( arg1 IN VARCHAR2, arg2 OUT NUMBER ) AS
BEGIN
arg2 := arg1.Length;
END my_proc;
FUNCTION my_func( arg1 IN NUMBER ) RETURN VARCHAR2 AS
BEGIN
RETURN arg1.ToString;
END my_func;
END my_pkg;
/
DECLARE
v_arg2 NUMBER;
BEGIN
my_pkg.my_proc( 'Hello World', v_arg2 );
DBMS_OUTPUT.Put_Line( v_arg2 );
DBMS_OUTPUT.Put_Line( my_pkg.my_func( 10 ) );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.Put_Line( SQLERRM );
END;
/
|
IF OBJECT_ID('print_employee', 'P') IS NOT NULL
DROP PROCEDURE print_employee;
GO
CREATE PROCEDURE print_employee(@empno FLOAT) AS
BEGIN
SET NOCOUNT ON;
SELECT @v_ename = ename, @v_job = job, @v_sal = sal FROM emp WHERE @empno = @empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
DBMS_OUTPUT.PUT_LINE('Job Title: ' + isnull(v_job, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that employee number.');
END
END;
GO
|
CREATE OR REPLACE PROCEDURE print_employee(empno NUMBER) IS
BEGIN
SELECT ename, job, sal INTO v_ename, v_job, v_sal FROM emp WHERE empno = empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Job Title: ' || v_job);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that employee number.');
END;
/
|
declare
@salary float := 1500;
tax set @number = 0;
begin
if @salary < 1000 begin
set @tax = @salary * 0.1;
end
else if @salary < 2000 begin
set @tax = @salary * 0.15;
end
else if @salary < 3000 begin
set @tax = @salary * 0.2;
end
else begin
set @tax = @salary * 0.25;
end
dbms_output.put_line('Salary: ' + isnull(@salary, ''));
dbms_output.put_line('Tax: ' + isnull(tax, ''));
exception
when others then
dbms_output.put_line('Error occurred: ' + isnull(sqlerrm, ''));
end;
|
declare
salary number := 1500;
tax number := 0;
begin
if salary < 1000 then
tax := salary * 0.1;
elsif salary < 2000 then
tax := salary * 0.15;
elsif salary < 3000 then
tax := salary * 0.2;
else
tax := salary * 0.25;
end if;
dbms_output.put_line('Salary: ' || salary);
dbms_output.put_line('Tax: ' || tax);
exception
when others then
dbms_output.put_line('Error occurred: ' || sqlerrm);
end;
|
BEGIN
DECLARE emp CURSOR FOR
SELECT *
FROM employees
WHERE department_id = :dept_id;
OPEN emp;
FETCH emp INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** e's salary
set @emp.salary = @emp_salary + (@emp_salary * :raise);
*** ee's salary
UPDATE employees
SET salary = @emp_salary
WHERE employee_id = @emp_employee_id;
*** alary
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' + isnull(@emp_employee_id, '') + ': ' + isnull(@emp_salary, ''));
FETCH emp INTO;
END;
CLOSE emp;
DEALLOCATE emp;
COMMIT;
EXCEPTION
WHEN others THEN
*** that occur
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END; | BEGIN
FOR emp IN (
SELECT *
FROM employees
WHERE department_id = :dept_id
) LOOP
-- Calculate employee's salary
emp.salary := emp.salary + (emp.salary * :raise);
-- Update the employee's salary
UPDATE employees
SET salary = emp.salary
WHERE employee_id = emp.employee_id;
-- Log the updated salary
DBMS_OUTPUT.PUT_LINE('Updated salary for employee ' || emp.employee_id || ': ' || emp.salary);
END LOOP;
COMMIT;
EXCEPTION
WHEN others THEN
-- Handle any errors that occur
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
ROLLBACK;
END; |
DECLARE
@v_salary DECIMAL(10, 2);
BEGIN
SELECT @v_salary = salary FROM emp WHERE empno = 100;
IF @v_salary IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(@v_salary, ''));
END
END; | DECLARE
v_salary NUMBER(10, 2);
BEGIN
SELECT salary INTO v_salary FROM emp WHERE empno = 100;
IF v_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END IF;
END; |
CREATE TABLE Customers (
Customer_ID BIGINT PRIMARY KEY,
Customer_Name VARCHAR(50) NOT NULL
);
CREATE TABLE Orders (
Order_ID BIGINT PRIMARY KEY,
Customer_ID BIGINT REFERENCES Customers(Customer_ID),
Order_Date DATE,
Total_Amount DECIMAL(10,2)
);
CREATE PROCEDURE InsertCustomer (
@p_Customer_Name IN AS VARCHAR2
)
IS
BEGIN
INSERT INTO Customers(Customer_Name) VALUES (p_Customer_Name);
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 Error while inserting customer', 16, 1);
END;
CREATE FUNCTION GetCustomerOrders (
@p_Customer_ID IN NUMBER
DECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END;
IS
v_cursor SYS_REFCURSOR;
BEGIN
SELECT * FROM Orders WHERE Customer_ID = p_Customer_ID;
RETURN v_cursor;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20001 Error while fetching customer orders', 16, 1);
END;
BEGIN
DECLARE
@v_customer_id BIGINT;
v_cursor SYS_REFCURSOR;
BEGIN
dbo.INSERTCustomer('John Doe');
SELECT @v_customer_id = Customer_ID FROM Customers WHERE Customer_Name = 'John Doe';
SET @sql = dbo.GetCustomerOrders(@v_customer_id);
EXECUTE sp_executesql @sql;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20002 Error while processing transaction', 16, 1);
END;
END;
|
CREATE TABLE Customers (
Customer_ID NUMBER(10) PRIMARY KEY,
Customer_Name VARCHAR2(50) NOT NULL
);
CREATE TABLE Orders (
Order_ID NUMBER(10) PRIMARY KEY,
Customer_ID NUMBER(10) REFERENCES Customers(Customer_ID),
Order_Date DATE,
Total_Amount NUMBER(10,2)
);
CREATE PROCEDURE InsertCustomer (
p_Customer_Name IN VARCHAR2
)
IS
BEGIN
INSERT INTO Customers(Customer_Name) VALUES (p_Customer_Name);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'Error while inserting customer');
END;
CREATE FUNCTION GetCustomerOrders (
p_Customer_ID IN NUMBER
) RETURN SYS_REFCURSOR
IS
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR
SELECT * FROM Orders WHERE Customer_ID = p_Customer_ID;
RETURN v_cursor;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Error while fetching customer orders');
END;
BEGIN
DECLARE
v_customer_id NUMBER(10);
v_cursor SYS_REFCURSOR;
BEGIN
INSERTCustomer('John Doe');
SELECT Customer_ID INTO v_customer_id FROM Customers WHERE Customer_Name = 'John Doe';
OPEN v_cursor FOR GetCustomerOrders(v_customer_id);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20002, 'Error while processing transaction');
END;
END;
|
DECLARE
@employee_id FLOAT;
BEGIN
SELECT @employee_id = @employee_id
FROM employees
WHERE last_name = 'Smith';
IF @employee_id IS NULL BEGIN
RAISERROR('-20001 No employee found with last name Smith', 16, 1);
END
ELSE BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE @employee_id = @employee_id;
COMMIT;
END
END;
|
DECLARE
employee_id NUMBER;
BEGIN
SELECT employee_id
INTO employee_id
FROM employees
WHERE last_name = 'Smith';
IF employee_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with last name Smith');
ELSE
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = employee_id;
COMMIT;
END IF;
END;
|
BEGIN
-- De... SQLINES DEMO ***
DECLARE
@v_emp_id DECIMAL(19,4) /* Use -meta option EMPLOYEE.emp_id%TYPE */;
v_emp_name EMPLOYEE.emp_name%TYPE;
v_salary EMPLOYEE.salary%TYPE;
v_dept_id DEPARTMENT.dept_id%TYPE;
v_dept_name DEPARTMENT.dept_name%TYPE;
v_err_msg dbo.VARCHAR2(200);
BEGIN
*** rmation
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM EMPLOYEE
WHERE emp_id = 100;
*** formation
SELECT @v_dept_id = dept_id, @v_dept_name = dept_name
FROM DEPARTMENT
WHERE dept_id = @v_emp_id;
-- Di... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
DBMS_OUTPUT.PUT_LINE('Department ID: ' + isnull(v_dept_id, ''));
DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(v_dept_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_err_msg = 'No employee found with ID ' + isnull(@v_emp_id, '');
RAISERROR(-20000, v_err_msg, 16, 1);
END
END;
END; | BEGIN
-- Declare variables
DECLARE
v_emp_id EMPLOYEE.emp_id%TYPE;
v_emp_name EMPLOYEE.emp_name%TYPE;
v_salary EMPLOYEE.salary%TYPE;
v_dept_id DEPARTMENT.dept_id%TYPE;
v_dept_name DEPARTMENT.dept_name%TYPE;
v_err_msg VARCHAR2(200);
BEGIN
-- Get employee information
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM EMPLOYEE
WHERE emp_id = 100;
-- Get department information
SELECT dept_id, dept_name
INTO v_dept_id, v_dept_name
FROM DEPARTMENT
WHERE dept_id = v_emp_id;
-- Display results
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('Department ID: ' || v_dept_id);
DBMS_OUTPUT.PUT_LINE('Department Name: ' || v_dept_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_err_msg := 'No employee found with ID ' || v_emp_id;
RAISE_APPLICATION_ERROR(-20000, v_err_msg);
END;
END; |
WITH RECURSIVE CTE AS (
SELECT *, LEVEL AS depth
FROM employee
WHERE manager_id IS NULL
UNION ALL
SELECT e.*, depth + 1
FROM CTE c
JOIN employee e ON c.employee_id = e.manager_id
)
SELECT employee_id, name, depth
FROM CTE
WHERE depth = 3;
IF OBJECT_ID('withdraw_funds', 'P') IS NOT NULL
DROP PROCEDURE withdraw_funds;
GO
CREATE PROCEDURE withdraw_funds(
@IN account_id AS NUMBER,
IN amount NUMBER,
OUT success BOOLEAN
)
AS
BEGIN
UPDATE account
SET balance = balance - amount
WHERE account_id = account_id;
IF @@ROWCOUNT = 1 BEGIN
set @success = TRUE;
END
ELSE BEGIN
set @success = FALSE;
RAISERROR('-20001 Withdrawal failed', 16, 1);
END
END;
IF OBJECT_ID('calculate_tax', 'FN') IS NOT NULL
DROP FUNCTION calculate_tax;
GO
CREATE FUNCTION calculate_tax(
@IN income NUMBER
DECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
DETERMINISTIC
AS
BEGIN
IF income > 100000 BEGIN
RETURN income * 0.3;
END
ELSE BEGIN
RETURN income * 0.2;
END
END;
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE('Iteration ' + isnull(i, ''));
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
CREATE TABLE customers (
customer_id BIGINT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
phone VARCHAR(20),
CONSTRAINT FK_email FOREIGN KEY (email) REFERENCES contacts (email)
); | WITH RECURSIVE CTE AS (
SELECT *, LEVEL AS depth
FROM employee
WHERE manager_id IS NULL
UNION ALL
SELECT e.*, depth + 1
FROM CTE c
JOIN employee e ON c.employee_id = e.manager_id
)
SELECT employee_id, name, depth
FROM CTE
WHERE depth = 3;
CREATE OR REPLACE PROCEDURE withdraw_funds(
IN account_id NUMBER,
IN amount NUMBER,
OUT success BOOLEAN
)
AS
BEGIN
UPDATE account
SET balance = balance - amount
WHERE account_id = account_id;
IF SQL%ROWCOUNT = 1 THEN
success := TRUE;
ELSE
success := FALSE;
RAISE_APPLICATION_ERROR(-20001, 'Withdrawal failed');
END IF;
END;
CREATE OR REPLACE FUNCTION calculate_tax(
IN income NUMBER
)
RETURN NUMBER
DETERMINISTIC
AS
BEGIN
IF income > 100000 THEN
RETURN income * 0.3;
ELSE
RETURN income * 0.2;
END IF;
END;
BEGIN
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE('Iteration ' || i);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
CREATE TABLE customers (
customer_id NUMBER(10) PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
email VARCHAR2(100) UNIQUE,
phone VARCHAR2(20),
CONSTRAINT FK_email FOREIGN KEY (email) REFERENCES contacts (email)
); |
DECLARE
@v_empid FLOAT;
v_ename dbo.VARCHAR2(30);
v_salary NUMBER;
BEGIN
SELECT TOP 2 @v_empid = empid, @v_ename = ename, @v_salary = salary FROM emp;
IF v_salary > 5000 BEGIN
SET @v_salary = v_salary * 1.1;
UPDATE emp SET salary = v_salary WHERE empid = @v_empid;
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No matching records found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END; | DECLARE
v_empid NUMBER;
v_ename VARCHAR2(30);
v_salary NUMBER;
BEGIN
SELECT empid, ename, salary INTO v_empid, v_ename, v_salary FROM emp WHERE rownum < 3;
IF v_salary > 5000 THEN
v_salary := v_salary * 1.1;
UPDATE emp SET salary = v_salary WHERE empid = v_empid;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No matching records found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END; |
CREATE PROCEDURE my_proc (
@p_id IN AS NUMBER
) AS
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = p_id;
COMMIT;
END;
CREATE FUNCTION my_func (
@p_value IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN p_value * 2;
END;
BEGIN
DECLARE
@v_id FLOAT;
v_result NUMBER;
BEGIN
SET @v_id = 1234;
SELECT @v_result = dbo.my_func(@v_id);
DBMS_OUTPUT.PUT_LINE('Result: ' + isnull(v_result, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(@@ERROR, '') + ' - ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE PROCEDURE my_proc (
p_id IN NUMBER
) AS
BEGIN
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = p_id;
COMMIT;
END;
CREATE FUNCTION my_func (
p_value IN NUMBER
) RETURN NUMBER AS
BEGIN
RETURN p_value * 2;
END;
BEGIN
DECLARE
v_id NUMBER;
v_result NUMBER;
BEGIN
v_id := 1234;
SELECT my_func(v_id) INTO v_result FROM dual;
DBMS_OUTPUT.PUT_LINE('Result: ' || v_result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLCODE || ' - ' || SQLERRM);
END;
END;
|
IF OBJECT_ID('get_avg_salary', 'P') IS NOT NULL
DROP PROCEDURE get_avg_salary;
GO
CREATE PROCEDURE get_avg_salary(
@deptno IN AS NUMBER,
avg_sal OUT NUMBER
)
AS
CURSOR c1 IS
SELECT AVG(salary) FROM emp WHERE deptno = deptno;
BEGIN
OPEN c1;
FETCH c1 INTO @avg_sal;
CLOSE c1;
END;
/
DECLARE
@avg_salary FLOAT;
BEGIN
dbo.get_avg_salary(20, @avg_salary);
DBMS_OUTPUT.PUT_LINE('Average salary for department 20: ' + isnull(@avg_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the specified department.');
END
END;
/
|
CREATE OR REPLACE PROCEDURE get_avg_salary(
deptno IN NUMBER,
avg_sal OUT NUMBER
)
AS
CURSOR c1 IS
SELECT AVG(salary) FROM emp WHERE deptno = deptno;
BEGIN
OPEN c1;
FETCH c1 INTO avg_sal;
CLOSE c1;
END;
/
DECLARE
avg_salary NUMBER;
BEGIN
get_avg_salary(20, avg_salary);
DBMS_OUTPUT.PUT_LINE('Average salary for department 20: ' || avg_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the specified department.');
END;
/
|
CREATE PROCEDURE EmpBonus(@emp_id IN AS NUMBER, bonus_amount IN NUMBER) IS
total_salary NUMBER;
BEGIN
SELECT @total_salary = salary FROM employees WHERE employee_id = emp_id;
IF total_salary >= 50000 BEGIN
UPDATE employees SET salary = salary + bonus_amount WHERE employee_id = emp_id;
END
ELSE BEGIN
RAISERROR('-20001 Bonus not applicable for salary less than 50000', 16, 1);
END
END;
|
CREATE PROCEDURE EmpBonus(emp_id IN NUMBER, bonus_amount IN NUMBER) IS
total_salary NUMBER;
BEGIN
SELECT salary INTO total_salary FROM employees WHERE employee_id = emp_id;
IF total_salary >= 50000 THEN
UPDATE employees SET salary = salary + bonus_amount WHERE employee_id = emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Bonus not applicable for salary less than 50000');
END IF;
END;
|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @salary FLOAT;
SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 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(@emp_id FLOAT, @new_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
COMMIT;
EXCEPTION
WHEN INVALID_ARGUMENT THEN
RAISERROR('-20002 Invalid salary', 16, 1);
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
salary SET @NUMBER = dbo.get_employee_salary(@emp_id);
BEGIN
*** y 10%
SET @salary = salary * 1.10;
dbo.update_employee_salary(@emp_id, salary);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20003 Salary update failed due to duplicate salary', 16, 1);
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
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN INVALID_ARGUMENT THEN
RAISE_APPLICATION_ERROR(-20002, 'Invalid salary');
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
salary NUMBER := get_employee_salary(emp_id);
BEGIN
-- Increase salary by 10%
salary := salary * 1.10;
update_employee_salary(emp_id, salary);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20003, 'Salary update failed due to duplicate salary');
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 FLOAT)
RETURNS SYS_REFCURSOR
AS
BEGIN
DECLARE @emp_info SYS_REFCURSOR;
SELECT employee_id,
first_name,
email,
salary
FROM employees
WHERE employee_id = @emp_id;
RETURN @emp_info;
END;
BEGIN
DECLARE
@emp_cur SYS_REFCURSOR;
emp_id SET @NUMBER = 100;
BEGIN
SET @emp_cur = dbo.get_employee_info(emp_id);
WHILE 1=1 BEGIN
FETCH emp_cur
INTO @emp_id, @first_name, @email, @salary;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(first_name, ''));
DBMS_OUTPUT.PUT_LINE('Email: ' + isnull(email, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
END;
CLOSE emp_cur;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' + isnull(emp_id, ''));
END
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_info(emp_id NUMBER)
RETURN SYS_REFCURSOR
IS
emp_info SYS_REFCURSOR;
BEGIN
OPEN emp_info FOR
SELECT employee_id,
first_name,
email,
salary
FROM employees
WHERE employee_id = emp_id;
RETURN emp_info;
END;
BEGIN
DECLARE
emp_cur SYS_REFCURSOR;
emp_id NUMBER := 100;
BEGIN
emp_cur := get_employee_info(emp_id);
LOOP
FETCH emp_cur
INTO emp_id, first_name, email, salary;
EXIT WHEN emp_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Email: ' || email);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
END LOOP;
CLOSE emp_cur;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || emp_id);
END;
END;
|
BEGIN
DECLARE
cursor_name cursor for SELECT * FROM EMP;
emp_no EMP.emp_no%type;
BEGIN
OPEN cursor_name;
FETCH cursor_name INTO @emp_no;
CLOSE cursor_name;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
BEGIN
DECLARE
cursor_name cursor for SELECT * FROM EMP;
emp_no EMP.emp_no%type;
BEGIN
OPEN cursor_name;
FETCH cursor_name INTO emp_no;
CLOSE cursor_name;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
END;
|
BEGIN
DECLARE
@emp_name VARCHAR(20);
emp_salary NUMBER;
BEGIN
SELECT @emp_name = ename, @emp_salary = sal FROM emp WHERE empno = 7369;
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('Employee not found');
END
END;
END;
/ | BEGIN
DECLARE
emp_name VARCHAR2(20);
emp_salary NUMBER;
BEGIN
SELECT ename, sal INTO emp_name, emp_salary FROM emp WHERE empno = 7369;
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('Employee not found');
END;
END;
/ |
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_salary = salary FROM emp WHERE emp_id = 10;
IF v_salary > 5000 BEGIN
UPDATE emp SET salary = salary * 1.1 WHERE emp_id = @v_emp_id;
END
COMMIT;
IF @@fetch_status <> 0 BEGIN
RAISERROR('-20001 No employee found with that ID.', 16, 1);
END
END;
|
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT emp_id, salary INTO v_emp_id, v_salary FROM emp WHERE emp_id = 10;
IF v_salary > 5000 THEN
UPDATE emp SET salary = salary * 1.1 WHERE emp_id = v_emp_id;
END IF;
COMMIT;
EXCEPTION
WHEN no_data_found THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with that ID.');
END;
|
DECLARE
@x FLOAT;
y NUMBER;
BEGIN
SELECT @x = a, @y = b FROM table WHERE id = 1;
IF @x > y BEGIN
DBMS_OUTPUT.PUT_LINE('x is greater than y');
END
ELSE IF @x = y BEGIN
DBMS_OUTPUT.PUT_LINE('x is equal to y');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('y is greater than x');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
END; | DECLARE
x NUMBER;
y NUMBER;
BEGIN
SELECT a, b INTO x, y FROM table WHERE id = 1;
IF x > y THEN
DBMS_OUTPUT.PUT_LINE('x is greater than y');
ELSIF x = y THEN
DBMS_OUTPUT.PUT_LINE('x is equal to y');
ELSE
DBMS_OUTPUT.PUT_LINE('y is greater than x');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
END; |
IF OBJECT_ID('sp_demo', 'P') IS NOT NULL
DROP PROCEDURE sp_demo;
GO
CREATE PROCEDURE sp_demo(@p_input IN AS VARCHAR2) AS
BEGIN
DECLARE @v_output VARCHAR(100);
BEGIN
SELECT @v_output = UPPER(p_input);
DBMS_OUTPUT.PUT_LINE('Output: ' + isnull(@v_output, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END;
|
CREATE OR REPLACE PROCEDURE sp_demo(p_input IN VARCHAR2) AS
BEGIN
DECLARE v_output VARCHAR2(100);
BEGIN
SELECT UPPER(p_input) INTO v_output FROM dual;
DBMS_OUTPUT.PUT_LINE('Output: ' || v_output);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END;
|
CREATE PROCEDURE get_customer_orders(
@custid IN AS NUMBER,
orders OUT SYS_REFCURSOR)
IS
CURSOR c_orders IS
SELECT order_id, order_date, order_total
FROM orders
WHERE customer_id = custid;
BEGIN
EXECUTE (@c_orders);
END;
BEGIN
DECLARE
@l_custid FLOAT := 12345;
l_orders SYS_REFCURSOR;
BEGIN
dbo.get_customer_orders(@l_custid, l_orders);
WHILE 1=1 BEGIN
FETCH l_orders INTO @l_order_id, @l_order_date, @l_order_total;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(l_order_id, '') + ', Order Date: ' + isnull(l_order_date, '') + ', Order Total: ' + isnull(l_order_total, ''));
END;
CLOSE l_orders;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(@l_custid, ''));
END
WHEN OTHERS THEN
RAISE;
END;
END;
|
CREATE PROCEDURE get_customer_orders(
custid IN NUMBER,
orders OUT SYS_REFCURSOR)
IS
CURSOR c_orders IS
SELECT order_id, order_date, order_total
FROM orders
WHERE customer_id = custid;
BEGIN
OPEN orders FOR c_orders;
END;
BEGIN
DECLARE
l_custid NUMBER := 12345;
l_orders SYS_REFCURSOR;
BEGIN
get_customer_orders(l_custid, l_orders);
LOOP
FETCH l_orders INTO l_order_id, l_order_date, l_order_total;
EXIT WHEN l_orders%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Order ID: ' || l_order_id || ', Order Date: ' || l_order_date || ', Order Total: ' || l_order_total);
END LOOP;
CLOSE l_orders;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || l_custid);
WHEN OTHERS THEN
RAISE;
END;
END;
|
BEGIN
SELECT @v_count = COUNT(*) FROM emp WHERE deptno = :deptno;
IF v_count = 0 BEGIN
RAISERROR('-20001 No employees found in department ' + isnull(:deptno, ''), 16, 1);
END
DECLARE rec CURSOR FOR SELECT * FROM emp WHERE deptno = :deptno; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_ename, '') + ' - ' + isnull(@rec_sal, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END; | BEGIN
SELECT COUNT(*) INTO v_count FROM emp WHERE deptno = :deptno;
IF v_count = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No employees found in department ' || :deptno);
END IF;
FOR rec IN (SELECT * FROM emp WHERE deptno = :deptno) LOOP
DBMS_OUTPUT.PUT_LINE(rec.ename || ' - ' || rec.sal);
END LOOP;
END; |
IF OBJECT_ID('employee_data', 'P') IS NOT NULL
DROP PROCEDURE employee_data;
GO
CREATE PROCEDURE employee_data(@p_emp_id IN AS NUMBER) AS
BEGIN
UPDATE employees SET salary = salary + (salary * 0.1) WHERE employee_id = p_emp_id;
COMMIT;
END;
DECLARE
@salary FLOAT;
BEGIN
SELECT @salary = @salary FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Current salary:' + isnull(@salary, ''));
dbo.employee_data(100);
SELECT @salary = @salary FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('New salary: ' + isnull(@salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@p_emp_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN (SELECT name FROM employees WHERE employee_id = p_emp_id);
END;
DECLARE
@employee_name VARCHAR(50);
BEGIN
SET @employee_name = dbo.get_employee_name(100);
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@employee_name, ''));
END;
|
CREATE OR REPLACE PROCEDURE employee_data(p_emp_id IN NUMBER) AS
BEGIN
UPDATE employees SET salary = salary + (salary * 0.1) WHERE employee_id = p_emp_id;
COMMIT;
END;
DECLARE
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Current salary:' || salary);
employee_data(100);
SELECT salary INTO salary FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('New salary: ' || salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
CREATE OR REPLACE FUNCTION get_employee_name(p_emp_id IN NUMBER) RETURN VARCHAR2 AS
BEGIN
RETURN (SELECT name FROM employees WHERE employee_id = p_emp_id);
END;
DECLARE
employee_name VARCHAR2(50);
BEGIN
employee_name := get_employee_name(100);
DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_name);
END;
|
IF OBJECT_ID('generate_report', 'P') IS NOT NULL
DROP PROCEDURE generate_report;
GO
CREATE PROCEDURE generate_report (
@start_date DATE,
@end_date DATE
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@result_table SYS_REFCURSOR;
BEGIN
SELECT *
FROM orders
WHERE order_date BETWEEN @start_date AND @end_date;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20001 Error fetching results', 16, 1);
END;
RETURN @result_table;
END;
|
CREATE OR REPLACE PROCEDURE generate_report (
start_date DATE,
end_date DATE
)
AS
BEGIN
DECLARE
result_table SYS_REFCURSOR;
BEGIN
OPEN result_table FOR
SELECT *
FROM orders
WHERE order_date BETWEEN start_date AND end_date;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Error fetching results');
END;
RETURN result_table;
END generate_report;
|
DECLARE
@v_name VARCHAR(20);
v_salary NUMBER;
BEGIN
SELECT @v_name = name, @v_salary = salary FROM employees WHERE id = 1;
IF v_salary > 1000
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_name, '') + ' has a high salary: ' + isnull(v_salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_name, '') + ' has a low salary: ' + isnull(v_salary, ''));
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
/ | DECLARE
v_name VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT name, salary INTO v_name, v_salary FROM employees WHERE id = 1;
IF v_salary > 1000
THEN
DBMS_OUTPUT.PUT_LINE(v_name || ' has a high salary: ' || v_salary);
ELSE
DBMS_OUTPUT.PUT_LINE(v_name || ' has a low salary: ' || v_salary);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
/ |
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure (
@p_empno IN AS NUMBER
) IS
v_bonus NUMBER;
BEGIN
SELECT @v_bonus = bonus
FROM employee
WHERE empno = p_empno;
IF v_bonus IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employee
SET bonus = v_bonus + 100
WHERE empno = p_empno;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE my_procedure (
p_empno IN NUMBER
) IS
v_bonus NUMBER;
BEGIN
SELECT bonus
INTO v_bonus
FROM employee
WHERE empno = p_empno;
IF v_bonus IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employee
SET bonus = v_bonus + 100
WHERE empno = p_empno;
COMMIT;
END;
|
IF OBJECT_ID('get_employee_count', 'FN') IS NOT NULL
DROP FUNCTION get_employee_count;
GO
CREATE FUNCTION get_employee_count(@department_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
emp_count NUMBER;
BEGIN
SELECT @emp_count = COUNT(*) FROM employees WHERE department_id = department_id;
RETURN emp_count;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN -1;
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;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
DECLARE
@emp_id FLOAT := 100;
new_salary SET @NUMBER = 40000;
BEGIN
dbo.update_employee_salary(@emp_id, new_salary);
COMMIT;
END;
|
CREATE OR REPLACE FUNCTION get_employee_count(department_id IN NUMBER) RETURN NUMBER IS
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees WHERE department_id = department_id;
RETURN emp_count;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
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 OTHERS THEN
RAISE;
END;
DECLARE
emp_id NUMBER := 100;
new_salary NUMBER := 40000;
BEGIN
update_employee_salary(emp_id, new_salary);
COMMIT;
END;
|
DECLARE
@num_emp FLOAT := 10;
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= @num_emp BEGIN
IF i = 5 BEGIN
EXIT;
END
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(i, '') + ' processed.');
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing employees.');
END;
/
|
DECLARE
num_emp NUMBER := 10;
BEGIN
FOR i IN 1 .. num_emp LOOP
IF i = 5 THEN
EXIT;
END IF;
DBMS_OUTPUT.PUT_LINE('Employee ' || i || ' processed.');
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing employees.');
END;
/
|
BEGIN
-- De... SQLINES DEMO ***
DECLARE
@v_name VARCHAR(30);
v_salary ROW_NUMBER(10, 2);
BEGIN
*** into the employees table
INSERT INTO employees (name, salary)
VALUES ('John Doe', 10000);
*** rted ID
SELECT @v_employee_id = employee_id FROM employees ORDER BY employee_id DESC;
*** of the new employee
UPDATE employees SET salary = 12000 WHERE employee_id = v_employee_id;
*** ployee
DELETE FROM employees WHERE employee_id = v_employee_id;
*** ees with a salary greater than 10000
SELECT * FROM employees WHERE salary > 10000;
-- Ra... SQLINES DEMO ***
RAISERROR('-20001 An error occurred', 16, 1);
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(@@ERROR, ''));
END;
END;
|
BEGIN
-- Declare variables
DECLARE
v_name VARCHAR2(30);
v_salary NUMBER(10, 2);
BEGIN
-- Insert a new row into the employees table
INSERT INTO employees (name, salary)
VALUES ('John Doe', 10000);
-- Get the last inserted ID
SELECT employee_id INTO v_employee_id FROM employees ORDER BY employee_id DESC;
-- Update the salary of the new employee
UPDATE employees SET salary = 12000 WHERE employee_id = v_employee_id;
-- Delete the new employee
DELETE FROM employees WHERE employee_id = v_employee_id;
-- Select all employees with a salary greater than 10000
SELECT * FROM employees WHERE salary > 10000;
-- Raise an error
RAISE_APPLICATION_ERROR(-20001, 'An error occurred');
EXCEPTION
-- Handle the error
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLCODE);
END;
END;
|
CREATE PROCEDURE my_procedure (@in_param IN AS VARCHAR2) AS
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(in_param, '') + ' ' + isnull(i, ''));
END;
commit;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
|
CREATE PROCEDURE my_procedure (in_param IN VARCHAR2) AS
BEGIN
FOR i IN 1 .. 10 LOOP
DBMS_OUTPUT.PUT_LINE(in_param || ' ' || i);
END LOOP;
commit;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
ROLLBACK;
END;
|
CREATE TABLE emps (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL
);
CREATE SEQUENCE emp_seq START WITH 1000;
CREATE PROCEDURE add_emp(
@emp_id FLOAT,
@name VARCHAR(4000),
@salary FLOAT
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO emps (
name,
salary
)
VALUES (
@name,
@salary
);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Duplicate employee id', 16, 1);
END;
CREATE FUNCTION get_emp_salary(
@emp_id FLOAT
) RETURNS FLOAT
AS
BEGIN
DECLARE @salary DECIMAL(10,2);
SELECT @salary = @salary
FROM emps
WHERE @emp_id = @emp_id;
RETURN @salary;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
DECLARE
@emp_id FLOAT;
name dbo.VARCHAR2(50);
salary ROW_NUMBER(10,2);
BEGIN
SET @emp_id = 1001;
SET @name = 'John Smith';
SET @salary = 50000;
dbo.add_emp(@emp_id, name, salary);
SET @emp_id = 1002;
SET @name = 'Jane Doe';
SET @salary = 60000;
dbo.add_emp(@emp_id, name, salary);
END;
|
CREATE TABLE emps (
emp_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
salary NUMBER(10,2) NOT NULL
);
CREATE SEQUENCE emp_seq START WITH 1000;
CREATE PROCEDURE add_emp(
emp_id NUMBER,
name VARCHAR2,
salary NUMBER
)
IS
BEGIN
INSERT INTO emps (
emp_id,
name,
salary
)
VALUES (
emp_seq.NEXTVAL,
name,
salary
);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Duplicate employee id');
END;
CREATE FUNCTION get_emp_salary(
emp_id NUMBER
) RETURN NUMBER
IS
salary NUMBER(10,2);
BEGIN
SELECT salary
INTO salary
FROM emps
WHERE emp_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
DECLARE
emp_id NUMBER;
name VARCHAR2(50);
salary NUMBER(10,2);
BEGIN
emp_id := 1001;
name := 'John Smith';
salary := 50000;
add_emp(emp_id, name, salary);
emp_id := 1002;
name := 'Jane Doe';
salary := 60000;
add_emp(emp_id, name, salary);
END;
|
CREATE PROCEDURE get_employee(@emp_id IN AS NUMBER) AS
BEGIN
SELECT @emp_record = * FROM employees WHERE employee_id = emp_id;
IF emp_record.is_null BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
RETURN emp_record;
END;
DECLARE
@emp_id := 10;
emp_record employee%ROWTYPE;
BEGIN
dbo.get_employee(@emp_id);
DBMS_OUTPUT.PUT_LINE(isnull(emp_record.first_name, '') + ' ' + isnull(emp_record.last_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' + isnull(@emp_id, ''));
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
CREATE PROCEDURE get_employee(emp_id IN NUMBER) AS
BEGIN
SELECT * INTO emp_record FROM employees WHERE employee_id = emp_id;
IF emp_record.is_null THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
RETURN emp_record;
END;
DECLARE
emp_id := 10;
emp_record employee%ROWTYPE;
BEGIN
get_employee(emp_id);
DBMS_OUTPUT.PUT_LINE(emp_record.first_name || ' ' || emp_record.last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID ' || emp_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred');
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SET @v_empno = 7839;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_empno;
IF @@ROWCOUNT > 0 BEGIN
COMMIT;
END
ELSE BEGIN
ROLLBACK;
END
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISERROR('-20001 Error occurred during salary update', 16, 1);
END;
/ | DECLARE
v_empno NUMBER;
BEGIN
v_empno := 7839;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_empno;
IF SQL%ROWCOUNT > 0 THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
-- Handle errors here
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Error occurred during salary update');
END;
/ |
CREATE PROCEDURE my_proc (
@param1 IN AS NUMBER,
param2 IN OUT VARCHAR2
)
AS
BEGIN
*** iables
v_temp NUMBER;
*** ocal variable
set @v_temp = param1 * 2;
*** ter
set @param2 = 'Updated value: ' + isnull(v_temp, '');
*** the OUT parameter is too long
IF LEN(param2) > 255 BEGIN
RAISERROR('-20001 OUT parameter is too long!', 16, 1);
END
*** ction
COMMIT;
EXCEPTION
WHEN OTHERS THEN
*** saction
ROLLBACK;
-- Lo... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END my_proc;
|
CREATE PROCEDURE my_proc (
param1 IN NUMBER,
param2 IN OUT VARCHAR2
)
AS
BEGIN
-- Declare local variables
v_temp NUMBER;
-- Assign value to local variable
v_temp := param1 * 2;
-- Update OUT parameter
param2 := 'Updated value: ' || v_temp;
-- Raise an error if the OUT parameter is too long
IF LENGTH(param2) > 255 THEN
RAISE_APPLICATION_ERROR(-20001, 'OUT parameter is too long!');
END IF;
-- Commit the transaction
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- Rollback the transaction
ROLLBACK;
-- Log the error
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END my_proc;
|
IF OBJECT_ID('get_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE get_employee_salary;
GO
CREATE PROCEDURE get_employee_salary(@emp_id IN AS NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE
@emp_id FLOAT := 100;
salary NUMBER;
BEGIN
dbo.get_employee_salary(@emp_id, salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE OR REPLACE PROCEDURE get_employee_salary(emp_id IN NUMBER, OUT salary OUT NUMBER) AS
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
END;
BEGIN
DECLARE
emp_id NUMBER := 100;
salary NUMBER;
BEGIN
get_employee_salary(emp_id, salary);
DBMS_OUTPUT.PUT_LINE('Salary: ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
|
IF OBJECT_ID('random_snippet', 'P') IS NOT NULL
DROP PROCEDURE random_snippet;
GO
CREATE PROCEDURE random_snippet(@x IN AS NUMBER) IS
l_avg ROW_NUMBER(10,2);
BEGIN
SELECT @l_avg = AVG(salary) FROM emp WHERE deptno = x;
IF l_avg > 10000 BEGIN
UPDATE emp SET salary = salary * 1.10 WHERE deptno = x;
END
ELSE BEGIN
UPDATE emp SET salary = salary * 1.05 WHERE deptno = x;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE random_snippet(x IN NUMBER) IS
l_avg NUMBER(10,2);
BEGIN
SELECT AVG(salary) INTO l_avg FROM emp WHERE deptno = x;
IF l_avg > 10000 THEN
UPDATE emp SET salary = salary * 1.10 WHERE deptno = x;
ELSE
UPDATE emp SET salary = salary * 1.05 WHERE deptno = x;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
UPDATE emp SET salary = salary + 1000 WHERE empno = @v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
END IF;
UPDATE emp SET salary = salary + 1000 WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@TYPE t_test IS TABLE OF NUMBER;
l_test SET @t_test = dbo.t_test();
l_salary NUMBER;
BEGIN
SELECT @l_salary = salary FROM emp WHERE empno = 7369;
IF l_salary IS NOT NULL BEGIN
SET @l_test = isnull(l_test, '') + isnull(l_salary, '');
END
ELSE BEGIN
RAISERROR('-20101 Salary not found.', 16, 1);
END
END;
|
DECLARE
TYPE t_test IS TABLE OF NUMBER;
l_test t_test := t_test();
l_salary NUMBER;
BEGIN
SELECT salary INTO l_salary FROM emp WHERE empno = 7369;
IF l_salary IS NOT NULL THEN
l_test := l_test || l_salary;
ELSE
RAISE_APPLICATION_ERROR(-20101, 'Salary not found.');
END IF;
END;
|
BEGIN
DECLARE
@x FLOAT;
BEGIN
SELECT @x = COUNT(*);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error!');
END;
IF @x > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Record found');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No records found');
END
END; | BEGIN
DECLARE
x NUMBER;
BEGIN
SELECT COUNT(*) INTO x FROM DUAL;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error!');
END;
IF x > 0 THEN
DBMS_OUTPUT.PUT_LINE('Record found');
ELSE
DBMS_OUTPUT.PUT_LINE('No records found');
END IF;
END; |
CREATE PROCEDURE update_employee_salary(
@emp_id IN AS NUMBER,
new_salary IN NUMBER)
IS
salary_updated SET @NUMBER = 0;
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
SELECT @salary_updated = COUNT(*) FROM employees WHERE employee_id = emp_id AND salary = new_salary;
IF salary_updated = 1 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Error updating salary.');
END
END;
|
CREATE PROCEDURE update_employee_salary(
emp_id IN NUMBER,
new_salary IN NUMBER)
IS
salary_updated NUMBER := 0;
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
SELECT COUNT(*) INTO salary_updated FROM employees WHERE employee_id = emp_id AND salary = new_salary;
IF salary_updated = 1 THEN
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
ELSE
DBMS_OUTPUT.PUT_LINE('Error updating salary.');
END IF;
END;
|