I want to create a procedure that deletes rows from the Employee table. It should accept 1 parameter that is department name; only delete the employee records belonging to that department. Display how many employees were deleted else raise "DeptNotFoundException" and print the message 'No Records found.'.
EMPLOYEE:
Column name Data type Constraints
EMP_ID NUMBER(5) PK
EMP_NAME VARCHAR2(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR2(25)
Sample Output:
2 Employee record(s) got deleted.
The code I tried is -
create or replace procedure DELETE_EMPLOYEE(v_dept IN EMPLOYEE.dept%TYPE)
is
begin
delete EMPLOYEE where dept = v_dept;
commit;
end;
/
It works fine to delete the rows, but I don't know how to find the count of the deleted rows and display it in the result, please help regarding this. Thank you for help.
Hint: sql%rowcount.
SQL> create or replace procedure DELETE_EMPLOYEE(v_dept IN EMPLOYEE.dept%TYPE)
2 is
3 begin
4 delete EMPLOYEE where dept = v_dept;
5 dbms_output.put_Line('deleted ' || sql%rowcount || ' row(s)');
6 end;
7 /
Procedure created.
SQL> set serveroutput on
SQL> exec delete_employee(10);
deleted 3 row(s)
PL/SQL procedure successfully completed.
SQL>
Also, I wouldn't commit in the procedure; let the caller decide and commit (or not).
Related
please help me.
Where I am wrong in my code.
Works OK if column (EMP_ID) you used in the WHEN clause exists. Though, as William commented, why would you check it? It can't be NULL, that's for sure (as it is a primary key), but - what does it matter if it is positive, 0, or negative?
SQL> create table employee
2 (emp_id number primary key,
3 emp_name varchar2(10),
4 salary number
5 );
Table created.
SQL> create or replace trigger trg_bie
2 after insert on employee
3 for each row
4 begin
5 dbms_output.put_line('New employee inserted');
6 end;
7 /
Trigger created.
SQL> set serveroutput on
SQL>
SQL> insert into employee
2 (emp_id, emp_name, salary)
3 values
4 (1, 'Little', 10);
New employee inserted --> here's a message
1 row created.
SQL>
create or replace trigger trigger1
after insert on employee
for each row
begin
dbms_output.put_line('NEW EMPLOYEE DETAILS INSERTED');
end;
No need to write WHEN, as emp_id is mentioned as Primary Key.
CTS :)
Write a program that gives all employees in Mechanical department,
(i) 15% pay increase.
(ii) display a message displaying how many employees were awarded the increase. if no Employees found then print the message 'No Records found'.
Employee :
columnname Data type constraints
EMPID NUMBER(5) PK
EMP_NAME VARCHAR(25) NOT NULL
SALARY NUMBER(10,2)
DEPT VARCHAR(25)
EMP_ID EMP_NAME SALARY DEPT
101 TOM 54000 MECH
102 WILLIAM 43000 CSE
103 JOHN 34560 MECH
104 SMITH 56000 CSE
105 STEVE 23450 IT
Sample Output : 2 Employee got increment.
and i did like this,
create or replace procedure empsal as
emp employee%rowtype;
sal number ;
cursor cr is select * from employees where dept='mech';
begin
open cr;
loop
fetch cr into emp;
exit when ce%notfound;
sal:=emp.salary+(emp.salary*15/100);
update employee set salary=sal where dept='mech';
end loop;
close cr;
if(sql%found) then
dbms_output.put_line(sql%rowcount);
else
dbms_output.put_line('no records found');
end;
but its shows the COMPILATION ERROR
No need to do make it that complex.
SQL> set serveroutput on
SQL> create or replace procedure empsal as
2 begin
3 update employee set
4 salary = salary * 1.15
5 where dept = 'MECH';
6
7 dbms_output.put_line(case when sql%rowcount = 0 then 'No records found'
8 else sql%rowcount || ' employees got increment'
9 end);
10 end;
11 /
Procedure created.
SQL> exec empsal;
2 employees got increment
PL/SQL procedure successfully completed.
SQL>
As of your compilation errors:
if table name is employee, don't use employees (while declaring a cursor)
if cursor name is cr, don't use ce for it (exit statement)
if misses end if
As of logical errors:
if department name is MECH, don't reference it as mech (letter case matters)
doing it in a loop - and without where clause in update statement - you're increasing salary for everyone as many times as there are employees in the MECH department.
in if, you're referencing a cursor that is already closed so ... no use of it
I am trying to insert below details in two table. but it shows error. where i am wrong?
create or replace PROCEDURE ADD_customer_order(
customer_id in varchar, Shipping_id_arg in number,
order_date_arg in date, Total_price_arg in decimal,
inventory_id_arg in number, order_quantity_arg in number)
AS
BEGIN
INSERT INTO customer_order (customer_id,Shipping_id,Order_date,total_price) VALUES(customer_id_arg,Shipping_id_arg,order_date_arg, total_price_arg);
insert into order_details (inventory_id,order_quantity) values(scope_identity(),inventory_id_arg,order_quantity_arg);
END;
It helps if you format it nicely.
create or replace procedure add_customer_order(
customer_id in varchar2, shipping_id_arg in number,
order_date_arg in date , total_price_arg in decimal,
inventory_id_arg in number , order_quantity_arg in number)
as
begin
insert into customer_order
(customer_id , shipping_id , order_date , total_price)
values
(customer_id_arg, shipping_id_arg, order_date_arg, total_price_arg);
insert into order_details
( inventory_id , order_quantity)
values
(scope_identity(), inventory_id_arg, order_quantity_arg);
end;
Doing so, you easily note that the second INSERT is invalid, as you're inserting 3 values into 2 columns:
insert into order_details
( inventory_id , order_quantity)
values
(scope_identity(), inventory_id_arg, order_quantity_arg);
Either remove scope_identity() (what is it?), or include additional column into the column list you're inserting into.
After reading your comment, it seems that returning clause might help. See the following example (somewhat simpler than yours; didn't feel like typing that much). Trigger is used to auto-increment ORDER_ID column. in CUSTOMER_ORDER table (I'm on 11g XE; don't have identity columns here).
SQL> create table customer_order (order_id number, customer_id number);
Table created.
SQL> create table order_details (order_id number, inventory_id number);
Table created.
SQL> create sequence seqo;
Sequence created.
SQL> create or replace trigger trg_co
2 before insert on customer_order
3 for each row
4 begin
5 :new.order_id := seqo.nextval;
6 end;
7 /
Trigger created.
Procedure: note local variable declared in line #4 and returning clause in line #7:
SQL> create or replace procedure p_test
2 (par_customer_id in number, par_inventory_id in number)
3 is
4 l_order_id customer_order.order_id%type;
5 begin
6 insert into customer_order (customer_id) values (par_customer_id)
7 returning order_id into l_order_id;
8
9 insert into order_details (order_id, inventory_id)
10 values (l_order_id, par_inventory_id);
11 end;
12 /
Procedure created.
Testing:
SQL> exec p_test(100, 200);
PL/SQL procedure successfully completed.
SQL> exec p_test (235, 2230);
PL/SQL procedure successfully completed.
SQL> select * From customer_order;
ORDER_ID CUSTOMER_ID
---------- -----------
1 100
2 235
SQL> select * From order_details;
ORDER_ID INVENTORY_ID
---------- ------------
1 200
2 2230
SQL>
The same ORDER_ID value is used in both tables.
I am trying to create a trigger that runs when I insert in table 'cuenta' and make an insert in table 'cuenta_log', one of the values of this insert is gotten by accept input.
create or replace trigger trigger_new_account
AFTER INSERT ON cuenta FOR EACH ROW
accept vstring prompt "Please enter your name: ";
declare v_line varchar2(50);
begin
v_line:= 'Hello '||'&vstring';
insert into cuentas_log (fecha,cuenta,cliente)
values (now(),:new.idcuenta,v_line);
end;
cuenta_log structure is like that:
cuenta_log
("FECHA" DATE DEFAULT (sysdate),
"CUENTA" NUMBER(38,0),
"CLIENTE" VARCHAR2(50 BYTE)
)
Trigger fires on certain event; yours, after insert into table named cuenta. It doesn't work interactively with end users. You can't expect it to prompt anyone to enter anything. Besides, accept is a SQL*Plus command, it won't work in PL/SQL.
So, here's what you might have done: sample tables first:
SQL> create table cuenta (fecha date, cuenta number);
Table created.
SQL> create table cuenta_log (fecha date, cuenta number, cliente varchar2(30));
Table created.
Trigger:
SQL> create or replace trigger trigger_new_account
2 after insert on cuenta
3 for each row
4 begin
5 insert into cuenta_log(fecha, cuenta, cliente)
6 values
7 (sysdate, :new.cuenta, 'Hello ' || user);
8 end;
9 /
Trigger created.
Testing:
SQL> insert into cuenta (fecha, cuenta) values (sysdate, 100);
1 row created.
SQL> select * From cuenta;
FECHA CUENTA
------------------- ----------
11.05.2020 12:31:17 100
SQL> select * From cuenta_log;
FECHA CUENTA CLIENTE
------------------- ---------- ------------------------------
11.05.2020 12:31:17 100 Hello SCOTT
SQL>
I have a table called phonebook and it has two columns (firstName, LastName). I want to create a table of lastName index by firstName using cursor, and I wrote this code:
CREATE OR REPLACE PROCEDURE proc1 AS
TYPE tableNames IS TABLE OF VARCHAR2(20) INDEX BY VARCHAR(20);
v1 tableNames;
v_firstName PHONEBOOK.FIRSTNAME%TYPE;
v_lastName PHONEBOOK.LASTNAME%TYPE;
CURSOR c_name IS SELECT FIRSTNAME, LASTNAME FROM PHONEBOOK;
BEGIN
OPEN c_name;
LOOP
FETCH c_name INTO v_firstName, v_lastName;
EXIT WHEN c_name%NOTFOUND;
v1(v_firstName) := v_lastName;
END LOOP;
FOR idx IN v1.FIRST..v1.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (v1(idx));
END LOOP;
CLOSE c_name;
END;
/
It has been successfully compiled. When I run this procedure it should print lastNames which stored in the tableNames but it gave me an error:
ORA-06502 "PL/SQL: numeric or value error"
Cause: An arithmetic, numeric, string, conversion, or constraint error
occurred. For example, this error occurs if an attempt is made to
assign the value NULL to a variable declared NOT NULL, or if an
attempt is made to assign an integer larger than 99 to a variable
declared NUMBER(2).
Action: Change the data, how it is manipulated, or how it is declared so
that values do not violate constraints.
Please help me to solve this problem
Not FOR, but WHILE. Also, I used cursor FOR loop as a source; easier to write & maintain.
SQL> create table phonebook (firstname varchar2(10), lastname varchar2(10));
Table created.
SQL> insert into phonebook
2 select 'Little', 'Foot' from dual union all
3 select 'Mc' , 'Donalds' from dual;
2 rows created.
SQL> create or replace procedure proc1 as
2 type tablenames is table of varchar2(10) index by varchar2(10);
3 v1 tablenames;
4 idx varchar2(10);
5 begin
6 for cur_r in (select firstname, lastname
7 from phonebook
8 )
9 loop
10 v1(cur_r.firstname) := cur_r.lastname;
11 end loop;
12
13 idx := v1.first;
14 while idx is not null loop
15 dbms_output.put_line(v1(idx));
16 idx := v1.next(idx);
17 end loop;
18 end;
19 /
Procedure created.
SQL> exec proc1;
Foot
Donalds
PL/SQL procedure successfully completed.
SQL>