Opening Hours :7AM to 9PM
S.NO | Attribute & Description |
---|---|
1 | %FOUND Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or more rows or a SELECT INTO statement returned one or more rows. Otherwise, it returns FALSE. |
2 | %NOTFOUND The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT INTO statement returned no rows. Otherwise, it returns FALSE. |
3 | %ISOPEN Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor automatically after executing its associated SQL statement. |
4 | %ROWCOUNT Returns the number of rows affected by an INSERT, UPDATE, or DELETE statement, or returned by a SELECT INTO statement. |
Select * from customers; +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | +----+----------+-----+-----------+----------+The following program will update the table and increase the salary of each customer by 500 and use the SQL%ROWCOUNT attribute to determine the number of rows affected −
DECLARE total_rows number(2); BEGIN UPDATE customers SET salary = salary + 500; IF sql%notfound THEN dbms_output.put_line('no customers selected'); ELSIF sql%found THEN total_rows := sql%rowcount; dbms_output.put_line( total_rows || ' customers selected '); END IF; END; /output:
Select * from customers; +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2500.00 | | 2 | Khilan | 25 | Delhi | 2000.00 | | 3 | kaushik | 23 | Kota | 2500.00 | | 4 | Chaitali | 25 | Mumbai | 7000.00 | | 5 | Hardik | 27 | Bhopal | 9000.00 | | 6 | Komal | 22 | MP | 5000.00 | +----+----------+-----+-----------+----------+
CURSOR cursor_name IS select_statement;
CURSOR c_customers IS SELECT id, name, address FROM customers;
OPEN c_customers;
FETCH c_customers INTO c_id, c_name, c_addr;
CLOSE c_customers;
DECLARE c_id customers.id%type; c_name cutomers.name%type; c_addr customers.address%type; CURSOR c_customers is SELECT id, name, address FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_id, c_name, c_addr; EXIT WHEN c_customers%notfound; dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr); END LOOP; CLOSE c_customers; END; /output:
DECLARE c_id customers.id%type; c_name customers.name%type; c_addr customers.address%type; c_salary customers.salary%type; CURSOR c_customers is SELECT id, name, address,salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_id, c_name, c_addr,c_salary; EXIT WHEN c_customers%notfound; if(c_salary>5000) then dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr||' ' || c_salary); end if; END LOOP; CLOSE c_customers; END; /
create or replace procedure empsal as c_id customers.id%type; c_name customers.name%type; c_addr customers.address%type; c_salary customers.salary%type; CURSOR c_customers is SELECT id, name, address,salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_id, c_name, c_addr,c_salary; EXIT WHEN c_customers%notfound; if(c_salary>5000) then dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr||' ' || c_salary); end if; END LOOP; CLOSE c_customers; END empsal; /Procedure Calling:
Begin empsal(); end;
Declare c_sal customers.salary%type; sumsal int:=0; CURSOR c_customers is SELECT salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_sal; EXIT WHEN c_customers%notfound; sumsal:=sumsal+c_sal; END LOOP; CLOSE c_customers; dbms_output.put_line(sumsal); END ; /
create or replace function sumempsal return int as c_sal customers.salary%type; sumsal int:=0; CURSOR c_customers is SELECT salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_sal; EXIT WHEN c_customers%notfound; sumsal:=sumsal+c_sal; END LOOP; CLOSE c_customers; return(sumsal); END sumempsal ;Function Calling:
Declare s int; Begin s:=empsumsal(); dbms_output.put_line(s); end;
create or replace function empsumsal RETURN number IS total int:= 0; c_salary customers.salary%type; CURSOR c_customers is SELECT salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_salary; EXIT WHEN c_customers%notfound; if(c_salary>5000) then total:=total+c_salary; end if; END LOOP; return total; CLOSE c_customers; END; /Function Calling:
Declare s int; Begin s:=empsumsal(); dbms_output.put_line(s); end;
DECLARE c_sal customers.salary%type; c_id customers.id%type; CURSOR c_customers is SELECT id,salary FROM customers; BEGIN OPEN c_customers; LOOP FETCH c_customers into c_id, c_sal; EXIT WHEN c_customers%notfound; if(c_sal<2000) then update customers set salary=c_sal+100 where id=c_id; elsif (c_sal<=5000) then update customers set salary=c_sal+300 where id=c_id; elsif (c_sal<=19000) then update customers set salary=c_sal+500 where id=c_id; end if; END LOOP; CLOSE c_customers; END; /