sqlserver
stringlengths 59
30k
| plsql
stringlengths 54
4.67k
|
---|---|
SET @SERVEROUTPUT ON
DECLARE
v_empno SET @NUMBER = 8765;
v_salary NUMBER;
BEGIN
SELECT @v_salary = salary
FROM emp
WHERE empno = v_empno;
IF v_salary > 10000
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee earns more than 10000.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee earns less than 10000.');
END
IF @@FETCH_STATUS <> 0
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
/ | SET SERVEROUTPUT ON
DECLARE
v_empno NUMBER := 8765;
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM emp
WHERE empno = v_empno;
IF v_salary > 10000
THEN
DBMS_OUTPUT.PUT_LINE('Employee earns more than 10000.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee earns less than 10000.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
/ |
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@p1 IN AS NUMBER, p2 IN VARCHAR2) AS
BEGIN
*** p1 and p2
UPDATE my_table SET col1 = p1, col2 = p2 WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Procedure executed successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
BEGIN
dbo.my_proc(10, 'Hello World');
END;
|
CREATE OR REPLACE PROCEDURE my_proc (p1 IN NUMBER, p2 IN VARCHAR2) AS
BEGIN
-- Do something with p1 and p2
UPDATE my_table SET col1 = p1, col2 = p2 WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Procedure executed successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
BEGIN
my_proc(10, 'Hello World');
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal FROM emp WHERE empno = 7839;
IF v_sal > 10000 BEGIN
UPDATE emp SET sal = v_sal * 1.10 WHERE empno = @v_empno;
END
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for ' + isnull(v_ename, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
BEGIN
SELECT empno, ename, sal INTO v_empno, v_ename, v_sal FROM emp WHERE empno = 7839;
IF v_sal > 10000 THEN
UPDATE emp SET sal = v_sal * 1.10 WHERE empno = v_empno;
END IF;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully for ' || v_ename);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
|
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@emp_id IN AS NUMBER,
new_salary IN NUMBER)
IS
BEGIN
*** ee's salary
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
*** te was successful
IF @@ROWCOUNT = 0 BEGIN
*** the update failed
RAISERROR('-20001 No employee found with ID ' + isnull(emp_id, ''), 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
emp_id IN NUMBER,
new_salary IN NUMBER)
IS
BEGIN
-- Update the employee's salary
UPDATE employees
SET salary = new_salary
WHERE employee_id = emp_id;
-- Check if the update was successful
IF SQL%ROWCOUNT = 0 THEN
-- Raise an error if the update failed
RAISE_APPLICATION_ERROR(-20001, 'No employee found with ID ' || emp_id);
END IF;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
BEGIN
SELECT @v_empno = empno, @v_ename = ename, @v_sal = sal, @v_comm = comm, @v_deptno = deptno
FROM emp
WHERE empno = 7369;
IF v_comm IS NULL BEGIN
SET @v_comm = 0;
END
dbms_output.put_line('Employee Number: ' + isnull(@v_empno, ''));
dbms_output.put_line('Employee Name: ' + isnull(v_ename, ''));
dbms_output.put_line('Salary: ' + isnull(v_sal, ''));
dbms_output.put_line('Commission: ' + isnull(v_comm, ''));
dbms_output.put_line('Department Number: ' + isnull(v_deptno, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END
END;
/
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
v_sal NUMBER;
v_comm NUMBER;
v_deptno NUMBER;
BEGIN
SELECT empno, ename, sal, comm, deptno
INTO v_empno, v_ename, v_sal, v_comm, v_deptno
FROM emp
WHERE empno = 7369;
IF v_comm IS NULL THEN
v_comm := 0;
END IF;
dbms_output.put_line('Employee Number: ' || v_empno);
dbms_output.put_line('Employee Name: ' || v_ename);
dbms_output.put_line('Salary: ' || v_sal);
dbms_output.put_line('Commission: ' || v_comm);
dbms_output.put_line('Department Number: ' || v_deptno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found.');
END;
/
|
CREATE PROCEDURE create_table (@table_name VARCHAR(4000), @column_name VARCHAR(4000), @data_type VARCHAR(4000), @constraint VARCHAR(4000)) AS
BEGIN
SET NOCOUNT ON;
EXECUTE sp_executesql @'CREATE TABLE ' || isnull(@table_name, '') + ' (' + isnull(@column_name, '') + ' ' + isnull(@data_type, '') + ' ' + isnull(@constraint, '') + ')';
END;
CREATE PROCEDURE insert_data (@table_name VARCHAR(4000), @column_name VARCHAR(4000), @value VARCHAR(4000)) AS
BEGIN
SET NOCOUNT ON;
EXECUTE sp_executesql @'INSERT INTO ' || isnull(@table_name, '') + ' (' + isnull(@column_name, '') + ') VALUES (' + isnull(@value, '') + ')';
END;
CREATE PROCEDURE update_data (@table_name VARCHAR(4000), @column_name VARCHAR(4000), @value VARCHAR(4000), @condition VARCHAR(4000)) AS
BEGIN
SET NOCOUNT ON;
EXECUTE sp_executesql @'UPDATE ' || isnull(@table_name, '') + ' SET ' + isnull(@column_name, '') + ' = ' + isnull(@value, '') + ' WHERE ' + isnull(@condition, '');
END;
CREATE PROCEDURE delete_data (@table_name VARCHAR(4000), @condition VARCHAR(4000)) AS
BEGIN
SET NOCOUNT ON;
EXECUTE sp_executesql @'DELETE FROM ' || isnull(@table_name, '') + ' WHERE ' + isnull(@condition, '');
END;
CREATE FUNCTION get_data (@table_name VARCHAR(4000), @column_name VARCHAR(4000), @condition VARCHAR(4000)) RETURNS VARCHAR(4000) AS
BEGIN
DECLARE @RETURN (SELECT @column_name FROM table_name WHERE @condition
return null;
end;);
BEGIN
-- Cr... SQLINES DEMO ***
dbo.create_table('test_table', 'id', 'NUMBER', 'PRIMARY KEY');
*** the table
dbo.insert_data('test_table', 'id', '1');
dbo.insert_data('test_table', 'id', '2');
*** e table
dbo.update_data('test_table', 'id', '3', 'id = 1');
*** the table
dbo.delete_data('test_table', 'id = 2');
*** table
SELECT dbo.get_data('test_table', 'id', 'id = 1');
END;
|
PROCEDURE create_table (table_name VARCHAR2, column_name VARCHAR2, data_type VARCHAR2, constraint VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE ' || table_name || ' (' || column_name || ' ' || data_type || ' ' || constraint || ')';
END;
PROCEDURE insert_data (table_name VARCHAR2, column_name VARCHAR2, value VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO ' || table_name || ' (' || column_name || ') VALUES (' || value || ')';
END;
PROCEDURE update_data (table_name VARCHAR2, column_name VARCHAR2, value VARCHAR2, condition VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'UPDATE ' || table_name || ' SET ' || column_name || ' = ' || value || ' WHERE ' || condition;
END;
PROCEDURE delete_data (table_name VARCHAR2, condition VARCHAR2) IS
BEGIN
EXECUTE IMMEDIATE 'DELETE FROM ' || table_name || ' WHERE ' || condition;
END;
FUNCTION get_data (table_name VARCHAR2, column_name VARCHAR2, condition VARCHAR2) RETURN VARCHAR2 IS
RETURN (SELECT column_name FROM table_name WHERE condition);
BEGIN
-- Create a table
create_table('test_table', 'id', 'NUMBER', 'PRIMARY KEY');
-- Insert data into the table
insert_data('test_table', 'id', '1');
insert_data('test_table', 'id', '2');
-- Update data in the table
update_data('test_table', 'id', '3', 'id = 1');
-- Delete data from the table
delete_data('test_table', 'id = 2');
-- Get data from the table
SELECT get_data('test_table', 'id', 'id = 1');
END;
|
sql
CREATE PROCEDURE emp_salary_raise (
@emp_id IN AS NUMBER,
raise_percent IN NUMBER
)
IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
UPDATE employees SET salary = salary * (1 + raise_percent / 100) WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
@v_emp_id FLOAT := 100;
v_raise_percent SET @NUMBER = 10;
BEGIN
dbo.emp_salary_raise(@v_emp_id, v_raise_percent);
END;
| sql
CREATE PROCEDURE emp_salary_raise (
emp_id IN NUMBER,
raise_percent IN NUMBER
)
IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
UPDATE employees SET salary = salary * (1 + raise_percent / 100) WHERE employee_id = emp_id;
COMMIT;
END;
DECLARE
v_emp_id NUMBER := 100;
v_raise_percent NUMBER := 10;
BEGIN
emp_salary_raise(v_emp_id, v_raise_percent);
END;
|
CREATE PROCEDURE randomize_table(@table_name varchar(4000))
AS
BEGIN
SET NOCOUNT ON;
-- De... SQLINES DEMO ***
v_id number;
v_value dbo.varchar2(20);
*** rows in the table
DECLARE row_rec CURSOR FOR SELECT id, value FROM table_name; OPEN row_rec;
FETCH row_rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** number
set @v_id = DBMS_RANDOM.VALUE(1000);
set @v_value = DBMS_RANDOM.STRING('a', 20);
-- Up... SQLINES DEMO ***
UPDATE table_name SET id = v_id, value = v_value
WHERE id = @row_rec_id;
FETCH row_rec INTO;
END;
CLOSE row_rec;
DEALLOCATE row_rec;
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('Error: ' + ISNULL(SQLERRM, ''));
END;
GO
|
CREATE PROCEDURE randomize_table(table_name varchar2)
AS
BEGIN
-- Declare variables
v_id number;
v_value varchar2(20);
-- Loop through the rows in the table
FOR row_rec IN (SELECT id, value FROM table_name) LOOP
-- Generate a random number
v_id := DBMS_RANDOM.VALUE(1000);
v_value := DBMS_RANDOM.STRING('a', 20);
-- Update the row
UPDATE table_name SET id = v_id, value = v_value
WHERE id = row_rec.id;
END LOOP;
EXCEPTION
-- Handle any errors
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('Error: ' || SQLERRM);
END;
/
|
CREATE PROCEDURE GetEmployeeInfo(@IN EmployeeId AS NUMBER, OUT EmployeeDetails VARCHAR2) AS
BEGIN
SELECT @EmployeeDetails = ename FROM emp WHERE empno = EmployeeId;
IF EmployeeDetails IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE @EmployeeName VARCHAR(100);
dbo.GetEmployeeInfo(100, @EmployeeName);
DBMS_OUTPUT.PUT_LINE('Employee name: ' + ISNULL(@EmployeeName, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting employee information: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE PROCEDURE GetEmployeeInfo(IN EmployeeId NUMBER, OUT EmployeeDetails VARCHAR2) AS
BEGIN
SELECT ename INTO EmployeeDetails FROM emp WHERE empno = EmployeeId;
IF EmployeeDetails IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
BEGIN
DECLARE EmployeeName VARCHAR2(100);
GetEmployeeInfo(100, EmployeeName);
DBMS_OUTPUT.PUT_LINE('Employee name: ' || EmployeeName);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error getting employee information: ' || SQLERRM);
END;
|
BEGIN
-- So... SQLINES DEMO ***
UPDATE t SET c = c + 1 WHERE id = 1;
*** back
IF @@ROWCOUNT = 0 BEGIN
SAVEPOINT sp;
ROLLBACK TO sp;
END
*** an autogenerated primary key and return it's ID
INSERT INTO t (id, c) VALUES (DEFAULT, 10); SET @v_id = SCOPE_IDENTITY();
*** cedure with an out parameter to get the max ID
CALL proc_get_max_id(v_max_id OUT);
*** rsor
DECLARE r CURSOR FOR SELECT * FROM t; OPEN r;
FETCH r INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** d on the cursor data
UPDATE t SET c = c + @r_c WHERE id = @r_id;
FETCH r INTO;
END;
CLOSE r;
DEALLOCATE r;
*** n if the number of rows affected is 0
IF @@ROWCOUNT = 0 BEGIN
RAISERROR('-20000 No rows affected', 16, 1);
END
END;
|
BEGIN
-- Some actions
UPDATE t SET c = c + 1 WHERE id = 1;
-- If error, roll it back
IF SQL%ROWCOUNT = 0 THEN
SAVEPOINT sp;
ROLLBACK TO sp;
END IF;
-- Insert a row with an autogenerated primary key and return it's ID
INSERT INTO t (id, c) VALUES (DEFAULT, 10) RETURNING id INTO v_id;
-- Call a stored procedure with an out parameter to get the max ID
CALL proc_get_max_id(v_max_id OUT);
-- Loop through a cursor
FOR r IN (SELECT * FROM t) LOOP
-- Update a row based on the cursor data
UPDATE t SET c = c + r.c WHERE id = r.id;
END LOOP;
-- Raise an exception if the number of rows affected is 0
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'No rows affected');
END IF;
END;
|
CREATE PROCEDURE my_proc(@param1 IN AS NUMBER)
AS
rec RECORD (id NUMBER, name dbo.VARCHAR2(100));
stmt EXECUTE sp_executesql @'SELECT * FROM my_table WHERE id = :1', N'@param1', @param1;
FETCH stmt INTO @rec;
IF rec.id IS NOT NULL BEGIN
UPDATE my_table SET name = 'Updated' WHERE id = rec.id;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20000 Item not found', 16, 1);
END
END my_proc;
|
CREATE PROCEDURE my_proc(param1 IN NUMBER)
AS
rec RECORD (id NUMBER, name VARCHAR2(100));
stmt EXECUTE IMMEDIATE 'SELECT * FROM my_table WHERE id = :1' USING param1;
FETCH stmt INTO rec;
IF rec.id IS NOT NULL THEN
UPDATE my_table SET name = 'Updated' WHERE id = rec.id;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Item not found');
END IF;
END my_proc;
|
DECLARE
-- Sa... SQLINES DEMO ***
@v_dept_id FLOAT := 10;
v_emp_id SET @NUMBER = 100;
v_emp_name dbo.VARCHAR2(50) := 'John Doe';
BEGIN
*** oyee into the database
INSERT INTO EMPLOYEES (DEPARTMENT_ID, EMPLOYEE_ID, EMPLOYEE_NAME)
VALUES (@v_dept_id, v_emp_id, v_emp_name);
*** ee's name
UPDATE EMPLOYEES
SET EMPLOYEE_NAME = 'Jane Doe'
WHERE EMPLOYEE_ID = v_emp_id;
*** ee from the database
DELETE FROM EMPLOYEES
WHERE EMPLOYEE_ID = v_emp_id;
END; | DECLARE
-- Sample data
v_dept_id NUMBER := 10;
v_emp_id NUMBER := 100;
v_emp_name VARCHAR2(50) := 'John Doe';
BEGIN
-- Insert a new employee into the database
INSERT INTO EMPLOYEES (DEPARTMENT_ID, EMPLOYEE_ID, EMPLOYEE_NAME)
VALUES (v_dept_id, v_emp_id, v_emp_name);
-- Update the employee's name
UPDATE EMPLOYEES
SET EMPLOYEE_NAME = 'Jane Doe'
WHERE EMPLOYEE_ID = v_emp_id;
-- Delete the employee from the database
DELETE FROM EMPLOYEES
WHERE EMPLOYEE_ID = v_emp_id;
END; |
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL
DROP FUNCTION get_employee_name;
GO
CREATE FUNCTION get_employee_name(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
IS
employee_name dbo.VARCHAR2(50);
BEGIN
SELECT @employee_name = name FROM employees WHERE employee_id = employee_id;
RETURN employee_name;
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
@employee_id FLOAT := 1000;
employee_name dbo.VARCHAR2(50);
BEGIN
SET @employee_name = dbo.get_employee_name(@employee_id);
IF employee_name IS NULL BEGIN
RAISERROR('-20001 Employee does not exist.', 16, 1);
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(employee_name, ''));
dbo.update_employee_salary(@employee_id, 50000);
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_name(employee_id IN NUMBER) RETURN VARCHAR2
IS
employee_name VARCHAR2(50);
BEGIN
SELECT name INTO employee_name FROM employees WHERE employee_id = employee_id;
RETURN employee_name;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER)
IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
DECLARE
employee_id NUMBER := 1000;
employee_name VARCHAR2(50);
BEGIN
employee_name := get_employee_name(employee_id);
IF employee_name IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee does not exist.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee name: ' || employee_name);
update_employee_salary(employee_id, 50000);
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_emp_id = employee_id, @v_emp_name = isnull(last_name, '') + ', ' + isnull(first_name, '')
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
IF @v_emp_id IS NULL BEGIN
RAISERROR('-20001 Invalid employee ID', 16, 1);
END
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.');
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.');
END
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(20);
BEGIN
SELECT employee_id, last_name || ', ' || first_name
INTO v_emp_id, v_emp_name
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
IF v_emp_id IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid employee ID');
END IF;
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = v_emp_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully.');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with the specified ID.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('TEST_PROC', 'P') IS NOT NULL
DROP PROCEDURE TEST_PROC;
GO
CREATE PROCEDURE TEST_PROC (
@NUM_IN IN AS NUMBER,
STR_IN IN VARCHAR2
)
AS
V_OUT NUMBER;
BEGIN
SELECT @V_OUT = COUNT(*) WHERE STR_IN = 'TEST';
DBMS_OUTPUT.PUT_LINE(V_OUT);
IF V_OUT > 0 BEGIN
UPDATE DUAL SET NUM_IN = NUM_IN + 1;
END
RAISERROR('-20001 Custom Error: ' + ISNULL(STR_IN, ''), 16, 1);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
|
CREATE OR REPLACE PROCEDURE TEST_PROC (
NUM_IN IN NUMBER,
STR_IN IN VARCHAR2
)
AS
V_OUT NUMBER;
BEGIN
SELECT COUNT(*) INTO V_OUT FROM DUAL WHERE STR_IN = 'TEST';
DBMS_OUTPUT.PUT_LINE(V_OUT);
IF V_OUT > 0 THEN
UPDATE DUAL SET NUM_IN = NUM_IN + 1;
END IF;
RAISE_APPLICATION_ERROR(-20001, 'Custom Error: ' || STR_IN);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
|
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE get_customer_orders;
GO
CREATE PROCEDURE get_customer_orders(@customer_id IN AS NUMBER, cursor OUT SYS_REFCURSOR)
AS
BEGIN
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
END;
DECLARE
@customer_id FLOAT := 1234;
orders SYS_REFCURSOR;
BEGIN
dbo.get_customer_orders(@customer_id, orders);
WHILE 1=1 BEGIN
FETCH orders INTO @order_id, @order_date, @total_amount;
IF @@FETCH_STATUS <> 0 BREAK;
*** the order information
END;
CLOSE orders;
END;
|
CREATE OR REPLACE PROCEDURE get_customer_orders(customer_id IN NUMBER, cursor OUT SYS_REFCURSOR)
AS
BEGIN
OPEN cursor FOR
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = customer_id;
END;
DECLARE
customer_id NUMBER := 1234;
orders SYS_REFCURSOR;
BEGIN
get_customer_orders(customer_id, orders);
LOOP
FETCH orders INTO order_id, order_date, total_amount;
EXIT WHEN orders%NOTFOUND;
-- Do something with the order information
END LOOP;
CLOSE orders;
END;
|
CREATE PROCEDURE update_employee(
@employee_id IN AS NUMBER,
salary IN NUMBER,
department_id IN NUMBER,
is_manager IN BOOLEAN
) AS
BEGIN
*** ee's salary
UPDATE employees SET salary = salary
WHERE employee_id = employee_id;
*** ee's department
UPDATE employees SET department_id = department_id
WHERE employee_id = employee_id;
*** ee's manager status
UPDATE employees SET is_manager = is_manager
WHERE employee_id = employee_id;
END;
|
CREATE PROCEDURE update_employee(
employee_id IN NUMBER,
salary IN NUMBER,
department_id IN NUMBER,
is_manager IN BOOLEAN
) AS
BEGIN
-- Update the employee's salary
UPDATE employees SET salary = salary
WHERE employee_id = employee_id;
-- Update the employee's department
UPDATE employees SET department_id = department_id
WHERE employee_id = employee_id;
-- Update the employee's manager status
UPDATE employees SET is_manager = is_manager
WHERE employee_id = employee_id;
END;
|
IF OBJECT_ID('update_employee', 'P') IS NOT NULL
DROP PROCEDURE update_employee;
GO
CREATE PROCEDURE update_employee(
@p_employee_id IN AS NUMBER,
p_salary IN NUMBER)
IS
BEGIN
*** ee's salary
UPDATE employees SET salary = p_salary
WHERE employee_id = p_employee_id;
*** te was successful
IF @@ROWCOUNT = 0 BEGIN
*** the update failed
RAISERROR('-20001 Employee not found', 16, 1);
END
EXCEPTION
WHEN OTHERS THEN
*** errors that may occur
RAISE;
END;
|
CREATE OR REPLACE PROCEDURE update_employee(
p_employee_id IN NUMBER,
p_salary IN NUMBER)
IS
BEGIN
-- Update the employee's salary
UPDATE employees SET salary = p_salary
WHERE employee_id = p_employee_id;
-- Check if the update was successful
IF SQL%ROWCOUNT = 0 THEN
-- Raise an error if the update failed
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
EXCEPTION
WHEN OTHERS THEN
-- Handle any other errors that may occur
RAISE;
END;
|
DECLARE
-- De... SQLINES DEMO ***
@v_num1 FLOAT := 10;
v_num2 SET @NUMBER = 5;
v_result NUMBER;
BEGIN
*** ons
SET @v_result = @v_num1 + v_num2;
-- Ou... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('The result is: ' + isnull(v_result, ''));
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
-- Declare variables
v_num1 NUMBER := 10;
v_num2 NUMBER := 5;
v_result NUMBER;
BEGIN
-- Perform calculations
v_result := v_num1 + v_num2;
-- Output the result
DBMS_OUTPUT.PUT_LINE('The result is: ' || v_result);
EXCEPTION
-- Handle any errors
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE TestProc(@in num1 AS NUMBER) AS
BEGIN
UPDATE emp SET sal = sal * num1 WHERE deptno = 10;
COMMIT;
EXPLAIN PLAN FOR UPDATE emp SET @sal = sal * num1 WHERE deptno = 10;
END;
/
BEGIN
dbo.TestProc(1.25);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK;
RAISE DBMS_STANDARD.NO_DATA_FOUND;
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
SELECT * FROM emp WHERE deptno = 10;
|
CREATE PROCEDURE TestProc(in num1 NUMBER) AS
BEGIN
UPDATE emp SET sal = sal * num1 WHERE deptno = 10;
COMMIT;
EXPLAIN PLAN FOR UPDATE emp SET sal = sal * num1 WHERE deptno = 10;
END;
/
BEGIN
TestProc(1.25);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
ROLLBACK;
RAISE DBMS_STANDARD.NO_DATA_FOUND;
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
SELECT * FROM emp WHERE deptno = 10;
|
BEGIN
*** nto a table
INSERT INTO employees (id, name, salary) VALUES (100, 'John Doe', 10000);
*** n a table
UPDATE employees SET salary = salary + 1000 WHERE id = 100;
*** rom a table
DELETE FROM employees WHERE id = 100;
*** cedure
CALL get_employee_count();
-- Ex... SQLINES DEMO ***
BEGIN
*** y throw an exception
EXCEPTION
WHEN OTHERS THEN
*** ion
END;
END;
|
BEGIN
-- Insert a record into a table
INSERT INTO employees (id, name, salary) VALUES (100, 'John Doe', 10000);
-- Update a record in a table
UPDATE employees SET salary = salary + 1000 WHERE id = 100;
-- Delete a record from a table
DELETE FROM employees WHERE id = 100;
-- Call a stored procedure
CALL get_employee_count();
-- Exception handling
BEGIN
-- Some code that may throw an exception
EXCEPTION
WHEN OTHERS THEN
-- Handle the exception
END;
END;
|
DECLARE
@emp_id FLOAT;
BEGIN
SELECT @emp_id = employee_id
FROM employees
WHERE last_name = 'Smith';
IF @emp_id IS NOT NULL BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = @emp_id;
END
ELSE BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
COMMIT;
END;
|
DECLARE
emp_id NUMBER;
BEGIN
SELECT employee_id
INTO emp_id
FROM employees
WHERE last_name = 'Smith';
IF emp_id IS NOT NULL THEN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
COMMIT;
END;
|
IF OBJECT_ID('test_proc', 'P') IS NOT NULL
DROP PROCEDURE test_proc;
GO
CREATE PROCEDURE test_proc (
@p_empno IN AS NUMBER,
p_ename OUT VARCHAR2,
p_sal OUT NUMBER,
p_rc OUT VARCHAR2
)
AS
BEGIN
SELECT @p_ename = ename, @p_sal = sal
FROM emp
WHERE empno = p_empno;
IF @@FETCH_STATUS <> 0 BEGIN
set @p_rc = 'ENOENT';
END
END;
|
CREATE OR REPLACE PROCEDURE test_proc (
p_empno IN NUMBER,
p_ename OUT VARCHAR2,
p_sal OUT NUMBER,
p_rc OUT VARCHAR2
)
AS
BEGIN
SELECT ename, sal
INTO p_ename, p_sal
FROM emp
WHERE empno = p_empno;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_rc := 'ENOENT';
END;
|
IF OBJECT_ID('Validate_User', 'P') IS NOT NULL
DROP PROCEDURE Validate_User;
GO
CREATE PROCEDURE Validate_User(@p_username VARCHAR(4000), @p_password VARCHAR(4000))
AS
BEGIN
SET NOCOUNT ON;
IF @p_username IS NULL OR @p_password IS NULL BEGIN
RAISERROR('-20101 Username or password cannot be null', 16, 1);
END
*** exists
SELECT @l_user_count = COUNT(*)
FROM Users
WHERE username = @p_username;
IF l_user_count = 0 BEGIN
RAISERROR('-20102 Invalid username', 16, 1);
END
*** word is correct
SELECT @l_stored_password = password
FROM Users
WHERE username = @p_username;
IF l_stored_password <> @p_password BEGIN
RAISERROR('-20103 Invalid password', 16, 1);
END
END;
|
CREATE OR REPLACE PROCEDURE Validate_User(p_username VARCHAR2, p_password VARCHAR2)
IS
BEGIN
IF p_username IS NULL OR p_password IS NULL THEN
RAISE_APPLICATION_ERROR(-20101, 'Username or password cannot be null');
END IF;
-- Check if the user exists
SELECT COUNT(*)
INTO l_user_count
FROM Users
WHERE username = p_username;
IF l_user_count = 0 THEN
RAISE_APPLICATION_ERROR(-20102, 'Invalid username');
END IF;
-- Check if the password is correct
SELECT password
INTO l_stored_password
FROM Users
WHERE username = p_username;
IF l_stored_password <> p_password THEN
RAISE_APPLICATION_ERROR(-20103, 'Invalid password');
END IF;
END Validate_User;
|
CREATE PROCEDURE ProcessOrders
(
@ORDER_ID IN AS NUMBER
)
IS
ORDER_TOTAL NUMBER;
BEGIN
SELECT @ORDER_TOTAL = SUM(QUANTITY*UNIT_PRICE)
FROM ORDER_DETAILS
WHERE ORDER_ID = ORDER_ID;
IF ORDER_TOTAL >= 100 BEGIN
UPDATE ORDERS SET STATUS = 'Shipped'
WHERE ORDER_ID = ORDER_ID;
END
ELSE BEGIN
UPDATE ORDERS SET STATUS = 'Processing'
WHERE ORDER_ID = ORDER_ID;
END
END;
|
CREATE PROCEDURE ProcessOrders
(
ORDER_ID IN NUMBER
)
IS
ORDER_TOTAL NUMBER;
BEGIN
SELECT SUM(QUANTITY*UNIT_PRICE) INTO ORDER_TOTAL
FROM ORDER_DETAILS
WHERE ORDER_ID = ORDER_ID;
IF ORDER_TOTAL >= 100 THEN
UPDATE ORDERS SET STATUS = 'Shipped'
WHERE ORDER_ID = ORDER_ID;
ELSE
UPDATE ORDERS SET STATUS = 'Processing'
WHERE ORDER_ID = ORDER_ID;
END IF;
END;
|
DECLARE
@deptno FLOAT := 20;
empno NUMBER;
CURSOR dept_emps IS
SELECT empno
FROM emp
WHERE @deptno = @deptno;
BEGIN
OPEN dept_emps;
WHILE 1=1 BEGIN
FETCH dept_emps INTO @empno;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Employee Number: ' + isnull(empno, ''));
END;
CLOSE dept_emps;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found in department ' + isnull(@deptno, ''));
END
END;
|
DECLARE
deptno NUMBER := 20;
empno NUMBER;
CURSOR dept_emps IS
SELECT empno
FROM emp
WHERE deptno = deptno;
BEGIN
OPEN dept_emps;
LOOP
FETCH dept_emps INTO empno;
EXIT WHEN dept_emps%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Employee Number: ' || empno);
END LOOP;
CLOSE dept_emps;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found in department ' || deptno);
END;
|
BEGIN
DECLARE @emp_id FLOAT;
DECLARE @salary FLOAT;
DECLARE @bonus FLOAT;
BEGIN
SELECT @emp_id = employee_id, @salary = @salary FROM employees WHERE last_name = 'King';
IF @salary > 10000 BEGIN
SET @bonus = @salary * 0.10;
END
ELSE BEGIN
SET @bonus = 0;
END
UPDATE employees SET salary = @salary + @bonus WHERE employee_id = @emp_id;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END; | BEGIN
DECLARE emp_id NUMBER;
DECLARE salary NUMBER;
DECLARE bonus NUMBER;
BEGIN
SELECT employee_id, salary INTO emp_id, salary FROM employees WHERE last_name = 'King';
IF salary > 10000 THEN
bonus := salary * 0.10;
ELSE
bonus := 0;
END IF;
UPDATE employees SET salary = salary + bonus WHERE employee_id = emp_id;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END; |
IF OBJECT_ID('calculate_total_sales', 'P') IS NOT NULL
DROP PROCEDURE calculate_total_sales;
GO
CREATE PROCEDURE calculate_total_sales
(
@order_id IN AS NUMBER,
total_sales OUT NUMBER
)
AS
BEGIN
SELECT @total_sales = SUM(quantity * unit_price)
FROM order_details
WHERE order_id = order_id;
END;
DECLARE
@total_sales FLOAT;
BEGIN
dbo.calculate_total_sales(100, @total_sales);
DBMS_OUTPUT.PUT_LINE('Total sales for order 100: ' + isnull(@total_sales, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for order ' + isnull(order_id, ''));
END
END;
CREATE FUNCTION get_product_name
(
@product_id IN NUMBER
DECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN (
SELECT product_name
FROM products
WHERE product_id = product_id
);
END;
DECLARE
@product_name VARCHAR(100);
BEGIN
SET @product_name = dbo.get_product_name(200);
DBMS_OUTPUT.PUT_LINE('Product name for product ID 200: ' + isnull(@product_name, ''));
END;
|
CREATE OR REPLACE PROCEDURE calculate_total_sales
(
order_id IN NUMBER,
total_sales OUT NUMBER
)
AS
BEGIN
SELECT SUM(quantity * unit_price)
INTO total_sales
FROM order_details
WHERE order_id = order_id;
END;
DECLARE
total_sales NUMBER;
BEGIN
calculate_total_sales(100, total_sales);
DBMS_OUTPUT.PUT_LINE('Total sales for order 100: ' || total_sales);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for order ' || order_id);
END;
CREATE FUNCTION get_product_name
(
product_id IN NUMBER
)
RETURN VARCHAR2
AS
BEGIN
RETURN (
SELECT product_name
FROM products
WHERE product_id = product_id
);
END;
DECLARE
product_name VARCHAR2(100);
BEGIN
product_name := get_product_name(200);
DBMS_OUTPUT.PUT_LINE('Product name for product ID 200: ' || product_name);
END;
|
BEGIN
DECLARE rec CURSOR FOR SELECT * FROM employees;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** of each employee
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = @rec_employee_id;
*** d salary
DBMS_OUTPUT.PUT_LINE('Updated salary for ' + isnull(@rec_employee_name, '') + ': ' + isnull(@rec_salary, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
DECLARE
@v_salary FLOAT;
BEGIN
*** the employee with employee_id = 100
SELECT @v_salary = salary FROM employees WHERE employee_id = 100;
*** n if the salary is less than 10000
IF @v_salary < 10000 BEGIN
RAISERROR('-20001 Salary must be greater than or equal to 10000', 16, 1);
END
END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @v_salary FLOAT;
SELECT @v_salary = salary FROM employees WHERE @employee_id = @employee_id;
RETURN @v_salary;
END; | BEGIN
FOR rec IN (SELECT * FROM employees)
LOOP
-- Update the salary of each employee
UPDATE employees SET salary = salary * 1.10
WHERE employee_id = rec.employee_id;
-- Output the updated salary
DBMS_OUTPUT.PUT_LINE('Updated salary for ' || rec.employee_name || ': ' || rec.salary);
END LOOP;
END;
DECLARE
v_salary NUMBER;
BEGIN
-- Get the salary of the employee with employee_id = 100
SELECT salary INTO v_salary FROM employees WHERE employee_id = 100;
-- Raise an exception if the salary is less than 10000
IF v_salary < 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary must be greater than or equal to 10000');
END IF;
END;
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id NUMBER) RETURN NUMBER IS
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = employee_id;
RETURN v_salary;
END; |
DECLARE
@TYPE num_arr IS TABLE OF NUMBER;
v_nums num_arr;
BEGIN
SELECT t.num [BULK] COLLECT INTO v_nums
FROM (SELECT LEVEL num FROM DUAL CONNECT BY LEVEL <= 10) t;
DECLARE @i INT = 1 ;
WHILE @i <= v_nums.COUNT BEGIN
DBMS_OUTPUT.PUT_LINE('Number ' + isnull(i, '') + ': ' + isnull(dbo.v_nums(i), ''));
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END; | DECLARE
TYPE num_arr IS TABLE OF NUMBER;
v_nums num_arr;
BEGIN
SELECT t.num BULK COLLECT INTO v_nums
FROM (SELECT LEVEL num FROM DUAL CONNECT BY LEVEL <= 10) t;
FOR i IN 1 .. v_nums.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Number ' || i || ': ' || v_nums(i));
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred.');
END; |
IF OBJECT_ID('GetCustomerOrders', 'FN') IS NOT NULL
DROP FUNCTION GetCustomerOrders;
GO
CREATE FUNCTION GetCustomerOrders(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END; IS
orders CURSOR;
BEGIN
SELECT order_id, order_date FROM orders WHERE customer_id = customer_id;
RETURN orders;
END;
DECLARE
@orders SYS_REFCURSOR;
BEGIN
SET @orders = dbo.GetCustomerOrders(12345);
WHILE 1=1 BEGIN
FETCH orders INTO @order_id, @order_date;
IF @@FETCH_STATUS <> 0 BREAK;
*** the order information
END;
CLOSE orders;
END;
|
CREATE OR REPLACE FUNCTION GetCustomerOrders(customer_id IN NUMBER) RETURN SYS_REFCURSOR IS
orders CURSOR;
BEGIN
OPEN orders FOR
SELECT order_id, order_date FROM orders WHERE customer_id = customer_id;
RETURN orders;
END;
DECLARE
orders SYS_REFCURSOR;
BEGIN
orders := GetCustomerOrders(12345);
LOOP
FETCH orders INTO order_id, order_date;
EXIT WHEN orders%NOTFOUND;
-- Do something with the order information
END LOOP;
CLOSE orders;
END;
|
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL
DROP FUNCTION get_employee_details;
GO
CREATE FUNCTION get_employee_details(@emp_id FLOAT) RETURNS employee_details_type AS
BEGIN
DECLARE @emp_details employee_details_type;
SELECT @emp_details = * FROM employees WHERE employee_id = @emp_id;
RETURN @emp_details;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
WHEN OTHERS THEN
RAISE;
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT) AS
BEGIN
SET NOCOUNT ON;
UPDATE employees SET salary = @new_salary WHERE employee_id = @emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
WHEN OTHERS THEN
RAISE;
END;
DECLARE
@emp_details employee_details_type;
BEGIN
SET @emp_details = dbo.get_employee_details(1);
IF @emp_details IS NOT NULL BEGIN
dbo.update_employee_salary(emp_details.employee_id, emp_details.salary * 1.10);
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_details(emp_id NUMBER) RETURN employee_details_type IS
emp_details employee_details_type;
BEGIN
SELECT * INTO emp_details FROM employees WHERE employee_id = emp_id;
RETURN emp_details;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN OTHERS THEN
RAISE;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
WHEN OTHERS THEN
RAISE;
END;
DECLARE
emp_details employee_details_type;
BEGIN
emp_details := get_employee_details(1);
IF emp_details IS NOT NULL THEN
update_employee_salary(emp_details.employee_id, emp_details.salary * 1.10);
END IF;
END;
|
declare
@v_empno float := 10;
v_ename dbo.varchar2(10);
v_job dbo.varchar2(10);
begin
select @v_ename = ename, @v_job = job from emp where empno = @v_empno;
if v_job = 'MANAGER' begin
dbms_output.put_line('Employee ' + isnull(@v_empno, '') + ' is a MANAGER');
end
else begin
dbms_output.put_line('Employee ' + isnull(@v_empno, '') + ' is not a MANAGER');
end
if @@fetch_status <> 0 begin
dbms_output.put_line('No employee found with empno ' + isnull(@v_empno, ''));
end
end;
|
declare
v_empno number := 10;
v_ename varchar2(10);
v_job varchar2(10);
begin
select ename, job into v_ename, v_job from emp where empno = v_empno;
if v_job = 'MANAGER' then
dbms_output.put_line('Employee ' || v_empno || ' is a MANAGER');
else
dbms_output.put_line('Employee ' || v_empno || ' is not a MANAGER');
end if;
exception
when no_data_found then
dbms_output.put_line('No employee found with empno ' || v_empno);
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 > 5000 BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_emp_id;
END
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 > 5000 THEN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_emp_id;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(100);
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary
FROM employees
WHERE emp_id = 10;
IF v_salary > 50000 BEGIN
UPDATE employees SET salary = salary * 1.1
WHERE emp_id = @v_emp_id;
END
ELSE IF v_salary > 30000 BEGIN
UPDATE employees SET salary = salary * 1.05
WHERE emp_id = @v_emp_id;
END
ELSE BEGIN
UPDATE employees SET salary = salary * 1.02
WHERE emp_id = @v_emp_id;
END
COMMIT;
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(100);
v_salary NUMBER;
BEGIN
SELECT emp_id, emp_name, salary
INTO v_emp_id, v_emp_name, v_salary
FROM employees
WHERE emp_id = 10;
IF v_salary > 50000 THEN
UPDATE employees SET salary = salary * 1.1
WHERE emp_id = v_emp_id;
ELSIF v_salary > 30000 THEN
UPDATE employees SET salary = salary * 1.05
WHERE emp_id = v_emp_id;
ELSE
UPDATE employees SET salary = salary * 1.02
WHERE emp_id = v_emp_id;
END IF;
COMMIT;
END;
|
CREATE PROCEDURE my_procedure (
@p_in IN AS VARCHAR2,
p_out OUT VARCHAR2,
p_inout IN OUT VARCHAR2
) IS
BEGIN
*** p_in, p_out, and p_inout
IF p_in IS NULL BEGIN
RAISERROR('-20001 Input parameter cannot be NULL.', 16, 1);
END
SELECT @p_out = MAX(value) FROM my_table WHERE name = p_in;
set @p_inout = isnull(p_inout, '') + ' (modified)';
EXCEPTION
WHEN OTHERS THEN
*** here
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE my_procedure (
p_in IN VARCHAR2,
p_out OUT VARCHAR2,
p_inout IN OUT VARCHAR2
) IS
BEGIN
-- Do something with p_in, p_out, and p_inout
IF p_in IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Input parameter cannot be NULL.');
END IF;
SELECT MAX(value) INTO p_out FROM my_table WHERE name = p_in;
p_inout := p_inout || ' (modified)';
EXCEPTION
WHEN OTHERS THEN
-- Handle any errors here
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT := 100;
v_ename dbo.VARCHAR2(10) := 'SCOTT';
BEGIN
SELECT @v_ename = name FROM departments WHERE department_id = 20;
UPDATE employees SET name = v_ename WHERE employee_id = @v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@v_empno, '') + ' updated to ' + isnull(v_ename, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee with employee ID ' + isnull(@v_empno, '') + ' found');
END
END;
|
DECLARE
v_empno NUMBER := 100;
v_ename VARCHAR2(10) := 'SCOTT';
BEGIN
SELECT name INTO v_ename FROM departments WHERE department_id = 20;
UPDATE employees SET name = v_ename WHERE employee_id = v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Employee ' || v_empno || ' updated to ' || v_ename);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with employee ID ' || v_empno || ' found');
END;
|
DECLARE @v_salary DECIMAL(10,2);
BEGIN
SELECT @v_salary = salary FROM employees WHERE emp_id = 1;
IF @v_salary > 5000 BEGIN
UPDATE employees SET salary = @v_salary + 500 WHERE emp_id = 1;
END
COMMIT;
END;
|
DECLARE v_salary NUMBER(10,2);
BEGIN
SELECT salary INTO v_salary FROM employees WHERE emp_id = 1;
IF v_salary > 5000 THEN
UPDATE employees SET salary = v_salary + 500 WHERE emp_id = 1;
END IF;
COMMIT;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_ename dbo.VARCHAR2(25);
BEGIN
SELECT @v_ename = ename FROM emp WHERE empno = @v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_ename, ''));
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error updating salary!');
END;
END;
|
DECLARE
v_empno NUMBER := 10;
v_ename VARCHAR2(25);
BEGIN
SELECT ename INTO v_ename FROM emp WHERE empno = v_empno;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_ename);
BEGIN
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully!');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Error updating salary!');
END;
END;
|
IF OBJECT_ID('insert_data', 'P') IS NOT NULL
DROP PROCEDURE insert_data;
GO
CREATE PROCEDURE insert_data(@p_id IN AS NUMBER, p_name IN VARCHAR2, p_age IN NUMBER)
AS
BEGIN
INSERT INTO persons (id, name, age) VALUES (p_id, p_name, p_age);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISERROR('-20100 Duplicate value in unique index.', 16, 1);
END;
IF OBJECT_ID('get_name', 'FN') IS NOT NULL
DROP FUNCTION get_name;
GO
CREATE FUNCTION get_name(@p_id IN NUMBERDECLARE )
RETURN
BEGIN VARCHAR2
RETURN NULL;
END;
AS
BEGIN
RETURN (SELECT name FROM persons WHERE id = p_id);
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20200 No data found for given ID.', 16, 1);
END
END;
*** tion
DECLARE
@v_age FLOAT;
BEGIN
SELECT @v_age = age FROM persons WHERE id = 1;
IF @v_age > 20 BEGIN
DBMS_OUTPUT.PUT_LINE('Age is greater than 20.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Age is less than or equal to 20.');
END
END;
-- Er... SQLINES DEMO ***
BEGIN
DELETE FROM persons WHERE id = 1;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found to delete.');
END
END;
|
CREATE OR REPLACE PROCEDURE insert_data(p_id IN NUMBER, p_name IN VARCHAR2, p_age IN NUMBER)
AS
BEGIN
INSERT INTO persons (id, name, age) VALUES (p_id, p_name, p_age);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20100, 'Duplicate value in unique index.');
END;
CREATE OR REPLACE FUNCTION get_name(p_id IN NUMBER)
RETURN VARCHAR2
AS
BEGIN
RETURN (SELECT name FROM persons WHERE id = p_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20200, 'No data found for given ID.');
END;
-- Conditional execution
DECLARE
v_age NUMBER;
BEGIN
SELECT age INTO v_age FROM persons WHERE id = 1;
IF v_age > 20 THEN
DBMS_OUTPUT.PUT_LINE('Age is greater than 20.');
ELSE
DBMS_OUTPUT.PUT_LINE('Age is less than or equal to 20.');
END IF;
END;
-- Error handling
BEGIN
DELETE FROM persons WHERE id = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found to delete.');
END;
|
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc (@n IN AS NUMBER) IS
v_result SET @NUMBER = 0;
BEGIN
IF n > 10 BEGIN
set @v_result = n * 2;
END
ELSE BEGIN
set @v_result = n + 1;
END
DBMS_OUTPUT.PUT_LINE(v_result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE OR REPLACE PROCEDURE my_proc (n IN NUMBER) IS
v_result NUMBER := 0;
BEGIN
IF n > 10 THEN
v_result := n * 2;
ELSE
v_result := n + 1;
END IF;
DBMS_OUTPUT.PUT_LINE(v_result);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
|
CREATE PROCEDURE usp_update_employee(
@IN v_empno AS NUMBER,
IN v_ename VARCHAR2,
IN v_job VARCHAR2,
IN v_mgr NUMBER,
IN v_sal NUMBER,
IN v_comm NUMBER,
IN v_deptno NUMBER,
OUT v_status VARCHAR2
)
AS
BEGIN
BEGIN
UPDATE employees
SET ename = v_ename, job = v_job,
mgr = v_mgr, sal = v_sal,
comm = v_comm, deptno = v_deptno
WHERE empno = v_empno;
set @v_status = 'SUCCESS';
IF @@FETCH_STATUS <> 0 BEGIN
set @v_status = 'NOT_FOUND';
EXCEPTION WHEN OTHERS THEN
set @v_status = 'ERROR';
END
END;
END;
|
CREATE PROCEDURE usp_update_employee(
IN v_empno NUMBER,
IN v_ename VARCHAR2,
IN v_job VARCHAR2,
IN v_mgr NUMBER,
IN v_sal NUMBER,
IN v_comm NUMBER,
IN v_deptno NUMBER,
OUT v_status VARCHAR2
)
AS
BEGIN
BEGIN
UPDATE employees
SET ename = v_ename, job = v_job,
mgr = v_mgr, sal = v_sal,
comm = v_comm, deptno = v_deptno
WHERE empno = v_empno;
v_status := 'SUCCESS';
EXCEPTION WHEN NO_DATA_FOUND THEN
v_status := 'NOT_FOUND';
EXCEPTION WHEN OTHERS THEN
v_status := 'ERROR';
END;
END;
|
*** with Error Handling
IF OBJECT_ID('my_proc', 'P') IS NOT NULL
DROP PROCEDURE my_proc;
GO
CREATE PROCEDURE my_proc
(@param1 IN AS NUMBER, param2 IN NUMBER, OUT result OUT NUMBER)
AS
BEGIN
-- Ex... SQLINES DEMO ***
set @result = param1 * param2;
*** error
EXCEPTION
WHEN OTHERS THEN
*** raise a custom exception
DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, ''));
RAISERROR('-20001 Error in my_proc', 16, 1);
END;
|
-- Stored Procedure with Error Handling
CREATE OR REPLACE PROCEDURE my_proc
(param1 IN NUMBER, param2 IN NUMBER, OUT result OUT NUMBER)
AS
BEGIN
-- Execute some logic
result := param1 * param2;
-- Handle potential error
EXCEPTION
WHEN OTHERS THEN
-- Log the error and raise a custom exception
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
RAISE_APPLICATION_ERROR(-20001, 'Error in my_proc');
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(200);
v_emp_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_emp_salary = emp_salary
FROM employees
WHERE emp_id = 10;
IF v_emp_salary > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' is eligible for a bonus.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' is not eligible for a bonus.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 10.');
END
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(200);
v_emp_salary NUMBER;
BEGIN
SELECT emp_id, emp_name, emp_salary
INTO v_emp_id, v_emp_name, v_emp_salary
FROM employees
WHERE emp_id = 10;
IF v_emp_salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' is eligible for a bonus.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' is not eligible for a bonus.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with ID 10.');
END;
|
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary FROM employees WHERE employee_id = 10;
IF @v_salary > 10000 BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = 10;
END
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary INTO v_salary FROM employees WHERE employee_id = 10;
IF v_salary > 10000 THEN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = 10;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
DECLARE
@v_empno FLOAT := 10;
v_ename dbo.VARCHAR2(20) := 'John Doe';
BEGIN
*** oyee
INSERT INTO employees (empno, ename)
VALUES (@v_empno, v_ename);
*** ee's name
UPDATE employees
SET ename = 'Jane Doe'
WHERE empno = @v_empno;
-- De... SQLINES DEMO ***
DELETE FROM employees
WHERE empno = @v_empno;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with empno ' + isnull(@v_empno, ''));
END
END;
|
DECLARE
v_empno NUMBER := 10;
v_ename VARCHAR2(20) := 'John Doe';
BEGIN
-- Insert a new employee
INSERT INTO employees (empno, ename)
VALUES (v_empno, v_ename);
-- Update the employee's name
UPDATE employees
SET ename = 'Jane Doe'
WHERE empno = v_empno;
-- Delete the employee
DELETE FROM employees
WHERE empno = v_empno;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee found with empno ' || v_empno);
END;
|
IF OBJECT_ID('subtract_salary', 'P') IS NOT NULL
DROP PROCEDURE subtract_salary;
GO
CREATE PROCEDURE subtract_salary(@emp_id IN AS NUMBER, amt IN NUMBER) AS
new_salary NUMBER;
BEGIN
SELECT @new_salary = salary FROM employees WHERE employee_id = emp_id;
set @new_salary = new_salary - amt;
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
/
BEGIN
dbo.subtract_salary(100, 200);
END;
/
|
CREATE OR REPLACE PROCEDURE subtract_salary(emp_id IN NUMBER, amt IN NUMBER) AS
new_salary NUMBER;
BEGIN
SELECT salary INTO new_salary FROM employees WHERE employee_id = emp_id;
new_salary := new_salary - amt;
UPDATE employees SET salary = new_salary WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Salary updated successfully.');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while updating salary.');
END;
/
BEGIN
subtract_salary(100, 200);
END;
/
|
DECLARE
@x INTEGER := 10;
BEGIN
IF @x > 5 BEGIN
SET @x = @x + 1;
END
ELSE BEGIN
SET @x = @x - 1;
END
IF @x > 10 BEGIN
RAISE PROGRAM_ERROR;
END
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20;
INSERT INTO orders ( customer_id, order_date, total_amount) VALUES ( 10, GETDATE(), 100);
COMMIT;
EXCEPTION
WHEN PROGRAM_ERROR THEN
ROLLBACK;
END; | DECLARE
x INTEGER := 10;
BEGIN
IF x > 5 THEN
x := x + 1;
ELSE
x := x - 1;
END IF;
IF x > 10 THEN
RAISE PROGRAM_ERROR;
END IF;
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20;
INSERT INTO orders (order_id, customer_id, order_date, total_amount) VALUES (order_seq.NEXTVAL, 10, SYSDATE, 100);
COMMIT;
EXCEPTION
WHEN PROGRAM_ERROR THEN
ROLLBACK;
END; |
CREATE PROCEDURE delete_employee(@emp_id INT) AS
BEGIN
SET NOCOUNT ON;
DELETE FROM employees
WHERE employee_id = @emp_id;
COMMIT;
END;
|
PROCEDURE delete_employee(emp_id INT) IS
BEGIN
DELETE FROM employees
WHERE employee_id = emp_id;
COMMIT;
END;
|
BEGIN
DECLARE
@v_salary FLOAT;
BEGIN
SELECT @v_salary = salary
FROM employees
WHERE employee_id = 10;
IF @@fetch_status <> 0 BEGIN
SET @v_salary = 0;
END
END;
IF @v_salary > 10000 BEGIN
UPDATE employees
SET salary = @v_salary + 1000
WHERE employee_id = 10;
END
END; | BEGIN
DECLARE
v_salary NUMBER;
BEGIN
SELECT salary
INTO v_salary
FROM employees
WHERE employee_id = 10;
EXCEPTION
WHEN no_data_found THEN
v_salary := 0;
END;
IF v_salary > 10000 THEN
UPDATE employees
SET salary = v_salary + 1000
WHERE employee_id = 10;
END IF;
END; |
BEGIN
CREATE PROCEDURE get_customer_orders(@cust_id IN AS NUMBER)
AS
orders_cursor SYS_REFCURSOR;
BEGIN
SELECT order_id, order_date, order_total
FROM orders
WHERE customer_id = cust_id;
RETURN orders_cursor;
END;
END;
DECLARE
@orders_cursor SYS_REFCURSOR;
customer_id SET @NUMBER = 12345;
BEGIN
SET @orders_cursor = dbo.get_customer_orders(customer_id);
WHILE 1=1 BEGIN
FETCH orders_cursor INTO @order_id, @order_date, @order_total;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE(isnull(order_id, '') + ' ' + isnull(order_date, '') + ' ' + isnull(order_total, ''));
END;
CLOSE orders_cursor;
END; | BEGIN
CREATE PROCEDURE get_customer_orders(cust_id IN NUMBER)
AS
orders_cursor SYS_REFCURSOR;
BEGIN
OPEN orders_cursor FOR
SELECT order_id, order_date, order_total
FROM orders
WHERE customer_id = cust_id;
RETURN orders_cursor;
END;
END;
DECLARE
orders_cursor SYS_REFCURSOR;
customer_id NUMBER := 12345;
BEGIN
orders_cursor := get_customer_orders(customer_id);
LOOP
FETCH orders_cursor INTO order_id, order_date, order_total;
EXIT WHEN orders_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(order_id || ' ' || order_date || ' ' || order_total);
END LOOP;
CLOSE orders_cursor;
END; |
CREATE TABLE orders (
order_id BIGINT PRIMARY KEY,
customer_id BIGINT NOT NULL,
item_id BIGINT NOT NULL,
quantity BIGINT NOT NULL,
order_date DATE NOT NULL,
ship_date DATE,
status VARCHAR(20) NOT NULL
);
CREATE INDEX idx_orders_customer_id ON orders (customer_id);
CREATE INDEX idx_orders_item_id ON orders (item_id);
CREATE SEQUENCE orders_seq;
IF OBJECT_ID('get_order_total', 'FN') IS NOT NULL
DROP FUNCTION get_order_total;
GO
CREATE FUNCTION get_order_total(@order_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @total FLOAT;
SELECT @total = SUM(quantity * unit_price) FROM order_details WHERE @order_id = @order_id;
RETURN @total;
END;
IF OBJECT_ID('process_orders', 'P') IS NOT NULL
DROP PROCEDURE process_orders;
GO
CREATE PROCEDURE process_orders(@order_ids VARCHAR(4000)) AS
CURSOR order_cursor IS
SELECT order_id, customer_id
FROM orders
WHERE order_id IN (order_ids);
BEGIN
DECLARE order_rec CURSOR FOR order_cursor LOOP
BEGIN
-- Pr... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
-- Ha... SQLINES DEMO ***
END;
FETCH order_rec INTO;
END;
CLOSE order_rec;
DEALLOCATE order_rec;
END;
|
CREATE TABLE orders (
order_id NUMBER(10) PRIMARY KEY,
customer_id NUMBER(10) NOT NULL,
item_id NUMBER(10) NOT NULL,
quantity NUMBER(10) NOT NULL,
order_date DATE NOT NULL,
ship_date DATE,
status VARCHAR2(20) NOT NULL
);
CREATE INDEX idx_orders_customer_id ON orders (customer_id);
CREATE INDEX idx_orders_item_id ON orders (item_id);
CREATE SEQUENCE orders_seq;
CREATE OR REPLACE FUNCTION get_order_total(order_id NUMBER) RETURN NUMBER IS
total NUMBER;
BEGIN
SELECT SUM(quantity * unit_price) INTO total FROM order_details WHERE order_id = order_id;
RETURN total;
END;
CREATE OR REPLACE PROCEDURE process_orders(order_ids VARCHAR2) IS
CURSOR order_cursor IS
SELECT order_id, customer_id
FROM orders
WHERE order_id IN (order_ids);
BEGIN
FOR order_rec IN order_cursor LOOP
BEGIN
-- Process order
EXCEPTION
WHEN OTHERS THEN
-- Handle error
END;
END LOOP;
END;
|
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE get_customer_orders;
GO
CREATE PROCEDURE get_customer_orders(@customer_id IN AS NUMBER)
AS
CURSOR c_orders IS
SELECT order_date, order_total
FROM orders
WHERE customer_id = customer_id;
BEGIN
DECLARE rec CURSOR FOR c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order date: ' + ISNULL(CONVERT(VARCHAR(10), @rec_order_date, 101), '') +
', Order total: ' + isnull(@rec_order_total, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
|
CREATE OR REPLACE PROCEDURE get_customer_orders(customer_id IN NUMBER)
AS
CURSOR c_orders IS
SELECT order_date, order_total
FROM orders
WHERE customer_id = customer_id;
BEGIN
FOR rec IN c_orders LOOP
DBMS_OUTPUT.PUT_LINE('Order date: ' || TO_CHAR(rec.order_date, 'MM/DD/YYYY') ||
', Order total: ' || rec.order_total);
END LOOP;
END;
|
IF OBJECT_ID('calculate_sales_total', 'P') IS NOT NULL
DROP PROCEDURE calculate_sales_total;
GO
CREATE PROCEDURE calculate_sales_total(
@p_order_id IN AS NUMBER,
p_total OUT NUMBER
)
IS
l_line_total NUMBER;
BEGIN
SELECT @p_total = SUM(line_total)
FROM order_lines
WHERE order_id = p_order_id;
IF p_total IS NULL BEGIN
set @p_total = 0;
END
END;
|
CREATE OR REPLACE PROCEDURE calculate_sales_total(
p_order_id IN NUMBER,
p_total OUT NUMBER
)
IS
l_line_total NUMBER;
BEGIN
SELECT SUM(line_total)
INTO p_total
FROM order_lines
WHERE order_id = p_order_id;
IF p_total IS NULL THEN
p_total := 0;
END IF;
END;
|
BEGIN
DECLARE @v_id FLOAT;
BEGIN
SELECT @v_id = id FROM table_name WHERE name = 'John';
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_id = -1;
END
END;
END;
CREATE FUNCTION get_average_salary(@department_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
v_average_salary NUMBER;
BEGIN
SELECT @v_average_salary = AVG(salary) FROM employee_table WHERE department_id = department_id;
RETURN v_average_salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN -1;
END
END;
DECLARE
@v_name VARCHAR(20);
BEGIN
DECLARE @i INT = 1 ;
WHILE @i <= 10 BEGIN
SET @v_name = 'John' + isnull(i, '');
INSERT INTO table_name (name) VALUES (@v_name);
END;
END;
BEGIN
BEGIN
RAISERROR('-20001 An error occurred', 16, 1);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END; | BEGIN
DECLARE v_id NUMBER;
BEGIN
SELECT id INTO v_id FROM table_name WHERE name = 'John';
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_id := -1;
END;
END;
CREATE FUNCTION get_average_salary(department_id IN NUMBER) RETURN NUMBER IS
v_average_salary NUMBER;
BEGIN
SELECT AVG(salary) INTO v_average_salary FROM employee_table WHERE department_id = department_id;
RETURN v_average_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN -1;
END;
DECLARE
v_name VARCHAR2(20);
BEGIN
FOR i IN 1 .. 10 LOOP
v_name := 'John' || i;
INSERT INTO table_name (name) VALUES (v_name);
END LOOP;
END;
BEGIN
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'An error occurred');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred');
END;
END; |
DECLARE
@numEmps FLOAT;
BEGIN
SELECT @numEmps = COUNT(*) FROM employees;
IF @numEmps > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employees found.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found.');
RAISERROR('-20001 No employees found.', 16, 1);
END
END;
|
DECLARE
numEmps NUMBER;
BEGIN
SELECT COUNT(*) INTO numEmps FROM employees;
IF numEmps > 0 THEN
DBMS_OUTPUT.PUT_LINE('Employees found.');
ELSE
DBMS_OUTPUT.PUT_LINE('No employees found.');
RAISE_APPLICATION_ERROR(-20001, 'No employees found.');
END IF;
END;
|
CREATE PROCEDURE create_employee(
@first_name IN AS VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER
) IS
-- De... SQLINES DEMO ***
emp_id NUMBER;
BEGIN
*** oyee into the database
INSERT INTO employees (first_name, last_name, salary)
VALUES (first_name, last_name, salary);
SET @emp_id = SCOPE_IDENTITY();
-- Co... SQLINES DEMO ***
COMMIT;
*** ee ID
RETURN emp_id;
EXCEPTION
-- Ha... SQLINES DEMO ***
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
*** rocedure
set @emp_id = dbo.create_employee('John', 'Doe', 50000);
*** yee ID
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(emp_id, ''));
END;
|
PROCEDURE create_employee(
first_name IN VARCHAR2,
last_name IN VARCHAR2,
salary IN NUMBER
) IS
-- Declare variables
emp_id NUMBER;
BEGIN
-- Insert a new employee into the database
INSERT INTO employees (first_name, last_name, salary)
VALUES (first_name, last_name, salary)
RETURNING employee_id INTO emp_id;
-- Commit the changes
COMMIT;
-- Return the employee ID
RETURN emp_id;
EXCEPTION
-- Handle any errors
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
BEGIN
-- Call the stored procedure
emp_id := create_employee('John', 'Doe', 50000);
-- Display the employee ID
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
END;
|
BEGIN
DECLARE
@salary FLOAT := 5000;
BEGIN
IF @salary > 6000 BEGIN
dbo.RAISE_SALARY(@salary);
END
IF @@FETCH_STATUS <> 0 BEGIN
NULL;
END
END;
COMMIT;
END; | BEGIN
DECLARE
salary NUMBER := 5000;
BEGIN
IF salary > 6000 THEN
RAISE_SALARY(salary);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
COMMIT;
END; |
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE )
RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER)
RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = emp_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
|
DECLARE
@emp_name VARCHAR(50);
BEGIN
SELECT @emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = 10;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
DECLARE
emp_name VARCHAR2(50);
BEGIN
SELECT first_name || ' ' || last_name
INTO emp_name
FROM employees
WHERE employee_id = 10;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
DECLARE
-- De... SQLINES DEMO ***
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
v_emp_salary NUMBER;
v_bonus ROW_NUMBER(10,2);
BEGIN
*** information
SELECT @v_emp_id = employee_id, @v_emp_name = employee_name, @v_emp_salary = salary
FROM employees
WHERE employee_id = 100;
-- Ca... SQLINES DEMO ***
SET @v_bonus = v_emp_salary * 0.1;
*** onus
UPDATE employees
SET bonus = v_bonus
WHERE employee_id = @v_emp_id;
-- Co... SQLINES DEMO ***
COMMIT;
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN others THEN
-- Ro... SQLINES DEMO ***
ROLLBACK;
-- Lo... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
-- Declare variables
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_emp_salary NUMBER;
v_bonus NUMBER(10,2);
BEGIN
-- Retrieve employee information
SELECT employee_id, employee_name, salary
INTO v_emp_id, v_emp_name, v_emp_salary
FROM employees
WHERE employee_id = 100;
-- Calculate bonus
v_bonus := v_emp_salary * 0.1;
-- Update employee bonus
UPDATE employees
SET bonus = v_bonus
WHERE employee_id = v_emp_id;
-- Commit changes
COMMIT;
-- Handle errors
EXCEPTION
WHEN others THEN
-- Rollback changes
ROLLBACK;
-- Log error
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_sal float;
BEGIN
SELECT @v_sal = salary FROM employees WHERE employee_id = 10;
IF @v_sal IS NULL BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE employees SET salary = @v_sal * 1.10 WHERE employee_id = 10;
END;
|
DECLARE
v_sal number;
BEGIN
SELECT salary INTO v_sal FROM employees WHERE employee_id = 10;
IF v_sal IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE employees SET salary = v_sal * 1.10 WHERE employee_id = 10;
END;
|
DECLARE
@l_empno FLOAT;
l_salary NUMBER;
BEGIN
SELECT @l_empno = empno, @l_salary = salary FROM emp WHERE empno = 10;
IF l_salary > 10000 BEGIN
RAISERROR('-20001 Salary too high for employee ' + isnull(@l_empno, ''), 16, 1);
END
UPDATE emp SET salary = l_salary + 500 WHERE empno = @l_empno;
COMMIT;
END;
|
DECLARE
l_empno NUMBER;
l_salary NUMBER;
BEGIN
SELECT empno, salary INTO l_empno, l_salary FROM emp WHERE empno = 10;
IF l_salary > 10000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high for employee ' || l_empno);
END IF;
UPDATE emp SET salary = l_salary + 500 WHERE empno = l_empno;
COMMIT;
END;
|
DECLARE
v_cursor CURSOR FOR SELECT * FROM employees;
BEGIN
DECLARE v_emp CURSOR FOR v_cursor OPEN v_emp;
FETCH v_emp INTO;
WHILE @@FETCH_STATUS=0
BEGIN
IF @v_emp_salary > 10000 BEGIN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = @v_emp_employee_id;
END
ELSE IF @v_emp_salary > 5000 BEGIN
UPDATE employees SET salary = salary * 1.05 WHERE employee_id = @v_emp_employee_id;
END
ELSE BEGIN
UPDATE employees SET salary = salary * 1.02 WHERE employee_id = @v_emp_employee_id;
END
FETCH v_emp INTO;
END;
CLOSE v_emp;
DEALLOCATE v_emp;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_cursor CURSOR FOR SELECT * FROM employees;
BEGIN
FOR v_emp IN v_cursor LOOP
IF v_emp.salary > 10000 THEN
UPDATE employees SET salary = salary * 1.10 WHERE employee_id = v_emp.employee_id;
ELSIF v_emp.salary > 5000 THEN
UPDATE employees SET salary = salary * 1.05 WHERE employee_id = v_emp.employee_id;
ELSE
UPDATE employees SET salary = salary * 1.02 WHERE employee_id = v_emp.employee_id;
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_cursor SYS_REFCURSOR;
BEGIN
SELECT * FROM emp;
WHILE 1=1 BEGIN
FETCH v_cursor INTO @v_empno, @v_ename, @v_sal;
IF @@FETCH_STATUS <> 0 BREAK;
DBMS_OUTPUT.PUT_LINE('Empno: ' + isnull(v_empno, '') + ', Ename: ' + isnull(v_ename, '') + ', Salary:' + isnull(v_sal, ''));
END;
CLOSE v_cursor;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found.');
END
END; | DECLARE
v_cursor SYS_REFCURSOR;
BEGIN
OPEN v_cursor FOR
SELECT * FROM emp;
LOOP
FETCH v_cursor INTO v_empno, v_ename, v_sal;
EXIT WHEN v_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Empno: ' || v_empno || ', Ename: ' || v_ename || ', Salary:' || v_sal);
END LOOP;
CLOSE v_cursor;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found.');
END; |
DECLARE
@v_empno SMALLINT;
BEGIN
SELECT @v_empno = empno FROM emp WHERE ename = 'KING';
UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno;
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER(4);
BEGIN
SELECT empno INTO v_empno FROM emp WHERE ename = 'KING';
UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
|
*** rocedure to insert a new employee
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL
DROP PROCEDURE insert_employee;
GO
CREATE PROCEDURE insert_employee(
@last_name IN AS VARCHAR2,
first_name IN VARCHAR2,
email IN VARCHAR2,
salary IN NUMBER,
department_id IN NUMBER
) AS
BEGIN
*** into the employees table
INSERT INTO employees (last_name, first_name, email, salary, department_id)
VALUES (last_name, first_name, email, salary, department_id);
-- Co... SQLINES DEMO ***
COMMIT;
END;
*** to calculate the total salary for a department
IF OBJECT_ID('get_department_salary', 'FN') IS NOT NULL
DROP FUNCTION get_department_salary;
GO
CREATE FUNCTION get_department_salary(
@department_id IN NUMBER
DECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; AS
BEGIN
*** al salary for the department
SELECT SUM(salary)
FROM employees
WHERE department_id = department_id;
END;
*** tement to select all employees
SELECT *
FROM employees;
*** w construct to conditionally execute a block of code
IF salary > 100000 BEGIN
*** he salary is greater than 100,000
UPDATE employees
SET bonus = 10000
WHERE salary > 100000;
END
*** cenario
BEGIN
*** block of code
*** s, the exception is handled and the error message is displayed
BEGIN
-- Do... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
END;
|
-- Create a stored procedure to insert a new employee
CREATE OR REPLACE PROCEDURE insert_employee(
last_name IN VARCHAR2,
first_name IN VARCHAR2,
email IN VARCHAR2,
salary IN NUMBER,
department_id IN NUMBER
) AS
BEGIN
-- Insert a new row into the employees table
INSERT INTO employees (last_name, first_name, email, salary, department_id)
VALUES (last_name, first_name, email, salary, department_id);
-- Commit the changes
COMMIT;
END;
-- Create a function to calculate the total salary for a department
CREATE OR REPLACE FUNCTION get_department_salary(
department_id IN NUMBER
) RETURN NUMBER AS
BEGIN
-- Calculate the total salary for the department
SELECT SUM(salary)
FROM employees
WHERE department_id = department_id;
END;
-- Execute a SQL statement to select all employees
SELECT *
FROM employees;
-- Use a control flow construct to conditionally execute a block of code
IF salary > 100000 THEN
-- Do something if the salary is greater than 100,000
UPDATE employees
SET bonus = 10000
WHERE salary > 100000;
END IF;
-- Handle an error scenario
BEGIN
-- Try to execute a block of code
-- If an error occurs, the exception is handled and the error message is displayed
BEGIN
-- Do something
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END;
|
IF OBJECT_ID('sp_insert_customer', 'P') IS NOT NULL
DROP PROCEDURE sp_insert_customer;
GO
CREATE PROCEDURE sp_insert_customer(
@p_name IN AS VARCHAR2,
p_address IN VARCHAR2,
p_phone IN VARCHAR2)
IS
v_customer_id NUMBER;
BEGIN
INSERT INTO customers (name, address, phone)
VALUES (p_name, p_address, p_phone);
SET @v_customer_id = SCOPE_IDENTITY();
COMMIT;
DBMS_OUTPUT.PUT_LINE('Customer inserted with ID: ' + isnull(v_customer_id, ''));
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error inserting customer: ' + ISNULL(SQLERRM, ''));
ROLLBACK;
END;
|
CREATE OR REPLACE PROCEDURE sp_insert_customer(
p_name IN VARCHAR2,
p_address IN VARCHAR2,
p_phone IN VARCHAR2)
IS
v_customer_id NUMBER;
BEGIN
INSERT INTO customers (name, address, phone)
VALUES (p_name, p_address, p_phone)
RETURNING customer_id INTO v_customer_id;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Customer inserted with ID: ' || v_customer_id);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Error inserting customer: ' || SQLERRM);
ROLLBACK;
END;
|
CREATE FUNCTION get_order_total(@order_id FLOAT) RETURNS FLOAT AS
BEGIN
DECLARE @order_total FLOAT;
SELECT @order_total = SUM(quantity * unit_price) FROM order_items WHERE @order_id = @order_id;
RETURN @order_total;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
CREATE PROCEDURE update_order_status(@order_id FLOAT, @new_status VARCHAR(4000)) AS
BEGIN
SET NOCOUNT ON;
UPDATE orders SET status = @new_status WHERE @order_id = @order_id;
END;
DECLARE
@order_id FLOAT := 12345;
order_total NUMBER;
BEGIN
SET @order_total = dbo.get_order_total(@order_id);
IF order_total > 100 BEGIN
dbo.update_order_status(@order_id, 'Shipped');
END
ELSE IF order_total > 50 BEGIN
dbo.update_order_status(@order_id, 'Processing');
END
ELSE BEGIN
dbo.update_order_status(@order_id, 'Pending');
END
END;
|
CREATE FUNCTION get_order_total(order_id NUMBER) RETURN NUMBER IS
order_total NUMBER;
BEGIN
SELECT SUM(quantity * unit_price) INTO order_total FROM order_items WHERE order_id = order_id;
RETURN order_total;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE PROCEDURE update_order_status(order_id NUMBER, new_status VARCHAR2) IS
BEGIN
UPDATE orders SET status = new_status WHERE order_id = order_id;
END;
DECLARE
order_id NUMBER := 12345;
order_total NUMBER;
BEGIN
order_total := get_order_total(order_id);
IF order_total > 100 THEN
update_order_status(order_id, 'Shipped');
ELSIF order_total > 50 THEN
update_order_status(order_id, 'Processing');
ELSE
update_order_status(order_id, 'Pending');
END IF;
END;
|
CREATE TABLE employees(
emp_id FLOAT IDENTITY() PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary FLOAT NOT NULL
);
CREATE PROCEDURE give_raise(@emp_id FLOAT, @raise_amount FLOAT)
AS
BEGIN
SET NOCOUNT ON;
UPDATE employees
SET salary = salary + @raise_amount
WHERE @emp_id = @emp_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
DECLARE
@v_emp_id FLOAT;
v_raise_amount NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_raise_amount = salary
FROM employees
WHERE name = 'John Doe';
dbo.give_raise(@v_emp_id, v_raise_amount * 0.1);
END;
|
CREATE TABLE employees(
emp_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
salary NUMBER NOT NULL
);
CREATE PROCEDURE give_raise(emp_id NUMBER, raise_amount NUMBER)
AS
BEGIN
UPDATE employees
SET salary = salary + raise_amount
WHERE emp_id = emp_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END;
DECLARE
v_emp_id NUMBER;
v_raise_amount NUMBER;
BEGIN
SELECT emp_id, salary
INTO v_emp_id, v_raise_amount
FROM employees
WHERE name = 'John Doe';
give_raise(v_emp_id, v_raise_amount * 0.1);
END;
|
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20),
salary DECIMAL(8,2),
dept_id SMALLINT REFERENCES departments(dept_id)
);
CREATE TABLE departments (
dept_id SMALLINT PRIMARY KEY,
dept_name VARCHAR(20)
);
CREATE TYPE address_type AS OBJECT (
street_address VARCHAR2(30),
city dbo.VARCHAR2(20),
state dbo.VARCHAR2(2),
zip_code dbo.VARCHAR2(10)
);
CREATE TABLE addresses (
address_id INT PRIMARY KEY,
address_type address_type,
emp_id INT REFERENCES employees(emp_id)
);
CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
emp_salary ROW_NUMBER(8,2);
BEGIN
SELECT @emp_salary = salary FROM employees WHERE emp_id = emp_id;
RETURN emp_salary;
END;
CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE emp_id = emp_id;
END;
BEGIN
DECLARE
@emp_id INT;
emp_salary ROW_NUMBER(8,2);
employee_address address_type;
BEGIN
SELECT @emp_id = @emp_id, @first_name = first_name, @last_name = last_name, @emp_salary = salary
FROM employees WHERE last_name = 'Smith';
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('===================');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(last_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(emp_salary, ''));
SELECT @employee_address = address_type FROM addresses WHERE @emp_id = @emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Address:');
DBMS_OUTPUT.PUT_LINE('=================');
DBMS_OUTPUT.PUT_LINE('Street Address: ' + isnull(employee_address.street_address, ''));
DBMS_OUTPUT.PUT_LINE('City: ' + isnull(employee_address.city, ''));
DBMS_OUTPUT.PUT_LINE('State: ' + isnull(employee_address.state, ''));
DBMS_OUTPUT.PUT_LINE('Zip Code: ' + isnull(employee_address.zip_code, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee with the last name Smith was found.');
END
END;
END;
|
CREATE TABLE employees (
emp_id NUMBER(6) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
salary NUMBER(8,2),
dept_id NUMBER(2) REFERENCES departments(dept_id)
);
CREATE TABLE departments (
dept_id NUMBER(2) PRIMARY KEY,
dept_name VARCHAR2(20)
);
CREATE TYPE address_type AS OBJECT (
street_address VARCHAR2(30),
city VARCHAR2(20),
state VARCHAR2(2),
zip_code VARCHAR2(10)
);
CREATE TABLE addresses (
address_id NUMBER(6) PRIMARY KEY,
address_type address_type,
emp_id NUMBER(6) REFERENCES employees(emp_id)
);
CREATE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS
emp_salary NUMBER(8,2);
BEGIN
SELECT salary INTO emp_salary FROM employees WHERE emp_id = emp_id;
RETURN emp_salary;
END;
CREATE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE emp_id = emp_id;
END;
BEGIN
DECLARE
emp_id NUMBER(6);
emp_salary NUMBER(8,2);
employee_address address_type;
BEGIN
SELECT emp_id, first_name, last_name, salary INTO emp_id, first_name, last_name, emp_salary
FROM employees WHERE last_name = 'Smith';
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('===================');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || emp_salary);
SELECT address_type INTO employee_address FROM addresses WHERE emp_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Address:');
DBMS_OUTPUT.PUT_LINE('=================');
DBMS_OUTPUT.PUT_LINE('Street Address: ' || employee_address.street_address);
DBMS_OUTPUT.PUT_LINE('City: ' || employee_address.city);
DBMS_OUTPUT.PUT_LINE('State: ' || employee_address.state);
DBMS_OUTPUT.PUT_LINE('Zip Code: ' || employee_address.zip_code);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with the last name Smith was found.');
END;
END;
|
declare
@i smallint;
j row_number(2);
begin
declare @i int = 1;
while @i <= 10 begin
declare @j int = 1;
while @j <= 10 begin
dbms_output.put_line(isnull(@i, '') + ' * ' + isnull(j, '') + ' = ' + isnull(@i * j, ''));
end;
end;
exception
when others then
dbms_output.put_line('An error occurred.');
end;
/ | declare
i number(2);
j number(2);
begin
for i in 1..10 loop
for j in 1..10 loop
dbms_output.put_line(i || ' * ' || j || ' = ' || i * j);
end loop;
end loop;
exception
when others then
dbms_output.put_line('An error occurred.');
end;
/ |
DECLARE
@employee_name VARCHAR(50);
salary NUMBER;
BEGIN
SELECT @employee_name = @employee_name, @salary = salary
FROM employees
WHERE employee_id = 100;
IF salary >= 10000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus');
END
END;
|
DECLARE
employee_name VARCHAR2(50);
salary NUMBER;
BEGIN
SELECT employee_name, salary
INTO employee_name, salary
FROM employees
WHERE employee_id = 100;
IF salary >= 10000 THEN
DBMS_OUTPUT.PUT_LINE('Employee is eligible for bonus');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee is not eligible for bonus');
END IF;
END;
|
IF OBJECT_ID('sample_proc', 'P') IS NOT NULL
DROP PROCEDURE sample_proc;
GO
CREATE PROCEDURE sample_proc(
@param1 IN AS VARCHAR2,
param2 IN NUMBER,
param_out OUT VARCHAR2)
IS
-- Lo... SQLINES DEMO ***
var1 NUMBER;
var2 dbo.VARCHAR2(20);
BEGIN
-- As... SQLINES DEMO ***
set @var1 = param2 * 2;
-- SQ... SQLINES DEMO ***
SELECT @var2 = name
FROM employees
WHERE id = param1;
*** assignment
set @param_out = var2;
-- Er... SQLINES DEMO ***
IF var1 < 0 BEGIN
RAISERROR('-20000 Invalid parameter: ' + isnull(param2, ''), 16, 1);
END
-- Co... SQLINES DEMO ***
IF var1 = 0 BEGIN
set @param_out = 'Even'; END
ELSE IF var1 = 1 BEGIN
set @param_out = 'Odd'; END
ELSE
set @param_out = 'Unknown'; END
END;
|
CREATE OR REPLACE PROCEDURE sample_proc(
param1 IN VARCHAR2,
param2 IN NUMBER,
param_out OUT VARCHAR2)
IS
-- Local variables
var1 NUMBER;
var2 VARCHAR2(20);
BEGIN
-- Assignment
var1 := param2 * 2;
-- SQL statement
SELECT name INTO var2
FROM employees
WHERE id = param1;
-- Output parameter assignment
param_out := var2;
-- Error handling
IF var1 < 0 THEN
RAISE_APPLICATION_ERROR(-20000, 'Invalid parameter: ' || param2);
END IF;
-- Control flow
CASE var1
WHEN 0 THEN
param_out := 'Even';
WHEN 1 THEN
param_out := 'Odd';
ELSE
param_out := 'Unknown';
END CASE;
END;
|
IF OBJECT_ID('get_customer_orders', 'FN') IS NOT NULL
DROP FUNCTION get_customer_orders;
GO
CREATE FUNCTION get_customer_orders(@customer_id IN NUMBERDECLARE ) RETURN
BEGIN SYS_REFCURSOR
RETURN NULL;
END;
AS
orders_ref CURSOR;
BEGIN
SELECT order_id, order_date FROM orders WHERE customer_id = customer_id;
RETURN orders_ref;
END;
BEGIN
DECLARE rec CURSOR FOR SELECT customer_id FROM customers; OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE order_record CURSOR FOR SELECT * FROM TABLE(dbo.get_customer_orders(@rec_customer_id)); OPEN order_record;
FETCH order_record INTO;
WHILE @@FETCH_STATUS=0
BEGIN
*** the order record
FETCH order_record INTO;
END;
CLOSE order_record;
DEALLOCATE order_record;
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
IF @@FETCH_STATUS <> 0 BEGIN
END
*** und error
WHEN OTHERS THEN
-- Ha... SQLINES DEMO ***
END;
|
CREATE OR REPLACE FUNCTION get_customer_orders(customer_id IN NUMBER) RETURN SYS_REFCURSOR
AS
orders_ref CURSOR;
BEGIN
OPEN orders_ref FOR
SELECT order_id, order_date FROM orders WHERE customer_id = customer_id;
RETURN orders_ref;
END;
BEGIN
FOR rec IN (SELECT customer_id FROM customers) LOOP
FOR order_record IN (SELECT * FROM TABLE(get_customer_orders(rec.customer_id))) LOOP
-- Do something with the order record
END LOOP;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Handle no data found error
WHEN OTHERS THEN
-- Handle other errors
END;
|
BEGIN
-- De... SQLINES DEMO ***
v_emp_id NUMBER;
v_first_name dbo.VARCHAR2(50);
v_last_name dbo.VARCHAR2(50);
v_salary NUMBER;
v_error_flag SET @BOOLEAN = FALSE;
-- Ge... SQLINES DEMO ***
SELECT @v_emp_id = emp_id, @v_first_name = first_name, @v_last_name = last_name, @v_salary = salary
FROM employees
WHERE emp_id = 10;
-- Ch... SQLINES DEMO ***
IF v_emp_id IS NULL BEGIN
set @v_error_flag = TRUE;
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
-- Pr... SQLINES DEMO ***
IF NOT v_error_flag BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('------------------');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(v_emp_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(v_first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(v_last_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(v_salary, ''));
END
END;
/ | BEGIN
-- Declare variables
v_emp_id NUMBER;
v_first_name VARCHAR2(50);
v_last_name VARCHAR2(50);
v_salary NUMBER;
v_error_flag BOOLEAN := FALSE;
-- Get employee data
SELECT emp_id, first_name, last_name, salary
INTO v_emp_id, v_first_name, v_last_name, v_salary
FROM employees
WHERE emp_id = 10;
-- Check for errors
IF v_emp_id IS NULL THEN
v_error_flag := TRUE;
DBMS_OUTPUT.PUT_LINE('Employee not found');
END IF;
-- Print employee data
IF NOT v_error_flag THEN
DBMS_OUTPUT.PUT_LINE('Employee Details:');
DBMS_OUTPUT.PUT_LINE('------------------');
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || v_first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || v_last_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
END IF;
END;
/ |
IF OBJECT_ID('sp_example', 'P') IS NOT NULL
DROP PROCEDURE sp_example;
GO
CREATE PROCEDURE sp_example(@id IN AS NUMBER) AS
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = id;
COMMIT;
END;
DECLARE
@v_salary FLOAT;
v_emp_name dbo.VARCHAR2(20);
BEGIN
SELECT @v_salary = salary, @v_emp_name = isnull(first_name, '') + ' ' + isnull(last_name, '')
FROM employees
WHERE employee_id = 1;
IF @v_salary IS NULL BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + ISNULL(CONVERT(VARCHAR, @v_salary), ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found for the specified employee ID');
END
END;
SELECT *;
|
CREATE OR REPLACE PROCEDURE sp_example(id IN NUMBER) AS
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE employee_id = id;
COMMIT;
END;
DECLARE
v_salary NUMBER;
v_emp_name VARCHAR2(20);
BEGIN
SELECT salary, first_name || ' ' || last_name INTO v_salary, v_emp_name
FROM employees
WHERE employee_id = 1;
IF v_salary IS NULL THEN
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
DBMS_OUTPUT.PUT_LINE('Salary: ' || TO_CHAR(v_salary));
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for the specified employee ID');
END;
SELECT * FROM dual;
|
sql
IF OBJECT_ID('sp_update_customer', 'P') IS NOT NULL
DROP PROCEDURE sp_update_customer;
GO
CREATE PROCEDURE sp_update_customer(
@in_customer_id IN AS NUMBER,
in_name IN VARCHAR2,
in_address IN VARCHAR2,
in_email IN VARCHAR2
)
BEGIN
*** er record
UPDATE customers
SET name = in_name,
address = in_address,
email = in_email
WHERE customer_id = in_customer_id;
-- Co... SQLINES DEMO ***
COMMIT;
END;
BEGIN
*** rocedure
dbo.sp_update_customer(10, 'John Doe', '123 Main Street', 'john.doe@example.com');
-- Ha... SQLINES DEMO ***
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
| sql
CREATE OR REPLACE PROCEDURE sp_update_customer(
in_customer_id IN NUMBER,
in_name IN VARCHAR2,
in_address IN VARCHAR2,
in_email IN VARCHAR2
)
BEGIN
-- Update the customer record
UPDATE customers
SET name = in_name,
address = in_address,
email = in_email
WHERE customer_id = in_customer_id;
-- Commit the changes
COMMIT;
END;
BEGIN
-- Call the stored procedure
sp_update_customer(10, 'John Doe', '123 Main Street', 'john.doe@example.com');
-- Handle an error
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
|
CREATE PROCEDURE emp_bonus(@emp_id IN AS NUMBER, bonus IN OUT NUMBER) AS
BEGIN
SELECT @bonus = bonus
FROM emp
WHERE emp_id = emp_id;
IF bonus IS NOT NULL BEGIN
UPDATE emp
SET bonus = bonus * 1.10
WHERE emp_id = emp_id;
END
ELSE BEGIN
RAISERROR('-20000 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE @bonus FLOAT;
dbo.emp_bonus(100, @bonus);
DBMS_OUTPUT.PUT_LINE('Bonus: ' + isnull(@bonus, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
|
CREATE PROCEDURE emp_bonus(emp_id IN NUMBER, bonus IN OUT NUMBER) AS
BEGIN
SELECT bonus INTO bonus
FROM emp
WHERE emp_id = emp_id;
IF bonus IS NOT NULL THEN
UPDATE emp
SET bonus = bonus * 1.10
WHERE emp_id = emp_id;
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Employee not found');
END IF;
END;
BEGIN
DECLARE bonus NUMBER;
emp_bonus(100, bonus);
DBMS_OUTPUT.PUT_LINE('Bonus: ' || bonus);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
|
IF OBJECT_ID('get_customer_orders', 'P') IS NOT NULL
DROP PROCEDURE get_customer_orders;
GO
CREATE PROCEDURE get_customer_orders (
@cust_id IN AS NUMBER
) AS
BEGIN
DECLARE rec CURSOR FOR
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = cust_id;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@rec_order_id, '') + ' | ' + isnull(@rec_order_date, '') + ' | ' + isnull(@rec_total_amount, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
/ | CREATE OR REPLACE PROCEDURE get_customer_orders (
cust_id IN NUMBER
) AS
BEGIN
FOR rec IN (
SELECT order_id, order_date, total_amount
FROM orders
WHERE customer_id = cust_id
) LOOP
DBMS_OUTPUT.PUT_LINE(rec.order_id || ' | ' || rec.order_date || ' | ' || rec.total_amount);
END LOOP;
END;
/ |
DECLARE
@v_emp_name VARCHAR(100);
v_emp_salary NUMBER;
BEGIN
SELECT @v_emp_name = ename, @v_emp_salary = sal FROM emp WHERE empno = 7369;
IF v_emp_salary > 20000 BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_emp_name, '') + ' has a high salary of ' + isnull(v_emp_salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_emp_name, '') + ' has a low salary of ' + isnull(v_emp_salary, ''));
END
IF @@fetch_status <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employee found with that employee number.');
END
END;
/
|
DECLARE
v_emp_name VARCHAR2(100);
v_emp_salary NUMBER;
BEGIN
SELECT ename, sal INTO v_emp_name, v_emp_salary FROM emp WHERE empno = 7369;
IF v_emp_salary > 20000 THEN
DBMS_OUTPUT.PUT_LINE(v_emp_name || ' has a high salary of ' || v_emp_salary);
ELSE
DBMS_OUTPUT.PUT_LINE(v_emp_name || ' has a low salary of ' || v_emp_salary);
END IF;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('No employee found with that employee number.');
END;
/
|
BEGIN
DECLARE @num_rows FLOAT;
DECLARE @total_sales FLOAT;
BEGIN
*** rows in the table
SELECT @num_rows = COUNT(*) FROM sales;
*** return 0
IF @num_rows = 0 BEGIN
RETURN 0;
END
-- Ge... SQLINES DEMO ***
SELECT @total_sales = SUM(sales) FROM sales;
*** sales
RETURN @total_sales;
END;
END; | BEGIN
DECLARE num_rows NUMBER;
DECLARE total_sales NUMBER;
BEGIN
-- Get the number of rows in the table
SELECT COUNT(*) INTO num_rows FROM sales;
-- If no rows exist, return 0
IF num_rows = 0 THEN
RETURN 0;
END IF;
-- Get the total sales
SELECT SUM(sales) INTO total_sales FROM sales;
-- Return the total sales
RETURN total_sales;
END;
END; |
CREATE PROCEDURE GetEmployeeInfo (
@EmpID IN AS NUMBER, OUT FirstName VARCHAR(20), OUT LastName VARCHAR(20), OUT Salary OUT NUMBER
) AS
BEGIN
SELECT @FirstName = FirstName, @LastName = LastName, @Salary = Salary
FROM Employees
WHERE EmployeeId = EmpID;
IF SQL%NOTFOUND BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
DECLARE
@EmpID FLOAT := 100;
FirstName VARCHAR(20);
LastName VARCHAR(20);
Salary NUMBER;
BEGIN
dbo.GetEmployeeInfo(@EmpID, FirstName, LastName, Salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('FirstName: ' + ISNULL(FirstName, ''));
DBMS_OUTPUT.PUT_LINE('LastName: ' + ISNULL(LastName, ''));
DBMS_OUTPUT.PUT_LINE('Salary: ' + Isnull(Salary, ''));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE PROCEDURE GetEmployeeInfo (
EmpID IN NUMBER, OUT FirstName VARCHAR(20), OUT LastName VARCHAR(20), OUT Salary OUT NUMBER
) AS
BEGIN
SELECT FirstName, LastName, Salary
INTO FirstName, LastName, Salary
FROM Employees
WHERE EmployeeId = EmpID;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
DECLARE
EmpID NUMBER := 100;
FirstName VARCHAR(20);
LastName VARCHAR(20);
Salary NUMBER;
BEGIN
GetEmployeeInfo(EmpID, FirstName, LastName, Salary);
DBMS_OUTPUT.PUT_LINE('Employee Information:');
DBMS_OUTPUT.PUT_LINE('FirstName: ' || FirstName);
DBMS_OUTPUT.PUT_LINE('LastName: ' || LastName);
DBMS_OUTPUT.PUT_LINE('Salary: ' || Salary);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
DECLARE
@v_count FLOAT;
BEGIN
SELECT @v_count = COUNT(*) FROM employees;
IF @v_count > 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employees table has ' + isnull(@v_count, '') + ' rows.');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employees table is empty.');
END
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found in employees table.');
END
END; | DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM employees;
IF v_count > 0 THEN
DBMS_OUTPUT.PUT_LINE('Employees table has ' || v_count || ' rows.');
ELSE
DBMS_OUTPUT.PUT_LINE('Employees table is empty.');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found in employees table.');
END; |
declare
@type cursor_type is ref cursor;
c cursor_type;
cursor employees is
select * from employees;
begin
select * from employees;
while 1=1 begin
fetch c into @emp_id, @emp_name;
if @@fetch_status <> 0 break;
dbms_output.put_line(isnull(emp_id, '') + ' ' + isnull(emp_name, ''));
end;
close c;
end;
/ | declare
type cursor_type is ref cursor;
c cursor_type;
cursor employees is
select * from employees;
begin
open c for select * from employees;
loop
fetch c into emp_id, emp_name;
exit when c%notfound;
dbms_output.put_line(emp_id || ' ' || emp_name);
end loop;
close c;
end;
/ |
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL
DROP FUNCTION get_employee_salary;
GO
CREATE FUNCTION get_employee_salary(@employee_id IN NUMBERDECLARE ) RETURN
BEGIN NUMBER
RETURN NULL;
END; IS
salary NUMBER;
BEGIN
SELECT @salary = salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
BEGIN
DECLARE
@employee_id FLOAT := 100;
old_salary NUMBER;
new_salary NUMBER;
BEGIN
SELECT @old_salary = salary FROM employees WHERE @employee_id = @employee_id;
SET @new_salary = old_salary * 1.1;
dbo.update_employee_salary(@employee_id, new_salary);
COMMIT;
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
END;
END;
|
CREATE OR REPLACE FUNCTION get_employee_salary(employee_id IN NUMBER) RETURN NUMBER IS
salary NUMBER;
BEGIN
SELECT salary INTO salary FROM employees WHERE employee_id = employee_id;
RETURN salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
CREATE OR REPLACE PROCEDURE update_employee_salary(employee_id IN NUMBER, new_salary IN NUMBER) IS
BEGIN
UPDATE employees SET salary = new_salary WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
BEGIN
DECLARE
employee_id NUMBER := 100;
old_salary NUMBER;
new_salary NUMBER;
BEGIN
SELECT salary INTO old_salary FROM employees WHERE employee_id = employee_id;
new_salary := old_salary * 1.1;
update_employee_salary(employee_id, new_salary);
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END;
END;
|
DECLARE
@emp_no FLOAT;
BEGIN
SET @emp_no = 101;
SELECT @name = name
FROM employees
WHERE emp_id = @emp_no;
IF name IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE('Employee not found');
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(name, ''));
END
END; | DECLARE
emp_no NUMBER;
BEGIN
emp_no := 101;
SELECT name
INTO name
FROM employees
WHERE emp_id = emp_no;
IF name IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Employee not found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee name: ' || name);
END IF;
END; |
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL
DROP PROCEDURE update_employee_salary;
GO
CREATE PROCEDURE update_employee_salary(
@p_employee_id IN AS NUMBER,
p_new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = p_new_salary
WHERE employee_id = p_employee_id;
COMMIT;
END;
DECLARE
@employee_id FLOAT;
new_salary NUMBER;
BEGIN
SELECT @employee_id = @employee_id, @new_salary = salary
FROM employees
WHERE last_name = 'Doe';
IF new_salary > 50000 BEGIN
RAISERROR('-20001 Salary too high', 16, 1);
END
CALL update_employee_salary(@employee_id, new_salary);
END;
|
CREATE OR REPLACE PROCEDURE update_employee_salary(
p_employee_id IN NUMBER,
p_new_salary IN NUMBER
)
AS
BEGIN
UPDATE employees SET salary = p_new_salary
WHERE employee_id = p_employee_id;
COMMIT;
END;
DECLARE
employee_id NUMBER;
new_salary NUMBER;
BEGIN
SELECT employee_id, salary
INTO employee_id, new_salary
FROM employees
WHERE last_name = 'Doe';
IF new_salary > 50000 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary too high');
END IF;
CALL update_employee_salary(employee_id, new_salary);
END;
|
CREATE PROCEDURE sp_insert_employee (
@emp_id IN AS NUMBER,
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
phone_number IN VARCHAR2,
salary IN NUMBER,
job_id IN VARCHAR2,
manager_id IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
employee_id,
first_name,
last_name,
email,
phone_number,
salary,
job_id,
manager_id
)
VALUES (
emp_id,
first_name,
last_name,
email,
phone_number,
salary,
job_id,
manager_id
);
EXCEPTION
WHEN dup_val THEN
raiserror('-20100 Duplicate value found.', 16, 1);
END;
DECLARE
@emp_count FLOAT;
BEGIN
SELECT @emp_count = COUNT(*) FROM employees;
IF @emp_count > 100 BEGIN
raiserror('-20200 Too many employees.', 16, 1);
END
END;
BEGIN
DECLARE rec CURSOR FOR
SELECT
*
FROM employees
WHERE
department_id = 20;
OPEN rec;
FETCH rec INTO;
WHILE @@FETCH_STATUS=0
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@rec_employee_id, ''));
DBMS_OUTPUT.PUT_LINE('First Name: ' + isnull(@rec_first_name, ''));
DBMS_OUTPUT.PUT_LINE('Last Name: ' + isnull(@rec_last_name, ''));
FETCH rec INTO;
END;
CLOSE rec;
DEALLOCATE rec;
END;
|
CREATE PROCEDURE sp_insert_employee (
emp_id IN NUMBER,
first_name IN VARCHAR2,
last_name IN VARCHAR2,
email IN VARCHAR2,
phone_number IN VARCHAR2,
salary IN NUMBER,
job_id IN VARCHAR2,
manager_id IN NUMBER
)
AS
BEGIN
INSERT INTO employees (
employee_id,
first_name,
last_name,
email,
phone_number,
salary,
job_id,
manager_id
)
VALUES (
emp_id,
first_name,
last_name,
email,
phone_number,
salary,
job_id,
manager_id
);
EXCEPTION
WHEN dup_val THEN
raise_application_error(-20100, 'Duplicate value found.');
END;
DECLARE
emp_count NUMBER;
BEGIN
SELECT COUNT(*) INTO emp_count FROM employees;
IF emp_count > 100 THEN
raise_application_error(-20200, 'Too many employees.');
END IF;
END;
BEGIN
FOR rec IN (
SELECT
*
FROM employees
WHERE
department_id = 20
) LOOP
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || rec.employee_id);
DBMS_OUTPUT.PUT_LINE('First Name: ' || rec.first_name);
DBMS_OUTPUT.PUT_LINE('Last Name: ' || rec.last_name);
END LOOP;
END;
|
DECLARE
@v_customer_id FLOAT;
v_customer_name dbo.VARCHAR2(50);
v_error_code NUMBER;
v_error_message dbo.VARCHAR2(200);
BEGIN
BEGIN
SELECT @v_customer_id = customer_id, @v_customer_name = customer_name
FROM customers
WHERE customer_id = 1;
IF @@FETCH_STATUS <> 0 BEGIN
SET @v_error_code = 100;
SET @v_error_message = 'Customer not found';
END
END;
IF v_error_code IS NULL BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(@v_customer_id, '') + ' ' + isnull(v_customer_name, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE(isnull(v_error_code, '') + ' ' + isnull(v_error_message, ''));
END
END;
|
DECLARE
v_customer_id NUMBER;
v_customer_name VARCHAR2(50);
v_error_code NUMBER;
v_error_message VARCHAR2(200);
BEGIN
BEGIN
SELECT customer_id, customer_name
INTO v_customer_id, v_customer_name
FROM customers
WHERE customer_id = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_error_code := 100;
v_error_message := 'Customer not found';
END;
IF v_error_code IS NULL THEN
DBMS_OUTPUT.PUT_LINE(v_customer_id || ' ' || v_customer_name);
ELSE
DBMS_OUTPUT.PUT_LINE(v_error_code || ' ' || v_error_message);
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
v_emp_name dbo.VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = 1;
IF v_salary > 50000 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' has a high salary: ' + isnull(v_salary, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(v_emp_name, '') + ' has a low salary: ' + isnull(v_salary, ''));
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_emp_id NUMBER;
v_emp_name VARCHAR2(50);
v_salary NUMBER;
BEGIN
SELECT emp_id, emp_name, salary INTO v_emp_id, v_emp_name, v_salary FROM employees WHERE emp_id = 1;
IF v_salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' has a high salary: ' || v_salary);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee ' || v_emp_name || ' has a low salary: ' || v_salary);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
CREATE PROCEDURE my_proc (@p_num IN AS NUMBER) IS
v_num NUMBER;
BEGIN
SELECT @v_num = num FROM my_table WHERE id = p_num;
IF v_num IS NULL BEGIN
RAISERROR('-20001 Record not found.', 16, 1);
END
DBMS_OUTPUT.PUT_LINE('Record found: ' + isnull(v_num, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No data found.');
END
END my_proc;
|
CREATE PROCEDURE my_proc (p_num IN NUMBER) IS
v_num NUMBER;
BEGIN
SELECT num INTO v_num FROM my_table WHERE id = p_num;
IF v_num IS NULL THEN
RAISE_APPLICATION_ERROR(-20001, 'Record not found.');
END IF;
DBMS_OUTPUT.PUT_LINE('Record found: ' || v_num);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found.');
END my_proc;
|
DECLARE
@v_empno SMALLINT;
v_salary ROW_NUMBER(8);
BEGIN
SELECT @v_empno = empno, @v_salary = salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 BEGIN
UPDATE emp SET salary = v_salary + 500 WHERE empno = @v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated for ' + isnull(@v_empno, ''));
END
ELSE BEGIN
DBMS_OUTPUT.PUT_LINE('No update required for ' + isnull(@v_empno, ''));
END
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
DECLARE
v_empno NUMBER(4);
v_salary NUMBER(8);
BEGIN
SELECT empno, salary INTO v_empno, v_salary FROM emp WHERE empno = 7839;
IF v_salary > 10000 THEN
UPDATE emp SET salary = v_salary + 500 WHERE empno = v_empno;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Salary updated for ' || v_empno);
ELSE
DBMS_OUTPUT.PUT_LINE('No update required for ' || v_empno);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
END;
|
object_id, is not
drop procedure example_proc;
go
create procedure example_proc as number, p_name varchar2 is
l_var number;
begin
example_table id p_id;
l_var 0 begin
raiserror, 16, 1;
end
else begin
example_table set name p_name id p_id;
end
end;
|
create or replace procedure example_proc (p_id in number, p_name in varchar2) is
l_var number;
begin
select count(*) into l_var from example_table where id = p_id;
if l_var = 0 then
raise_application_error(-20001, 'Record not found.');
else
update example_table set name = p_name where id = p_id;
end if;
end;
|
BEGIN
*** and cursors
DECLARE
@v_employee_id FLOAT;
v_employee_name dbo.VARCHAR2(100);
c_employees CURSOR FOR SELECT employee_id, employee_name FROM employees;
BEGIN
-- Op... SQLINES DEMO ***
OPEN c_employees;
WHILE 1=1 BEGIN
*** w from the cursor
FETCH c_employees INTO @v_employee_id, @v_employee_name;
*** there are no more rows
IF @@FETCH_STATUS <> 0 BREAK;
-- Pr... SQLINES DEMO ***
DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_employee_id, '') + ', Employee Name: ' + isnull(v_employee_name, ''));
END;
-- Cl... SQLINES DEMO ***
CLOSE c_employees;
*** that may occur
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END
END;
END;
|
BEGIN
-- Declare variables and cursors
DECLARE
v_employee_id NUMBER;
v_employee_name VARCHAR2(100);
c_employees CURSOR FOR SELECT employee_id, employee_name FROM employees;
BEGIN
-- Open the cursor
OPEN c_employees;
LOOP
-- Fetch the next row from the cursor
FETCH c_employees INTO v_employee_id, v_employee_name;
-- Exit the loop if there are no more rows
EXIT WHEN c_employees%NOTFOUND;
-- Process the row
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id || ', Employee Name: ' || v_employee_name);
END LOOP;
-- Close the cursor
CLOSE c_employees;
EXCEPTION
-- Handle any errors that may occur
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employees found.');
END;
END;
|
DECLARE
@v_empno FLOAT;
v_ename dbo.VARCHAR2(20);
BEGIN
SELECT @v_empno = empno, @v_ename = ename FROM emp WHERE empno = 7839;
IF @v_empno IS NOT NULL BEGIN
UPDATE emp SET ename = 'KING' WHERE empno = 7839;
COMMIT;
END
ELSE BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
|
DECLARE
v_empno NUMBER;
v_ename VARCHAR2(20);
BEGIN
SELECT empno, ename INTO v_empno, v_ename FROM emp WHERE empno = 7839;
IF v_empno IS NOT NULL THEN
UPDATE emp SET ename = 'KING' WHERE empno = 7839;
COMMIT;
ELSE
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
END;
|
DECLARE
@v_emp_id FLOAT;
BEGIN
SELECT @v_emp_id = emp_id FROM emp WHERE name = 'John';
IF @v_emp_id IS NULL BEGIN
ROLLBACK;
RAISERROR('-20001 Employee not found', 16, 1);
END
UPDATE emp SET salary = salary * 1.1 WHERE emp_id = @v_emp_id;
COMMIT;
END;
/
|
DECLARE
v_emp_id NUMBER;
BEGIN
SELECT emp_id INTO v_emp_id FROM emp WHERE name = 'John';
IF v_emp_id IS NULL THEN
ROLLBACK;
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END IF;
UPDATE emp SET salary = salary * 1.1 WHERE emp_id = v_emp_id;
COMMIT;
END;
/
|
CREATE TABLE orders (id FLOAT PRIMARY KEY, customer_id FLOAT, product_id FLOAT);
CREATE TABLE customers (id FLOAT PRIMARY KEY, name VARCHAR(100));
CREATE TABLE products (id FLOAT PRIMARY KEY, name VARCHAR(100));
CREATE PROCEDURE add_order(@customer_id FLOAT, @product_id FLOAT)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO orders (customer_id, product_id) VALUES (@customer_id, @product_id);
END;
CREATE FUNCTION get_customer_name(@customer_id FLOAT) RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @name VARCHAR(100);
SELECT @name = @name FROM customers WHERE id = @customer_id;
RETURN @name;
END;
DECLARE
@customer_id FLOAT;
customer_name VARCHAR(100);
product_id NUMBER;
BEGIN
SELECT @customer_id = id FROM customers WHERE name = 'John Doe';
SELECT @customer_name = name FROM get_customer_name(@customer_id);
SELECT @product_id = id FROM products WHERE name = 'Product A';
dbo.add_order(@customer_id, product_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, ''));
END;
|
CREATE TABLE orders (id NUMBER PRIMARY KEY, customer_id NUMBER, product_id NUMBER);
CREATE TABLE customers (id NUMBER PRIMARY KEY, name VARCHAR(100));
CREATE TABLE products (id NUMBER PRIMARY KEY, name VARCHAR(100));
CREATE PROCEDURE add_order(customer_id NUMBER, product_id NUMBER)
AS
BEGIN
INSERT INTO orders (customer_id, product_id) VALUES (customer_id, product_id);
END;
CREATE FUNCTION get_customer_name(customer_id NUMBER) RETURN VARCHAR(100)
AS
name VARCHAR(100);
BEGIN
SELECT name INTO name FROM customers WHERE id = customer_id;
RETURN name;
END;
DECLARE
customer_id NUMBER;
customer_name VARCHAR(100);
product_id NUMBER;
BEGIN
SELECT id INTO customer_id FROM customers WHERE name = 'John Doe';
SELECT name INTO customer_name FROM get_customer_name(customer_id);
SELECT id INTO product_id FROM products WHERE name = 'Product A';
add_order(customer_id, product_id);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
|
IF OBJECT_ID('example_proc', 'P') IS NOT NULL
DROP PROCEDURE example_proc;
GO
CREATE PROCEDURE example_proc (
@p_name IN AS VARCHAR2,
p_age IN NUMBER
) IS
v_msg dbo.VARCHAR2(200);
v_status NUMBER;
BEGIN
SELECT @v_msg = 'Name: ' + isnull(p_name, '') + ', Age: ' + isnull(p_age, '');
DBMS_OUTPUT.PUT_LINE(v_msg);
IF p_age < 18 BEGIN
set @v_status = -1;
RAISERROR('-20001 Age must be at least 18', 16, 1);
END
IF @@FETCH_STATUS <> 0 BEGIN
set @v_status = -1;
RAISERROR('-20002 No data found', 16, 1);
END
WHEN OTHERS THEN
set @v_status = -1;
RAISERROR('-20003 Unexpected error', 16, 1);
END;
END example_proc;
|
CREATE OR REPLACE PROCEDURE example_proc (
p_name IN VARCHAR2,
p_age IN NUMBER
) IS
v_msg VARCHAR2(200);
v_status NUMBER;
BEGIN
SELECT 'Name: ' || p_name || ', Age: ' || p_age INTO v_msg FROM DUAL;
DBMS_OUTPUT.PUT_LINE(v_msg);
IF p_age < 18 THEN
v_status := -1;
RAISE_APPLICATION_ERROR(-20001, 'Age must be at least 18');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_status := -1;
RAISE_APPLICATION_ERROR(-20002, 'No data found');
WHEN OTHERS THEN
v_status := -1;
RAISE_APPLICATION_ERROR(-20003, 'Unexpected error');
END;
END example_proc;
|
CREATE PROCEDURE update_employee(
@employee_id IN AS NUMBER,
salary IN NUMBER,
department_id IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = salary,
department_id = department_id
WHERE employee_id = employee_id;
IF @@FETCH_STATUS <> 0 BEGIN
RAISERROR('-20001 Employee not found', 16, 1);
END
END;
/
CREATE FUNCTION get_max_salary() RETURNS FLOAT AS
BEGIN
DECLARE @max_salary FLOAT;
SELECT @max_salary = MAX(salary) FROM employees;
RETURN @max_salary;
IF @@FETCH_STATUS <> 0 BEGIN
RETURN NULL;
END
END;
GO
BEGIN
DECLARE
@salary FLOAT;
BEGIN
EXECUTE sp_executesql @'SELECT salary FROM employees WHERE employee_id = 10', N'@salary output', @salary output;
DBMS_OUTPUT.PUT_LINE('Salary of employee 10 is ' + isnull(@salary, ''));
IF @@FETCH_STATUS <> 0 BEGIN
DBMS_OUTPUT.PUT_LINE('Employee 10 not found');
END
END;
EXECUTE sp_executesql @'UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20';
EXCEPTION
WHEN OTHERS THEN
RAISERROR('-20002 Error updating employee salaries', 16, 1);
END;
/
|
CREATE PROCEDURE update_employee(
employee_id IN NUMBER,
salary IN NUMBER,
department_id IN NUMBER
) AS
BEGIN
UPDATE employees
SET salary = salary,
department_id = department_id
WHERE employee_id = employee_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found');
END;
/
FUNCTION get_max_salary RETURN NUMBER IS
max_salary NUMBER;
BEGIN
SELECT MAX(salary) INTO max_salary FROM employees;
RETURN max_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
/
BEGIN
DECLARE
salary NUMBER;
BEGIN
EXECUTE IMMEDIATE 'SELECT salary FROM employees WHERE employee_id = 10' INTO salary;
DBMS_OUTPUT.PUT_LINE('Salary of employee 10 is ' || salary);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee 10 not found');
END;
EXECUTE IMMEDIATE 'UPDATE employees SET salary = salary * 1.10 WHERE department_id = 20';
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20002, 'Error updating employee salaries');
END;
/
|