473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help creating PL/SQL procedure using function

Hello,
I am new to PL/SQL and having GREAT difficulty with the following:
Create a stored procedure which processes one employee record (pass
an employee number as a parameter to the procedure). Call your function,
TOTAL_PAY, created in a previous step and insert a record into the BONUS table if
the employee’s total pay is at least $2000. The record to be inserted should have an
increased amount for commission by 10% if they have a commission or it should set
it to $100 if commission is null. Otherwise (total pay is not at least 2000), display a
message with DBMS_OUTPUT.PUT_LINE which shows the employee’s name and
salary such as “Employee SMITH has total pay of $1800”.


I have created TOTAL_PAY as:
Expand|Select|Wrap|Line Numbers
  1. create or replace function total_pay (emp_no IN emp.empno%type)
  2. return NUMBER
  3. is 
  4. CURSOR grp_cur
  5. is 
  6. select SUM (SAL + COMM)
  7. from emp
  8. where  empno = emp_no;
  9. return_value NUMBER;
  10. Begin
  11. OPEN grp_cur;
  12. FETCH grp_cur into return_value;
  13. CLOSE grp_cur;
  14. RETURN return_value;
  15. END;
Nov 11 '13 #1
1 1265
pcbala
5
Hi n33dh3lp,
I have understood your requirements. But, it is advice-able if you do your work within a PACKAGE. I have created it inside a single package. Please refer it you have not yet got the answer.
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE
  2. PACKAGE dummy_dummy AS
  3. PROCEDURE try_emp_try 
  4. (
  5. p_empid IN VARCHAR2
  6. );
  7.  
  8. FUNCTION dummy_total
  9. (
  10. p_empno IN  emp.empno%TYPE,
  11. p_sal   IN  NUMBER,
  12. p_comm  IN  NUMBER
  13. )
  14. RETURN NUMBER;
  15. END;
  16.  
The above is the package specification.
The below coding is the package body.
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE
  2. PACKAGE BODY dummy_dummy AS
  3. PROCEDURE try_emp_try(p_empid IN VARCHAR2)IS
  4. v_empno         NUMBER(10);
  5. v_sal           NUMBER(10);
  6. v_comm          NUMBER(10);
  7. v_total         NUMBER(10);
  8. v_name          VARCHAR2(30);
  9. v_job           VARCHAR2(30);
  10. v_mgr           VARCHAR2(30);
  11. v_hiredate      VARCHAR2(30);
  12. v_deptno        VARCHAR2(30);
  13. v_sex           VARCHAR2(1);
  14. v_dummy         VARCHAR2(1000);
  15.  
  16. CURSOR c_dummy IS
  17. SELECT ename,mgr,hiredate,sal,comm,deptno,sex 
  18. FROM emp
  19. WHERE  empno=p_empid;
  20.  
  21. BEGIN
  22. OPEN   c_dummy;
  23. FETCH  c_dummy  INTO v_name,v_mgr,v_hiredate,v_sal,v_comm,v_deptno,v_sex;
  24. CLOSE  c_dummy;
  25. DBMS_OUTPUT.PUT_LINE('Details of '|| p_empid || ' are' || ' ' || v_name || ' ' || v_mgr || ' ' || v_hiredate || ' ' || v_sal || ' ' || v_comm || ' ' || v_deptno || ' ' || v_sex);
  26. --v_total   :=  v_sal + v_comm;
  27. v_empno   :=  p_empid;
  28. --DBMS_OUTPUT.PUT_LINE('v_total ' ||v_total);
  29. v_dummy   :=  dummy_total(v_empno,v_sal,v_comm);
  30. END try_emp_try;
  31. FUNCTION dummy_total(p_empno IN emp.empno%TYPE,p_sal IN NUMBER,p_comm NUMBER)
  32. RETURN NUMBER
  33. IS
  34. v_sal   NUMBER(10);
  35. v_comm  NUMBER(10);
  36. v_bonus NUMBER(10);
  37. BEGIN
  38. v_sal   :=p_sal;
  39. v_comm  :=p_comm;
  40. v_bonus :=v_sal+(v_sal/10);
  41. IF v_sal>2000 AND v_comm IS NOT NULL THEN
  42. INSERT INTO dummy_bonus VALUES(p_empno,v_bonus);
  43. DBMS_OUTPUT.PUT_LINE('After insert into bonus SINCE v_sal>2000 AND v_comm IS NOT NULL'||p_empno || ' ' ||v_bonus);
  44. ELSIF v_sal>2000  AND v_comm IS NULL THEN
  45. INSERT INTO dummy_bonus VALUES(p_empno,100);
  46. DBMS_OUTPUT.PUT_LINE('After insert into bonus SINCE v_sal>2000 AND v_comm IS NULL'||p_empno || ' ' ||100);
  47. ELSE
  48. DBMS_OUTPUT.PUT_LINE('The total pay of the employee is < 2000 Rupees');
  49. END IF;
  50. RETURN v_sal;
  51. END dummy_total;
  52. END dummy_dummy;
  53.  
Now, the package is built and ready.
I am going to run it now.
The bonus table that i created is dummy_bonus. Initailly, i do not have any values in it. I will run it for two cases.
1.Having >2000
2.Having >2000 and a NULL commision.
[1].
Expand|Select|Wrap|Line Numbers
  1. SET SERVEROUTPUT ON
  2. BEGIN
  3. dummy_dummy.try_emp_try(101);
  4. END;
  5.  
The output is,
Details of 101 are JANAKI 7841 09-MAR-10 16765 101 30 F
After insert into bonus SINCE v_sal>2000 AND v_comm IS NOT NULL101 18442.
[2].
Expand|Select|Wrap|Line Numbers
  1. SET SERVEROUTPUT ON
  2. BEGIN
  3. dummy_dummy.try_emp_try(111);
  4. END;
  5.  
The output is,
Details of 111 are priya 7841 09-MAR-10 5000 50 F
After insert into bonus SINCE v_sal>2000 AND v_comm IS NULL111 100
NOW, THE TASK IS COMPLETED.
The enteries in the dummy_bonus table which was initaially empty are,
111 100
101 18442
I hope that the above information were useful.
:-)
Nov 19 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike | last post by:
Hi, I am trying to insert parameters into a stored procedure using DAAB (see code at the bottom of this post). I am getting the following error: Object reference not set to an instance of an...
5
by: Peter Erickson | last post by:
I am running postgresql 7.4.2 and having problems creating a trigger function properly. I keep getting the following error: ERROR: OLD used in query that is not in rule I have a table called...
0
by: Mike P | last post by:
I am using a SqlDataSource with a GridView and stored proc, and I have specified exactly the same update parameters for the SqlDataSource in exactly the same order as my stored proc. Yet I keep...
6
by: radhikutti | last post by:
Hi All, Please explain me the Adv and DisAdv between Procedures and functions Thanks in Advance Radhi
0
by: David Lozzi | last post by:
Howdy, ASP.Net 2.0 using VB on SQL 2005 This is a two fold issue. I have a DetailsView control which users can insert or edit items. Editing works great. Insert works great however I need...
1
by: frankwu | last post by:
exam is coming.i still feel headache about procedure using cursor. here is example: Q.Create a cursor stored procedure called PriceChange to read the title id and price from the titles tables which...
3
AdusumalliGopikumar
by: AdusumalliGopikumar | last post by:
Hello, We can return the value by using a procedure using out parameter then what is the use to go for functions in pl/sql?
1
by: speranza | last post by:
i have multiple checkboxes on my form.i am trying to add them with stored procedure but it gives me Procedure or function konut_ekle has too many arguments specified protected void...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
1
by: qwedster | last post by:
Hola! In the following code snippet, I am creating User Defined Function (T-SQL) programmatically into database from C# Code: BTW, this code originally I wrote for creating StoredProcedure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.