sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE id = id;
RETURN salary;
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(@id IN AS NUMBER, salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary WHERE id = id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Duplicate value on unique index', 16, 1);
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE id = id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(id IN NUMBER, salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary WHERE id = id;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Duplicate value on unique index');
END;
|
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
*** late the total salary of all employees
CREATE FUNCTION get_total_salary() RETURNS FLOAT
AS
BEGIN
DECLARE @total_salary FLOAT = 0;
SELECT @total_salary = SUM(salary) FROM employees;
RETURN @total_salary;
END;
*** create a new table
CREATE TABLE orders (
order_id FLOAT PRIMARY KEY,
customer_id FLOAT REFERENCES customers(customer_id),
order_date DATE,
total_amount FLOAT
);
*** truct to handle different cases
IF total_salary > 100000 BEGIN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 100,000');
END
ELSE IF total_salary > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 50,000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to 50,000');
END
*** enario
BEGIN
*** a duplicate record
INSERT INTO employees (employee_id, name, salary) VALUES (1, 'John Doe', 50000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate value on unique index');
END;
|
PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
COMMIT;
END;
-- Function to calculate the total salary of all employees
FUNCTION get_total_salary() RETURN NUMBER
IS
total_salary NUMBER := 0;
BEGIN
SELECT SUM(salary) INTO total_salary FROM employees;
RETURN total_salary;
END;
-- SQL statement to create a new table
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER REFERENCES customers(customer_id),
order_date DATE,
total_amount NUMBER
);
-- Control flow construct to handle different cases
IF total_salary > 100000 THEN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 100,000');
ELSIF total_salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('Total salary is greater than 50,000');
ELSE
DBMS_OUTPUT.PUT_LINE('Total salary is less than or equal to 50,000');
END IF;
-- Error handling scenario
BEGIN
-- Attempt to insert a duplicate record
INSERT INTO employees (employee_id, name, salary) VALUES (1, 'John Doe', 50000);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
DBMS_OUTPUT.PUT_LINE('Error: Duplicate value on unique index');
END;
|
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE employee_id = 10;
IF v_salary > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 50000');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 50000');
END
BEGIN
UPDATE employees
SET salary = v_salary + (v_salary * 0.1)
WHERE employee_id = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary');
END;
END; | DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE employee_id = 10;
IF v_salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('Salary is greater than 50000');
ELSE
DBMS_OUTPUT.PUT_LINE('Salary is less than or equal to 50000');
END IF;
BEGIN
UPDATE employees
SET salary = v_salary + (v_salary * 0.1)
WHERE employee_id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary');
END;
END; |
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(10);
v_job dbo.VARCHAR2(10);
v_sal NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_job = job, @v_sal = sal
FROM emp
WHERE empno = 7369;
IF v_sal > 2000 BEGIN
UPDATE emp SET sal = sal * 1.10
WHERE empno = @v_empno;
END
DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(v_ename, '') + ', Job: ' + isnull(v_job, '') + ', Salary: ' + isnull(v_sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(10);
v_job VARCHAR2(10);
v_sal NUMBER;
BEGIN
SELECT empno, ename, job, sal
INTO v_empno, v_ename, v_job, v_sal
FROM emp
WHERE empno = 7369;
IF v_sal > 2000 THEN
UPDATE emp SET sal = sal * 1.10
WHERE empno = v_empno;
END IF;
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_ename || ', Job: ' || v_job || ', Salary: ' || v_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_emp_id FLOAT := 100;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = @v_emp_id;
IF v_salary > 50000 BEGIN
UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = @v_emp_id;
END
ELSE BEGIN
RAISERROR('-20001 Salary too low for bonus', 16, 1);
END
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
|
DECLARE
v_emp_id NUMBER := 100;
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id;
IF v_salary > 50000 THEN
UPDATE employees SET salary = v_salary + 1000 WHERE employee_id = v_emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary too low for bonus');
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
|
DECLARE
@v_deptno FLOAT := 20;
v_ename dbo.VARCHAR2(20);
BEGIN
SELECT @v_ename = ename FROM emp WHERE deptno = @v_deptno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found for deptno ' + isnull(@v_deptno, ''));
END
END;
|
DECLARE
v_deptno NUMBER := 20;
v_ename VARCHAR2(20);
BEGIN
SELECT ename INTO v_ename FROM emp WHERE deptno = v_deptno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found for deptno ' || v_deptno);
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT @v_ename = ename, @v_salary = salary FROM emp WHERE empno = 7369;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END; | DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT ename, salary INTO v_ename, v_salary FROM emp WHERE empno = 7369;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END; |
DECLARE
@emp_id FLOAT;
salary NUMBER;
BEGIN
SELECT @emp_id = employee_id, @salary = salary
FROM employees
WHERE last_name = 'King';
IF salary > 10000 BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @emp_id;
END
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
DECLARE
emp_id NUMBER;
salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO emp_id, salary
FROM employees
WHERE last_name = 'King';
IF salary > 10000 THEN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = emp_id;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
DECLARE
@name VARCHAR(50);
salary NUMBER;
BEGIN
SELECT @name = @name, @salary = salary
FROM employees
WHERE id = 1;
IF salary IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees
SET salary = salary + (salary * 0.10)
WHERE id = 1;
COMMIT;
END;
|
DECLARE
name VARCHAR2(50);
salary NUMBER;
BEGIN
SELECT name, salary
INTO name, salary
FROM employees
WHERE id = 1;
IF salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees
SET salary = salary + (salary * 0.10)
WHERE id = 1;
COMMIT;
END;
|
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL
DROP PROCEDURE emp_bonus;
GO
CREATE PROCEDURE emp_bonus(@emp_id IN AS NUMBER, bonus_amt IN NUMBER) AS
BEGIN
UPDATE employees SET salary = salary + bonus_amt
WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
@emp_id FLOAT;
bonus_amt NUMBER;
BEGIN
SELECT @emp_id = employee_id, @bonus_amt = salary
FROM employees
WHERE last_name = 'Jones';
dbo.emp_bonus(@emp_id, bonus_amt);
END;
|
CREATE OR REPLACE PROCEDURE emp_bonus(emp_id IN NUMBER, bonus_amt IN NUMBER) AS
BEGIN
UPDATE employees SET salary = salary + bonus_amt
WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
emp_id NUMBER;
bonus_amt NUMBER;
BEGIN
SELECT employee_id, salary
INTO emp_id, bonus_amt
FROM employees
WHERE last_name = 'Jones';
emp_bonus(emp_id, bonus_amt);
END;
|
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@id FLOAT) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @name VARCHAR(100);
SELECT @name = ename FROM emp WHERE empno = @id;
RETURN @name;
IF @@fetch_status <> 0 BEGIN
RETURN NULL;
END
END;
DECLARE
@ename VARCHAR(100);
BEGIN
SET @ename = dbo.get_employee_name(10);
IF @ename IS NULL BEGIN
-- Em... ***
END
ELSE BEGIN
-- Em... ***
END
-- Ot... ***
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(id NUMBER) RETURN VARCHAR2 IS
name VARCHAR2(100);
BEGIN
SELECT ename INTO name FROM emp WHERE empno = id;
RETURN name;
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
END;
DECLARE
ename VARCHAR2(100);
BEGIN
ename := get_employee_name(10);
IF ename IS NULL THEN
-- Employee not found
ELSE
-- Employee found
END IF;
-- Other statements...
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc
AS NUMBER,
p2 VARCHAR2,
OUT p3 OUT NUMBER
l_var1 NUMBER;
BEGIN
set p1 2;
set l_var1;
p2 BEGIN
RAERR p2, , 16, 1;
END
INTO my_tablecol1, col2 p1, p2;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAE;
END;
|
CREATE OR REPLACE PROCEDURE my_proc(
p1 IN NUMBER,
p2 IN VARCHAR2,
OUT p3 OUT NUMBER)
IS
l_var1 NUMBER;
BEGIN
l_var1 := p1 * 2;
p3 := l_var1;
IF p2 = 'Y' THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid parameter value: ' || p2);
END IF;
INSERT INTO my_table(col1, col2) VALUES(p1, p2);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE my_proc (@param1 IN AS VARCHAR2, param2 OUT NUMBER)
AS
BEGIN
SELECT @param2 = COUNT(*) FROM my_table WHERE name LIKE param1;
EXCEPTION
WHEN OTHERS THEN
set @param2 = -1;
ROLLBACK;
END;
DECLARE
@my_var1 VARCHAR(20);
BEGIN
dbo.my_proc('John', @my_var1);
IF @my_var1 > 0 BEGIN
UPDATE my_table SET age = age + 1 WHERE name LIKE 'John';
END
END;
|
CREATE PROCEDURE my_proc (param1 IN VARCHAR2, param2 OUT NUMBER)
AS
BEGIN
SELECT COUNT(*) INTO param2 FROM my_table WHERE name LIKE param1;
EXCEPTION
WHEN OTHERS THEN
param2 := -1;
ROLLBACK;
END;
DECLARE
my_var1 VARCHAR2(20);
BEGIN
my_proc('John', my_var1);
IF my_var1 > 0 THEN
UPDATE my_table SET age = age + 1 WHERE name LIKE 'John';
END IF;
END;
|
DECLARE
@v_name VARCHAR(20);
v_age ROW_NUMBER(2);
BEGIN
SELECT @v_name = name, @v_age = age FROM employees WHERE id = 1;
IF v_age > 18 BEGIN
DBMS_OUTPUT.PUT_LINE('Adult');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Minor');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END; | DECLARE
v_name VARCHAR2(20);
v_age NUMBER(2);
BEGIN
SELECT name, age INTO v_name, v_age FROM employees WHERE id = 1;
IF v_age > 18 THEN
DBMS_OUTPUT.PUT_LINE('Adult');
ELSE
DBMS_OUTPUT.PUT_LINE('Minor');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END; |
CREATE TABLE users (
id BIGINT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE PROCEDURE insert_user (
@p_name VARCHAR(4000)
) AS
BEGIN
SET NOCOUNT ON;
INSERT INTO users (name) VALUES (@p_name);
END;
BEGIN
dbo.INSERT_USER('John Doe');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting user: ' + ISNULL(SQLERRM, ''));
END;
SELECT * FROM users WHERE name LIKE '%John%';
|
CREATE TABLE users (
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(255) NOT NULL
);
CREATE PROCEDURE insert_user (
p_name VARCHAR2
) AS
BEGIN
INSERT INTO users (name) VALUES (p_name);
END;
BEGIN
INSERT_USER('John Doe');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting user: ' || SQLERRM);
END;
SELECT * FROM users WHERE name LIKE '%John%';
|
*** rocedure to calculate the factorial of a number
CREATE PROCEDURE GetFactorial(@Num IN AS NUMBER, OUT Result OUT NUMBER)
AS
BEGIN
Set @Result = 1;
DECLARE @I INT = 1 ;
WHILE @I <= Num BEGIN
Set @Result = Result * I;
END;
END;
/
*** to check if a number is even
CREATE FUNCTION IsEven(@Num IN NUMBERDECLARE ) RETURN
BEGIN BOOLEAN
RETURN NULL;
END;
AS
BEGIN
RETURN (Num % 2 = 0);
END;
/
*** e and assign a value
DECLARE
@v_num FLOAT := 5;
BEGIN
*** a table
INSERT INTO Employees (ID, Name, Salary)
VALUES (@v_num, 'John Doe', 10000);
*** table
UPDATE Employees SET Salary = 15000
WHERE ID = @v_num;
*** a table
DELETE FROM Employees
WHERE ID = @v_num;
*** test error handling
RAISERROR('-20000 Custom error message', 16, 1);
END;
/
|
-- Create a stored procedure to calculate the factorial of a number
CREATE PROCEDURE GetFactorial(Num IN NUMBER, OUT Result OUT NUMBER)
AS
BEGIN
Result := 1;
FOR I IN 1 .. Num LOOP
Result := Result * I;
END LOOP;
END;
/
-- Create a function to check if a number is even
CREATE FUNCTION IsEven(Num IN NUMBER) RETURN BOOLEAN
AS
BEGIN
RETURN (Num % 2 = 0);
END;
/
-- Declare a variable and assign a value
DECLARE
v_num NUMBER := 5;
BEGIN
-- Insert a row into a table
INSERT INTO Employees (ID, Name, Salary)
VALUES (v_num, 'John Doe', 10000);
-- Update a row in a table
UPDATE Employees SET Salary = 15000
WHERE ID = v_num;
-- Delete a row from a table
DELETE FROM Employees
WHERE ID = v_num;
-- Raise an error to test error handling
RAISE_APPLICATION_ERROR(-20000, 'Custom error message');
END;
/
|
CREATE PROCEDURE get_employee_salary(
@employee_id IN AS NUMBER,
salary OUT NUMBER
)
AS
BEGIN
SELECT @salary = salary
FROM employees
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @salary = 0;
END
END;
|
CREATE PROCEDURE get_employee_salary(
employee_id IN NUMBER,
salary OUT NUMBER
)
AS
BEGIN
SELECT salary
INTO salary
FROM employees
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
salary := 0;
END;
|
DECLARE
-- Va... ***
@v_emp_id FLOAT;
v_salary NUMBER;
v_commission NUMBER;
v_bonus NUMBER;
BEGIN
*** Call
PRC_GET_EMPLOYEE_INFO(@v_emp_id, v_salary, OUT v_commission, OUT v_bonus);
-- Co... ***
IF v_commission > 0 BEGIN
-- SQ... ***
UPDATE employees SET salary = salary + v_commission
WHERE employee_id = @v_emp_id;
END
ELSE BEGIN
-- Er... ***
RAISERROR('-20001 No commission earned.', 16, 1);
END
END;
/
|
DECLARE
-- Variables
v_emp_id NUMBER;
v_salary NUMBER;
v_commission NUMBER;
v_bonus NUMBER;
BEGIN
-- Stored Procedure Call
PRC_GET_EMPLOYEE_INFO(v_emp_id, v_salary, OUT v_commission, OUT v_bonus);
-- Control Flow
IF v_commission > 0 THEN
-- SQL Statement
UPDATE employees SET salary = salary + v_commission
WHERE employee_id = v_emp_id;
ELSE
-- Error Handling
RAISE_APPLICATION_ERROR(-20001, 'No commission earned.');
END IF;
END;
/
|
DECLARE
@v_sal FLOAT;
BEGIN
SELECT @v_sal = salary FROM employees WHERE employee_id = 1;
IF @v_sal > 10000 BEGIN
UPDATE employees SET salary = 11000 WHERE employee_id = 1;
END
ELSE BEGIN
UPDATE employees SET salary = 9000 WHERE employee_id = 1;
END
END;
|
DECLARE
v_sal NUMBER;
BEGIN
SELECT salary INTO v_sal FROM employees WHERE employee_id = 1;
IF v_sal > 10000 THEN
UPDATE employees SET salary = 11000 WHERE employee_id = 1;
ELSE
UPDATE employees SET salary = 9000 WHERE employee_id = 1;
END IF;
END;
|
[
BEGIN
FOR rec IN (SELECT * FROM table1 WHERE ...) LOOP
-- Do something with the row
END LOOP;
IF (ERROR_CODE() = 20000) THEN
-- Log error and return false
RETURN FALSE;
END IF;
-- Continue executing code
FUNCTION my_func(param1, param2) RETURN NUMBER IS
DECLARE
local_var NUMBER;
BEGIN
local_var := param1 + param2;
RETURN local_var;
END;
END my_func;
CALL my_func('A', 5);
IF (CONDITION) THEN
-- Do something
EXIT;
END IF;
-- More code
PROCEDURE my_proc(param1, param2) IS
BEGIN
-- Do something
RAISE_APPLICATION_ERROR(-20000, 'Error message');
END;
END my_proc;
BEGIN
my_proc('A', 5);
EXCEPTION
WHEN OTHERS THEN
-- Handle error
END;
END;] | `
BEGIN
FOR rec IN (SELECT * FROM table1 WHERE ...) LOOP
-- Do something with the row
END LOOP;
IF (ERROR_CODE() = 20000) THEN
-- Log error and return false
RETURN FALSE;
END IF;
-- Continue executing code
FUNCTION my_func(param1, param2) RETURN NUMBER IS
DECLARE
local_var NUMBER;
BEGIN
local_var := param1 + param2;
RETURN local_var;
END;
END my_func;
CALL my_func('A', 5);
IF (CONDITION) THEN
-- Do something
EXIT;
END IF;
-- More code
PROCEDURE my_proc(param1, param2) IS
BEGIN
-- Do something
RAISE_APPLICATION_ERROR(-20000, 'Error message');
END;
END my_proc;
BEGIN
my_proc('A', 5);
EXCEPTION
WHEN OTHERS THEN
-- Handle error
END;
END;` |
IF OBJECT_ID('get_employee_info', 'FN') IS NOT NULL
DROP FUNCTION get_employee_info;
GO
CREATE FUNCTION get_employee_info(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; AS
rc SYS_REFCURSOR;
BEGIN
SELECT * FROM employees WHERE employee_id = emp_id;
RETURN rc;
END get_employee_info;
BEGIN
DECLARE
@emp_info SYS_REFCURSOR;
emp_id SET @NUMBER = 100;
BEGIN
SET @emp_info = dbo.get_employee_info(emp_id);
WHILE 1=1 BEGIN
FETCH emp_info INTO @emp_id, @first_name, @last_name;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + ISNULL(CONVERT(VARCHAR, emp_id), ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, ''));
END;
DBMS_OUTPUT.PUT_LINE('Employee information retrieved successfully.');
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 IN NUMBER) RETURN SYS_REFCURSOR AS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR
SELECT * FROM employees WHERE employee_id = emp_id;
RETURN rc;
END get_employee_info;
BEGIN
DECLARE
emp_info SYS_REFCURSOR;
emp_id NUMBER := 100;
BEGIN
emp_info := get_employee_info(emp_id);
LOOP
FETCH emp_info INTO emp_id, first_name, last_name;
EXIT WHEN emp_info%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || TO_CHAR(emp_id));
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Employee information retrieved successfully.');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID: ' || emp_id);
END;
END;
|
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL
DROP PROCEDURE sample_proc;
GO
CREATE PROCEDURE sample_proc (
@p_id IN AS NUMBER,
OUT p_name OUT VARCHAR2
) AS
BEGIN
SELECT @p_name = name FROM table_name WHERE id = p_id;
IF @@FETCH_STATUS <> 0 BEGIN
raiserror('-20000 No data found for id ' + isnull(p_id, ''), 16, 1);
END
WHEN OTHERS THEN
raise;
END;
IF OBJECT_ID('get_max_id', 'FN') IS NOT NULL
DROP FUNCTION get_max_id;
GO
CREATE FUNCTION get_max_id (
@t_name IN VARCHAR2
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN (
SELECT MAX(id) FROM t_name
);
END;
BEGIN
DECLARE
@emp_count FLOAT;
BEGIN
SELECT @emp_count = COUNT(*) FROM employees;
IF @emp_count > 10000 BEGIN
raiserror('-20100 Employee count is too high', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
SET @emp_count = 0;
END
END;
END;
|
CREATE OR REPLACE PROCEDURE sample_proc (
p_id IN NUMBER,
OUT p_name OUT VARCHAR2
) AS
BEGIN
SELECT name INTO p_name FROM table_name WHERE id = p_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
raise_application_error(-20000, 'No data found for id ' || p_id);
WHEN OTHERS THEN
raise;
END;
CREATE OR REPLACE FUNCTION get_max_id (
t_name IN VARCHAR2
) RETURN NUMBER AS
BEGIN
RETURN (
SELECT MAX(id) FROM t_name
);
END;
BEGIN
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees;
IF emp_count > 10000 THEN
raise_application_error(-20100, 'Employee count is too high');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
emp_count := 0;
END;
END;
|
CREATE PROCEDURE update_sales(
@IN customer_id AS NUMBER,
IN product_id NUMBER,
IN quantity NUMBER
)
AS
BEGIN
UPDATE sales
SET quantity = quantity + :quantity
WHERE customer_id = :customer_id
AND product_id = :product_id;
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20001 No such sale found', 16, 1);
END
COMMIT;
END;
BEGIN
dbo.update_sales(1, 2, 10);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE update_sales(
IN customer_id NUMBER,
IN product_id NUMBER,
IN quantity NUMBER
)
AS
BEGIN
UPDATE sales
SET quantity = quantity + :quantity
WHERE customer_id = :customer_id
AND product_id = :product_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No such sale found');
END IF;
COMMIT;
END;
BEGIN
update_sales(1, 2, 10);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL
DROP PROCEDURE emp_bonus;
GO
CREATE PROCEDURE emp_bonus(
@emp_id IN AS NUMBER,
bonus OUT NUMBER
) AS
BEGIN
SELECT @bonus = bonus
FROM employees
WHERE emp_id = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @bonus = 0;
END
END;
|
CREATE OR REPLACE PROCEDURE emp_bonus(
emp_id IN NUMBER,
bonus OUT NUMBER
) AS
BEGIN
SELECT bonus INTO bonus
FROM employees
WHERE emp_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
bonus := 0;
END;
|
CREATE PROCEDURE InsertCustomer(
@cust_name IN AS VARCHAR2,
cust_address IN VARCHAR2,
cust_email IN VARCHAR2,
cust_phone IN VARCHAR2
) AS
v_id NUMBER;
BEGIN
SELECT @v_id = customer_id FROM customers WHERE customer_email = cust_email;
IF v_id IS NULL BEGIN
INSERT INTO customers(customer_name, customer_address, customer_email, customer_phone)
VALUES(cust_name, cust_address, cust_email, cust_phone);
COMMIT;
END
ELSE BEGIN
RAISERROR('-20000 Customer already exists', 16, 1);
END
END;
CREATE FUNCTION GetAverageOrderValue(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
v_total_amount NUMBER;
v_order_count NUMBER;
BEGIN
SELECT @v_total_amount = SUM(order_amount) COUNT(*) INTO v_order_count
FROM orders WHERE customer_id = customer_id;
RETURN v_total_amount / v_order_count;
END;
SQL> BEGIN
TRY
INSERT INTO customers(customer_name, customer_address, customer_email, customer_phone)
VALUES('John Doe', '123 Main Street', 'john.doe@example.com', '555-123-4567');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting customer: ' + ISNULL(SQLERRM, ''));
END;
END;
/
|
CREATE PROCEDURE InsertCustomer(
cust_name IN VARCHAR2,
cust_address IN VARCHAR2,
cust_email IN VARCHAR2,
cust_phone IN VARCHAR2
) AS
v_id NUMBER;
BEGIN
SELECT customer_id INTO v_id FROM customers WHERE customer_email = cust_email;
IF v_id IS NULL THEN
INSERT INTO customers(customer_name, customer_address, customer_email, customer_phone)
VALUES(cust_name, cust_address, cust_email, cust_phone);
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Customer already exists');
END IF;
END;
CREATE FUNCTION GetAverageOrderValue(customer_id IN NUMBER) RETURN NUMBER AS
v_total_amount NUMBER;
v_order_count NUMBER;
BEGIN
SELECT SUM(order_amount) INTO v_total_amount, COUNT(*) INTO v_order_count
FROM orders WHERE customer_id = customer_id;
RETURN v_total_amount / v_order_count;
END;
SQL> BEGIN
TRY
INSERT INTO customers(customer_name, customer_address, customer_email, customer_phone)
VALUES('John Doe', '123 Main Street', 'john.doe@example.com', '555-123-4567');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error inserting customer: ' || SQLERRM);
END;
END;
/
|
DECLARE
@v_emp_id FLOAT := 10;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = @v_emp_id;
IF v_salary < 10000 BEGIN
RAISERROR('-20101 Salary below minimum threshold', 16, 1);
END
UPDATE employees SET salary = v_salary * 1.1 WHERE employee_id = @v_emp_id;
COMMIT;
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 unexpected error occurred');
ROLLBACK;
END;
|
DECLARE
v_emp_id NUMBER := 10;
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id;
IF v_salary < 10000 THEN
RAISE_APPLICATION_ERROR(-20101, 'Salary below minimum threshold');
END IF;
UPDATE employees SET salary = v_salary * 1.1 WHERE employee_id = v_emp_id;
COMMIT;
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 unexpected error occurred');
ROLLBACK;
END;
|
DECLARE
@v_empno VARCHAR(4000) /* Use -meta option EMPLOYEE.empno%TYPE */;
v_salary EMPLOYEE.salary%TYPE;
BEGIN
SELECT @v_empno = empno, @v_salary = salary FROM employee WHERE empno = 100;
IF v_salary > 10000 BEGIN
UPDATE employee SET salary = v_salary * 1.1 WHERE empno = @v_empno;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Salary too low for a raise', 16, 1);
END
END;
|
DECLARE
v_empno EMPLOYEE.empno%TYPE;
v_salary EMPLOYEE.salary%TYPE;
BEGIN
SELECT empno, salary INTO v_empno, v_salary FROM employee WHERE empno = 100;
IF v_salary > 10000 THEN
UPDATE employee SET salary = v_salary * 1.1 WHERE empno = v_empno;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Salary too low for a raise');
END IF;
END;
|
BEGIN
*** definition
CREATE PROCEDURE my_proc(
@p_in IN AS NUMBER,
p_out OUT NUMBER
) IS
BEGIN
set @p_out = p_in * 2;
END;
-- Fu... ***
CREATE FUNCTION my_func(@p_in FLOAT) RETURNS FLOAT AS
BEGIN
RETURN @p_in + 1;
END;
-- SQ... ***
SELECT * FROM my_table
WHERE id = 1;
*** truct
IF p_in > 0 BEGIN
set @p_out = p_in * 2;
END
-- Er... ***
BEGIN
*** se an error
EXCEPTION
WHEN OTHERS THEN
-- Er... ***
END;
END;
|
BEGIN
-- Stored procedure definition
PROCEDURE my_proc(
p_in IN NUMBER,
p_out OUT NUMBER
) IS
BEGIN
p_out := p_in * 2;
END;
-- Function definition
FUNCTION my_func(p_in NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_in + 1;
END;
-- SQL statement
SELECT * FROM my_table
WHERE id = 1;
-- Control flow construct
IF p_in > 0 THEN
p_out := p_in * 2;
END IF;
-- Error handling
BEGIN
-- Code that may raise an error
EXCEPTION
WHEN OTHERS THEN
-- Error handling code
END;
END;
|
IF OBJECT_ID('increment_sales_by_product_category', 'P') IS NOT NULL
DROP PROCEDURE increment_sales_by_product_category;
GO
CREATE PROCEDURE increment_sales_by_product_category(
@category_id IN AS NUMBER,
amount IN NUMBER)
IS
BEGIN
UPDATE products
SET sales = sales + amount
WHERE product_category_id = category_id;
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE increment_sales_by_product_category(
category_id IN NUMBER,
amount IN NUMBER)
IS
BEGIN
UPDATE products
SET sales = sales + amount
WHERE product_category_id = category_id;
COMMIT;
END;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 7369;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
ELSE BEGIN
UPDATE emp SET salary = salary * 1.10 WHERE empno = @v_empno;
END
COMMIT;
END;
|
DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 7369;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
ELSE
UPDATE emp SET salary = salary * 1.10 WHERE empno = v_empno;
END IF;
COMMIT;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_salary = salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000 BEGIN
UPDATE emp
SET salary = salary * 1.1
WHERE empno = @v_empno;
END
ELSE BEGIN
UPDATE emp
SET salary = salary * 1.05
WHERE empno = @v_empno;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_salary NUMBER;
BEGIN
SELECT empno, ename, salary
INTO v_empno, v_ename, v_salary
FROM emp
WHERE empno = 7839;
IF v_salary > 10000 THEN
UPDATE emp
SET salary = salary * 1.1
WHERE empno = v_empno;
ELSE
UPDATE emp
SET salary = salary * 1.05
WHERE empno = v_empno;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_emp_id FLOAT := 10;
v_emp_name dbo.VARCHAR2(50);
v_emp_salary NUMBER;
BEGIN
SELECT @v_emp_name = ename, @v_emp_salary = sal
FROM emp
WHERE empno = @v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + ISNULL(CONVERT(VARCHAR, v_emp_salary), ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER := 10;
v_emp_name VARCHAR2(50);
v_emp_salary NUMBER;
BEGIN
SELECT ename, sal
INTO v_emp_name, v_emp_salary
FROM emp
WHERE empno = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || TO_CHAR(v_emp_salary));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE update_salary (
@emp_id IN AS NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE FUNCTION get_employee_name (
@emp_id IN NUMBER
DECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT name
FROM employees
WHERE employee_id = emp_id
);
END;
DECLARE
@employee_id FLOAT := 100;
new_salary SET @NUMBER = 10000;
emp_name dbo.VARCHAR2(50);
BEGIN
dbo.update_salary(@employee_id, new_salary);
SET @emp_name = dbo.get_employee_name(@employee_id);
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for employee ' + isnull(emp_name, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
|
CREATE PROCEDURE update_salary (
emp_id IN NUMBER,
new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
CREATE FUNCTION get_employee_name (
emp_id IN NUMBER
)
RETURN VARCHAR2
AS
BEGIN
RETURN (
SELECT name
FROM employees
WHERE employee_id = emp_id
);
END;
DECLARE
employee_id NUMBER := 100;
new_salary NUMBER := 10000;
emp_name VARCHAR2(50);
BEGIN
update_salary(employee_id, new_salary);
emp_name := get_employee_name(employee_id);
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for employee ' || emp_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
|
CREATE PROCEDURE test_proc (@id IN AS VARCHAR2) AS
x NUMBER;
BEGIN
SELECT @x = COUNT(*) FROM t WHERE id = id;
IF x = 0 BEGIN
RAISERROR('-20001 No rows found', 16, 1);
END
UPDATE t SET name = 'updated name' WHERE id = id;
END;
|
CREATE PROCEDURE test_proc (id IN VARCHAR2) AS
x NUMBER;
BEGIN
SELECT COUNT(*) INTO x FROM t WHERE id = id;
IF x = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found');
END IF;
UPDATE t SET name = 'updated name' WHERE id = id;
END;
|
IF OBJECT_ID('update_customer', 'P') IS NOT NULL
DROP PROCEDURE update_customer;
GO
CREATE PROCEDURE update_customer(@id IN AS NUMBER, name IN VARCHAR2) IS
BEGIN
UPDATE customers SET name = name WHERE id = id;
COMMIT;
END;
/
DECLARE
@name VARCHAR(50);
BEGIN
SELECT @name = @name FROM customers WHERE id = 1;
IF @name IS NOT NULL BEGIN
dbo.UPDATE_CUSTOMER(1, 'New Name');
DBMS_OUTPUT.PUT_LINE('Customer updated.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Customer not found.');
END
END;
/
|
CREATE OR REPLACE PROCEDURE update_customer(id IN NUMBER, name IN VARCHAR2) IS
BEGIN
UPDATE customers SET name = name WHERE id = id;
COMMIT;
END;
/
DECLARE
name VARCHAR2(50);
BEGIN
SELECT name INTO name FROM customers WHERE id = 1;
IF name IS NOT NULL THEN
UPDATE_CUSTOMER(1, 'New Name');
DBMS_OUTPUT.PUT_LINE('Customer updated.');
ELSE
DBMS_OUTPUT.PUT_LINE('Customer not found.');
END IF;
END;
/
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = employee_id
FROM employees
WHERE last_name = 'King';
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 No employee with last name King', 16, 1);
END
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_emp_id;
COMMIT;
END;
/
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT employee_id
INTO v_emp_id
FROM employees
WHERE last_name = 'King';
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee with last name King');
END IF;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_emp_id;
COMMIT;
END;
/
|
CREATE FUNCTION get_employee_info(@employee_id IN NUMBERDECLARE )
RETURN
BEGIN employee_record
return null;
end;
IS
emp employee_record;
BEGIN
SELECT @emp = * FROM employees WHERE employee_id = employee_id;
RETURN emp;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE @i INT = 1;
WHILE @i <= 5 BEGIN
set @emp = dbo.get_employee_info(i);
DBMS_OUTPUT.PUT_LINE(isnull(emp.first_name, '') + ' ' + isnull(emp.last_name, ''));
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while fetching employee info.');
END;
|
CREATE FUNCTION get_employee_info(employee_id IN NUMBER)
RETURN employee_record
IS
emp employee_record;
BEGIN
SELECT * INTO emp FROM employees WHERE employee_id = employee_id;
RETURN emp;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
FOR i IN 1..5 LOOP
emp := get_employee_info(i);
DBMS_OUTPUT.PUT_LINE(emp.first_name || ' ' || emp.last_name);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while fetching employee info.');
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
*** by 10%
UPDATE emp
SET salary = salary * 1.10
WHERE emp_id = @v_emp_id;
END
*** that may have occurred
IF @@ERROR <> 0 BEGIN
RAISERROR('-20001 Error updating employee salary', 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 > 10000 THEN
-- Raise the salary by 10%
UPDATE emp
SET salary = salary * 1.10
WHERE emp_id = v_emp_id;
END IF;
-- Handle any errors that may have occurred
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Error updating employee salary');
END IF;
END;
|
CREATE PROCEDURE get_employee_info (
@emp_id IN AS NUMBER,
OUT emp_name VARCHAR2,
OUT emp_salary NUMBER
) AS
BEGIN
SELECT @emp_name = ename, @emp_salary = sal FROM emp WHERE empno = emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
set @emp_name = NULL;
set @emp_salary = 0;
END
END;
/
DECLARE
@emp_name VARCHAR(20);
emp_salary NUMBER;
BEGIN
dbo.get_employee_info(100, @emp_name, emp_salary);
IF @emp_name IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(emp_salary, ''));
END;
/
|
CREATE PROCEDURE get_employee_info (
emp_id IN NUMBER,
OUT emp_name VARCHAR2,
OUT emp_salary NUMBER
) AS
BEGIN
SELECT ename, sal INTO emp_name, emp_salary FROM emp WHERE empno = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
emp_name := NULL;
emp_salary := 0;
END;
/
DECLARE
emp_name VARCHAR2(20);
emp_salary NUMBER;
BEGIN
get_employee_info(100, emp_name, emp_salary);
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || emp_salary);
END;
/
|
DECLARE
@sal FLOAT := 70000;
comm SET @NUMBER = @sal * 0.1;
tax SET @NUMBER = 0;
BEGIN
IF @sal <= 50000 BEGIN
SET @tax = @sal * 0.15;
END
ELSE IF @sal <= 70000 BEGIN
SET @tax = @sal * 0.20;
END
ELSE BEGIN
SET @tax = @sal * 0.25;
END
UPDATE employees SET tax = tax WHERE salary = @sal;
COMMIT;
END;
|
DECLARE
sal NUMBER := 70000;
comm NUMBER := sal * 0.1;
tax NUMBER := 0;
BEGIN
IF sal <= 50000 THEN
tax := sal * 0.15;
ELSIF sal <= 70000 THEN
tax := sal * 0.20;
ELSE
tax := sal * 0.25;
END IF;
UPDATE employees SET tax = tax WHERE salary = sal;
COMMIT;
END;
|
DECLARE
@TYPE numbers_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
emp_salaries SET @numbers_table = dbo.numbers_table();
emp_average SET @NUMBER = 0;
BEGIN
DECLARE @emp_id INT = 1 ;
WHILE @emp_id <= 100 BEGIN
dbo.emp_salaries(emp_id) := DBMS_RANDOM.RANDOM;
END;
DECLARE @emp_id INT = 1 ;
WHILE @emp_id <= 100 BEGIN
SET @emp_average = emp_average + dbo.emp_salaries(emp_id);
END;
SET @emp_average = emp_average / 100;
DBMS_OUTPUT.PUT_LINE('Average salary: ' + isnull(emp_average, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END; | DECLARE
TYPE numbers_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
emp_salaries numbers_table := numbers_table();
emp_average NUMBER := 0;
BEGIN
FOR emp_id IN 1 .. 100 LOOP
emp_salaries(emp_id) := DBMS_RANDOM.RANDOM;
END LOOP;
FOR emp_id IN 1 .. 100 LOOP
emp_average := emp_average + emp_salaries(emp_id);
END LOOP;
emp_average := emp_average / 100;
DBMS_OUTPUT.PUT_LINE('Average salary: ' || emp_average);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END; |
IF OBJECT_ID('sproc_name', 'P') IS NOT NULL
DROP PROCEDURE sproc_name;
GO
CREATE PROCEDURE sproc_name(
@param1 IN AS NUMBER,
param2 IN OUT VARCHAR2
)
IS
BEGIN
*** ecords.
UPDATE table_name SET column_name = param1 WHERE column_name = param2;
-- Er... ***
IF @@ERROR <> 0 BEGIN
RAISERROR('-20001 Update failed', 16, 1);
END
*** ameter
set @param2 = 'Updated successfully';
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
RAISERROR('-20002 An error occurred', 16, 1);
END;
SELECT * FROM table_name WHERE condition;
WHILE 1=1 BEGIN
FETCH table_name INTO @rec;
IF @@FETCH_STATUS <> 0 BREAK;
*** the record.
END;
CREATE FUNCTION func_name(
@param1 IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END;
IS
BEGIN
*** ulations.
RETURN param1 + 10;
END;
|
CREATE OR REPLACE PROCEDURE sproc_name(
param1 IN NUMBER,
param2 IN OUT VARCHAR2
)
IS
BEGIN
-- Update database records.
UPDATE table_name SET column_name = param1 WHERE column_name = param2;
-- Error handling
IF SQLCODE <> 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Update failed');
END IF;
-- Return output parameter
param2 := 'Updated successfully';
EXCEPTION
WHEN OTHERS THEN
-- Handle errors here.
RAISE_APPLICATION_ERROR(-20002, 'An error occurred');
END;
SELECT * FROM table_name WHERE condition;
LOOP
FETCH table_name INTO rec;
EXIT WHEN table_name%NOTFOUND;
-- Do something with the record.
END LOOP;
CREATE FUNCTION func_name(
param1 IN NUMBER
) RETURN NUMBER
IS
BEGIN
-- Perform some calculations.
RETURN param1 + 10;
END;
|
if object_id('add_employee', 'P') is not null
drop procedure add_employee;
go
create procedure add_employee(
@ename in as varchar2,
job in varchar2,
mgr in number,
sal in number
)
as
empno integer;
begin
select @empno = max(empno)+1 from emp;
insert into emp(empno, ename, job, mgr, sal)
values(empno, ename, job, mgr, sal);
commit;
exception
when dup_val_on_index then
rollback;
end;
|
create or replace procedure add_employee(
ename in varchar2,
job in varchar2,
mgr in number,
sal in number
)
as
empno integer;
begin
select max(empno)+1 into empno from emp;
insert into emp(empno, ename, job, mgr, sal)
values(empno, ename, job, mgr, sal);
commit;
exception
when dup_val_on_index then
rollback;
end;
|
CREATE PROCEDURE get_customer_orders(@cust_id IN AS NUMBER) AS
order_cursor SYS_REFCURSOR;
BEGIN
SELECT order_id, order_date, total_price
FROM orders
WHERE customer_id = cust_id;
RETURN order_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No orders found for customer.', 16, 1);
END
END;
|
CREATE PROCEDURE get_customer_orders(cust_id IN NUMBER) AS
order_cursor SYS_REFCURSOR;
BEGIN
OPEN order_cursor FOR
SELECT order_id, order_date, total_price
FROM orders
WHERE customer_id = cust_id;
RETURN order_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No orders found for customer.');
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@id IN AS INTEGER) AS
v_name VARCHAR(100);
BEGIN
SELECT @v_name = name FROM t_names WHERE id = id;
IF v_name IS NULL BEGIN
RAISERROR('-20001 Name not found', 16, 1);
END
ELSE BEGIN
dbms_output.put_line('Name: ' + isnull(v_name, ''));
END
END;
|
CREATE OR REPLACE PROCEDURE my_proc (id IN INTEGER) AS
v_name VARCHAR(100);
BEGIN
SELECT name INTO v_name FROM t_names WHERE id = id;
IF v_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Name not found');
ELSE
dbms_output.put_line('Name: ' || v_name);
END IF;
END;
|
*** rocedure
CREATE PROCEDURE my_proc (@p_id IN AS NUMBER, p_name IN VARCHAR2)
AS
BEGIN
-- Lo... ***
v_id NUMBER;
v_name dbo.VARCHAR2(20);
*** ameters
set @v_id = p_id;
set @v_name = p_name;
*** ations
-- ..... ***
-- Re... ***
RETURN;
END;
*** rocedure
BEGIN
dbo.my_proc(10, 'John Doe');
END;
-- Cr... ***
CREATE FUNCTION my_func (@p_id IN NUMBERDECLARE , p_name
begin IN
RETURN NULL;
END; VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
-- Lo... ***
v_id NUMBER;
v_name dbo.VARCHAR2(20);
*** ameters
set @v_id = p_id;
set @v_name = p_name;
*** ations
-- ..... ***
-- Re... ***
RETURN v_name;
END;
-- Ca... ***
BEGIN
DECLARE
@v_result VARCHAR(20);
BEGIN
SET @v_result = dbo.my_func(10, 'John Doe');
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
NULL;
END;
END;
*** ement
SELECT * FROM my_table
WHERE id = 10;
*** tructs
IF v_id > 10 BEGIN
-- Do... ***
END
ELSE BEGIN
-- Do... ***
END
*** enarios
BEGIN
*** me code
-- ..... ***
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
NULL;
END;
|
-- Create a stored procedure
CREATE PROCEDURE my_proc (p_id IN NUMBER, p_name IN VARCHAR2)
AS
BEGIN
-- Local variables
v_id NUMBER;
v_name VARCHAR2(20);
-- Get the input parameters
v_id := p_id;
v_name := p_name;
-- Perform some operations
-- ...
-- Return the results
RETURN;
END;
-- Call the stored procedure
BEGIN
my_proc(10, 'John Doe');
END;
-- Create a function
CREATE FUNCTION my_func (p_id IN NUMBER, p_name IN VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
-- Local variables
v_id NUMBER;
v_name VARCHAR2(20);
-- Get the input parameters
v_id := p_id;
v_name := p_name;
-- Perform some operations
-- ...
-- Return the result
RETURN v_name;
END;
-- Call the function
BEGIN
DECLARE
v_result VARCHAR2(20);
BEGIN
v_result := my_func(10, 'John Doe');
EXCEPTION
WHEN OTHERS THEN
-- Handle the error
NULL;
END;
END;
-- Create a SQL statement
SELECT * FROM my_table
WHERE id = 10;
-- Control flow constructs
IF v_id > 10 THEN
-- Do something
ELSE
-- Do something else
END IF;
-- Error handling scenarios
BEGIN
-- Try to execute some code
-- ...
EXCEPTION
WHEN OTHERS THEN
-- Handle the error
NULL;
END;
|
DECLARE
cur_employees CURSOR FOR SELECT * FROM employees;
rec_employee employees%ROWTYPE;
v_emp_id SET @NUMBER = &emp_id;
BEGIN
OPEN cur_employees;
FETCH cur_employees INTO @rec_employee;
WHILE cur_employees%FOUND BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = rec_employee.employee_id;
FETCH cur_employees INTO @rec_employee;
END;
CLOSE cur_employees;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 No employee found with the specified ID.', 16, 1);
END
END;
|
DECLARE
cur_employees CURSOR FOR SELECT * FROM employees;
rec_employee employees%ROWTYPE;
v_emp_id NUMBER := &emp_id;
BEGIN
OPEN cur_employees;
FETCH cur_employees INTO rec_employee;
WHILE cur_employees%FOUND LOOP
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = rec_employee.employee_id;
FETCH cur_employees INTO rec_employee;
END LOOP;
CLOSE cur_employees;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'No employee found with the specified ID.');
END;
|
-- De... ***
v_emp_id NUMBER;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
-- Op... ***
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 100;
-- Pr... ***
WHILE 1=1 BEGIN
-- Fe... ***
FETCH employees INTO @v_emp_id, @v_emp_name, @v_salary;
*** no more data
IF @@FETCH_STATUS <> 0 BREAK;
-- Pr... ***
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_salary, ''));
END;
-- Cl... ***
CLOSE employees;
END;
|
-- Declare variables
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
-- Open the cursor
BEGIN
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 100;
-- Process the cursor
LOOP
-- Fetch the data
FETCH employees INTO v_emp_id, v_emp_name, v_salary;
-- Exit the loop if no more data
EXIT WHEN employees%NOTFOUND;
-- Process the data
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_salary);
END LOOP;
-- Close the cursor
CLOSE employees;
END;
|
sql
CREATE PROCEDURE update_dept
(
@deptno IN AS NUMBER,
dname IN VARCHAR2,
loc IN VARCHAR2
)
AS
BEGIN
UPDATE dept SET dname = dname, loc = loc WHERE deptno = deptno;
COMMIT;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
RAISERROR('-20000 Too many rows updated!', 16, 1);
END;
DECLARE
@sal VARCHAR(4000) /* Use -meta option BONUS.bonus%TYPE */;
BEGIN
SET @sal = BONUS.calc_bonus(7521);
DBMS_OUTPUT.put_line(@sal);
END;
CREATE FUNCTION format_date
(
@date IN DATE
DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN CONVERT(VARCHAR(10), date, 103);
END;
BEGIN
DECLARE r CURSOR FOR SELECT * FROM emp; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.put_line(isnull(@r_empno, '') + ' ' + isnull(@r_ename, ''));
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
END;
| sql
CREATE PROCEDURE update_dept
(
deptno IN NUMBER,
dname IN VARCHAR2,
loc IN VARCHAR2
)
AS
BEGIN
UPDATE dept SET dname = dname, loc = loc WHERE deptno = deptno;
COMMIT;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
RAISE_APPLICATION_ERROR(-20000, 'Too many rows updated!');
END;
DECLARE
sal BONUS.bonus%TYPE;
BEGIN
sal := BONUS.calc_bonus(7521);
DBMS_OUTPUT.put_line(sal);
END;
CREATE FUNCTION format_date
(
date IN DATE
) RETURN VARCHAR2
AS
BEGIN
RETURN TO_CHAR(date, 'DD/MM/YYYY');
END;
BEGIN
FOR r IN (SELECT * FROM emp) LOOP
DBMS_OUTPUT.put_line(r.empno || ' ' || r.ename);
END LOOP;
END;
|
IF OBJECT_ID('sales_report_by_date', 'P') IS NOT NULL
DROP PROCEDURE sales_report_by_date;
GO
CREATE PROCEDURE sales_report_by_date(
@p_start_date IN AS DATE,
p_end_date IN DATE
)
AS
CURSOR c_sales IS
SELECT *
FROM sales
WHERE sale_date BETWEEN p_start_date AND p_end_date;
BEGIN
DECLARE rec CURSOR FOR c_sales LOOP
DBMS_OUTPUT.PUT_LINE(
'Sale ID: ' + isnull(@rec_sale_id, '') + ', Customer ID: ' + isnull(@rec_customer_id, '') + ', Amount: ' + isnull(@rec_amount, '')
);
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing the sales report.');
END;
|
CREATE OR REPLACE PROCEDURE sales_report_by_date(
p_start_date IN DATE,
p_end_date IN DATE
)
AS
CURSOR c_sales IS
SELECT *
FROM sales
WHERE sale_date BETWEEN p_start_date AND p_end_date;
BEGIN
FOR rec IN c_sales LOOP
DBMS_OUTPUT.PUT_LINE(
'Sale ID: ' || rec.sale_id || ', Customer ID: ' || rec.customer_id || ', Amount: ' || rec.amount
);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while processing the sales report.');
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
RETURN -1;
END
RETURN salary;
END;
BEGIN
DECLARE @bonus FLOAT := 1000;
DECLARE @emp_salary FLOAT;
BEGIN
SET @emp_salary = dbo.get_employee_salary(12345);
IF @emp_salary > 5000 BEGIN
SET @bonus = @bonus * 2;
END
UPDATE employees SET bonus = @bonus WHERE employee_id = 12345;
END;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
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;
IF salary IS NULL THEN
RETURN -1;
END IF;
RETURN salary;
END;
BEGIN
DECLARE bonus NUMBER := 1000;
DECLARE emp_salary NUMBER;
BEGIN
emp_salary := get_employee_salary(12345);
IF emp_salary > 5000 THEN
bonus := bonus * 2;
END IF;
UPDATE employees SET bonus = bonus WHERE employee_id = 12345;
END;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END;
|
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL
DROP FUNCTION get_customer_orders;
GO
CREATE FUNCTION get_customer_orders(@customer_id FLOAT)
RETURNS SYS_REFCURSOR
AS
BEGIN
DECLARE @orders_cur SYS_REFCURSOR;
SELECT order_id, order_date, total_amount
FROM orders
WHERE @customer_id = @customer_id;
RETURN @orders_cur;
END;
GO
DECLARE
@orders_refcur SYS_REFCURSOR;
BEGIN
SET @orders_refcur = dbo.get_customer_orders(100);
WHILE 1=1 BEGIN
FETCH orders_refcur INTO @order_id, @order_date, @total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(order_id, '') + ' ' + isnull(order_date, '') + ' ' + isnull(total_amount, ''));
END;
CLOSE orders_refcur;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id NUMBER)
RETURN SYS_REFCURSOR
IS
orders_cur SYS_REFCURSOR;
BEGIN
OPEN orders_cur FOR
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
RETURN orders_cur;
END;
/
DECLARE
orders_refcur SYS_REFCURSOR;
BEGIN
orders_refcur := get_customer_orders(100);
LOOP
FETCH orders_refcur INTO order_id, order_date, total_amount;
EXIT WHEN orders_refcur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(order_id || ' ' || order_date || ' ' || total_amount);
END LOOP;
CLOSE orders_refcur;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
|
*** information
BEGIN
SELECT *
FROM employees
WHERE emp_id IN (
SELECT emp_id
FROM departments
WHERE dept_name = 'Sales'
);
END;
/
-- In... ***
DECLARE
@v_emp_id FLOAT;
BEGIN
INSERT INTO employees (emp_name, emp_email)
VALUES ('John Doe', 'john.doe@example.com');
*** newly inserted employee
SELECT @v_emp_id = dbo.LAST_INSERT_ID();
END;
/
*** nformation
DECLARE
@v_emp_id FLOAT := 100;
BEGIN
UPDATE employees
SET emp_name = 'Jane Doe'
WHERE emp_id = @v_emp_id;
END;
/
-- De... ***
DECLARE
@v_emp_id FLOAT := 105;
BEGIN
DELETE FROM employees
WHERE emp_id = @v_emp_id;
*** oyee was successfully deleted
IF EXISTS (
SELECT 1
FROM employees
WHERE emp_id = @v_emp_id
) BEGIN
RAISERROR('-20001 Employee was not deleted', 16, 1);
END
END;
/
*** to calculate employee bonus
IF OBJECT_ID('CalcBonus', 'P') IS NOT NULL
DROP PROCEDURE CalcBonus;
GO
CREATE PROCEDURE CalcBonus (@p_emp_id IN AS NUMBER)
IS
v_bonus NUMBER;
BEGIN
SELECT @v_bonus = bonus
FROM employees
WHERE emp_id = p_emp_id;
IF v_bonus IS NULL BEGIN
RAISERROR('-20002 Invalid employee ID', 16, 1);
END
ELSE BEGIN
*** ere
END
END CalcBonus;
/
*** if a string is palindrome
IF OBJECT_ID('IsPalindrome', 'FN') IS NOT NULL
DROP FUNCTION IsPalindrome;
GO
CREATE FUNCTION IsPalindrome (@p_str VARCHAR(4000))
RETURNS BIT
AS
BEGIN
RETURN NULL;
*** k palindrome
END;
GO
*** truct
IF dbo.IsPalindrome('racecar') BEGIN
DBMS_OUTPUT.PUT_LINE('racecar is a palindrome');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('racecar is not a palindrome');
END
*** enario
BEGIN
*** se an exception
EXCEPTION
WHEN OTHERS THEN
-- Er... ***
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
-- Retrieve employee information
BEGIN
SELECT *
FROM employees
WHERE emp_id IN (
SELECT emp_id
FROM departments
WHERE dept_name = 'Sales'
);
END;
/
-- Insert new employee
DECLARE
v_emp_id NUMBER;
BEGIN
INSERT INTO employees (emp_name, emp_email)
VALUES ('John Doe', 'john.doe@example.com');
-- Get the ID of the newly inserted employee
SELECT LAST_INSERT_ID() INTO v_emp_id;
END;
/
-- Update employee information
DECLARE
v_emp_id NUMBER := 100;
BEGIN
UPDATE employees
SET emp_name = 'Jane Doe'
WHERE emp_id = v_emp_id;
END;
/
-- Delete employee
DECLARE
v_emp_id NUMBER := 105;
BEGIN
DELETE FROM employees
WHERE emp_id = v_emp_id;
-- Check if the employee was successfully deleted
IF EXISTS (
SELECT 1
FROM employees
WHERE emp_id = v_emp_id
) THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee was not deleted');
END IF;
END;
/
-- Stored procedure to calculate employee bonus
CREATE OR REPLACE PROCEDURE CalcBonus (p_emp_id IN NUMBER)
IS
v_bonus NUMBER;
BEGIN
SELECT bonus
INTO v_bonus
FROM employees
WHERE emp_id = p_emp_id;
IF v_bonus IS NULL THEN
RAISE_APPLICATION_ERROR(-20002, 'Invalid employee ID');
ELSE
-- Calculate bonus here
END IF;
END CalcBonus;
/
-- Function to check if a string is palindrome
CREATE OR REPLACE FUNCTION IsPalindrome (p_str VARCHAR2)
RETURN BOOLEAN
IS
BEGIN
-- Code here to check palindrome
END IsPalindrome;
/
-- Control flow construct
IF IsPalindrome('racecar') THEN
DBMS_OUTPUT.PUT_LINE('racecar is a palindrome');
ELSE
DBMS_OUTPUT.PUT_LINE('racecar is not a palindrome');
END IF;
-- Error handling scenario
BEGIN
-- Code that may raise an exception
EXCEPTION
WHEN OTHERS THEN
-- Error handling code
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE TABLE orders ( order_id BIGINT PRIMARY KEY, customer_id BIGINT NOT NULL, product_id BIGINT NOT NULL, quantity INT NOT NULL, order_date DATE NOT NULL );
CREATE TABLE customers ( customer_id BIGINT PRIMARY KEY, customer_name VARCHAR(50) NOT NULL, customer_address VARCHAR(100) NOT NULL, customer_phone VARCHAR(20) );
CREATE TABLE products ( product_id BIGINT PRIMARY KEY, product_name VARCHAR(50) NOT NULL, product_description VARCHAR(100) NOT NULL, product_price DECIMAL(10,2) NOT NULL );
CREATE PROCEDURE create_order ( @p_customer_id IN AS NUMBER, p_product_id IN NUMBER, p_quantity IN NUMBER ) IS
BEGIN
INSERT INTO orders ( customer_id, product_id, quantity, order_date ) VALUES ( p_customer_id, p_product_id, p_quantity, GETDATE() );
EXCEPTION
WHEN others THEN
ROLLBACK;
RAISE;
END create_order;
CREATE FUNCTION get_order_total ( @p_order_id IN NUMBER DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
l_total ROW_NUMBER(10,2);
BEGIN
SELECT @l_total = SUM ( quantity * product_price )
FROM orders o JOIN products p
ON o.product_id = p.product_id
WHERE order_id = p_order_id;
RETURN l_total;
END get_order_total;
|
CREATE TABLE orders ( order_id NUMBER(10) PRIMARY KEY, customer_id NUMBER(10) NOT NULL, product_id NUMBER(10) NOT NULL, quantity NUMBER(5) NOT NULL, order_date DATE NOT NULL );
CREATE TABLE customers ( customer_id NUMBER(10) PRIMARY KEY, customer_name VARCHAR2(50) NOT NULL, customer_address VARCHAR2(100) NOT NULL, customer_phone VARCHAR2(20) );
CREATE TABLE products ( product_id NUMBER(10) PRIMARY KEY, product_name VARCHAR2(50) NOT NULL, product_description VARCHAR2(100) NOT NULL, product_price NUMBER(10,2) NOT NULL );
PROCEDURE create_order ( p_customer_id IN NUMBER, p_product_id IN NUMBER, p_quantity IN NUMBER ) IS
BEGIN
INSERT INTO orders ( order_id, customer_id, product_id, quantity, order_date ) VALUES ( order_id_seq.NEXTVAL, p_customer_id, p_product_id, p_quantity, SYSDATE );
EXCEPTION
WHEN others THEN
ROLLBACK;
RAISE;
END create_order;
FUNCTION get_order_total ( p_order_id IN NUMBER ) RETURN NUMBER IS
l_total NUMBER(10,2);
BEGIN
SELECT SUM ( quantity * product_price ) INTO l_total
FROM orders o JOIN products p
ON o.product_id = p.product_id
WHERE order_id = p_order_id;
RETURN l_total;
END get_order_total;
|
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 get_employee_salary;
BEGIN
DECLARE @bonus FLOAT := 1000;
DECLARE @commission FLOAT;
SELECT @commission = @commission FROM employees WHERE employee_id = 10;
IF @commission IS NULL BEGIN
SET @bonus = 0;
END
UPDATE employees SET salary = salary + @bonus WHERE employee_id = 10;
COMMIT;
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 get_employee_salary;
BEGIN
DECLARE bonus NUMBER := 1000;
DECLARE commission NUMBER;
SELECT commission INTO commission FROM employees WHERE employee_id = 10;
IF commission IS NULL THEN
bonus := 0;
END IF;
UPDATE employees SET salary = salary + bonus WHERE employee_id = 10;
COMMIT;
END;
|
DECLARE
@v_empno FLOAT := 100;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT @v_ename = ename, @v_sal = sal
FROM emp
WHERE empno = @v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
|
DECLARE
v_empno NUMBER := 100;
v_ename VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT ename, sal
INTO v_ename, v_sal
FROM emp
WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
|
PL/SQL
IF OBJECT_ID('GET_EMPLOYEE_SALARY', 'FN') IS NOT NULL
DROP FUNCTION GET_EMPLOYEE_SALARY;
GO
CREATE FUNCTION GET_EMPLOYEE_SALARY(@EMP_ID FLOAT)
RETURNS FLOAT
AS
BEGIN
DECLARE @SALARY FLOAT;
SELECT @SALARY = @SALARY FROM EMPLOYEES WHERE EMPLOYEE_ID = @EMP_ID;
IF @SALARY IS NULL BEGIN
RETURN 0; *** hen the employee doesn't exist
END
RETURN @SALARY;
END;
| PL/SQL
CREATE OR REPLACE FUNCTION GET_EMPLOYEE_SALARY(EMP_ID NUMBER)
RETURN NUMBER
IS
SALARY NUMBER;
BEGIN
SELECT SALARY INTO SALARY FROM EMPLOYEES WHERE EMPLOYEE_ID = EMP_ID;
IF SALARY IS NULL THEN
RETURN 0; -- Handle the case when the employee doesn't exist
END IF;
RETURN SALARY;
END GET_EMPLOYEE_SALARY;
|
DECLARE
@v_empno FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_empno = empno, @v_salary = salary
FROM emp
WHERE deptno = 20;
IF v_salary > 10000 BEGIN
RAISERROR('-20001 Salary too high.', 16, 1);
END
UPDATE emp
SET salary = salary * 1.10
WHERE empno = @v_empno;
COMMIT;
END; | DECLARE
v_empno NUMBER;
v_salary NUMBER;
BEGIN
SELECT empno, salary
INTO v_empno, v_salary
FROM emp
WHERE deptno = 20;
IF v_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high.');
END IF;
UPDATE emp
SET salary = salary * 1.10
WHERE empno = v_empno;
COMMIT;
END; |
IF OBJECT_ID('calc_bonus', 'P') IS NOT NULL
DROP PROCEDURE calc_bonus;
GO
CREATE PROCEDURE calc_bonus(@employee_id IN AS NUMBER, bonus_amount OUT NUMBER) IS
BEGIN
SELECT @bonus_amount = bonus
FROM employees
WHERE employee_id = employee_id;
IF bonus_amount IS NULL BEGIN
set @bonus_amount = 0;
END
set @bonus_amount = bonus_amount * 1.10;
IF @@FETCH_STATUS <> 0 BEGIN
set @bonus_amount = 0;
DBMS_OUTPUT.PUT_LINE('No data found for employee ID: ' + isnull(employee_id, ''));
END
END;
|
CREATE OR REPLACE PROCEDURE calc_bonus(employee_id IN NUMBER, bonus_amount OUT NUMBER) IS
BEGIN
SELECT bonus INTO bonus_amount
FROM employees
WHERE employee_id = employee_id;
IF bonus_amount IS NULL THEN
bonus_amount := 0;
END IF;
bonus_amount := bonus_amount * 1.10;
EXCEPTION
WHEN NO_DATA_FOUND THEN
bonus_amount := 0;
DBMS_OUTPUT.PUT_LINE('No data found for employee ID: ' || employee_id);
END;
|
CREATE PROCEDURE get_customer_order_history(@customer_id IN AS NUMBER) AS
BEGIN
orders customer_id customer_id;
END;
CREATE FUNCTION calculate_total_amount NUMBERDECLARE RETURNS
BEGIN NUMBER
RETURN ;
END; AS
BEGIN
RETURN quantity unit_price order_items order_id order_id;
END;
BEGIN
DECLARE BIT : FALSE;
WHILE FALSE BEGIN
condition TRUE BEGIN
SET TRUE;
END
END;
EXCEPTION
WHEN OTHERS THEN
END;
BEGIN
customers name 0 BEGIN
END
ELSE BEGIN
END
END;
|
CREATE PROCEDURE get_customer_order_history(customer_id IN NUMBER) AS
BEGIN
SELECT * FROM orders WHERE customer_id = customer_id;
END;
CREATE FUNCTION calculate_total_amount(order_id IN NUMBER) RETURNS NUMBER AS
BEGIN
RETURN (SELECT SUM(quantity * unit_price) FROM order_items WHERE order_id = order_id);
END;
BEGIN
DECLARE exit_flag BOOLEAN := FALSE;
WHILE exit_flag = FALSE LOOP
-- Perform some operations
IF condition = TRUE THEN
exit_flag := TRUE;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
-- Handle any unexpected errors
END;
BEGIN
IF (SELECT COUNT(*) FROM customers WHERE name = 'John Doe') > 0 THEN
-- Update customer information
-- ...
ELSE
-- Insert new customer
-- ...
END IF;
END;
|
CREATE PROCEDURE myproc (@id IN AS NUMBER) AS
BEGIN
DECLARE
@local_var FLOAT;
BEGIN
SELECT @local_var = name FROM mytable WHERE id = id;
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(@local_var, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Record not found');
END
END;
END;
|
CREATE PROCEDURE myproc (id IN NUMBER) AS
BEGIN
DECLARE
local_var NUMBER;
BEGIN
SELECT name INTO local_var FROM mytable WHERE id = id;
DBMS_OUTPUT.PUT_LINE('Name: ' || local_var);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Record not found');
END;
END;
|
create table my_table (id float, name varchar(255));
insert into my_table values (1, 'John');
insert into my_table values (2, 'Mary');
commit;
create procedure get_name(@id in as number, name out varchar2) as
begin
select @name = name from my_table where id = id;
end;
declare
@name varchar(255);
begin
dbo.get_name(1, @name);
dbms_output.put_line('Name: ' + isnull(@name, ''));
if @@fetch_status <> 0 begin
dbms_output.put_line('No data found for the given id.');
end
end;
|
create table my_table (id number, name varchar2(255));
insert into my_table values (1, 'John');
insert into my_table values (2, 'Mary');
commit;
create procedure get_name(id in number, name out varchar2) as
begin
select name into name from my_table where id = id;
end;
declare
name varchar2(255);
begin
get_name(1, name);
dbms_output.put_line('Name: ' || name);
exception
when no_data_found then
dbms_output.put_line('No data found for the given id.');
end;
|
DECLARE @x INT = 10;
SET @x = @x / 0;
BEGIN CATCH
PRINT 'Division by zero is not allowed.';
END CATCH; | DECLARE @x INT = 10;
SET @x = @x / 0;
BEGIN CATCH
PRINT 'Division by zero is not allowed.';
END CATCH; |
DECLARE
@v_id FLOAT;
v_name dbo.VARCHAR2(25);
v_salary NUMBER;
BEGIN
SELECT @v_id = id, @v_name = name, @v_salary = salary FROM employees WHERE id = 1;
IF v_salary > 5000 BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
UPDATE employees SET salary = v_salary * 1.1 WHERE id = @v_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20002 Employee not found', 16, 1);
END
END;
|
DECLARE
v_id NUMBER;
v_name VARCHAR2(25);
v_salary NUMBER;
BEGIN
SELECT id, name, salary INTO v_id, v_name, v_salary FROM employees WHERE id = 1;
IF v_salary > 5000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
END IF;
UPDATE employees SET salary = v_salary * 1.1 WHERE id = v_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20002, 'Employee not found');
END;
|
DECLARE
@v_emp_id INT;
v_salary ROW_NUMBER(10,2);
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'KING';
IF v_salary > 10000 BEGIN
UPDATE employees
SET salary = v_salary + 1000
WHERE employee_id = @v_emp_id;
END
ELSE BEGIN
RAISERROR('-20101 Salary is too low to increase', 16, 1);
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER(6);
v_salary NUMBER(10,2);
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'KING';
IF v_salary > 10000 THEN
UPDATE employees
SET salary = v_salary + 1000
WHERE employee_id = v_emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20101, 'Salary is too low to increase');
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_emp_id FLOAT := 100;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_emp_name = ename FROM emp WHERE empno = @v_emp_id;
IF v_emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for employee ID: ' + isnull(@v_emp_id, ''));
END
END;
/
|
DECLARE
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(20);
BEGIN
SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id;
IF v_emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for employee ID: ' || v_emp_id);
END;
/
|
IF OBJECT_ID('emp_details', 'P') IS NOT NULL
DROP PROCEDURE emp_details;
GO
CREATE PROCEDURE emp_details (@empno IN AS NUMBER) IS
ename dbo.VARCHAR2(20);
sal NUMBER;
BEGIN
SELECT @ename = ename, @sal = sal FROM emp WHERE empno = empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(ename, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(sal, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
CREATE OR REPLACE PROCEDURE emp_details (empno IN NUMBER) IS
ename VARCHAR2(20);
sal NUMBER;
BEGIN
SELECT ename, sal INTO ename, sal FROM emp WHERE empno = empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || ename);
DBMS_OUTPUT.PUT_LINE('Salary: ' || sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
@v_emp_id FLOAT := 10;
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_emp_id NUMBER := 10;
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_emp_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_employee(
@in_first_name in AS varchar2,
in_last_name in varchar2,
in_salary in number
) IS
err_code number;
exception
when others then
set @err_code = @@error;
dbms_output.put_line('Error code: ' + isnull(convert(varchar, err_code), ''));
dbms_output.put_line('Error message: ' + isnull(sqlerrm, ''));
end;
BEGIN
INSERT INTO employees (first_name, last_name, salary)
VALUES (in_first_name, in_last_name, in_salary);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dbms_output.put_line('Duplicate value on unique index');
END;
|
CREATE OR REPLACE PROCEDURE insert_employee(
in_first_name in varchar2,
in_last_name in varchar2,
in_salary in number
) IS
err_code number;
exception
when others then
err_code := sqlcode;
dbms_output.put_line('Error code: ' || to_char(err_code));
dbms_output.put_line('Error message: ' || sqlerrm);
end;
BEGIN
INSERT INTO employees (first_name, last_name, salary)
VALUES (in_first_name, in_last_name, in_salary);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dbms_output.put_line('Duplicate value on unique index');
END;
|
IF OBJECT_ID('emp_salary_avg', 'FN') IS NOT NULL
DROP FUNCTION emp_salary_avg;
GO
CREATE FUNCTION emp_salary_avg(@dept_no IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
avg_sal NUMBER;
BEGIN
SELECT @avg_sal = AVG(salary) FROM emp WHERE dept_no = dept_no;
RETURN avg_sal;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
/
|
CREATE OR REPLACE FUNCTION emp_salary_avg(dept_no IN NUMBER) RETURN NUMBER IS
avg_sal NUMBER;
BEGIN
SELECT AVG(salary) INTO avg_sal FROM emp WHERE dept_no = dept_no;
RETURN avg_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
/
|
IF OBJECT_ID('GetCustomerName', 'FN') IS NOT NULL
DROP FUNCTION GetCustomerName;
GO
CREATE FUNCTION GetCustomerName(@id FLOAT)
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @name VARCHAR(50);
SELECT @name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM customers
WHERE @id = @id;
RETURN @name;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
BEGIN
DECLARE @customer_name VARCHAR(50);
BEGIN
SET @customer_name = dbo.GetCustomerName(12345);
DBMS_OUTPUT.PUT_LINE('Customer name: ' + isnull(@customer_name, ''));
END;
END;
|
CREATE OR REPLACE FUNCTION GetCustomerName(id NUMBER)
RETURN VARCHAR2
IS
name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name INTO name
FROM customers
WHERE id = id;
RETURN name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
BEGIN
DECLARE customer_name VARCHAR2(50);
BEGIN
customer_name := GetCustomerName(12345);
DBMS_OUTPUT.PUT_LINE('Customer name: ' || customer_name);
END;
END;
|
IF OBJECT_ID('sp_UpdateEmployee', 'P') IS NOT NULL
DROP PROCEDURE sp_UpdateEmployee;
GO
CREATE PROCEDURE sp_UpdateEmployee(
@i_EmployeeID IN AS NUMBER,
i_FirstName IN VARCHAR2,
i_LastName IN VARCHAR2,
o_Success OUT BOOLEAN
) AS
BEGIN
UPDATE employees
SET first_name = i_FirstName,
last_name = i_LastName
WHERE employee_id = i_EmployeeID;
SET @o_Success = @@ROWCOUNT > 0;
END sp_UpdateEmployee;
|
CREATE OR REPLACE PROCEDURE sp_UpdateEmployee(
i_EmployeeID IN NUMBER,
i_FirstName IN VARCHAR2,
i_LastName IN VARCHAR2,
o_Success OUT BOOLEAN
) AS
BEGIN
UPDATE employees
SET first_name = i_FirstName,
last_name = i_LastName
WHERE employee_id = i_EmployeeID;
o_Success := SQL%ROWCOUNT > 0;
END sp_UpdateEmployee;
|
IF OBJECT_ID('my_procedure', 'P') IS NOT NULL
DROP PROCEDURE my_procedure;
GO
CREATE PROCEDURE my_procedure (
@input_parameter IN AS VARCHAR2
)
IS
v_cursor SYS_REFCURSOR;
v_row_count INTEGER;
BEGIN
SELECT * FROM my_table
WHERE column_name = input_parameter;
FETCH v_cursor INTO @v_row_count;
IF v_row_count = 0 BEGIN
RAISERROR('-20001 No rows found for input parameter: ' + isnull(input_parameter, ''), 16, 1);
END
CLOSE v_cursor;
END;
|
CREATE OR REPLACE PROCEDURE my_procedure (
input_parameter IN VARCHAR2
)
IS
v_cursor SYS_REFCURSOR;
v_row_count INTEGER;
BEGIN
OPEN v_cursor FOR
SELECT * FROM my_table
WHERE column_name = input_parameter;
FETCH v_cursor INTO v_row_count;
IF v_row_count = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'No rows found for input parameter: ' || input_parameter);
END IF;
CLOSE v_cursor;
END;
|
IF OBJECT_ID('update_customer', 'P') IS NOT NULL
DROP PROCEDURE update_customer;
GO
CREATE PROCEDURE update_customer (
@in_customer_id IN AS NUMBER,
in_new_name IN VARCHAR2,
out_status OUT VARCHAR2
) AS
v_old_name dbo.VARCHAR2(100);
BEGIN
SELECT @v_old_name = name FROM customers WHERE customer_id = in_customer_id;
UPDATE customers SET name = in_new_name WHERE customer_id = in_customer_id;
set @out_status = 'Customer name updated from ' + isnull(v_old_name, '') + ' to ' + isnull(in_new_name, '');
IF @@FETCH_STATUS <> 0 BEGIN
set @out_status = 'Customer not found';
END
WHEN TOO_MANY_ROWS THEN
set @out_status = 'Multiple customers found with same ID';
WHEN OTHERS THEN
set @out_status = 'Unexpected error occurred';
END;
DECLARE
@v_customer_id FLOAT := 1001;
v_new_name dbo.VARCHAR2(100) := 'John Doe';
v_status dbo.VARCHAR2(200);
BEGIN
dbo.update_customer(@v_customer_id, v_new_name, v_status);
DBMS_OUTPUT.PUT_LINE(v_status);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE update_customer (
in_customer_id IN NUMBER,
in_new_name IN VARCHAR2,
out_status OUT VARCHAR2
) AS
v_old_name VARCHAR2(100);
BEGIN
SELECT name INTO v_old_name FROM customers WHERE customer_id = in_customer_id;
UPDATE customers SET name = in_new_name WHERE customer_id = in_customer_id;
out_status := 'Customer name updated from ' || v_old_name || ' to ' || in_new_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
out_status := 'Customer not found';
WHEN TOO_MANY_ROWS THEN
out_status := 'Multiple customers found with same ID';
WHEN OTHERS THEN
out_status := 'Unexpected error occurred';
END;
DECLARE
v_customer_id NUMBER := 1001;
v_new_name VARCHAR2(100) := 'John Doe';
v_status VARCHAR2(200);
BEGIN
update_customer(v_customer_id, v_new_name, v_status);
DBMS_OUTPUT.PUT_LINE(v_status);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
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 employeeDECLARE %ROWTYPE
BEGIN IS
RETURN NULL;
END;
BEGIN
RETURN (SELECT * FROM employee WHERE emp_id = emp_id);
IF @@FETCH_STATUS <> 0 BEGIN
RETURN employee%ROWTYPE();
END
END get_employee_info;
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(@emp_id FLOAT, @new_salary FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employee SET salary = @new_salary WHERE @emp_id = @emp_id;
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20000 Error updating employee', 16, 1);
END;
BEGIN
DECLARE
BEGIN
SET @emp_info = dbo.get_employee_info(100);
IF @emp_info_salary < 50000 BEGIN
dbo.update_employee(100, 50000);
END
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END; | CREATE OR REPLACE FUNCTION get_employee_info(emp_id NUMBER)
RETURN employee%ROWTYPE IS
BEGIN
RETURN (SELECT * FROM employee WHERE emp_id = emp_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN employee%ROWTYPE();
END get_employee_info;
CREATE OR REPLACE PROCEDURE update_employee(emp_id NUMBER, new_salary NUMBER)
IS
BEGIN
UPDATE employee SET salary = new_salary WHERE emp_id = emp_id;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20000, 'Error updating employee');
END update_employee;
BEGIN
DECLARE emp_info employee%ROWTYPE;
BEGIN
emp_info := get_employee_info(100);
IF emp_info.salary < 50000 THEN
update_employee(100, 50000);
END IF;
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END; |
IF OBJECT_ID('EMP_BONUS', 'P') IS NOT NULL
DROP PROCEDURE EMP_BONUS;
GO
CREATE PROCEDURE EMP_BONUS(
@bonus_rate IN AS NUMBER,
emp_id IN NUMBER)
IS
BEGIN
UPDATE employees
SET salary = salary * (1 + bonus_rate)
WHERE employee_id = emp_id;
COMMIT;
END;
/
BEGIN
DECLARE
@bonus_pct FLOAT;
BEGIN
SELECT @bonus_pct = bonus_rate
FROM bonus_rates
WHERE level = 'MANAGER';
EMP_BONUS(bonus_rate := @bonus_pct, SET @emp_id = 100);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 No bonus rate found for MANAGER level', 16, 1);
END
END;
END;
/
|
CREATE OR REPLACE PROCEDURE EMP_BONUS(
bonus_rate IN NUMBER,
emp_id IN NUMBER)
IS
BEGIN
UPDATE employees
SET salary = salary * (1 + bonus_rate)
WHERE employee_id = emp_id;
COMMIT;
END;
/
BEGIN
DECLARE
bonus_pct NUMBER;
BEGIN
SELECT bonus_rate
INTO bonus_pct
FROM bonus_rates
WHERE level = 'MANAGER';
EMP_BONUS(bonus_rate := bonus_pct, emp_id := 100);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'No bonus rate found for MANAGER level');
END;
END;
/
|
IF OBJECT_ID('proc_example', 'P') IS NOT NULL
DROP PROCEDURE proc_example;
GO
CREATE PROCEDURE proc_example(
@param1 IN AS NUMBER,
param2 OUT VARCHAR2
) AS
BEGIN
SELECT @param2 = name FROM table1 WHERE id = param1;
IF param2 IS NULL BEGIN
RAISERROR('-20100 No record found for ID: ' + isnull(param1, ''), 16, 1);
END
END;
IF OBJECT_ID('func_example', 'FN') IS NOT NULL
DROP FUNCTION func_example;
GO
CREATE FUNCTION func_example(
@param1 IN NUMBER
DECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END; AS
BEGIN
RETURN (SELECT name FROM table1 WHERE id = param1);
END;
BEGIN
DECLARE
@v_param2 VARCHAR(100);
BEGIN
dbo.proc_example(12345, @v_param2);
DBMS_OUTPUT.PUT_LINE(@v_param2);
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No record found.');
END
END;
END;
SELECT * FROM table1 WHERE id IN (1, 2, 3)
UNION
SELECT * FROM table2 WHERE name LIKE '%XYZ%';
|
CREATE OR REPLACE PROCEDURE proc_example(
param1 IN NUMBER,
param2 OUT VARCHAR2
) AS
BEGIN
SELECT name INTO param2 FROM table1 WHERE id = param1;
IF param2 IS NULL THEN
RAISE_APPLICATION_ERROR(-20100, 'No record found for ID: ' || param1);
END IF;
END;
CREATE OR REPLACE FUNCTION func_example(
param1 IN NUMBER
) RETURN VARCHAR2 AS
BEGIN
RETURN (SELECT name FROM table1 WHERE id = param1);
END;
BEGIN
DECLARE
v_param2 VARCHAR2(100);
BEGIN
proc_example(12345, v_param2);
DBMS_OUTPUT.PUT_LINE(v_param2);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No record found.');
END;
END;
SELECT * FROM table1 WHERE id IN (1, 2, 3)
UNION
SELECT * FROM table2 WHERE name LIKE '%XYZ%';
|
BEGIN
DECLARE @v_emp_id FLOAT;
DECLARE @v_emp_name VARCHAR(100);
DECLARE @v_emp_salary FLOAT;
DECLARE @v_bonus FLOAT;
BEGIN
SET @v_emp_id = 100;
SET @v_emp_name = 'John Doe';
SET @v_emp_salary = 10000;
SET @v_bonus = 0;
IF @v_emp_salary > 5000 BEGIN
SET @v_bonus = @v_emp_salary * 0.1;
END
SELECT @v_emp_id, @v_emp_name, @v_emp_salary, @v_bonus;
END; | BEGIN
DECLARE v_emp_id NUMBER;
DECLARE v_emp_name VARCHAR2(100);
DECLARE v_emp_salary NUMBER;
DECLARE v_bonus NUMBER;
BEGIN
v_emp_id := 100;
v_emp_name := 'John Doe';
v_emp_salary := 10000;
v_bonus := 0;
IF v_emp_salary > 5000 THEN
v_bonus := v_emp_salary * 0.1;
END IF;
SELECT v_emp_id, v_emp_name, v_emp_salary, v_bonus FROM dual;
END; |
CREATE PROCEDURE GetCustomerOrders(@custID IN AS NUMBER) IS
CURSOR cust_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = custID;
BEGIN
DECLARE rec CURSOR FOR cust_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' + isnull(@rec_order_id, ''));
DBMS_OUTPUT.PUT_LINE('Order Date: ' + isnull(@rec_order_date, ''));
DBMS_OUTPUT.PUT_LINE('Total Amount: ' + isnull(@rec_total_amount, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + ISNULL(custID, ''));
END
END;
|
CREATE PROCEDURE GetCustomerOrders(custID IN NUMBER) IS
CURSOR cust_orders IS
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = custID;
BEGIN
FOR rec IN cust_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order ID: ' || rec.order_id);
DBMS_OUTPUT.PUT_LINE('Order Date: ' || rec.order_date);
DBMS_OUTPUT.PUT_LINE('Total Amount: ' || rec.total_amount);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || custID);
END;
|
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'SCOTT';
IF @v_empno IS NOT NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empno, '') + ' found.');
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'SCOTT';
IF v_empno IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' found.');
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
ELSE
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IF;
END;
|
if object_id('usp_update_employee', 'P') is not null
drop procedure usp_update_employee;
go
create procedure usp_update_employee(@emp_id in as number, emp_name in varchar2, emp_salary in number) is
begin
update employees set employee_name = emp_name, salary = emp_salary where employee_id = emp_id;
commit;
end usp_update_employee;
/ |
create or replace procedure usp_update_employee(emp_id in number, emp_name in varchar2, emp_salary in number) is
begin
update employees set employee_name = emp_name, salary = emp_salary where employee_id = emp_id;
commit;
end usp_update_employee;
/
|
DECLARE
@name VARCHAR(20);
salary ROW_NUMBER(10,2);
bonus ROW_NUMBER(10,2);
BEGIN
SELECT @name = ename, @salary = sal, @bonus = comm
FROM emp
WHERE empno = 7369;
IF bonus IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('No bonus for ' + isnull(@name, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@name, '') + ' Earns ' + isnull(bonus, '') + ' Bonus');
END
END;
|
DECLARE
name VARCHAR2(20);
salary NUMBER(10,2);
bonus NUMBER(10,2);
BEGIN
SELECT ename, sal, comm INTO name, salary, bonus
FROM emp
WHERE empno = 7369;
IF bonus IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No bonus for ' || name);
ELSE
DBMS_OUTPUT.PUT_LINE(name || ' Earns ' || bonus || ' Bonus');
END IF;
END;
|
CREATE PROCEDURE my_proc (@p_in IN AS VARCHAR2) AS
BEGIN
*** iables
v_local1 dbo.VARCHAR2(20);
*** a local variable
set @v_local1 = 'Hello, ' + isnull(p_in, '');
*** a table
INSERT INTO my_table (col1, col2) VALUES (v_local1, GETDATE());
-- Co... ***
COMMIT;
END;
/
|
CREATE PROCEDURE my_proc (p_in IN VARCHAR2) AS
BEGIN
-- Declare local variables
v_local1 VARCHAR2(20);
-- Assign a value to a local variable
v_local1 := 'Hello, ' || p_in;
-- Insert a row into a table
INSERT INTO my_table (col1, col2) VALUES (v_local1, SYSDATE);
-- Commit the changes
COMMIT;
END;
/
|
IF OBJECT_ID('add_employee', 'P') IS NOT NULL
DROP PROCEDURE add_employee;
GO
CREATE PROCEDURE add_employee(
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
salary
)
VALUES
(first_name, last_name, salary);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Duplicate value in unique index', 16, 1);
WHEN OTHERS THEN
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE add_employee(
first_name IN VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
first_name,
last_name,
salary
)
VALUES
(first_name, last_name, salary);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Duplicate value in unique index');
WHEN OTHERS THEN
RAISE;
END;
|
CREATE PROCEDURE sp_example(@p_in IN AS NUMBER) AS
BEGIN
UPDATE table SET col = col + 1 WHERE id = p_in;
COMMIT;
END;
CREATE FUNCTION fn_example(@p_in IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
RETURN p_in * 2;
END;
BEGIN
DECLARE
@v_var FLOAT;
BEGIN
SELECT @v_var = col FROM table WHERE id = 1;
dbms_output.put_line('Value: ' + isnull(@v_var, ''));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
CREATE PROCEDURE sp_example(p_in IN NUMBER) AS
BEGIN
UPDATE table SET col = col + 1 WHERE id = p_in;
COMMIT;
END;
CREATE FUNCTION fn_example(p_in IN NUMBER) RETURN NUMBER AS
BEGIN
RETURN p_in * 2;
END;
BEGIN
DECLARE
v_var NUMBER;
BEGIN
SELECT col INTO v_var FROM table WHERE id = 1;
dbms_output.put_line('Value: ' || v_var);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error occurred: ' || SQLERRM);
END;
END;
|
CREATE PROCEDURE get_customer_orders(@cust_id IN AS NUMBER) IS
orders_ref CURSOR FOR SELECT order_id, order_date FROM orders WHERE customer_id = cust_id;
BEGIN
OPEN orders_ref;
WHILE 1=1 BEGIN
FETCH orders_ref INTO @order_id, @order_date;
IF @@FETCH_STATUS <> 0 BREAK;
-- Pr... ***
END;
CLOSE orders_ref;
END;
/
DECLARE
@order_id FLOAT;
order_date DATE;
BEGIN
dbo.get_customer_orders(12345);
*** the orders
END;
/
CREATE FUNCTION get_total_sales() RETURNS FLOAT AS
BEGIN
DECLARE @total_sales FLOAT;
SELECT @total_sales = SUM(sales) FROM sales;
RETURN @total_sales;
END;
GO
SELECT dbo.get_total_sales();
|
CREATE PROCEDURE get_customer_orders(cust_id IN NUMBER) IS
orders_ref CURSOR FOR SELECT order_id, order_date FROM orders WHERE customer_id = cust_id;
BEGIN
OPEN orders_ref;
LOOP
FETCH orders_ref INTO order_id, order_date;
EXIT WHEN orders_ref%NOTFOUND;
-- Process the order
END LOOP;
CLOSE orders_ref;
END;
/
DECLARE
order_id NUMBER;
order_date DATE;
BEGIN
get_customer_orders(12345);
-- Do something with the orders
END;
/
CREATE FUNCTION get_total_sales() RETURN NUMBER IS
total_sales NUMBER;
BEGIN
SELECT SUM(sales) INTO total_sales FROM sales;
RETURN total_sales;
END;
/
SELECT get_total_sales();
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@emp_id IN AS NUMBER,
new_salary IN NUMBER
) AS
BEGIN
*** of the employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
-- Co... ***
COMMIT;
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
emp_id IN NUMBER,
new_salary IN NUMBER
) AS
BEGIN
-- Update the salary of the employee
UPDATE employees SET salary = new_salary
WHERE employee_id = emp_id;
-- Commit the changes
COMMIT;
END;
|
BEGIN
DECLARE @num_rows INTEGER;
DECLARE @cursor_name SYS_REFCURSOR;
BEGIN
SELECT * FROM table_name WHERE condition;
WHILE 1=1 BEGIN
FETCH cursor_name INTO @variable_list;
IF @@FETCH_STATUS <> 0 BREAK;
*** here
END;
CLOSE cursor_name;
IF @@FETCH_STATUS <> 0 BEGIN
END
*** und error
WHEN TOO_MANY_ROWS THEN
*** ows error
END;
END; | BEGIN
DECLARE num_rows INTEGER;
DECLARE cursor_name SYS_REFCURSOR;
BEGIN
OPEN cursor_name FOR
SELECT * FROM table_name WHERE condition;
LOOP
FETCH cursor_name INTO variable_list;
EXIT WHEN cursor_name%NOTFOUND;
-- process each row here
END LOOP;
CLOSE cursor_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- handle no data found error
WHEN TOO_MANY_ROWS THEN
-- handle too many rows error
END;
END; |
DECLARE
-- De... ***
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
v_emp_salary NUMBER;
BEGIN
*** ils
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_emp_salary = emp_salary
FROM employees
WHERE emp_id = 10;
*** alary
UPDATE employees
SET emp_salary = v_emp_salary * 1.10
WHERE emp_id = @v_emp_id;
-- Co... ***
COMMIT;
*** sage
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.');
EXCEPTION
-- Ha... ***
WHEN OTHERS THEN
-- Pr... ***
DBMS_OUTPUT.PUT_LINE('An error occurred while updating the employee salary.');
END; | DECLARE
-- Define variables
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_emp_salary NUMBER;
BEGIN
-- Get employee details
SELECT emp_id, emp_name, emp_salary
INTO v_emp_id, v_emp_name, v_emp_salary
FROM employees
WHERE emp_id = 10;
-- Update employee salary
UPDATE employees
SET emp_salary = v_emp_salary * 1.10
WHERE emp_id = v_emp_id;
-- Commit the changes
COMMIT;
-- Print success message
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.');
EXCEPTION
-- Handle exceptions
WHEN OTHERS THEN
-- Print error message
DBMS_OUTPUT.PUT_LINE('An error occurred while updating the employee salary.');
END; |
DECLARE
@v_empno FLOAT;
BEGIN
SELECT @v_empno = COUNT(*) FROM employees;
IF @v_empno > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employees found!');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found!');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found in the table!');
END
END;
|
DECLARE
v_empno NUMBER;
BEGIN
SELECT COUNT(*) INTO v_empno FROM employees;
IF v_empno > 0 THEN
DBMS_OUTPUT.PUT_LINE('Employees found!');
ELSE
DBMS_OUTPUT.PUT_LINE('No employees found!');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found in the table!');
END;
|
DECLARE
@emp_name VARCHAR(20);
BEGIN
SELECT @emp_name = ename FROM emp WHERE empno = 7788;
dbms_output.put_line('Employee Name: ' + isnull(@emp_name, ''));
IF @emp_name IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
/
|
DECLARE
emp_name VARCHAR2(20);
BEGIN
SELECT ename INTO emp_name FROM emp WHERE empno = 7788;
dbms_output.put_line('Employee Name: ' || emp_name);
IF emp_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
/
|
DECLARE
@v_salary FLOAT;
v_bonus NUMBER;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE employee_id = 1;
IF @v_salary > 5000 BEGIN
SET @v_bonus = @v_salary * 0.10;
END
ELSE BEGIN
SET @v_bonus = @v_salary * 0.05;
END
UPDATE employees
SET bonus = v_bonus
WHERE employee_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_salary NUMBER;
v_bonus NUMBER;
BEGIN
SELECT salary INTO v_salary
FROM employees
WHERE employee_id = 1;
IF v_salary > 5000 THEN
v_bonus := v_salary * 0.10;
ELSE
v_bonus := v_salary * 0.05;
END IF;
UPDATE employees
SET bonus = v_bonus
WHERE employee_id = 1;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE sp_my_proc (
@param1 IN AS NUMBER,
param2 IN OUT VARCHAR2
) AS
BEGIN
-- So... ***
set @param2 = 'Updated value';
EXCEPTION
WHEN OTHERS THEN
-- Er... ***
END;
-- Us... ***
DECLARE
@v_param2 VARCHAR(255);
BEGIN
dbo.sp_my_proc(1, @v_param2);
DBMS_OUTPUT.PUT_LINE(@v_param2);
END;
|
CREATE PROCEDURE sp_my_proc (
param1 IN NUMBER,
param2 IN OUT VARCHAR2
) AS
BEGIN
-- Some logic
param2 := 'Updated value';
EXCEPTION
WHEN OTHERS THEN
-- Error handling
END;
-- Usage
DECLARE
v_param2 VARCHAR2(255);
BEGIN
sp_my_proc(1, v_param2);
DBMS_OUTPUT.PUT_LINE(v_param2);
END;
|
CREATE PROCEDURE emp_salary_update(
@emp_id IN AS NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = salary + ? WHERE employee_id = ?;
COMMIT;
END;
DECLARE
@v_emp_id FLOAT;
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = employee_id, @v_salary = salary
FROM employees
WHERE last_name = 'Doe';
IF v_salary < 50000 BEGIN
dbo.emp_salary_update(@v_emp_id, 5000);
END
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20001 Error updating employee salary', 16, 1);
END;
|
CREATE PROCEDURE emp_salary_update(
emp_id IN NUMBER,
salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = salary + ? WHERE employee_id = ?;
COMMIT;
END;
DECLARE
v_emp_id NUMBER;
v_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO v_emp_id, v_salary
FROM employees
WHERE last_name = 'Doe';
IF v_salary < 50000 THEN
emp_salary_update(v_emp_id, 5000);
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Error updating employee salary');
END;
|
CREATE TABLE employees (
employee_id FLOAT PRIMARY KEY,
first_name VARCHAR(25),
last_name VARCHAR(25),
salary FLOAT
);
CREATE PROCEDURE get_employee_info(
@emp_id IN AS employees.employee_id%TYPE,
emp_fn OUT employees.first_name%TYPE,
emp_ln OUT employees.last_name%TYPE,
emp_sal OUT employees.salary%TYPE
)
IS
BEGIN
SELECT @emp_fn = first_name, @emp_ln = last_name, @emp_sal = salary
FROM employees
WHERE employee_id = emp_id;
END;
DECLARE
@emp_id FLOAT := 12345;
emp_fname dbo.VARCHAR2(25);
emp_lname dbo.VARCHAR2(25);
emp_sal NUMBER;
BEGIN
dbo.get_employee_info(@emp_id, emp_fname, emp_lname, emp_sal);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(emp_fname, '') + ' ' + isnull(emp_lname, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(emp_sal, ''));
IF emp_sal > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('High Earner');
END
END;
|
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(25),
last_name VARCHAR2(25),
salary NUMBER
);
CREATE PROCEDURE get_employee_info(
emp_id IN employees.employee_id%TYPE,
emp_fn OUT employees.first_name%TYPE,
emp_ln OUT employees.last_name%TYPE,
emp_sal OUT employees.salary%TYPE
)
IS
BEGIN
SELECT first_name, last_name, salary
INTO emp_fn, emp_ln, emp_sal
FROM employees
WHERE employee_id = emp_id;
END;
DECLARE
emp_id NUMBER := 12345;
emp_fname VARCHAR2(25);
emp_lname VARCHAR2(25);
emp_sal NUMBER;
BEGIN
get_employee_info(emp_id, emp_fname, emp_lname, emp_sal);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('Name: ' || emp_fname || ' ' || emp_lname);
DBMS_OUTPUT.PUT_LINE('Salary: ' || emp_sal);
IF emp_sal > 50000 THEN
DBMS_OUTPUT.PUT_LINE('High Earner');
END IF;
END;
|
CREATE PROCEDURE ADD_EMPLOYEE(@p_emp_id IN AS NUMBER, p_name IN VARCHAR2, p_dept_id IN NUMBER) AS
BEGIN
INSERT INTO EMPLOYEES (EMP_ID, NAME, DEPT_ID)
VALUES (p_emp_id, p_name, p_dept_id);
COMMIT;
END;
/
DECLARE
@emp_id FLOAT := 100;
emp_name dbo.VARCHAR2(20) := 'John Doe';
dept_id SET @NUMBER = 10;
BEGIN
dbo.ADD_EMPLOYEE(@emp_id, emp_name, dept_id);
SELECT * FROM EMPLOYEES WHERE @emp_id = @emp_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
/
|
CREATE PROCEDURE ADD_EMPLOYEE(p_emp_id IN NUMBER, p_name IN VARCHAR2, p_dept_id IN NUMBER) AS
BEGIN
INSERT INTO EMPLOYEES (EMP_ID, NAME, DEPT_ID)
VALUES (p_emp_id, p_name, p_dept_id);
COMMIT;
END;
/
DECLARE
emp_id NUMBER := 100;
emp_name VARCHAR2(20) := 'John Doe';
dept_id NUMBER := 10;
BEGIN
ADD_EMPLOYEE(emp_id, emp_name, dept_id);
SELECT * FROM EMPLOYEES WHERE EMP_ID = emp_id;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
/
|
CREATE PROCEDURE InsertEmployee(
@p_first_name IN AS VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2)
IS
v_employee_id NUMBER;
BEGIN
SELECT @v_employee_id = ISNULL(MAX(employee_id), 0) + 1 FROM employees;
INSERT INTO employees (employee_id, first_name, last_name, email) VALUES (v_employee_id, p_first_name, p_last_name, p_email);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20001 Duplicate email address', 16, 1);
END InsertEmployee;
|
PROCEDURE InsertEmployee(
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2)
IS
v_employee_id NUMBER;
BEGIN
SELECT NVL(MAX(employee_id), 0) + 1 INTO v_employee_id FROM employees;
INSERT INTO employees (employee_id, first_name, last_name, email) VALUES (v_employee_id, p_first_name, p_last_name, p_email);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20001, 'Duplicate email address');
END InsertEmployee;
|
IF OBJECT_ID('fetch_employee_details', 'P') IS NOT NULL
DROP PROCEDURE fetch_employee_details;
GO
CREATE PROCEDURE fetch_employee_details(@p_employee_id IN AS NUMBER)
IS
v_employee_name dbo.VARCHAR2(50);
BEGIN
SELECT @v_employee_name = employee_name FROM employees WHERE employee_id = p_employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_employee_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
END;
/
|
CREATE OR REPLACE PROCEDURE fetch_employee_details(p_employee_id IN NUMBER)
IS
v_employee_name VARCHAR2(50);
BEGIN
SELECT employee_name INTO v_employee_name FROM employees WHERE employee_id = p_employee_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.');
END;
/
|
*** rocedure to insert a new customer
IF OBJECT_ID('insert_customer', 'P') IS NOT NULL
DROP PROCEDURE insert_customer;
GO
CREATE PROCEDURE insert_customer (
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2)
IS
BEGIN
*** stomer into the database
INSERT INTO customers (first_name, last_name, email)
VALUES (first_name, last_name, email);
-- Co... ***
COMMIT;
END;
*** rocedure to insert a new customer
BEGIN
dbo.insert_customer('John', 'Doe', 'john.doe@example.com');
END;
*** to calculate the total sales for a given customer
IF OBJECT_ID('calculate_total_sales', 'FN') IS NOT NULL
DROP FUNCTION calculate_total_sales;
GO
CREATE FUNCTION calculate_total_sales (
@customer_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END;
IS
total_sales NUMBER;
BEGIN
*** al sales for the given customer
SELECT @total_sales = SUM(amount)
FROM sales
WHERE customer_id = customer_id;
*** sales
RETURN total_sales;
END;
*** to calculate the total sales for a given customer
DECLARE
@total_sales FLOAT;
BEGIN
SET @total_sales = dbo.calculate_total_sales(1);
*** the total sales
DBMS_OUTPUT.PUT_LINE('Total sales: ' + isnull(@total_sales, ''));
END;
*** ement to select all customers
SELECT * FROM customers;
*** flow construct to loop through all customers
DECLARE
@CURSOR customers_cursor IS
SELECT customer_id, first_name, last_name
FROM customers;
customer_record customers_cursor%ROWTYPE;
BEGIN
*** customers
OPEN customers_cursor;
WHILE 1=1 BEGIN
FETCH customers_cursor INTO @customer_record;
IF @@FETCH_STATUS <> 0 BREAK;
*** the customer record
DBMS_OUTPUT.PUT_LINE('Customer ID: ' + isnull(customer_record.customer_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(customer_record.first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(customer_record.last_name, ''));
END;
-- Cl... ***
CLOSE customers_cursor;
END;
*** andling scenario
BEGIN
*** a new customer with an invalid email address
INSERT INTO customers (first_name, last_name, email)
VALUES ('John', 'Doe', 'invalid email address');
EXCEPTION
WHEN OTHERS THEN
-- Ha... ***
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
-- Create a stored procedure to insert a new customer
CREATE OR REPLACE PROCEDURE insert_customer (
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2)
IS
BEGIN
-- Insert the new customer into the database
INSERT INTO customers (first_name, last_name, email)
VALUES (first_name, last_name, email);
-- Commit the changes
COMMIT;
END;
-- Call the stored procedure to insert a new customer
BEGIN
insert_customer('John', 'Doe', 'john.doe@example.com');
END;
-- Create a function to calculate the total sales for a given customer
CREATE OR REPLACE FUNCTION calculate_total_sales (
customer_id IN NUMBER)
RETURN NUMBER
IS
total_sales NUMBER;
BEGIN
-- Calculate the total sales for the given customer
SELECT SUM(amount) INTO total_sales
FROM sales
WHERE customer_id = customer_id;
-- Return the total sales
RETURN total_sales;
END;
-- Call the function to calculate the total sales for a given customer
DECLARE
total_sales NUMBER;
BEGIN
total_sales := calculate_total_sales(1);
-- Do something with the total sales
DBMS_OUTPUT.PUT_LINE('Total sales: ' || total_sales);
END;
-- Create a SQL statement to select all customers
SELECT * FROM customers;
-- Create a control flow construct to loop through all customers
DECLARE
CURSOR customers_cursor IS
SELECT customer_id, first_name, last_name
FROM customers;
customer_record customers_cursor%ROWTYPE;
BEGIN
-- Loop through all customers
OPEN customers_cursor;
LOOP
FETCH customers_cursor INTO customer_record;
EXIT WHEN customers_cursor%NOTFOUND;
-- Do something with the customer record
DBMS_OUTPUT.PUT_LINE('Customer ID: ' || customer_record.customer_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || customer_record.first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || customer_record.last_name);
END LOOP;
-- Close the cursor
CLOSE customers_cursor;
END;
-- Create an error handling scenario
BEGIN
-- Attempt to insert a new customer with an invalid email address
INSERT INTO customers (first_name, last_name, email)
VALUES ('John', 'Doe', 'invalid email address');
EXCEPTION
WHEN OTHERS THEN
-- Handle the error
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
salary DECIMAL(10,2) NOT NULL,
department_id SMALLINT REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id SMALLINT PRIMARY KEY,
department_name VARCHAR(30) NOT NULL,
location_id SMALLINT REFERENCES locations(location_id)
);
CREATE TABLE locations (
location_id SMALLINT PRIMARY KEY,
city VARCHAR(30) NOT NULL,
state_province VARCHAR(25) NOT NULL,
country_id VARCHAR(2) NOT NULL REFERENCES countries(country_id)
);
CREATE TABLE countries (
country_id VARCHAR(2) PRIMARY KEY,
country_name VARCHAR(40) NOT NULL
);
INSERT INTO countries (country_id, country_name) VALUES ('US', 'United States');
INSERT INTO countries (country_id, country_name) VALUES ('CA', 'Canada');
INSERT INTO countries (country_id, country_name) VALUES ('MX', 'Mexico');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (10, 'New York', 'New York', 'US');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (20, 'San Francisco', 'California', 'US');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (30, 'Toronto', 'Ontario', 'CA');
INSERT INTO departments (department_id, department_name, location_id) VALUES (10, 'Sales', 10);
INSERT INTO departments (department_id, department_name, location_id) VALUES (20, 'Marketing', 20);
INSERT INTO departments (department_id, department_name, location_id) VALUES (30, 'Engineering', 30);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (100, 'John', 'Doe', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (200, 'Jane', 'Smith', 60000, 20);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (300, 'Michael', 'Jones', 70000, 30);
*** to get employee details by employee ID
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL
DROP PROCEDURE get_employee_details;
GO
CREATE PROCEDURE get_employee_details (
@p_employee_id IN AS employees.employee_id%TYPE,
p_first_name OUT employees.first_name%TYPE,
p_last_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE,
p_department_id OUT employees.department_id%TYPE
) IS
BEGIN
SELECT @p_first_name = first_name, @p_last_name = last_name, @p_salary = salary, @p_department_id = department_id
FROM employees
WHERE employee_id = p_employee_id;
END;
*** late total salary for a department
IF OBJECT_ID('get_total_salary', 'FN') IS NOT NULL
DROP FUNCTION get_total_salary;
GO
CREATE FUNCTION get_total_salary (
@p_department_id IN departments.department_idDECLARE %TYPE
BEGIN
)
RETURN NULL;
END; RETURN employees.salary%TYPE IS
total_salary employees.salary%TYPE;
BEGIN
SELECT @total_salary = SUM(salary)
FROM employees
WHERE department_id = p_department_id;
RETURN total_salary;
END;
*** get employees with salary greater than a certain amount
SELECT *
FROM employees
WHERE salary > 55000;
*** truct to check if a department has any employees
DECLARE
@dept_id SMALLINT;
BEGIN
SELECT @dept_id = department_id
FROM departments
WHERE department_id = 40;
IF @dept_id IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Department does not exist.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Department exists.');
END
END;
*** enario to check for invalid department ID
BEGIN
SELECT department_name
FROM departments
WHERE department_id = 40;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Invalid department ID.');
END
END;
|
CREATE TABLE employees (
employee_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20) NOT NULL,
last_name VARCHAR2(20) NOT NULL,
salary NUMBER(10,2) NOT NULL,
department_id NUMBER(4) REFERENCES departments(department_id)
);
CREATE TABLE departments (
department_id NUMBER(4) PRIMARY KEY,
department_name VARCHAR2(30) NOT NULL,
location_id NUMBER(4) REFERENCES locations(location_id)
);
CREATE TABLE locations (
location_id NUMBER(4) PRIMARY KEY,
city VARCHAR2(30) NOT NULL,
state_province VARCHAR2(25) NOT NULL,
country_id VARCHAR2(2) NOT NULL REFERENCES countries(country_id)
);
CREATE TABLE countries (
country_id VARCHAR2(2) PRIMARY KEY,
country_name VARCHAR2(40) NOT NULL
);
INSERT INTO countries (country_id, country_name) VALUES ('US', 'United States');
INSERT INTO countries (country_id, country_name) VALUES ('CA', 'Canada');
INSERT INTO countries (country_id, country_name) VALUES ('MX', 'Mexico');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (10, 'New York', 'New York', 'US');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (20, 'San Francisco', 'California', 'US');
INSERT INTO locations (location_id, city, state_province, country_id) VALUES (30, 'Toronto', 'Ontario', 'CA');
INSERT INTO departments (department_id, department_name, location_id) VALUES (10, 'Sales', 10);
INSERT INTO departments (department_id, department_name, location_id) VALUES (20, 'Marketing', 20);
INSERT INTO departments (department_id, department_name, location_id) VALUES (30, 'Engineering', 30);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (100, 'John', 'Doe', 50000, 10);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (200, 'Jane', 'Smith', 60000, 20);
INSERT INTO employees (employee_id, first_name, last_name, salary, department_id) VALUES (300, 'Michael', 'Jones', 70000, 30);
-- Stored procedure to get employee details by employee ID
CREATE OR REPLACE PROCEDURE get_employee_details (
p_employee_id IN employees.employee_id%TYPE,
p_first_name OUT employees.first_name%TYPE,
p_last_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE,
p_department_id OUT employees.department_id%TYPE
) IS
BEGIN
SELECT first_name, last_name, salary, department_id
INTO p_first_name, p_last_name, p_salary, p_department_id
FROM employees
WHERE employee_id = p_employee_id;
END;
-- Function to calculate total salary for a department
CREATE OR REPLACE FUNCTION get_total_salary (
p_department_id IN departments.department_id%TYPE
) RETURN employees.salary%TYPE IS
total_salary employees.salary%TYPE;
BEGIN
SELECT SUM(salary)
INTO total_salary
FROM employees
WHERE department_id = p_department_id;
RETURN total_salary;
END;
-- SQL statement to get employees with salary greater than a certain amount
SELECT *
FROM employees
WHERE salary > 55000;
-- Control flow construct to check if a department has any employees
DECLARE
dept_id NUMBER(4);
BEGIN
SELECT department_id
INTO dept_id
FROM departments
WHERE department_id = 40;
IF dept_id IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Department does not exist.');
ELSE
DBMS_OUTPUT.PUT_LINE('Department exists.');
END IF;
END;
-- Error handling scenario to check for invalid department ID
BEGIN
SELECT department_name
FROM departments
WHERE department_id = 40;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Invalid department ID.');
END;
|