Plsql task
Upcoming SlideShare
Loading in...5
×
 

Plsql task

on

  • 1,811 views

Plsql task

Plsql task

Statistics

Views

Total Views
1,811
Views on SlideShare
1,811
Embed Views
0

Actions

Likes
2
Downloads
52
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Plsql task Plsql task Document Transcript

  • PLSQL:1)Write a PL/SQL block to accept a year input and check whether it is a leap year.2)Write a PL/SQL block which will accept the employee number and print the annual_salary as well as the bonus. The PL/SQL block should:• Calculate the annual salary as salary * 12• Calculate the bonus as indicated in the following table: Annual Salary Bonus>= 20,000 200019,999–10,000 1000<= 9,999 5003)Declare a cursor named EMP_CUR to select the employee’s last name, salary, and hiredate from the EMPLOYEES tableProcess each row from the cursor, and if the salary is greater than 15,000 and the hiredate is later than 01-FEB-1988, display the employee name, salary, and hire date inthe format shown in the following sample output:4)Create a PL/SQL block to retrieve and output the last name and department ID of each employee from theEMPLOYEES table for those employees whose EMPLOYEE_ID is less than 115.In the PL/SQL block, use a cursor FOR loop strategy instead of the OPEN / FETCH /CLOSE cursor methodsused in the previous practice.1. In the declarative section:• Create two associative arrays. The unique key column for both arrays should be of the BINARY INTEGERdata type. One array holds the employee’s last name and the other holds the department ID.• Declare a counter variable to be used in the executable section• Declare a cursor that selects the last name and department ID for employees whose ID is less than 1155)Create a table:CREATE TABLE analysis(ename Varchar2(20), years Number(2), sal Number(8,2));
  • Write a PL/SQL block that handles an exception, as follows:1. Declare variables for the employee last name, salary, and hire date. Use a substitutionvariable for the employee last name. Then, query the employees table for thelast_name, salary, and hire_date of the specified employee.2. If the employee has been with the organization for more than five years, and if thatemployee’s salary is less than 3,500, raise an exception. In the exception handler,perform the following:• Output the following information: employee last name and the message “duefor a raise,” similar to the following:Insert the last name, years of service, and salary into the analysis table.3. If there is no exception, output the employee last name and the message “not due for a raise,” similar to thefollowing:Verify the results by querying the analysis table. Use the following test cases to test the PL/SQL block.LAST_NAME MESSAGEAustin Not due for a raiseNayer Due for a raiseFripp Not due for a raiseKhoo Due for a raise6)Create, compile, and invoke the ADD_JOB procedure and review the results.a) Create a procedure called ADD_JOB to insert a new job into the JOBS table.Provide the ID and job title using two parameters.Note: You can create the procedure (and other objects) by entering the code in theSQL Worksheet area, and then click the Run Script (F5) icon. This creates andcompiles the procedure. To find out whether or not the procedure has any errors,click the procedure name in the procedure node, and then select Compile from thepop-up menu.b) Invoke the procedure with IT_DBA as the job ID and DatabaseAdministrator as the job title. Query the JOBS table and view the results.c) Invoke your procedure again, passing a job ID of ST_MAN and a job title ofStock Manager. What happens and why?
  • 7)7a. Create a procedure called UPD_JOB to update the job title. Provide the job ID anda new title using two parameters. Include the necessary exception handling if noupdate occurs.7b) Invoke the procedure to change the job title of the job ID IT_DBA to Data Administrator. Query the JOBStable and view the results.7c) Test the exception-handling section of the procedure by trying to update a job that does not exist. You canuse the job ID IT_WEB and the job title Web Master.8) Create a procedure called GET_EMPLOYEE to query the EMPLOYEES table, retrieving the salary and jobID for an employee when provided with the employee ID.a) Create a procedure that returns a value from the SALARY and JOB_ID columns for a specified employee ID.Remove syntax errors, if any, and then recompile the code.9)Create and invoke the GET_JOB function to return a job title.9a) Create and compile a function called GET_JOB to return a job title.b) Create a VARCHAR2 host variable called b_title, allowing a length of 35characters. Invoke the function with job ID SA_REP to return the value in thehost variable, and then print the host variable to view the result.9b)Create a VARCHAR2 host variable called b_title, allowing a length of 35characters. Invoke the function with job ID SA_REP to return the value in thehost variable, and then print the host variable to view the result.10) Create a function called GET_ANNUAL_COMP to return the annual salary computedfrom an employee’s monthly salary and commission passed as parameters.10 a) Create the GET_ANNUAL_COMP function, which accepts parameter values for themonthly salary and commission. Either or both values passed can be NULL, butthe function should still return a non-NULL annual salary. Use the following basicformula to calculate the annual salary:(salary*12) + (commission_pct*salary*12)10b) Use the function in a SELECT statement against the EMPLOYEES table foremployees in department 30.11)Create a procedure, ADD_EMPLOYEE, to insert a new employee into theEMPLOYEES table. The procedure should call a VALID_DEPTID function to checkwhether the department ID specified for the new employee exists in theDEPARTMENTS table.11a) Create a function called VALID_DEPTID to validate a specified department IDand return a BOOLEAN value of TRUE if the department exists.11b) Create the ADD_EMPLOYEE procedure to add an employee to the EMPLOYEEStable. The row should be added to the EMPLOYEES table if the VALID_DEPTIDfunction returns TRUE; otherwise, alert the user with an appropriate message.Provide the following parameters:- first_name- last_name- email
  • - job: Use SA_REP as the default.- mgr: Use 145 as the default.- sal: Use 1000 as the default.- comm: Use 0 as the default.- deptid: Use 30 as the default.- Use the EMPLOYEES_SEQ sequence to set the employee_id column. − Set the hire_date column to TRUNC(SYSDATE).11c) Call ADD_EMPLOYEE for the name Jane Harris in department 15,leaving other parameters with their default values. What is the result?11d) Add another employee named Joe Harris in department 80, leaving the remainingparameters with their default values. What is the result?12)1) Create a package specification and body called JOB_PKG, containing a copy of yourADD_JOB, UPD_JOB, and DEL_JOB procedures as well as your GET_JOB function.Note: Use the code from your previously saved procedures and functions whencreating the package. You can copy the code in a procedure or function, and thenpaste the code into the appropriate section of the package.a) Create the package specification including the procedures and function headingsas public constructs.b) Create the package body with the implementations for each of the subprograms.c) Invoke your ADD_JOB package procedure by passing the values IT_SYSAN andSYSTEMS ANALYST as parameters.13)Create and invoke a package that contains private and public constructs.a) Create a package specification and a package body called EMP_PKG that containsthe following procedures and function that you created earlier:i) ADD_EMPLOYEE procedure as a public constructii) GET_EMPLOYEE procedure as a public constructiii) VALID_DEPTID function as a private constructb) Invoke the EMP_PKG.ADD_EMPLOYEE procedure, using department ID 15 foremployee Jane Harris with the email ID JAHARRIS. Because department ID 15does not exist, you should get an error message as specified in the exceptionhandler of your procedure.c) Invoke the ADD_EMPLOYEE package procedure by using department ID 80 foremployee David Smith with the email ID DASMITH.14)Modify the code for the EMP_PKG package that you created earlier,and then overload the ADD_EMPLOYEE procedure. Next, you create two overloadedfunctions called GET_EMPLOYEE in the EMP_PKG package. You also add a publicprocedure to EMP_PKG to populate a private PL/SQL table of valid department IDs andmodify the VALID_DEPTID function to use the private PL/SQL table contents tovalidate department ID values. You also change the VALID_DEPTID validationprocessing function to use the private PL/SQL table of department IDs. Finally, you
  • reorganize the subprograms in the package specification and the body so that they are inalphabetical sequence.Modify the code for the EMP_PKG package that you created earlier, andoverload the ADD_EMPLOYEE procedure.a) In the package specification, add a new procedure called ADD_EMPLOYEE thataccepts the following three parameters:i) First nameii) Last name iii) Department IDb) Implement the new ADD_EMPLOYEE procedure in the package body as follows:i) Format the email address in uppercase characters, using the first letter of thefirst name concatenated with the first seven letters of the last name.ii) The procedure should call the existing ADD_EMPLOYEE procedure to performthe actual INSERT operation using its parameters and formatted email tosupply the values. iii) Click Run Script to create the package. Compile the package.c) Invoke the new ADD_EMPLOYEE procedure using the name Samuel Joplinto be added to department 30.15)In the EMP_PKG package, create two overloaded functions called GET_EMPLOYEE:a) In the package specification, add the following functions:i) The GET_EMPLOYEE function that accepts the parameter called p_emp_idbased on the employees.employee_id%TYPE type. This functionshould return EMPLOYEES%ROWTYPE.ii) The GET_EMPLOYEE function that accepts the parameter calledp_family_name of type employees.last_name%TYPE. This functionshould return EMPLOYEES%ROWTYPE.