Sunday, 14 February 2016

Write a PL/SQL block that will take an input in the form of department number. Decrease the salaries of the employees working in that department by 0.75%. If at least a record is updated keep the change permanent else print appropriate message and terminate the block.

Write a PL/SQL block that will take an input in the form of department number. 
Decrease the salaries of the employees working in that department by 0.75%.
 If at least a record is updated keep the change permanent else print appropriate message and terminate the block.

DECLARE
 DNO EMP.DEPTNO%TYPE;
 SALARY EMP.SAL%TYPE;
 DECR EMP.SAL%TYPE;
BEGIN
 SAVEPOINT S1;
 FOR DATA IN (SELECT EMPNO,DEPTNO,SAL FROM EMP WHERE DEPTNO=&DNO)
 LOOP
  DECR:=(DATA.SAL*0.75)/100;
  SALARY:=DATA.SAL-DECR;
  UPDATE EMP SET SAL=SALARY WHERE EMPNO=DATA.EMPNO;
 END LOOP;
 IF SQL%ROWCOUNT>=1 THEN
  COMMIT;
  DBMS_OUTPUT.PUT_LINE('One or More record(s) Updated. ');
 ELSE
  ROLLBACK TO S1;
  DBMS_OUTPUT.PUT_LINE('ROLLBACK THE UPDATION.');
 END IF;
END;
/

Write a PL/SQL block that will update the salaries of the employees working with SCOTT. If more than FOUR employees salaries are affected by the increment take back the increment, else give the increment permanently.

Write a PL/SQL block that will update the salaries of the employees working with SCOTT. 
If more than FOUR employees salaries are affected by the increment take back the increment,
 else give the increment permanently.

DECLARE
 ENM EMP.ENAME%TYPE;
 TOT EMP.SAL%TYPE;
 CNT NUMBER(3);
BEGIN
 TOT:=0;
 CNT:=0;
 SAVEPOINT A;
 FOR I IN (SELECT EMPNO,ENAME,SAL FROM EMP WHERE DEPTNO=(SELECT DEPTNO FROM EMP WHERE ENAME='SCOTT'))
 LOOP
  TOT:=SAL+1000;
  UPDATE EMP SET SAL=TOT WHERE EMPNO=I.EMPNO;
  CNT:=CNT+1;
  END LOOP;
 END LOOP;
 IF CNT>4 THEN
  ROLLBACK TO A;
  DBMS_OUTPUT.PUT_LINE('ROLLBACK THE UPDATION.');
 ELSE
  COMMIT;
  DBMS_OUTPUT.PUT_LINE('COMMITED THE UPDATION.');
 END IF;
END;
/