sqlserver
stringlengths
59
30k
plsql
stringlengths
54
4.67k
CREATE PROCEDURE add_customer ( @customer_name IN AS VARCHAR2, customer_address IN VARCHAR2, customer_phone IN VARCHAR2 ) AS -- Lo... *** l_customer_id NUMBER; BEGIN *** ata into the database INSERT INTO customers ( customer_name, customer_address, customer_phone ) VALUES ( customer_name, customer_address, customer_phone ); SET @l_customer_id = SCOPE_IDENTITY(); *** sage DBMS_OUTPUT.PUT_LINE ('Customer added successfully with ID: ' + isnull(l_customer_id, '')); EXCEPTION *** that occur WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('Error occurred while adding customer: ' + ISNULL(SQLERRM, '')); END;
CREATE PROCEDURE add_customer ( customer_name IN VARCHAR2, customer_address IN VARCHAR2, customer_phone IN VARCHAR2 ) AS -- Local variables l_customer_id NUMBER; BEGIN -- Insert customer data into the database INSERT INTO customers ( customer_name, customer_address, customer_phone ) VALUES ( customer_name, customer_address, customer_phone ) RETURNING customer_id INTO l_customer_id; -- Print success message DBMS_OUTPUT.PUT_LINE ('Customer added successfully with ID: ' || l_customer_id); EXCEPTION -- Handle any errors that occur WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('Error occurred while adding customer: ' || SQLERRM); END;
BEGIN -- De... *** l_deptno NUMBER; l_dname dbo.VARCHAR2(20); *** epartment number DBMS_OUTPUT.PUT_LINE('Enter department number:'); DBMS_INPUT.GET_LINE(l_deptno); *** e for department name SELECT @l_dname = dname FROM dept WHERE deptno = l_deptno; *** t name DBMS_OUTPUT.PUT_LINE('Department name: ' + isnull(l_dname, '')); *** found, raise an error IF l_dname IS NULL BEGIN RAISERROR('-20001 Department not found', 16, 1); END *** alary UPDATE emp SET sal = sal * 1.10 WHERE deptno = l_deptno; -- Co... *** COMMIT; *** essage DBMS_OUTPUT.PUT_LINE('Salary updated successfully'); EXCEPTION -- Ha... *** WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
BEGIN -- Declare variables l_deptno NUMBER; l_dname VARCHAR2(20); -- Prompt user for department number DBMS_OUTPUT.PUT_LINE('Enter department number:'); DBMS_INPUT.GET_LINE(l_deptno); -- Query the database for department name SELECT dname INTO l_dname FROM dept WHERE deptno = l_deptno; -- Display department name DBMS_OUTPUT.PUT_LINE('Department name: ' || l_dname); -- If no department found, raise an error IF l_dname IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Department not found'); END IF; -- Update employee salary UPDATE emp SET sal = sal * 1.10 WHERE deptno = l_deptno; -- Commit changes COMMIT; -- Display success message DBMS_OUTPUT.PUT_LINE('Salary updated successfully'); EXCEPTION -- Handle errors WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
IF OBJECT_ID('transfer_funds', 'P') IS NOT NULL DROP PROCEDURE transfer_funds; GO CREATE PROCEDURE transfer_funds(@sender_id IN AS NUMBER, receiver_id IN NUMBER, amount IN NUMBER) AS sender_balance NUMBER; receiver_balance NUMBER; BEGIN SELECT @sender_balance = balance FROM accounts WHERE account_id = sender_id; SELECT @receiver_balance = balance FROM accounts WHERE account_id = receiver_id; IF sender_balance >= amount BEGIN UPDATE accounts SET balance = balance - amount WHERE account_id = sender_id; UPDATE accounts SET balance = balance + amount WHERE account_id = receiver_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Transfer successful'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Insufficient funds'); END EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred during the transfer'); END;
CREATE OR REPLACE PROCEDURE transfer_funds(sender_id IN NUMBER, receiver_id IN NUMBER, amount IN NUMBER) AS sender_balance NUMBER; receiver_balance NUMBER; BEGIN SELECT balance INTO sender_balance FROM accounts WHERE account_id = sender_id; SELECT balance INTO receiver_balance FROM accounts WHERE account_id = receiver_id; IF sender_balance >= amount THEN UPDATE accounts SET balance = balance - amount WHERE account_id = sender_id; UPDATE accounts SET balance = balance + amount WHERE account_id = receiver_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Transfer successful'); ELSE DBMS_OUTPUT.PUT_LINE('Insufficient funds'); END IF; EXCEPTION WHEN OTHERS THEN ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred during the transfer'); END;
IF OBJECT_ID('update_employee', 'P') IS NOT NULL DROP PROCEDURE update_employee; GO CREATE PROCEDURE update_employee( @emp_id IN AS NUMBER, new_salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
CREATE OR REPLACE PROCEDURE update_employee( emp_id IN NUMBER, new_salary IN NUMBER ) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL DROP PROCEDURE get_employee_details; GO CREATE PROCEDURE get_employee_details( @p_employee_id IN AS OUT NUMBER ) AS v_employee_name dbo.VARCHAR2(100); v_salary NUMBER; BEGIN SELECT @v_employee_name = first_name, @v_employee_name = last_name, @v_salary = salary FROM employees WHERE employee_id = p_employee_id; DBMS_OUTPUT.put_line('Employee Name: ' + isnull(v_employee_name, '')); DBMS_OUTPUT.put_line('Salary: ' + isnull(v_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.put_line('Employee with ID ' + isnull(p_employee_id, '') + ' not found.'); END END; DECLARE @v_employee_id FLOAT := 10; BEGIN dbo.get_employee_details(@v_employee_id); END;
CREATE OR REPLACE PROCEDURE get_employee_details( p_employee_id IN OUT NUMBER ) AS v_employee_name VARCHAR2(100); v_salary NUMBER; BEGIN SELECT first_name, last_name, salary INTO v_employee_name, v_employee_name, v_salary FROM employees WHERE employee_id = p_employee_id; DBMS_OUTPUT.put_line('Employee Name: ' || v_employee_name); DBMS_OUTPUT.put_line('Salary: ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.put_line('Employee with ID ' || p_employee_id || ' not found.'); END; DECLARE v_employee_id NUMBER := 10; BEGIN get_employee_details(v_employee_id); END;
BEGIN DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = employee_id, @v_salary = salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 BEGIN RAISERROR('-20001 Salary is too high!', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN NULL; END END; END;
BEGIN DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT employee_id, salary INTO v_emp_id, v_salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary is too high!'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END; END;
DECLARE @v_name VARCHAR(20); v_balance ROW_NUMBER(12, 2); BEGIN SELECT @v_name = name, @v_balance = balance FROM accounts WHERE id = 10; IF v_balance < 100 BEGIN UPDATE accounts SET balance = balance + 100 WHERE id = 10; COMMIT; DBMS_OUTPUT.PUT_LINE('Balance updated successfully'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Balance is sufficient, no update needed'); END END;
DECLARE v_name VARCHAR2(20); v_balance NUMBER(12, 2); BEGIN SELECT name, balance INTO v_name, v_balance FROM accounts WHERE id = 10; IF v_balance < 100 THEN UPDATE accounts SET balance = balance + 100 WHERE id = 10; COMMIT; DBMS_OUTPUT.PUT_LINE('Balance updated successfully'); ELSE DBMS_OUTPUT.PUT_LINE('Balance is sufficient, no update needed'); END IF; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc(@p_id IN AS NUMBER) IS v_name dbo.VARCHAR2(100); BEGIN SELECT @v_name = name FROM employees WHERE employee_id = p_id; IF v_name IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_name, '')); END; /
CREATE OR REPLACE PROCEDURE my_proc(p_id IN NUMBER) IS v_name VARCHAR2(100); BEGIN SELECT name INTO v_name FROM employees WHERE employee_id = p_id; IF v_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name); END; /
CREATE PROCEDURE fetch_employee(@emp_id IN AS NUMBER) IS emp_record EMPLOYEE%ROWTYPE; BEGIN SELECT @emp_record = * FROM employees WHERE employee_id = emp_id; dbms_output.put_line('Employee Details:'); dbms_output.put_line('Employee ID: ' + isnull(emp_record.employee_id, '')); dbms_output.put_line('First Name: ' + isnull(emp_record.first_name, '')); dbms_output.put_line('Last Name: ' + isnull(emp_record.last_name, '')); dbms_output.put_line('Department ID: ' + isnull(emp_record.department_id, '')); dbms_output.put_line('Job Title: ' + isnull(emp_record.job_title, '')); dbms_output.put_line('Salary: ' + isnull(emp_record.salary, '')); END;
CREATE PROCEDURE fetch_employee(emp_id IN NUMBER) IS emp_record EMPLOYEE%ROWTYPE; BEGIN SELECT * INTO emp_record FROM employees WHERE employee_id = emp_id; dbms_output.put_line('Employee Details:'); dbms_output.put_line('Employee ID: ' || emp_record.employee_id); dbms_output.put_line('First Name: ' || emp_record.first_name); dbms_output.put_line('Last Name: ' || emp_record.last_name); dbms_output.put_line('Department ID: ' || emp_record.department_id); dbms_output.put_line('Job Title: ' || emp_record.job_title); dbms_output.put_line('Salary: ' || emp_record.salary); END;
IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@emp_id FLOAT, @new_salary FLOAT, @OUT err_msg AS VARCHAR2) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN set @err_msg = 'Employee not found.'; END END;
CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id NUMBER, new_salary NUMBER, OUT err_msg VARCHAR2) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN NO_DATA_FOUND THEN err_msg := 'Employee not found.'; END;
DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = 1; END;
DECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 1; END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = employee_id, @v_salary = salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 BEGIN DBMS_OUTPUT.put_line('Salary is greater than 10000'); END ELSE BEGIN DBMS_OUTPUT.put_line('Salary is not greater than 10000'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('An error occurred.'); END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT employee_id, salary INTO v_emp_id, v_salary FROM employees WHERE last_name = 'King'; IF v_salary > 10000 THEN DBMS_OUTPUT.put_line('Salary is greater than 10000'); ELSE DBMS_OUTPUT.put_line('Salary is not greater than 10000'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('An error occurred.'); END;
BEGIN DECLARE @i INT = 1; WHILE @i <= 10 BEGIN IF i MOD 2 = 0 BEGIN INSERT INTO EVEN_NUMBERS VALUES (i); END ELSE BEGIN INSERT INTO ODD_NUMBERS VALUES (i); END END; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20001 Error occurred while inserting data', 16, 1); END; IF OBJECT_ID('GET_EMPLOYEE_SALARY', 'FN') IS NOT NULL DROP FUNCTION GET_EMPLOYEE_SALARY; GO CREATE FUNCTION GET_EMPLOYEE_SALARY(@employee_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = employee_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN -1; END END; DECLARE @v_employee_id FLOAT := 100; v_salary NUMBER; BEGIN SET @v_salary = dbo.GET_EMPLOYEE_SALARY(@v_employee_id); IF v_salary = -1 BEGIN RAISERROR('-20002 Employee not found', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(v_salary, '')); END; IF OBJECT_ID('UPDATE_EMPLOYEE_SALARY', 'P') IS NOT NULL DROP PROCEDURE UPDATE_EMPLOYEE_SALARY; GO CREATE PROCEDURE UPDATE_EMPLOYEE_SALARY(@employee_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; COMMIT; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20003 Employee not found', 16, 1); END WHEN DUP_VAL_ON_INDEX THEN RAISERROR('-20004 Duplicate salary value found', 16, 1); END; DECLARE @v_employee_id FLOAT := 100; v_new_salary SET @NUMBER = 2000; BEGIN dbo.UPDATE_EMPLOYEE_SALARY(@v_employee_id, v_new_salary); DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully'); END; BEGIN DECLARE @v_emp_id FLOAT := 100; v_emp_name dbo.VARCHAR2(255); BEGIN SELECT @v_emp_name = employee_name FROM employees WHERE employee_id = @v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(v_emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20005 Employee not found', 16, 1); END END; END;
BEGIN FOR i IN 1..10 LOOP IF i MOD 2 = 0 THEN INSERT INTO EVEN_NUMBERS VALUES (i); ELSE INSERT INTO ODD_NUMBERS VALUES (i); END IF; END LOOP; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Error occurred while inserting data'); 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 -1; END; DECLARE v_employee_id NUMBER := 100; v_salary NUMBER; BEGIN v_salary := GET_EMPLOYEE_SALARY(v_employee_id); IF v_salary = -1 THEN RAISE_APPLICATION_ERROR(-20002, 'Employee not found'); END IF; DBMS_OUTPUT.PUT_LINE('Employee salary: ' || v_salary); 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 NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20003, 'Employee not found'); WHEN DUP_VAL_ON_INDEX THEN RAISE_APPLICATION_ERROR(-20004, 'Duplicate salary value found'); END; DECLARE v_employee_id NUMBER := 100; v_new_salary NUMBER := 2000; BEGIN UPDATE_EMPLOYEE_SALARY(v_employee_id, v_new_salary); DBMS_OUTPUT.PUT_LINE('Employee salary updated successfully'); END; BEGIN DECLARE v_emp_id NUMBER := 100; v_emp_name VARCHAR2(255); BEGIN SELECT employee_name INTO v_emp_name FROM employees WHERE employee_id = v_emp_id; DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20005, 'Employee not found'); END; END;
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL DROP PROCEDURE emp_bonus; GO CREATE PROCEDURE emp_bonus AS BEGIN SET NOCOUNT ON; UPDATE employees SET bonus = bonus + 1000 WHERE salary > 50000; END; IF OBJECT_ID('get_dept_salaries', 'FN') IS NOT NULL DROP FUNCTION get_dept_salaries; GO CREATE FUNCTION get_dept_salaries(@dept_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; AS total_salary NUMBER; BEGIN SELECT @total_salary = SUM(salary) FROM employees WHERE dept_id = dept_id; RETURN total_salary; END; BEGIN BEGIN INSERT INTO employees(emp_id, name, salary) VALUES (1, 'John', 50000); EXCEPTION WHEN dup_val_on_index THEN ROLLBACK; RAISERROR('-20001 Duplicate employee ID.', 16, 1); END; END;
CREATE OR REPLACE PROCEDURE emp_bonus AS BEGIN UPDATE employees SET bonus = bonus + 1000 WHERE salary > 50000; END; CREATE OR REPLACE FUNCTION get_dept_salaries(dept_id IN NUMBER) RETURN NUMBER AS total_salary NUMBER; BEGIN SELECT SUM(salary) INTO total_salary FROM employees WHERE dept_id = dept_id; RETURN total_salary; END; BEGIN BEGIN INSERT INTO employees(emp_id, name, salary) VALUES (1, 'John', 50000); EXCEPTION WHEN dup_val_on_index THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Duplicate employee ID.'); END; 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_new_salary IN NUMBER) IS BEGIN *** of the employee with the specified ID UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id; *** n if the employee ID is invalid IF SQL%NOTFOUND BEGIN RAISERROR('-20001 Invalid employee ID: ' + isnull(p_employee_id, ''), 16, 1); END END; /
CREATE OR REPLACE PROCEDURE update_employee( p_employee_id IN NUMBER, p_new_salary IN NUMBER) IS BEGIN -- Update the salary of the employee with the specified ID UPDATE employees SET salary = p_new_salary WHERE employee_id = p_employee_id; -- Raise an exception if the employee ID is invalid IF SQL%NOTFOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid employee ID: ' || p_employee_id); END IF; END; /
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; IF @@fetch_status <> 0 BEGIN RETURN -1; END END; DECLARE @emp_id FLOAT := 100; salary NUMBER; BEGIN SET @salary = dbo.get_employee_salary(@emp_id); IF salary > 50000 BEGIN DBMS_OUTPUT.put_line('Employee has a high salary'); END ELSE IF salary > 30000 BEGIN DBMS_OUTPUT.put_line('Employee has an average salary'); END ELSE BEGIN DBMS_OUTPUT.put_line('Employee has a low salary'); END END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN no_data_found THEN RETURN -1; END; DECLARE emp_id NUMBER := 100; salary NUMBER; BEGIN salary := get_employee_salary(emp_id); IF salary > 50000 THEN DBMS_OUTPUT.put_line('Employee has a high salary'); ELSIF salary > 30000 THEN DBMS_OUTPUT.put_line('Employee has an average salary'); ELSE DBMS_OUTPUT.put_line('Employee has a low salary'); END IF; END;
IF OBJECT_ID('my_proc', 'P') IS NOT NULL DROP PROCEDURE my_proc; GO CREATE PROCEDURE my_proc( @id IN AS NUMBER, name IN VARCHAR2) AS BEGIN *** o table INSERT INTO my_table (id, name) VALUES (id, name); *** record from table SELECT id, name FROM my_table WHERE id = id; DECLARE rec CURSOR FOR SELECT id, name FROM my_table WHERE id = id; OPEN rec; FETCH rec INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE('Record: ' + isnull(@rec_id, '') + ', ' + isnull(@rec_name, '')); FETCH rec INTO; END; CLOSE rec; DEALLOCATE rec; *** table UPDATE my_table SET name = isnull(name, '') + '_updated' WHERE id = id; *** m table DELETE FROM my_table WHERE id = id; -- Ha... *** WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate value on index'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Unexpected error'); END; END my_proc;
CREATE OR REPLACE PROCEDURE my_proc( id IN NUMBER, name IN VARCHAR2) AS BEGIN -- Insert record into table INSERT INTO my_table (id, name) VALUES (id, name); -- Select and print record from table SELECT id, name FROM my_table WHERE id = id; FOR rec IN (SELECT id, name FROM my_table WHERE id = id) LOOP DBMS_OUTPUT.PUT_LINE('Record: ' || rec.id || ', ' || rec.name); END LOOP; -- Update record in table UPDATE my_table SET name = name || '_updated' WHERE id = id; -- Delete record from table DELETE FROM my_table WHERE id = id; -- Handle error EXCEPTION WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate value on index'); WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Unexpected error'); END; END my_proc;
DECLARE @v_empno FLOAT; BEGIN SET @v_empno = 7788; IF @v_empno IS NULL BEGIN RAISERROR('-20101 Employee not found', 16, 1); END UPDATE employees SET salary = salary * 1.10 WHERE empno = @v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END END;
DECLARE v_empno NUMBER; BEGIN v_empno := 7788; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20101, 'Employee not found'); END IF; UPDATE employees SET salary = salary * 1.10 WHERE empno = v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END;
CREATE FUNCTION GetCustomerOrders(@customer_id IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; AS order_cur SYS_REFCURSOR; BEGIN SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = customer_id; RETURN order_cur; END; DECLARE @cust_id FLOAT := 100; orders SYS_REFCURSOR; BEGIN SET @orders = dbo.GetCustomerOrders(@cust_id); WHILE 1=1 BEGIN IF @@FETCH_STATUS <> 0 BREAK; FETCH orders INTO @order_id, @order_date, @total_amount; ISNULL(DBMS_OUTPUT.PUT_LINE, '') + isnull(order_id, '') + ' | ' + isnull(order_date, '') + ' | ' + isnull(total_amount, ''); END; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No orders found for customer ' + isnull(@cust_id, '')); END END;
CREATE FUNCTION GetCustomerOrders(customer_id IN NUMBER) RETURN SYS_REFCURSOR AS order_cur SYS_REFCURSOR; BEGIN OPEN order_cur FOR SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = customer_id; RETURN order_cur; END; DECLARE cust_id NUMBER := 100; orders SYS_REFCURSOR; BEGIN orders := GetCustomerOrders(cust_id); LOOP EXIT WHEN orders%NOTFOUND; FETCH orders INTO order_id, order_date, total_amount; DBMS_OUTPUT.PUT_LINE || order_id || ' | ' || order_date || ' | ' || total_amount; END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No orders found for customer ' || cust_id); END;
CREATE PROCEDURE sp_update_employee( @p_empno IN AS NUMBER, p_salary IN NUMBER, p_empname IN OUT VARCHAR2 ) IS L_EMP_EXISTS BOOLEAN; BEGIN SELECT @L_EMP_EXISTS = 1 FROM employees WHERE empno = p_empno; IF L_EMP_EXISTS IS TRUE BEGIN UPDATE employees SET salary = p_salary WHERE empno = p_empno; COMMIT; set @p_empname = UPPER(p_empname); END ELSE BEGIN ROLLBACK; RAISERROR('-20001 Employee not found', 16, 1); END END sp_update_employee;
CREATE PROCEDURE sp_update_employee( p_empno IN NUMBER, p_salary IN NUMBER, p_empname IN OUT VARCHAR2 ) IS L_EMP_EXISTS BOOLEAN; BEGIN SELECT 1 INTO L_EMP_EXISTS FROM employees WHERE empno = p_empno; IF L_EMP_EXISTS IS TRUE THEN UPDATE employees SET salary = p_salary WHERE empno = p_empno; COMMIT; p_empname := UPPER(p_empname); ELSE ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; END sp_update_employee;
DECLARE @curr_emp_salary FLOAT; emp_name dbo.VARCHAR2(100); BEGIN SELECT @curr_emp_salary = salary FROM employees WHERE emp_id = 1; IF @curr_emp_salary < 30000 BEGIN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = 1; SELECT @emp_name = ename FROM employees WHERE emp_id = 1; DBMS_OUTPUT.PUT_LINE('Salary increased for ' + isnull(emp_name, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Salary not increased for ' + isnull(emp_name, '')); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; /
DECLARE curr_emp_salary NUMBER; emp_name VARCHAR2(100); BEGIN SELECT salary INTO curr_emp_salary FROM employees WHERE emp_id = 1; IF curr_emp_salary < 30000 THEN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = 1; SELECT ename INTO emp_name FROM employees WHERE emp_id = 1; DBMS_OUTPUT.PUT_LINE('Salary increased for ' || emp_name); ELSE DBMS_OUTPUT.PUT_LINE('Salary not increased for ' || emp_name); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END; /
IF OBJECT_ID('myProcedure', 'P') IS NOT NULL DROP PROCEDURE myProcedure; GO CREATE PROCEDURE myProcedure(@p1 IN AS VARCHAR2, p2 OUT NUMBER) AS BEGIN SELECT @p2 = COUNT(*) FROM customer WHERE name = p1; IF p2 = 0 BEGIN RAISERROR('-20001 Customer not found.', 16, 1); END END;
CREATE OR REPLACE PROCEDURE myProcedure(p1 IN VARCHAR2, p2 OUT NUMBER) AS BEGIN SELECT COUNT(*) INTO p2 FROM customer WHERE name = p1; IF p2 = 0 THEN RAISE_APPLICATION_ERROR(-20001, 'Customer not found.'); END IF; END;
IF OBJECT_ID('get_avg_salary', 'P') IS NOT NULL DROP PROCEDURE get_avg_salary; GO CREATE PROCEDURE get_avg_salary ( @deptno IN AS NUMBER, OUT avg_sal OUT NUMBER ) IS CURSOR emp_cursor IS SELECT salary FROM emp WHERE deptno = deptno; total_salary SET @NUMBER = 0; emp_count SET @NUMBER = 0; BEGIN OPEN emp_cursor; WHILE 1=1 BEGIN FETCH emp_cursor INTO @total_salary; IF @@FETCH_STATUS <> 0 BREAK; set @emp_count = emp_count + 1; END; CLOSE emp_cursor; IF emp_count > 0 BEGIN set @avg_sal = total_salary / emp_count; END ELSE BEGIN set @avg_sal = 0; END END get_avg_salary;
CREATE OR REPLACE PROCEDURE get_avg_salary ( deptno IN NUMBER, OUT avg_sal OUT NUMBER ) IS CURSOR emp_cursor IS SELECT salary FROM emp WHERE deptno = deptno; total_salary NUMBER := 0; emp_count NUMBER := 0; BEGIN OPEN emp_cursor; LOOP FETCH emp_cursor INTO total_salary; EXIT WHEN emp_cursor%NOTFOUND; emp_count := emp_count + 1; END LOOP; CLOSE emp_cursor; IF emp_count > 0 THEN avg_sal := total_salary / emp_count; ELSE avg_sal := 0; END IF; END get_avg_salary;
IF OBJECT_ID('get_employee_details', 'FN') IS NOT NULL DROP FUNCTION get_employee_details; GO CREATE FUNCTION get_employee_details(@employee_id FLOAT) RETURNS SYS_REFCURSOR AS BEGIN DECLARE @emp_cur SYS_REFCURSOR; SELECT * FROM employees WHERE @employee_id = @employee_id; RETURN @emp_cur; END; GO BEGIN DECLARE emp_rec CURSOR FOR SELECT * FROM get_employee_details(100); OPEN emp_rec; FETCH emp_rec INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE(isnull(@emp_rec_first_name, '') + ' ' + isnull(@emp_rec_last_name, '')); FETCH emp_rec INTO; END; CLOSE emp_rec; DEALLOCATE emp_rec; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END; /
CREATE OR REPLACE FUNCTION get_employee_details(employee_id NUMBER) RETURN SYS_REFCURSOR IS emp_cur SYS_REFCURSOR; BEGIN OPEN emp_cur FOR SELECT * FROM employees WHERE employee_id = employee_id; RETURN emp_cur; END; / BEGIN FOR emp_rec IN (SELECT * FROM get_employee_details(100)) LOOP DBMS_OUTPUT.PUT_LINE(emp_rec.first_name || ' ' || emp_rec.last_name); END LOOP; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END; /
CREATE PROCEDURE proc_example(@p_input IN AS NUMBER) AS v_output NUMBER; BEGIN SELECT @v_output = COUNT(*) FROM table_name WHERE column_name = p_input; DBMS_OUTPUT.PUT_LINE('Number of rows found: ' + isnull(v_output, '')); IF v_output > 0 BEGIN UPDATE table_name SET column_name = column_name + 1 WHERE column_name = p_input; END ELSE BEGIN INSERT INTO table_name (column_name) VALUES (p_input); END END; DECLARE @p_number FLOAT := 10; *** lue here BEGIN dbo.proc_example(@p_number); END;
CREATE PROCEDURE proc_example(p_input IN NUMBER) AS v_output NUMBER; BEGIN SELECT COUNT(*) INTO v_output FROM table_name WHERE column_name = p_input; DBMS_OUTPUT.PUT_LINE('Number of rows found: ' || v_output); IF v_output > 0 THEN UPDATE table_name SET column_name = column_name + 1 WHERE column_name = p_input; ELSE INSERT INTO table_name (column_name) VALUES (p_input); END IF; END; DECLARE p_number NUMBER := 10; -- Pass any input value here BEGIN proc_example(p_number); END;
BEGIN DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); BEGIN OPEN emp_cursor; FETCH emp_cursor INTO @v_emp_id, @v_emp_name; WHILE emp_cursor%FOUND BEGIN DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '') + ', Employee Name: ' + isnull(v_emp_name, '')); FETCH emp_cursor INTO @v_emp_id, @v_emp_name; END; CLOSE emp_cursor; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employees found'); END END; END;
BEGIN DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); BEGIN OPEN emp_cursor; FETCH emp_cursor INTO v_emp_id, v_emp_name; WHILE emp_cursor%FOUND LOOP DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id || ', Employee Name: ' || v_emp_name); FETCH emp_cursor INTO v_emp_id, v_emp_name; END LOOP; CLOSE emp_cursor; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employees found'); END; END;
CREATE PROCEDURE sp_example( @param1 IN AS NUMBER, param2 OUT VARCHAR2 ) AS BEGIN *** param1 SELECT @param2 = name FROM table WHERE id = param1; IF @@FETCH_STATUS <> 0 BEGIN set @param2 = 'No data found'; END END; BEGIN dbo.sp_example(10, :param2); END;
CREATE PROCEDURE sp_example( param1 IN NUMBER, param2 OUT VARCHAR2 ) AS BEGIN -- Do something with param1 SELECT name INTO param2 FROM table WHERE id = param1; EXCEPTION WHEN NO_DATA_FOUND THEN param2 := 'No data found'; END; BEGIN sp_example(10, :param2); END;
IF OBJECT_ID('emp_bonus', 'P') IS NOT NULL DROP PROCEDURE emp_bonus; GO CREATE PROCEDURE emp_bonus (@emp_id IN AS NUMBER) AS BEGIN UPDATE employees SET salary = salary * 1.10 WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISERROR('-20001 Error updating employee bonus!', 16, 1); END emp_bonus;
CREATE OR REPLACE PROCEDURE emp_bonus (emp_id IN NUMBER) AS BEGIN UPDATE employees SET salary = salary * 1.10 WHERE employee_id = emp_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Error updating employee bonus!'); END emp_bonus;
DECLARE @v_empno FLOAT; BEGIN SELECT @v_empno = empno FROM emp WHERE ename = 'SCOTT'; IF @v_empno IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END UPDATE emp SET sal = sal * 1.10 WHERE empno = @v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No matching employee found.'); END WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate value on unique index.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;
DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE ename = 'SCOTT'; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; UPDATE emp SET sal = sal * 1.10 WHERE empno = v_empno; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No matching employee found.'); WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('Duplicate value on unique index.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;
IF OBJECT_ID('update_salary', 'P') IS NOT NULL DROP PROCEDURE update_salary; GO CREATE PROCEDURE update_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; / DECLARE @salary_updated BIT; BEGIN BEGIN dbo.update_salary(10, 50000); SET @salary_updated = TRUE; EXCEPTION WHEN OTHERS THEN SET @salary_updated = FALSE; END; IF @salary_updated = TRUE BEGIN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Error updating salary.'); END END; /
CREATE OR REPLACE PROCEDURE update_salary(emp_id IN NUMBER, new_salary IN NUMBER) AS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; COMMIT; END; / DECLARE salary_updated BOOLEAN; BEGIN BEGIN update_salary(10, 50000); salary_updated := TRUE; EXCEPTION WHEN OTHERS THEN salary_updated := FALSE; END; IF salary_updated = TRUE THEN DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); ELSE DBMS_OUTPUT.PUT_LINE('Error updating salary.'); END IF; END; /
DECLARE @v_empno FLOAT := 100; BEGIN IF @v_empno > 110 BEGIN DBMS_OUTPUT.PUT_LINE('Employee number is greater than 110'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee number is less than or equal to 110'); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
DECLARE v_empno NUMBER := 100; BEGIN IF v_empno > 110 THEN DBMS_OUTPUT.PUT_LINE('Employee number is greater than 110'); ELSE DBMS_OUTPUT.PUT_LINE('Employee number is less than or equal to 110'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;
CREATE TABLE employees ( id FLOAT, first_name VARCHAR(50), last_name VARCHAR(50), salary FLOAT, PRIMARY KEY (id) ); DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM employees WHERE id = 1; IF @v_salary > 10000 BEGIN UPDATE employees SET salary = salary * 1.1 WHERE id = 1; END END;
CREATE TABLE employees ( id NUMBER, first_name VARCHAR2(50), last_name VARCHAR2(50), salary NUMBER, PRIMARY KEY (id) ); DECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE id = 1; IF v_salary > 10000 THEN UPDATE employees SET salary = salary * 1.1 WHERE id = 1; END IF; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN NULL; END END; IF OBJECT_ID('update_employee_salary', 'P') IS NOT NULL DROP PROCEDURE update_employee_salary; GO CREATE PROCEDURE update_employee_salary(@emp_id IN AS NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN 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 NULL; END; CREATE OR REPLACE PROCEDURE update_employee_salary(emp_id IN NUMBER, new_salary IN NUMBER) IS BEGIN UPDATE employees SET salary = new_salary WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN NULL; END;
BEGIN -- De... *** v_emp_id NUMBER; v_salary NUMBER; -- Ge... *** SELECT @v_emp_id = employee_id FROM employees WHERE last_name = 'Smith'; -- Ge... *** SELECT @v_salary = salary FROM salaries WHERE employee_id = v_emp_id; *** s greater than $10,000 IF v_salary > 10000 BEGIN *** aise UPDATE salaries SET salary = salary * 1.1 WHERE employee_id = v_emp_id; END -- Co... *** COMMIT; END;
BEGIN -- Declare variables v_emp_id NUMBER; v_salary NUMBER; -- Get employee ID SELECT employee_id INTO v_emp_id FROM employees WHERE last_name = 'Smith'; -- Get employee salary SELECT salary INTO v_salary FROM salaries WHERE employee_id = v_emp_id; -- Check if salary is greater than $10,000 IF v_salary > 10000 THEN -- Give employee a raise UPDATE salaries SET salary = salary * 1.1 WHERE employee_id = v_emp_id; END IF; -- Commit changes COMMIT; END;
CREATE PROCEDURE multiply_numbers( @IN p_num1 AS NUMBER, IN p_num2 NUMBER, OUT p_result OUT NUMBER ) AS BEGIN set @p_result = p_num1 * p_num2; END; / DECLARE @v_num1 FLOAT := 10; v_num2 SET @NUMBER = 20; v_result NUMBER; BEGIN dbo.multiply_numbers(@v_num1, v_num2, v_result); DBMS_OUTPUT.PUT_LINE('The result is: ' + isnull(v_result, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found.'); END END; /
CREATE PROCEDURE multiply_numbers( IN p_num1 NUMBER, IN p_num2 NUMBER, OUT p_result OUT NUMBER ) AS BEGIN p_result := p_num1 * p_num2; END; / DECLARE v_num1 NUMBER := 10; v_num2 NUMBER := 20; v_result NUMBER; BEGIN multiply_numbers(v_num1, v_num2, v_result); DBMS_OUTPUT.PUT_LINE('The result is: ' || v_result); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found.'); END; /
DECLARE @v_empno SMALLINT; v_sal ROW_NUMBER(8,2); BEGIN SELECT @v_empno = empno, @v_sal = sal FROM emp WHERE empno = 7839; IF v_sal > 5000 BEGIN UPDATE emp SET sal = v_sal + 1000 WHERE empno = @v_empno; COMMIT; END ELSE BEGIN RAISERROR('-20102 Salary too low to grant raise', 16, 1); END END;
DECLARE v_empno NUMBER(4); v_sal NUMBER(8,2); BEGIN SELECT empno, sal INTO v_empno, v_sal FROM emp WHERE empno = 7839; IF v_sal > 5000 THEN UPDATE emp SET sal = v_sal + 1000 WHERE empno = v_empno; COMMIT; ELSE RAISE_APPLICATION_ERROR(-20102, 'Salary too low to grant raise'); END IF; END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(20); v_emp_salary NUMBER; v_dept_id NUMBER; BEGIN SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_emp_salary = emp_salary, @v_dept_id = dept_id FROM employees WHERE emp_id = 10; IF @v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END ELSE IF v_emp_salary < 5000 BEGIN UPDATE employees SET emp_salary = 5000 WHERE emp_id = @v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated to 5000'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Salary already above 5000'); END EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(20); v_emp_salary NUMBER; v_dept_id NUMBER; BEGIN SELECT emp_id, emp_name, emp_salary, dept_id INTO v_emp_id, v_emp_name, v_emp_salary, v_dept_id FROM employees WHERE emp_id = 10; IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); ELSIF v_emp_salary < 5000 THEN UPDATE employees SET emp_salary = 5000 WHERE emp_id = v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated to 5000'); ELSE DBMS_OUTPUT.PUT_LINE('Salary already above 5000'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
DECLARE v_cursor CURSOR FOR SELECT * FROM TBL_EMP; BEGIN OPEN v_cursor; WHILE 1=1 BEGIN FETCH v_cursor INTO @v_id, @v_name; IF @@FETCH_STATUS <> 0 BREAK; SYS.DBMS_OUTPUT.PUT_LINE(isnull(v_id, '') + ' - ' + isnull(v_name, '')); END; CLOSE v_cursor; EXCEPTION WHEN OTHERS THEN RAISERROR(-20001, SUBSTRING(SQLERRM, 1, 200), 16, 1); END;
DECLARE v_cursor CURSOR FOR SELECT * FROM TBL_EMP; BEGIN OPEN v_cursor; LOOP FETCH v_cursor INTO v_id, v_name; EXIT WHEN v_cursor%NOTFOUND; SYS.DBMS_OUTPUT.PUT_LINE(v_id || ' - ' || v_name); END LOOP; CLOSE v_cursor; EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR(-20001, SUBSTR(SQLERRM, 1, 200)); END;
DECLARE @v_id FLOAT; BEGIN SET @v_id = 10; IF @v_id > 5 BEGIN UPDATE employees SET salary = salary * 1.1 WHERE id = @v_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END; /
DECLARE v_id NUMBER; BEGIN v_id := 10; IF v_id > 5 THEN BEGIN UPDATE employees SET salary = salary * 1.1 WHERE id = v_id; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; END IF; END; /
CREATE PROCEDURE UpdateEmployeeSalary( @IN employee_id AS NUMBER, IN salary NUMBER ) AS *** of the employee with the specified employee ID UPDATE employees SET salary = salary WHERE employee_id = employee_id; END; BEGIN *** rocedure to update the salary of employee with ID 100 dbo.UpdateEmployeeSalary(100, 50000); *** that may occur EXCEPTION WHEN OTHERS THEN -- Lo... *** DBMS_OUTPUT.PUT_LINE('Error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE PROCEDURE UpdateEmployeeSalary( IN employee_id NUMBER, IN salary NUMBER ) AS -- Update the salary of the employee with the specified employee ID UPDATE employees SET salary = salary WHERE employee_id = employee_id; END; BEGIN -- Call the stored procedure to update the salary of employee with ID 100 UpdateEmployeeSalary(100, 50000); -- Handle any errors that may occur EXCEPTION WHEN OTHERS THEN -- Log the error DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM); END;
IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name(@id FLOAT) RETURNS VARCHAR(4000) AS BEGIN DECLARE @name VARCHAR(50); SELECT @name = isnull(first_name, '') + ' ' + isnull(last_name, '') FROM employees WHERE employee_id = @id; RETURN @name; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @emp_name VARCHAR(50); BEGIN SET @emp_name = dbo.get_employee_name(100); IF @emp_name IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@emp_name, '')); END END;
CREATE OR REPLACE FUNCTION get_employee_name(id NUMBER) RETURN VARCHAR2 IS name VARCHAR2(50); BEGIN SELECT first_name || ' ' || last_name INTO name FROM employees WHERE employee_id = id; RETURN name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE emp_name VARCHAR2(50); BEGIN emp_name := get_employee_name(100); IF emp_name IS NULL THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); ELSE DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_name); END IF; END;
CREATE PROCEDURE my_proc ( @id IN AS NUMBER, name IN VARCHAR2 ) IS BEGIN -- Up... *** UPDATE employees SET name = name WHERE id = id; -- Ha... *** EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE @v_id FLOAT := 1234; v_name dbo.VARCHAR2(50) := 'John Doe'; BEGIN dbo.my_proc(@v_id, v_name); END;
CREATE PROCEDURE my_proc ( id IN NUMBER, name IN VARCHAR2 ) IS BEGIN -- Update the table UPDATE employees SET name = name WHERE id = id; -- Handle exceptions EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE v_id NUMBER := 1234; v_name VARCHAR2(50) := 'John Doe'; BEGIN my_proc(v_id, v_name); END;
CREATE PROCEDURE my_proc (@param1 IN AS NUMBER) IS v_total NUMBER; BEGIN SELECT @v_total = SUM(value) FROM my_table WHERE id = param1; IF v_total IS NULL BEGIN RAISERROR('-20100 Invalid parameter value.', 16, 1); END UPDATE my_table SET value = v_total WHERE id = param1; END; IF OBJECT_ID('my_func', 'FN') IS NOT NULL DROP FUNCTION my_func; GO CREATE FUNCTION my_func (@param1 IN VARCHAR2DECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS BEGIN RETURN ISNULL(SUBSTRING(param1, 1, 1), '') + ISNULL(UPPER(SUBSTRING(param1, 2, LEN(param1))), '') + '!'; END; CREATE TABLE my_table ( id FLOAT PRIMARY KEY, name VARCHAR(255), value FLOAT ); INSERT INTO my_table (id, name, value) VALUES (1, 'John', 100); INSERT INTO my_table (id, name, value) VALUES (2, 'Jane', 200); DECLARE @v_result FLOAT; BEGIN dbo.my_proc(1); SELECT value FROM my_table WHERE id = 1 INTO v_result; END; SELECT dbo.my_func('hello');
CREATE PROCEDURE my_proc (param1 IN NUMBER) IS v_total NUMBER; BEGIN SELECT SUM(value) INTO v_total FROM my_table WHERE id = param1; IF v_total IS NULL THEN RAISE_APPLICATION_ERROR(-20100, 'Invalid parameter value.'); END IF; UPDATE my_table SET value = v_total WHERE id = param1; END; CREATE OR REPLACE FUNCTION my_func (param1 IN VARCHAR2) RETURN VARCHAR2 IS BEGIN RETURN SUBSTR(param1, 1, 1) || UPPER(SUBSTR(param1, 2)) || '!'; END; CREATE TABLE my_table ( id NUMBER PRIMARY KEY, name VARCHAR2(255), value NUMBER ); INSERT INTO my_table (id, name, value) VALUES (1, 'John', 100); INSERT INTO my_table (id, name, value) VALUES (2, 'Jane', 200); DECLARE v_result NUMBER; BEGIN my_proc(1); SELECT value FROM my_table WHERE id = 1 INTO v_result; END; SELECT my_func('hello') FROM DUAL;
BEGIN DECLARE @error_flag FLOAT; BEGIN /* *** atements */ EXCEPTION WHEN OTHERS THEN SET @error_flag = @@ERROR; IF @error_flag <> 0 BEGIN ROLLBACK; END END; END; /
BEGIN DECLARE error_flag NUMBER; BEGIN /* A batch of DML statements */ EXCEPTION WHEN OTHERS THEN error_flag := SQLCODE; IF error_flag <> 0 THEN ROLLBACK; END IF; END; END; /
IF OBJECT_ID('UpdateEmployeeSalary', 'P') IS NOT NULL DROP PROCEDURE UpdateEmployeeSalary; GO CREATE PROCEDURE UpdateEmployeeSalary(@emp_id IN AS NUMBER, salary IN NUMBER) AS BEGIN UPDATE employees SET salary = salary WHERE employee_id = emp_id; COMMIT; END; DECLARE @emp_id FLOAT := 100; salary SET @NUMBER = 15000; BEGIN dbo.UpdateEmployeeSalary(@emp_id, salary); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE @emp_name VARCHAR(50); dept_name dbo.VARCHAR2(50); BEGIN SELECT @emp_name = employee_name, @dept_name = department_name FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(@emp_name, '')); DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(dept_name, '')); END; IF OBJECT_ID('GetAverageSalary', 'FN') IS NOT NULL DROP FUNCTION GetAverageSalary; GO CREATE FUNCTION GetAverageSalary() RETURNS FLOAT AS BEGIN RETURN (SELECT AVG(salary) FROM employees); END; DECLARE @salary FLOAT := dbo.GetAverageSalary(); BEGIN DBMS_OUTPUT.PUT_LINE('Average Salary: ' + isnull(@salary, '')); END; BEGIN DECLARE emp CURSOR FOR SELECT employee_id, @salary FROM employees; OPEN emp; FETCH emp INTO; WHILE @@FETCH_STATUS=0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@emp_employee_id, '') + ', Salary: ' + isnull(@emp_salary, '')); FETCH emp INTO; END; CLOSE emp; DEALLOCATE emp; END;
CREATE OR REPLACE PROCEDURE UpdateEmployeeSalary(emp_id IN NUMBER, salary IN NUMBER) AS BEGIN UPDATE employees SET salary = salary WHERE employee_id = emp_id; COMMIT; END; DECLARE emp_id NUMBER := 100; salary NUMBER := 15000; BEGIN UpdateEmployeeSalary(emp_id, salary); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; DECLARE emp_name VARCHAR2(50); dept_name VARCHAR2(50); BEGIN SELECT employee_name, department_name INTO emp_name, dept_name FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Department Name: ' || dept_name); END; CREATE OR REPLACE FUNCTION GetAverageSalary() RETURN NUMBER AS BEGIN RETURN (SELECT AVG(salary) FROM employees); END; DECLARE salary NUMBER := GetAverageSalary(); BEGIN DBMS_OUTPUT.PUT_LINE('Average Salary: ' || salary); END; BEGIN FOR emp IN (SELECT employee_id, salary FROM employees) LOOP DBMS_OUTPUT.PUT_LINE('Employee ID: ' || emp.employee_id || ', Salary: ' || emp.salary); END LOOP; END;
CREATE PROCEDURE hello_world (@msg IN AS VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE(msg); END hello_world; CREATE FUNCTION random_number() RETURNS FLOAT AS BEGIN RETURN DBMS_RANDOM.VALUE; END; DECLARE @v_emp_id INT; v_emp_name dbo.VARCHAR2(100); BEGIN SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_id = 1; IF @v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found!', 16, 1); END END; BEGIN WHILE 1=1 BEGIN BEGIN DBMS_OUTPUT.PUT_LINE('This should never be executed.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred!'); ROLLBACK; END; EXIT; END; END;
CREATE PROCEDURE hello_world (msg IN VARCHAR2) IS BEGIN DBMS_OUTPUT.PUT_LINE(msg); END hello_world; CREATE FUNCTION random_number RETURN NUMBER AS BEGIN RETURN DBMS_RANDOM.VALUE; END random_number; DECLARE v_emp_id NUMBER(6); v_emp_name VARCHAR2(100); BEGIN SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_id = 1; IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found!'); END IF; END; BEGIN LOOP BEGIN DBMS_OUTPUT.PUT_LINE('This should never be executed.'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred!'); ROLLBACK; END; EXIT; END LOOP; END;
CREATE PROCEDURE get_employee_info(@emp_id IN AS NUMBER) AS emp_name dbo.VARCHAR2(50); salary NUMBER; BEGIN SELECT @emp_name = ename, @salary = sal FROM emp WHERE empno = emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(salary, '')); END;
CREATE PROCEDURE get_employee_info(emp_id IN NUMBER) AS emp_name VARCHAR2(50); salary NUMBER; BEGIN SELECT ename, sal INTO emp_name, salary FROM emp WHERE empno = emp_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Salary: ' || salary); END;
IF OBJECT_ID('calculate_order_total', 'P') IS NOT NULL DROP PROCEDURE calculate_order_total; GO CREATE PROCEDURE calculate_order_total(@order_id FLOAT) AS total NUMBER; BEGIN SELECT @total = SUM(quantity * price) FROM order_items WHERE order_id = @order_id; UPDATE orders SET total_amount = total WHERE order_id = @order_id; END; *** to get the customer's full name IF OBJECT_ID('get_customer_full_name', 'FN') IS NOT NULL DROP FUNCTION get_customer_full_name; GO CREATE FUNCTION get_customer_full_name(@customer_id FLOAT) RETURNS VARCHAR(4000) AS BEGIN RETURN ( SELECT isnull(first_name, '') + ' ' + isnull(last_name, '') FROM customers WHERE customer_id = @customer_id ); END; *** insert a new record into the 'customers' table INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@example.com'); *** truct to check if a customer exists IF EXISTS ( SELECT 1 FROM customers WHERE customer_id = 1 ) BEGIN *** do something END ELSE BEGIN *** exist, do something else END *** enario BEGIN *** y throw an error SELECT * FROM non_existing_table; IF @@FETCH_STATUS <> 0 BEGIN -- Ha... *** RAISERROR('-20001 No data found', 16, 1); END END;
CREATE OR REPLACE PROCEDURE calculate_order_total(@order_id NUMBER) AS total NUMBER; BEGIN SELECT SUM(quantity * price) INTO total FROM order_items WHERE order_id = @order_id; UPDATE orders SET total_amount = total WHERE order_id = @order_id; END; -- Create a function to get the customer's full name CREATE OR REPLACE FUNCTION get_customer_full_name(@customer_id NUMBER) RETURN VARCHAR2 IS BEGIN RETURN ( SELECT first_name || ' ' || last_name FROM customers WHERE customer_id = @customer_id ); END; -- SQL statement to insert a new record into the 'customers' table INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@example.com'); -- Control flow construct to check if a customer exists IF EXISTS ( SELECT 1 FROM customers WHERE customer_id = 1 ) THEN -- Customer exists, do something ELSE -- Customer does not exist, do something else END IF; -- Error handling scenario BEGIN -- Some code that may throw an error SELECT * FROM non_existing_table; EXCEPTION WHEN NO_DATA_FOUND THEN -- Handle the error RAISE_APPLICATION_ERROR(-20001, 'No data found'); END;
BEGIN DECLARE @v_emp_id INT; v_emp_name dbo.VARCHAR2(25); v_salary ROW_NUMBER(8); v_deptno ROW_NUMBER(2); *** ock begins here EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error message: ' + ISNULL(SQLERRM, '')); ROLLBACK; END; *** truct IF-THEN-ELSE begins here IF @v_emp_id IS NOT NULL BEGIN UPDATE employees SET salary = salary * (1 + (v_salary / 100)) WHERE emp_id = @v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Employee ID is NULL.'); END END;
BEGIN DECLARE v_emp_id NUMBER(6); v_emp_name VARCHAR2(25); v_salary NUMBER(8); v_deptno NUMBER(2); -- Error handling block begins here EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM); ROLLBACK; END; -- Control flow construct IF-THEN-ELSE begins here IF v_emp_id IS NOT NULL THEN UPDATE employees SET salary = salary * (1 + (v_salary / 100)) WHERE emp_id = v_emp_id; COMMIT; DBMS_OUTPUT.PUT_LINE('Salary updated successfully.'); ELSE DBMS_OUTPUT.PUT_LINE('Employee ID is NULL.'); END IF; END;
IF OBJECT_ID('insert_employee', 'P') IS NOT NULL DROP PROCEDURE insert_employee; GO CREATE PROCEDURE insert_employee ( @p_employee_id IN AS NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_phone_number IN VARCHAR2, p_hire_date IN DATE, p_job_id IN VARCHAR2, p_salary IN NUMBER, p_manager_id IN NUMBER, p_department_id IN NUMBER) IS BEGIN INSERT INTO employees ( employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, manager_id, department_id) VALUES ( p_employee_id, p_first_name, p_last_name, p_email, p_phone_number, p_hire_date, p_job_id, p_salary, p_manager_id, p_department_id); END; IF OBJECT_ID('get_employee_name', 'FN') IS NOT NULL DROP FUNCTION get_employee_name; GO CREATE FUNCTION get_employee_name ( @p_employee_id IN NUMBERDECLARE ) RETURN BEGIN VARCHAR2 RETURN NULL; END; IS l_first_name dbo.VARCHAR2(20); l_last_name dbo.VARCHAR2(20); BEGIN SELECT @l_first_name = first_name, @l_last_name = last_name FROM employees WHERE employee_id = p_employee_id; RETURN isnull(l_first_name, '') + ' ' + isnull(l_last_name, ''); END;
CREATE OR REPLACE PROCEDURE insert_employee ( p_employee_id IN NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_email IN VARCHAR2, p_phone_number IN VARCHAR2, p_hire_date IN DATE, p_job_id IN VARCHAR2, p_salary IN NUMBER, p_manager_id IN NUMBER, p_department_id IN NUMBER) IS BEGIN INSERT INTO employees ( employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, manager_id, department_id) VALUES ( p_employee_id, p_first_name, p_last_name, p_email, p_phone_number, p_hire_date, p_job_id, p_salary, p_manager_id, p_department_id); END; CREATE OR REPLACE FUNCTION get_employee_name ( p_employee_id IN NUMBER) RETURN VARCHAR2 IS l_first_name VARCHAR2(20); l_last_name VARCHAR2(20); BEGIN SELECT first_name, last_name INTO l_first_name, l_last_name FROM employees WHERE employee_id = p_employee_id; RETURN l_first_name || ' ' || l_last_name; END;
DECLARE @v_empno FLOAT; BEGIN SELECT TOP 1 @v_empno = empno FROM emp ; IF @v_empno IS NULL BEGIN RAISERROR('-20001 No employees found.', 16, 1); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('First employee number: ' + isnull(@v_empno, '')); END END;
DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE rownum <= 1; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'No employees found.'); ELSE DBMS_OUTPUT.PUT_LINE('First employee number: ' || v_empno); END IF; END;
BEGIN DECLARE @l_empno FLOAT; l_ename dbo.VARCHAR2(20); BEGIN SELECT @l_ename = ename, @l_empno = empno FROM emp WHERE empno = 7934; IF l_ename IS NULL BEGIN -- Em... *** DBMS_OUTPUT.PUT_LINE('Employee not found'); END ELSE BEGIN -- Em... *** DBMS_OUTPUT.PUT_LINE('Employee: ' + isnull(l_ename, '') + ', ' + isnull(@l_empno, '')); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END; END;
BEGIN DECLARE l_empno NUMBER; l_ename VARCHAR2(20); BEGIN SELECT ename, empno INTO l_ename, l_empno FROM emp WHERE empno = 7934; IF l_ename IS NULL THEN -- Employee not found DBMS_OUTPUT.PUT_LINE('Employee not found'); ELSE -- Employee found DBMS_OUTPUT.PUT_LINE('Employee: ' || l_ename || ', ' || l_empno); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END; END;
BEGIN -- De... *** v_emp_id NUMBER; v_salary NUMBER; v_bonus NUMBER; *** nd salary SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE name = 'John Doe'; -- Ca... *** set @v_bonus = v_salary * 0.1; *** bonus UPDATE employees SET bonus = v_bonus WHERE emp_id = v_emp_id; -- Co... *** COMMIT; -- Ha... *** EXCEPTION WHEN OTHERS THEN -- Ro... *** ROLLBACK; -- Lo... *** INSERT INTO errors (error_message) VALUES (SQLERRM); END;
BEGIN -- Declare variables v_emp_id NUMBER; v_salary NUMBER; v_bonus NUMBER; -- Get employee id and salary SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE name = 'John Doe'; -- Calculate bonus v_bonus := v_salary * 0.1; -- Update employee's bonus UPDATE employees SET bonus = v_bonus WHERE emp_id = v_emp_id; -- Commit changes COMMIT; -- Handle errors EXCEPTION WHEN OTHERS THEN -- Rollback changes ROLLBACK; -- Log error message INSERT INTO errors (error_message) VALUES (SQLERRM); END;
DECLARE @v_name VARCHAR(100); BEGIN SELECT @v_name = name FROM employees WHERE id = 1; IF @v_name IS NULL BEGIN RAISERROR('-20001 Employee not found.', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee name: ' + isnull(@v_name, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END; /
DECLARE v_name VARCHAR2(100); BEGIN SELECT name INTO v_name FROM employees WHERE id = 1; IF v_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found.'); END IF; DBMS_OUTPUT.PUT_LINE('Employee name: ' || v_name); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END; /
BEGIN -- De... *** p_id NUMBER; p_name dbo.VARCHAR2(20); BEGIN -- Op... *** DECLARE cur_rec CURSOR FOR SELECT product_id, product_name FROM products WHERE product_category = 'Electronics'; OPEN cur_rec; FETCH cur_rec INTO; WHILE @@FETCH_STATUS=0 BEGIN *** variables set @p_id = @cur_rec_product_id; set @p_name = @cur_rec_product_name; -- Up... *** UPDATE products SET product_price = product_price * 1.05 WHERE product_id = p_id; -- In... *** INSERT INTO orders ( product_id, quantity) VALUES ( p_id, 1); FETCH cur_rec INTO; END; CLOSE cur_rec; DEALLOCATE cur_rec; -- Cl... *** CLOSE cur_rec; -- Ha... *** IF @@FETCH_STATUS <> 0 BEGIN -- No... *** NULL; END END; END;
BEGIN -- Declare variables p_id NUMBER; p_name VARCHAR2(20); BEGIN -- Open a cursor FOR cur_rec IN (SELECT product_id, product_name FROM products WHERE product_category = 'Electronics') LOOP -- Assign values to variables p_id := cur_rec.product_id; p_name := cur_rec.product_name; -- Update the product UPDATE products SET product_price = product_price * 1.05 WHERE product_id = p_id; -- Insert a new order INSERT INTO orders (order_id, product_id, quantity) VALUES (order_seq.nextval, p_id, 1); END LOOP; -- Close the cursor CLOSE cur_rec; EXCEPTION -- Handle errors WHEN NO_DATA_FOUND THEN -- No records found NULL; END; END;
DECLARE @v_emp_id FLOAT := 10; v_emp_name dbo.VARCHAR2(20); BEGIN SELECT @v_emp_name = ename FROM emp WHERE empno = @v_emp_id; IF v_emp_name IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END *** v_emp_name EXCEPTION WHEN OTHERS THEN -- Ha... *** ROLLBACK; RAISE; END;
DECLARE v_emp_id NUMBER := 10; v_emp_name VARCHAR2(20); BEGIN SELECT ename INTO v_emp_name FROM emp WHERE empno = v_emp_id; IF v_emp_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; -- Do something with v_emp_name EXCEPTION WHEN OTHERS THEN -- Handle error ROLLBACK; RAISE; END;
BEGIN DECLARE @v_empno FLOAT; BEGIN SELECT @v_empno = empno FROM emp WHERE ename = 'SMITH'; IF @v_empno IS NULL BEGIN RAISERROR('-20001 Employee with name ''SMITH'' not found.', 16, 1); END END; END;
BEGIN DECLARE v_empno NUMBER; BEGIN SELECT empno INTO v_empno FROM emp WHERE ename = 'SMITH'; IF v_empno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee with name ''SMITH'' not found.'); END IF; END; END;
CREATE PROCEDURE ExampleProc(@p_id IN AS NUMBER) IS l_name dbo.VARCHAR2(100); BEGIN SELECT @l_name = name FROM MyTable WHERE id = p_id; IF l_name IS NULL BEGIN RAISERROR('-20001 No record found for ID ' + isnull(p_id, ''), 16, 1); END DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(l_name, '')); END; BEGIN dbo.ExampleProc(1); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END;
CREATE PROCEDURE ExampleProc(p_id IN NUMBER) IS l_name VARCHAR2(100); BEGIN SELECT name INTO l_name FROM MyTable WHERE id = p_id; IF l_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'No record found for ID ' || p_id); END IF; DBMS_OUTPUT.PUT_LINE('Name: ' || l_name); END; BEGIN ExampleProc(1); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; BEGIN DECLARE @emp_id FLOAT := 100; emp_salary NUMBER; BEGIN SELECT @emp_salary = dbo.get_employee_salary(@emp_id); DBMS_OUTPUT.PUT_LINE('Employee ' + isnull(@emp_id, '') + ' has a salary of ' + isnull(emp_salary, '')); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving employee salary.'); END; END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id IN NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; BEGIN DECLARE emp_id NUMBER := 100; emp_salary NUMBER; BEGIN SELECT get_employee_salary(emp_id) INTO emp_salary; DBMS_OUTPUT.PUT_LINE('Employee ' || emp_id || ' has a salary of ' || emp_salary); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving employee salary.'); END; END;
*** ion DECLARE @v_emp_id FLOAT := 10; v_salary NUMBER; BEGIN *** to fetch salary SELECT @v_salary = salary FROM employees WHERE emp_id = @v_emp_id; *** to check salary IF v_salary > 10000 BEGIN *** to increment salary UPDATE employees SET salary = salary * 1.10 WHERE emp_id = @v_emp_id; END ELSE BEGIN *** g for salary less than 10000 RAISERROR('-20001 Salary is less than 10000', 16, 1); END -- Co... *** COMMIT; END;
-- Variable declaration DECLARE v_emp_id NUMBER := 10; v_salary NUMBER; BEGIN -- Select statement to fetch salary SELECT salary INTO v_salary FROM employees WHERE emp_id = v_emp_id; -- If-else statement to check salary IF v_salary > 10000 THEN -- Update statement to increment salary UPDATE employees SET salary = salary * 1.10 WHERE emp_id = v_emp_id; ELSE -- Exception handling for salary less than 10000 RAISE_APPLICATION_ERROR(-20001, 'Salary is less than 10000'); END IF; -- Commit changes COMMIT; END;
CREATE FUNCTION get_customer_info (@custid IN NUMBERDECLARE ) RETURN BEGIN SYS_REFCURSOR RETURN NULL; END; IS CURSOR c1 IS SELECT name, city, balance FROM customer WHERE id = custid; BEGIN OPEN c1; RETURN c1; END; DECLARE @TYPE row_type IS RECORD ( name dbo.VARCHAR2(30), city dbo.VARCHAR2(20), balance ROW_NUMBER(10, 2) ); BEGIN DECLARE row CURSOR FOR SELECT * FROM TABLE(dbo.get_customer_info(100) ) LOOP DBMS_OUTPUT.PUT_LINE('Name: '; || isnull(@row_name, '') + ' City: ' + isnull(@row_city, '') + ' Balance: ' + isnull(@row_balance, '')); FETCH row INTO; END; CLOSE row; DEALLOCATE row; IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No customer found with id 100.'); END END;
CREATE FUNCTION get_customer_info (custid IN NUMBER) RETURN SYS_REFCURSOR IS CURSOR c1 IS SELECT name, city, balance FROM customer WHERE id = custid; BEGIN OPEN c1; RETURN c1; END; DECLARE TYPE row_type IS RECORD ( name VARCHAR2(30), city VARCHAR2(20), balance NUMBER(10, 2) ); BEGIN FOR row IN ( SELECT * FROM TABLE(get_customer_info(100) ) LOOP DBMS_OUTPUT.PUT_LINE('Name: ' || row.name || ' City: ' || row.city || ' Balance: ' || row.balance); END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No customer found with id 100.'); END;
DECLARE @v_deptno FLOAT; BEGIN SELECT @v_deptno = deptno FROM emp WHERE empno = 10; IF @v_deptno IS NULL BEGIN RAISERROR('-20001 Department not found', 16, 1); END END;
DECLARE v_deptno NUMBER; BEGIN SELECT deptno INTO v_deptno FROM emp WHERE empno = 10; IF v_deptno IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Department not found'); END IF; END;
BEGIN -- De... *** v_emp_id NUMBER; v_emp_name dbo.VARCHAR2(50); v_salary NUMBER; v_bonus NUMBER; v_dept_id NUMBER; v_dept_name dbo.VARCHAR2(50); *** nformation SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary, @v_bonus = bonus, @v_dept_id = dept_id FROM employees WHERE emp_id = 100; *** ompensation set @v_total_comp = v_salary + v_bonus; *** name SELECT @v_dept_name = dept_name FROM departments WHERE dept_id = v_dept_id; -- Ou... *** dbms_output.put_line('Employee Name: ' + isnull(v_emp_name, '')); dbms_output.put_line('Total Compensation: ' + isnull(v_total_comp, '')); dbms_output.put_line('Department Name: ' + isnull(v_dept_name, '')); END; /
BEGIN -- Declare variables v_emp_id NUMBER; v_emp_name VARCHAR2(50); v_salary NUMBER; v_bonus NUMBER; v_dept_id NUMBER; v_dept_name VARCHAR2(50); -- Select employee information SELECT emp_id, emp_name, salary, bonus, dept_id INTO v_emp_id, v_emp_name, v_salary, v_bonus, v_dept_id FROM employees WHERE emp_id = 100; -- Calculate total compensation v_total_comp := v_salary + v_bonus; -- Select department name SELECT dept_name INTO v_dept_name FROM departments WHERE dept_id = v_dept_id; -- Output results dbms_output.put_line('Employee Name: ' || v_emp_name); dbms_output.put_line('Total Compensation: ' || v_total_comp); dbms_output.put_line('Department Name: ' || v_dept_name); END; /
CREATE PROCEDURE InsertEmployee( @p_employee_id IN AS NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2 ) IS BEGIN INSERT INTO Employees (employee_id, first_name, last_name) VALUES (p_employee_id, p_first_name, p_last_name); COMMIT; END; / CREATE FUNCTION GetEmployeeById( @p_employee_id IN NUMBER DECLARE ) RETURN BEGIN Employees%ROWTYPE RETURN NULL; END; IS l_employee Employees%ROWTYPE; BEGIN SELECT @l_employee = * FROM Employees WHERE employee_id = p_employee_id; RETURN l_employee; END; /
PROCEDURE InsertEmployee( p_employee_id IN NUMBER, p_first_name IN VARCHAR2, p_last_name IN VARCHAR2 ) IS BEGIN INSERT INTO Employees (employee_id, first_name, last_name) VALUES (p_employee_id, p_first_name, p_last_name); COMMIT; END; / FUNCTION GetEmployeeById( p_employee_id IN NUMBER ) RETURN Employees%ROWTYPE IS l_employee Employees%ROWTYPE; BEGIN SELECT * INTO l_employee FROM Employees WHERE employee_id = p_employee_id; RETURN l_employee; END; /
DECLARE @emp_id INT; emp_name dbo.VARCHAR2(30); dept_id ROW_NUMBER(3); dept_name dbo.VARCHAR2(40); BEGIN SELECT @emp_id = employee_id, @emp_name = first_name, @dept_id = department_id FROM employees WHERE last_name = 'King'; SELECT @dept_name = department_name FROM departments WHERE department_id = dept_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(emp_name, '')); DBMS_OUTPUT.PUT_LINE('Department Name: ' + isnull(dept_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee with last name ''King'' found'); END END;
DECLARE emp_id NUMBER(6); emp_name VARCHAR2(30); dept_id NUMBER(3); dept_name VARCHAR2(40); BEGIN SELECT employee_id, first_name, department_id INTO emp_id, emp_name, dept_id FROM employees WHERE last_name = 'King'; SELECT department_name INTO dept_name FROM departments WHERE department_id = dept_id; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_name); DBMS_OUTPUT.PUT_LINE('Department Name: ' || dept_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee with last name ''King'' found'); END;
DECLARE @salary DECIMAL(10,2); DECLARE @city VARCHAR(25); BEGIN SELECT @salary = @salary FROM employees WHERE emp_id = 100; END; BEGIN SELECT @city = @city FROM employees WHERE dept_id = 10; END; IF @salary > 50000 BEGIN dbo.RAISE_SALARY(@salary * 0.10); END EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; CREATE PROCEDURE RAISE_SALARY(@amount FLOAT) AS BEGIN SET NOCOUNT ON; UPDATE employees SET salary = salary + @amount WHERE emp_id = 100; END;
DECLARE salary NUMBER(10,2); DECLARE city VARCHAR2(25); BEGIN SELECT salary INTO salary FROM employees WHERE emp_id = 100; END; BEGIN SELECT city INTO city FROM employees WHERE dept_id = 10; END; IF salary > 50000 THEN RAISE_SALARY(salary * 0.10); END IF; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; PROCEDURE RAISE_SALARY(amount NUMBER) IS BEGIN UPDATE employees SET salary = salary + amount WHERE emp_id = 100; END;
IF OBJECT_ID('sp_process_order', 'P') IS NOT NULL DROP PROCEDURE sp_process_order; GO CREATE PROCEDURE sp_process_order(@p_order_id IN AS NUMBER) IS BEGIN UPDATE orders SET status = 'processed' WHERE order_id = p_order_id; COMMIT; END; DECLARE @v_order_id FLOAT := 12345; BEGIN dbo.sp_process_order(@v_order_id); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; SELECT * FROM orders WHERE status = 'processed';
CREATE OR REPLACE PROCEDURE sp_process_order(p_order_id IN NUMBER) IS BEGIN UPDATE orders SET status = 'processed' WHERE order_id = p_order_id; COMMIT; END; DECLARE v_order_id NUMBER := 12345; BEGIN sp_process_order(v_order_id); EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END; SELECT * FROM orders WHERE status = 'processed';
IF OBJECT_ID('employee_details', 'P') IS NOT NULL DROP PROCEDURE employee_details; GO CREATE PROCEDURE employee_details(@emp_id IN AS NUMBER) AS BEGIN SELECT * FROM employees WHERE employee_id = emp_id; END; DECLARE @amount DECIMAL(10,2) := 500; BEGIN IF @amount > 1000 BEGIN UPDATE accounts SET balance = balance + @amount; END ELSE BEGIN UPDATE accounts SET balance = balance - @amount; END END; BEGIN BEGIN INSERT INTO orders (order_id, customer_id, order_date) VALUES (1, 1, GETDATE()); EXCEPTION WHEN OTHERS THEN -- Ha... *** END; END;
CREATE OR REPLACE PROCEDURE employee_details(emp_id IN NUMBER) AS BEGIN SELECT * FROM employees WHERE employee_id = emp_id; END; DECLARE amount NUMBER(10,2) := 500; BEGIN IF amount > 1000 THEN UPDATE accounts SET balance = balance + amount; ELSE UPDATE accounts SET balance = balance - amount; END IF; END; BEGIN BEGIN INSERT INTO orders (order_id, customer_id, order_date) VALUES (1, 1, SYSDATE); EXCEPTION WHEN OTHERS THEN -- Handle error END; END;
BEGIN *** e snippet DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(100); BEGIN *** tails SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_id = 1; *** tails DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.'); END END; END;
BEGIN -- Sample PL/SQL code snippet DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(100); BEGIN -- Fetch employee details SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_id = 1; -- Print employee details DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found with the given ID.'); 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, salary_increase IN NUMBER, OUT updated_salary OUT NUMBER ) AS BEGIN *** ee's salary UPDATE employees SET salary = salary + salary_increase WHERE employee_id = employee_id; *** alary SELECT @updated_salary = salary FROM employees WHERE employee_id = employee_id; IF @@FETCH_STATUS <> 0 BEGIN set @updated_salary = NULL; RAISE; END END;
CREATE OR REPLACE PROCEDURE update_employee_salary( employee_id IN NUMBER, salary_increase IN NUMBER, OUT updated_salary OUT NUMBER ) AS BEGIN -- Update the employee's salary UPDATE employees SET salary = salary + salary_increase WHERE employee_id = employee_id; -- Get the updated salary SELECT salary INTO updated_salary FROM employees WHERE employee_id = employee_id; EXCEPTION WHEN NO_DATA_FOUND THEN updated_salary := NULL; RAISE; END;
DECLARE @v_emp_id FLOAT; v_emp_name dbo.VARCHAR2(50); BEGIN SELECT @v_emp_id = emp_id, @v_emp_name = emp_name FROM employees WHERE emp_id = :p_emp_id; IF @v_emp_id IS NULL BEGIN RAISERROR('-20001 Employee not found.', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee ID: ' + isnull(@v_emp_id, '')); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); END;
DECLARE v_emp_id NUMBER; v_emp_name VARCHAR2(50); BEGIN SELECT emp_id, emp_name INTO v_emp_id, v_emp_name FROM employees WHERE emp_id = :p_emp_id; IF v_emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found.'); END IF; DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_emp_id); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); END;
CREATE PROCEDURE MyProcedure ( @param1 VARCHAR(50), @param2 INT ) AS BEGIN SET NOCOUNT ON; UPDATE MyTable SET MyColumn = @param1 WHERE MyID = @param2; IF @@ROWCOUNT > 0 BEGIN SELECT * FROM MyTable WHERE MyID = @param2; END; ELSE BEGIN RAISE ERROR('Row not found', -1); END; END; CREATE FUNCTION MyFunction ( @param1 VARCHAR(50) ) RETURNS INT AS BEGIN DECLARE @result INT; SELECT @result = COUNT(*) FROM MyTable WHERE MyColumn = @param1; RETURN @result; END; BEGIN BEGIN TRANSACTION; INSERT INTO MyTable (MyColumn) VALUES ('Test1'); INSERT INTO MyTable (MyColumn) VALUES ('Test2'); UPDATE MyTable SET MyColumn = 'Updated' WHERE MyColumn = 'Test1'; DELETE FROM MyTable WHERE MyColumn = 'Test2'; COMMIT TRANSACTION; END;
CREATE PROCEDURE MyProcedure ( @param1 VARCHAR(50), @param2 INT ) AS BEGIN UPDATE MyTable SET MyColumn = @param1 WHERE MyID = @param2; IF @@ROWCOUNT > 0 BEGIN SELECT * FROM MyTable WHERE MyID = @param2; END ELSE BEGIN RAISE ERROR('Row not found', -1); END; END; CREATE FUNCTION MyFunction ( @param1 VARCHAR(50) ) RETURNS INT AS BEGIN DECLARE @result INT; SELECT COUNT(*) INTO @result FROM MyTable WHERE MyColumn = @param1; RETURN @result; END; BEGIN BEGIN TRANSACTION; INSERT INTO MyTable (MyColumn) VALUES ('Test1'); INSERT INTO MyTable (MyColumn) VALUES ('Test2'); UPDATE MyTable SET MyColumn = 'Updated' WHERE MyColumn = 'Test1'; DELETE FROM MyTable WHERE MyColumn = 'Test2'; COMMIT TRANSACTION; END;
IF OBJECT_ID('perform_updates', 'P') IS NOT NULL DROP PROCEDURE perform_updates; GO CREATE PROCEDURE perform_updates(@p_id IN AS NUMBER, p_new_name IN VARCHAR2) IS -- Lo... *** l_old_name dbo.VARCHAR2(50); BEGIN *** of the person SELECT @l_old_name = name FROM persons WHERE id = p_id; *** 's name UPDATE persons SET name = p_new_name WHERE id = p_id; -- Co... *** COMMIT; *** d new names DBMS_OUTPUT.PUT_LINE('Old name: ' + isnull(l_old_name, '')); DBMS_OUTPUT.PUT_LINE('New name: ' + isnull(p_new_name, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No person found with id ' + isnull(p_id, '')); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END perform_updates;
CREATE OR REPLACE PROCEDURE perform_updates(p_id IN NUMBER, p_new_name IN VARCHAR2) IS -- Local variables l_old_name VARCHAR2(50); BEGIN -- Get the old name of the person SELECT name INTO l_old_name FROM persons WHERE id = p_id; -- Update the person's name UPDATE persons SET name = p_new_name WHERE id = p_id; -- Commit the changes COMMIT; -- Output the old and new names DBMS_OUTPUT.PUT_LINE('Old name: ' || l_old_name); DBMS_OUTPUT.PUT_LINE('New name: ' || p_new_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No person found with id ' || p_id); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END perform_updates;
IF OBJECT_ID('test_proc', 'P') IS NOT NULL DROP PROCEDURE test_proc; GO CREATE PROCEDURE test_proc (@a FLOAT, @b VARCHAR(4000)) AS BEGIN SET NOCOUNT ON; IF (@a < 0) BEGIN RAISERROR('-20001 Invalid value for parameter A', 16, 1); END UPDATE my_table SET col1 = @b WHERE col2 = @a; END; BEGIN dbo.test_proc(10, 'test'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
CREATE OR REPLACE PROCEDURE test_proc (a NUMBER, b VARCHAR2) IS BEGIN IF (a < 0) THEN RAISE_APPLICATION_ERROR(-20001, 'Invalid value for parameter A'); END IF; UPDATE my_table SET col1 = b WHERE col2 = a; END test_proc; BEGIN test_proc(10, 'test'); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
WITH EmployeeDetails AS ( SELECT EmployeeID, FirstName, LastName, Salary FROM Employees ) SELECT ed.FirstName, ed.LastName, ISNULL(ed.Salary, 0) AS Salary, ISNULL(AVG(ed.Salary), 0) [OVER] (PARTITION BY ed.LastName) AS AvgSalaryByLastName FROM EmployeeDetails ed; *** to insert a new employee IF OBJECT_ID('Insert_Employee', 'P') IS NOT NULL DROP PROCEDURE Insert_Employee; GO CREATE PROCEDURE Insert_Employee( @p_FirstName IN AS VARCHAR2, p_LastName IN VARCHAR2, p_Salary IN NUMBER ) IS BEGIN INSERT INTO Employees (FirstName, LastName, Salary) VALUES (p_FirstName, p_LastName, p_Salary); END Insert_Employee; *** late the total salary of all employees IF OBJECT_ID('Get_Total_Salary', 'FN') IS NOT NULL DROP FUNCTION Get_Total_Salary; GO CREATE FUNCTION Get_Total_Salary() RETURNS FLOAT AS BEGIN RETURN (SELECT SUM(Salary) FROM Employees); END;
WITH EmployeeDetails AS ( SELECT EmployeeID, FirstName, LastName, Salary FROM Employees ) SELECT ed.FirstName, ed.LastName, NVL(ed.Salary, 0) AS Salary, NVL(AVG(ed.Salary), 0) OVER (PARTITION BY ed.LastName) AS AvgSalaryByLastName FROM EmployeeDetails ed; -- Stored procedure to insert a new employee CREATE OR REPLACE PROCEDURE Insert_Employee( p_FirstName IN VARCHAR2, p_LastName IN VARCHAR2, p_Salary IN NUMBER ) IS BEGIN INSERT INTO Employees (FirstName, LastName, Salary) VALUES (p_FirstName, p_LastName, p_Salary); END Insert_Employee; -- Function to calculate the total salary of all employees CREATE OR REPLACE FUNCTION Get_Total_Salary RETURN NUMBER IS BEGIN RETURN (SELECT SUM(Salary) FROM Employees); END Get_Total_Salary;
BEGIN DECLARE @i INT = 1; WHILE @i <= 5 BEGIN dbms_output.put_line('Iteration ' + isnull(i, '') + ' of 5 completed.'); END; END;
BEGIN FOR i IN 1..5 LOOP dbms_output.put_line('Iteration ' || i || ' of 5 completed.'); END LOOP; END;
DECLARE @user_id INT; @user_name VARCHAR(50); BEGIN SELECT @user_id = @user_id, @user_name = @user_name FROM users WHERE username = 'testuser'; IF @user_id IS NULL BEGIN dbo.RAISE_EXCEPTION('User not found'); END UPDATE users SET password = 'new_password' WHERE id = @user_id; IF @@ERROR <> 0 BEGIN dbo.RAISE_EXCEPTION('Error updating user'); END END
DECLARE @user_id INT; @user_name VARCHAR(50); BEGIN SELECT @user_id, @user_name INTO @user_id, @user_name FROM users WHERE username = 'testuser'; IF @user_id IS NULL THEN RAISE_EXCEPTION('User not found'); END IF; UPDATE users SET password = 'new_password' WHERE id = @user_id; IF @@ERROR <> 0 THEN RAISE_EXCEPTION('Error updating user'); END IF; END
IF OBJECT_ID('proc_example', 'P') IS NOT NULL DROP PROCEDURE proc_example; GO CREATE PROCEDURE proc_example (@p_id IN AS NUMBER) AS BEGIN UPDATE table_name SET col_name = 'value' WHERE col_id = p_id; IF @@ERROR <> 0 BEGIN ROLLBACK; RAISERROR('-20000 Error updating table', 16, 1); END COMMIT; END;
CREATE OR REPLACE PROCEDURE proc_example (p_id IN NUMBER) AS BEGIN UPDATE table_name SET col_name = 'value' WHERE col_id = p_id; IF SQLCODE <> 0 THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20000, 'Error updating table'); END IF; COMMIT; END;
DECLARE @v_emp_id FLOAT; v_salary NUMBER; BEGIN SELECT @v_emp_id = emp_id, @v_salary = salary FROM employees WHERE emp_name = 'John Doe'; IF v_salary > 10000 BEGIN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = @v_emp_id; END ELSE IF v_salary < 5000 BEGIN RAISERROR('-20001 Salary too low', 16, 1); END COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_emp_id NUMBER; v_salary NUMBER; BEGIN SELECT emp_id, salary INTO v_emp_id, v_salary FROM employees WHERE emp_name = 'John Doe'; IF v_salary > 10000 THEN UPDATE employees SET salary = salary * 1.1 WHERE emp_id = v_emp_id; ELSIF v_salary < 5000 THEN RAISE_APPLICATION_ERROR(-20001, 'Salary too low'); END IF; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id; RETURN @salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @emp_salary FLOAT; BEGIN SET @emp_salary = dbo.get_employee_salary(100); DBMS_OUTPUT.PUT_LINE('Employee salary: ' + isnull(@emp_salary, '')); END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE emp_salary NUMBER; BEGIN emp_salary := get_employee_salary(100); DBMS_OUTPUT.PUT_LINE('Employee salary: ' || emp_salary); END;
CREATE PROCEDURE get_employee_details (@p_emp_id IN AS NUMBER) AS v_emp_name VARCHAR(25); v_emp_salary ROW_NUMBER(10,2); BEGIN SELECT @v_emp_name = emp_name, @v_emp_salary = emp_salary FROM employees WHERE emp_id = p_emp_id; IF v_emp_name IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_emp_salary, '')); END;
CREATE PROCEDURE get_employee_details (p_emp_id IN NUMBER) AS v_emp_name VARCHAR(25); v_emp_salary NUMBER(10,2); BEGIN SELECT emp_name, emp_salary INTO v_emp_name, v_emp_salary FROM employees WHERE emp_id = p_emp_id; IF v_emp_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END IF; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_emp_salary); END;
DECLARE @v_name VARCHAR(20); v_age NUMBER; BEGIN SELECT @v_name = name, @v_age = age FROM employees WHERE id = 1; IF v_age > 30 BEGIN UPDATE employees SET salary = salary * 1.1 WHERE name = @v_name; DBMS_OUTPUT.PUT_LINE('Salary updated for ' + isnull(@v_name, '')); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('No salary update for ' + isnull(@v_name, '')); END IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' + ISNULL(SQLERRM, '')); END; /
DECLARE v_name VARCHAR2(20); v_age NUMBER; BEGIN SELECT name, age INTO v_name, v_age FROM employees WHERE id = 1; IF v_age > 30 THEN UPDATE employees SET salary = salary * 1.1 WHERE name = v_name; DBMS_OUTPUT.PUT_LINE('Salary updated for ' || v_name); ELSE DBMS_OUTPUT.PUT_LINE('No salary update for ' || v_name); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM); END; /
DECLARE @employee_id FLOAT; BEGIN SET @employee_id = 123; *** department UPDATE employees SET department_id = 10 WHERE @employee_id = @employee_id; *** ful update IF @@ROWCOUNT > 0 BEGIN *** n if no rows affected RAISERROR('-20001 No rows affected', 16, 1); END -- Co... *** COMMIT; EXCEPTION WHEN OTHERS THEN -- Ha... *** ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred: ' + ISNULL(SQLERRM, '')); END;
DECLARE employee_id NUMBER; BEGIN employee_id := 123; -- Update employee's department UPDATE employees SET department_id = 10 WHERE employee_id = employee_id; -- Check for successful update IF SQL%ROWCOUNT > 0 THEN -- Raise an exception if no rows affected RAISE_APPLICATION_ERROR(-20001, 'No rows affected'); END IF; -- Commit the changes COMMIT; EXCEPTION WHEN OTHERS THEN -- Handle any errors ROLLBACK; DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM); END;
CREATE TABLE products (product_id FLOAT, name VARCHAR(50), price FLOAT, PRIMARY KEY (product_id)); INSERT INTO products (product_id, name, price) VALUES (1, 'Product 1', 100); INSERT INTO products (product_id, name, price) VALUES (2, 'Product 2', 200); INSERT INTO products (product_id, name, price) VALUES (3, 'Product 3', 300); CREATE FUNCTION get_product_name (@product_id FLOAT) RETURNS VARCHAR(4000) AS BEGIN DECLARE @v_name VARCHAR(50); SELECT @v_name = name FROM products WHERE @product_id = @product_id; RETURN @v_name; END; DECLARE @v_product_id FLOAT := 2; v_product_name dbo.VARCHAR2(50); BEGIN SET @v_product_name = dbo.get_product_name(@v_product_id); IF v_product_name IS NULL BEGIN RAISERROR('-20001 Product not found.', 16, 1); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Product Name: ' + isnull(v_product_name, '')); END END;
CREATE TABLE products (product_id NUMBER, name VARCHAR2(50), price NUMBER, PRIMARY KEY (product_id)); INSERT INTO products (product_id, name, price) VALUES (1, 'Product 1', 100); INSERT INTO products (product_id, name, price) VALUES (2, 'Product 2', 200); INSERT INTO products (product_id, name, price) VALUES (3, 'Product 3', 300); CREATE FUNCTION get_product_name (product_id NUMBER) RETURN VARCHAR2 IS v_name VARCHAR2(50); BEGIN SELECT name INTO v_name FROM products WHERE product_id = product_id; RETURN v_name; END; DECLARE v_product_id NUMBER := 2; v_product_name VARCHAR2(50); BEGIN v_product_name := get_product_name(v_product_id); IF v_product_name IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Product not found.'); ELSE DBMS_OUTPUT.PUT_LINE('Product Name: ' || v_product_name); END IF; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS salary NUMBER; BEGIN SELECT @salary = salary FROM employees WHERE employee_id = emp_id; RETURN salary; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END;
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;
BEGIN -- De... *** DECLARE @emp_id FLOAT; salary NUMBER; BEGIN *** nd salary SELECT @emp_id = @emp_id, @salary = salary FROM employees WHERE last_name = 'Smith'; *** exists IF @emp_id IS NULL BEGIN RAISERROR('-20000 Employee not found', 16, 1); END -- Up... *** UPDATE employees SET salary = salary * 1.10 WHERE @emp_id = @emp_id; -- Co... *** COMMIT; EXCEPTION -- Ha... *** WHEN OTHERS THEN ROLLBACK; RAISERROR('-20001 Error updating employee salary', 16, 1); END; END;
BEGIN -- Declare variables DECLARE emp_id NUMBER; salary NUMBER; BEGIN -- Get employee ID and salary SELECT emp_id, salary INTO emp_id, salary FROM employees WHERE last_name = 'Smith'; -- Check if employee exists IF emp_id IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'Employee not found'); END IF; -- Update salary UPDATE employees SET salary = salary * 1.10 WHERE emp_id = emp_id; -- Commit changes COMMIT; EXCEPTION -- Handle errors WHEN OTHERS THEN ROLLBACK; RAISE_APPLICATION_ERROR(-20001, 'Error updating employee salary'); END; END;
IF OBJECT_ID('add_customer', 'P') IS NOT NULL DROP PROCEDURE add_customer; GO CREATE PROCEDURE add_customer ( @p_customer_id IN AS NUMBER, p_name IN VARCHAR2, p_address IN VARCHAR2, p_city IN VARCHAR2, p_state IN VARCHAR2, p_zip IN VARCHAR2, p_phone IN VARCHAR2, p_email IN VARCHAR2 ) AS BEGIN *** stomer into the CUSTOMER table. INSERT INTO CUSTOMER ( customer_id, name, address, city, state, zip, phone, email ) VALUES ( p_customer_id, p_name, p_address, p_city, p_state, p_zip, p_phone, p_email ); END;
CREATE OR REPLACE PROCEDURE add_customer ( p_customer_id IN NUMBER, p_name IN VARCHAR2, p_address IN VARCHAR2, p_city IN VARCHAR2, p_state IN VARCHAR2, p_zip IN VARCHAR2, p_phone IN VARCHAR2, p_email IN VARCHAR2 ) AS BEGIN -- Insert the new customer into the CUSTOMER table. INSERT INTO CUSTOMER ( customer_id, name, address, city, state, zip, phone, email ) VALUES ( p_customer_id, p_name, p_address, p_city, p_state, p_zip, p_phone, p_email ); END;
IF OBJECT_ID('get_employee_details', 'P') IS NOT NULL DROP PROCEDURE get_employee_details; GO CREATE PROCEDURE get_employee_details( @emp_id IN AS NUMBER, OUT emp_name OUT VARCHAR2, OUT salary OUT NUMBER, OUT hire_date OUT DATE) IS BEGIN SELECT @emp_name = first_name, @emp_name = last_name, @salary = salary, @hire_date = hire_date FROM employees WHERE employee_id = emp_id; IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 No employee found with the given ID.', 16, 1); END END;
CREATE OR REPLACE PROCEDURE get_employee_details( emp_id IN NUMBER, OUT emp_name OUT VARCHAR2, OUT salary OUT NUMBER, OUT hire_date OUT DATE) IS BEGIN SELECT first_name, last_name, salary, hire_date INTO emp_name, emp_name, salary, hire_date FROM employees WHERE employee_id = emp_id; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'No employee found with the given ID.'); 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 *** iables old_salary NUMBER; BEGIN *** alary of the employee SELECT @old_salary = salary FROM employees WHERE employee_id = employee_id; *** of the employee UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; *** ry was updated successfully IF old_salary = new_salary BEGIN RAISERROR('-20001 Salary not updated', 16, 1); END -- Co... *** COMMIT; END; /
CREATE OR REPLACE PROCEDURE update_employee_salary( employee_id IN NUMBER, new_salary IN NUMBER ) IS -- Declare local variables old_salary NUMBER; BEGIN -- Get the current salary of the employee SELECT salary INTO old_salary FROM employees WHERE employee_id = employee_id; -- Update the salary of the employee UPDATE employees SET salary = new_salary WHERE employee_id = employee_id; -- Check if the salary was updated successfully IF old_salary = new_salary THEN RAISE_APPLICATION_ERROR(-20001, 'Salary not updated'); END IF; -- Commit the changes COMMIT; END; /
declare @v_salary float; begin select @v_salary = salary from employees where employee_id = 1; if @v_salary > 10000 begin dbo.raise_salary(@v_salary); end commit; end; create procedure raise_salary(@p_salary float) as begin set nocount on; update employees set salary = @p_salary * 1.1 where employee_id = 1; end;
declare v_salary number; begin select salary into v_salary from employees where employee_id = 1; if v_salary > 10000 then raise_salary(v_salary); end if; commit; end; procedure raise_salary(p_salary number) is begin update employees set salary = p_salary * 1.1 where employee_id = 1; end;
IF OBJECT_ID('get_total_sales', 'FN') IS NOT NULL DROP FUNCTION get_total_sales; GO CREATE FUNCTION get_total_sales(@product_id IN NUMBERDECLARE ) RETURN BEGIN NUMBER RETURN NULL; END; IS total_sales NUMBER; BEGIN SELECT @total_sales = SUM(quantity * unit_price) FROM sales WHERE product_id = product_id; RETURN total_sales; IF @@FETCH_STATUS <> 0 BEGIN RETURN NULL; END END; DECLARE @total_sales FLOAT; BEGIN SET @total_sales = dbo.get_total_sales(123); IF @total_sales IS NULL BEGIN RAISERROR('-20000 No sales data for product 123', 16, 1); END ELSE BEGIN DBMS_OUTPUT.PUT_LINE('Total sales for product 123: ' + isnull(@total_sales, '')); END END;
CREATE OR REPLACE FUNCTION get_total_sales(product_id IN NUMBER) RETURN NUMBER IS total_sales NUMBER; BEGIN SELECT SUM(quantity * unit_price) INTO total_sales FROM sales WHERE product_id = product_id; RETURN total_sales; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END; DECLARE total_sales NUMBER; BEGIN total_sales := get_total_sales(123); IF total_sales IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'No sales data for product 123'); ELSE DBMS_OUTPUT.PUT_LINE('Total sales for product 123: ' || total_sales); END IF; END;
BEGIN -- De... *** v_emp_id NUMBER; v_emp_name dbo.VARCHAR2(100); v_emp_salary NUMBER; *** mployee ID DBMS_OUTPUT.PUT_LINE('Enter employee ID: '); DBMS_INPUT.GET_LINE(v_emp_id); *** e to get employee details SELECT @v_emp_name = ename, @v_emp_salary = sal FROM emp WHERE empno = v_emp_id; *** exists IF v_emp_name IS NULL BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found.'); END ELSE BEGIN *** details DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(v_emp_name, '')); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' + isnull(v_emp_salary, '')); END END;
BEGIN -- Declare variables v_emp_id NUMBER; v_emp_name VARCHAR2(100); v_emp_salary NUMBER; -- Prompt user for employee ID DBMS_OUTPUT.PUT_LINE('Enter employee ID: '); DBMS_INPUT.GET_LINE(v_emp_id); -- Query the database to get employee details SELECT ename, sal INTO v_emp_name, v_emp_salary FROM emp WHERE empno = v_emp_id; -- Check if employee exists IF v_emp_name IS NULL THEN DBMS_OUTPUT.PUT_LINE('Employee not found.'); ELSE -- Display employee details DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name); DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_emp_salary); END IF; END;
CREATE PROCEDURE GetEmployeeInfo ( @emp_id IN AS NUMBER, OUT emp_name VARCHAR2, OUT salary NUMBER ) IS BEGIN SELECT @emp_name = first_name, @emp_name = last_name, @salary = salary FROM employees WHERE employee_id = emp_id; END GetEmployeeInfo; DECLARE @l_emp_id FLOAT := 100; l_emp_name dbo.VARCHAR2(50); l_salary NUMBER; BEGIN dbo.GetEmployeeInfo(@l_emp_id, l_emp_name, l_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' + isnull(l_emp_name, '')); DBMS_OUTPUT.PUT_LINE('Salary: ' + isnull(l_salary, '')); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('Employee not found'); END END;
CREATE PROCEDURE GetEmployeeInfo ( emp_id IN NUMBER, OUT emp_name VARCHAR2, OUT salary NUMBER ) IS BEGIN SELECT first_name, last_name, salary INTO emp_name, emp_name, salary FROM employees WHERE employee_id = emp_id; END GetEmployeeInfo; DECLARE l_emp_id NUMBER := 100; l_emp_name VARCHAR2(50); l_salary NUMBER; BEGIN GetEmployeeInfo(l_emp_id, l_emp_name, l_salary); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || l_emp_name); DBMS_OUTPUT.PUT_LINE('Salary: ' || l_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;
DECLARE @v_salary FLOAT; v_emp_id SET @NUMBER = 100; BEGIN SELECT @v_salary = salary FROM employees WHERE employee_id = v_emp_id; IF @v_salary IS NULL BEGIN RAISERROR('-20001 Employee not found', 16, 1); END ELSE BEGIN UPDATE employees SET salary = @v_salary * 1.10 WHERE employee_id = v_emp_id; END END;
DECLARE v_salary NUMBER; v_emp_id NUMBER := 100; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = v_emp_id; IF v_salary IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); ELSE UPDATE employees SET salary = v_salary * 1.10 WHERE employee_id = v_emp_id; END IF; END;
BEGIN -- De... *** DECLARE @v_emp_id FLOAT; DECLARE @v_emp_name VARCHAR(100); DECLARE @v_salary FLOAT; *** ils SELECT @v_emp_id = emp_id, @v_emp_name = emp_name, @v_salary = salary FROM employees WHERE emp_id = 1; *** etails UPDATE employees SET salary = @v_salary + 1000 WHERE emp_id = @v_emp_id; -- Co... *** COMMIT; *** mployee details SELECT emp_id, emp_name, salary FROM employees WHERE emp_id = @v_emp_id; END;
BEGIN -- Declare variables DECLARE v_emp_id NUMBER; DECLARE v_emp_name VARCHAR2(100); DECLARE v_salary NUMBER; -- Get employee details SELECT emp_id, emp_name, salary INTO v_emp_id, v_emp_name, v_salary FROM employees WHERE emp_id = 1; -- Update employee details UPDATE employees SET salary = v_salary + 1000 WHERE emp_id = v_emp_id; -- Commit changes COMMIT; -- Display updated employee details SELECT emp_id, emp_name, salary FROM employees WHERE emp_id = v_emp_id; END;
IF OBJECT_ID('get_employee_salary', 'FN') IS NOT NULL DROP FUNCTION get_employee_salary; GO CREATE FUNCTION get_employee_salary(@emp_id FLOAT) RETURNS FLOAT AS BEGIN DECLARE @salary FLOAT; SELECT @salary = @salary FROM employees WHERE employee_id = @emp_id; RETURN @salary; IF @@fetch_status <> 0 BEGIN RAISERROR('-20001 Employee not found', 16, 1); END END;
CREATE OR REPLACE FUNCTION get_employee_salary(emp_id NUMBER) RETURN NUMBER IS salary NUMBER; BEGIN SELECT salary INTO salary FROM employees WHERE employee_id = emp_id; RETURN salary; EXCEPTION WHEN no_data_found THEN RAISE_APPLICATION_ERROR(-20001, 'Employee not found'); END;
CREATE PROCEDURE my_proc (@id IN AS NUMBER) AS BEGIN DECLARE @x FLOAT; BEGIN SELECT @x = name FROM my_table WHERE id = id; DBMS_OUTPUT.PUT_LINE(@x); IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found for id: ' + isnull(id, '')); END END; END;
CREATE PROCEDURE my_proc (id IN NUMBER) AS BEGIN DECLARE x NUMBER; BEGIN SELECT name INTO x FROM my_table WHERE id = id; DBMS_OUTPUT.PUT_LINE(x); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found for id: ' || id); END; END;
BEGIN DECLARE @i INT = 1 ; WHILE @i <= 10 BEGIN DBMS_OUTPUT.PUT_LINE('Hello world!'); COMMIT; END; BEGIN DECLARE @v_salary FLOAT; BEGIN SELECT @v_salary = salary FROM emp WHERE empno = 7369; IF @v_salary IS NULL BEGIN RAISERROR('-20000 No employee with that id', 16, 1); END IF @@FETCH_STATUS <> 0 BEGIN RAISERROR('-20001 No data found', 16, 1); END WHEN TOO_MANY_ROWS THEN RAISERROR('-20002 Too many rows returned', 16, 1); END; END; IF i > 10 BEGIN ROLLBACK; END ELSE BEGIN COMMIT; END END;
BEGIN FOR i IN 1 .. 10 LOOP DBMS_OUTPUT.PUT_LINE('Hello world!'); COMMIT; END LOOP; BEGIN DECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM emp WHERE empno = 7369; IF v_salary IS NULL THEN RAISE_APPLICATION_ERROR(-20000, 'No employee with that id'); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'No data found'); WHEN TOO_MANY_ROWS THEN RAISE_APPLICATION_ERROR(-20002, 'Too many rows returned'); END; END; IF i > 10 THEN ROLLBACK; ELSE COMMIT; END IF; END;
DECLARE @v_salary FLOAT; v_bonus NUMBER; BEGIN SELECT @v_salary = salary, @v_bonus = bonus FROM employees WHERE employee_id = 1; IF @v_salary > 50000 BEGIN SET @v_bonus = v_bonus * 1.1; END UPDATE employees SET bonus = v_bonus WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
DECLARE v_salary NUMBER; v_bonus NUMBER; BEGIN SELECT salary, bonus INTO v_salary, v_bonus FROM employees WHERE employee_id = 1; IF v_salary > 50000 THEN v_bonus := v_bonus * 1.1; END IF; UPDATE employees SET bonus = v_bonus WHERE employee_id = 1; COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; RAISE; END;
BEGIN -- De... *** v_id NUMBER; v_name dbo.VARCHAR2(20); -- Ge... *** SELECT @v_id = id, @v_name = name FROM table_name WHERE id = 1; -- Di... *** DBMS_OUTPUT.PUT_LINE('ID: ' + isnull(v_id, '')); DBMS_OUTPUT.PUT_LINE('Name: ' + isnull(v_name, '')); -- Er... *** IF @@FETCH_STATUS <> 0 BEGIN DBMS_OUTPUT.PUT_LINE('No data found.'); END WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;
BEGIN -- Declare variables v_id NUMBER; v_name VARCHAR2(20); -- Get input values SELECT id, name INTO v_id, v_name FROM table_name WHERE id = 1; -- Display results DBMS_OUTPUT.PUT_LINE('ID: ' || v_id); DBMS_OUTPUT.PUT_LINE('Name: ' || v_name); -- Error handling EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data found.'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An unexpected error occurred.'); END;