Home » SQL & PL/SQL » SQL & PL/SQL » package -error (4.1.3 sql developer)
|
Re: package -error [message #649050 is a reply to message #649049] |
Thu, 10 March 2016 16:18 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Please see the comment lines in the code below for corrections. In the future, please try to write and test one small piece of code at a time before adding the next. Use show errors to see what errors you have, fix those, then run again, check for errors, and fix those, and so on, working through one error at a time, until it runs without error and does what you want. Also, when posting, remember to provide any tables or other items that are necessary for testing your code.
SCOTT@orcl> -- tables needed for package body creation:
SCOTT@orcl> create table even_tab (my_num number)
2 /
Table created.
SCOTT@orcl> create table odd_tab (my_num number)
2 /
Table created.
SCOTT@orcl> -- creation of package specification:
SCOTT@orcl> create or replace package demopackage as
2 function get_random (p_start number, p_end number) return number;
3 function is_even (p_number number) return boolean;
4 procedure insert_num (p_num1 number, p_num2 number);
5 end demopackage;
6 /
Package created.
SCOTT@orcl> show errors
No errors.
SCOTT@orcl> -- creation of package body:
SCOTT@orcl> -- create of replace package body demopackage as
SCOTT@orcl> -- create OR replace, as below, not create OF replace as above:
SCOTT@orcl> create or replace package body demopackage as
2 function get_random (p_start number, p_end number) return number as
3 l_num number;
4 begin
5 -- l_num := dbms_random.value (p_start number, p_end number);
6 -- there should not be any data type parameters when you call a function, as above, should be as below:
7 l_num := dbms_random.value (p_start, p_end);
8 return l_num;
9 end get_random;
10
11 -- function is_even (p_num number) return boolean as
12 -- parameter names in package specification and package body must match;
13 -- it was p_number in the package specification, not p_num as above, so it must be p_number in the package body as below:
14 function is_even (p_number number) return boolean as
15 begin
16 -- if (mod (p_num, 2) = 0) then return true;
17 -- again the parameter name must match, so it needs to be p_number as below, not p_num as above:
18 if mod (p_number, 2) = 0 then return true;
19 else return false;
20 end if;
21 end is_even;
22
23 procedure insert_num (p_num1 number, p_num2 number) as
24 my_num number;
25 begin
26 -- my_num := get_random (p_num1 ,p_num2);
27 -- need to round or truncate or something if you want whole numbers:
28 my_num := round (get_random (p_num1, p_num2));
29 if (is_even(my_num)) then insert into even_tab values(my_num);
30 else insert into odd_tab values(my_num);
31 end if;
32 end insert_num;
33 end demopackage;
34 /
Package body created.
SCOTT@orcl> show errors
No errors.
SCOTT@orcl> -- test execution:
SCOTT@orcl> exec demopackage.insert_num (1, 20)
PL/SQL procedure successfully completed.
SCOTT@orcl> exec demopackage.insert_num (1, 20)
PL/SQL procedure successfully completed.
SCOTT@orcl> exec demopackage.insert_num (1, 20)
PL/SQL procedure successfully completed.
SCOTT@orcl> exec demopackage.insert_num (1, 20)
PL/SQL procedure successfully completed.
SCOTT@orcl> select * from even_tab
2 /
MY_NUM
----------
16
8
2 rows selected.
SCOTT@orcl> select * from odd_tab
2 /
MY_NUM
----------
3
5
2 rows selected.
|
|
|
|
|
Re: package -error [message #649081 is a reply to message #649072] |
Fri, 11 March 2016 18:31 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
M123 wrote on Fri, 11 March 2016 11:40hi barbara
slightest spelling mistake not visible.
if i made the same mistake in some hundreds line of codes then ?
thank you for the suggestions.
It depends on where your spelling mistakes are. If they are something like "CREATE OF REPLACE" instead of "CREATE OR REPLACE" in multiple scripts, then you can use a text editor like notepad and do some global replacing. If the mistakes are in source code that has already been generated, then you can create the procedure below and execute it as shown below to generate a script that you can run to change all the code. In either case, be very careful to check what you are changing before saving text or running a script.
-- procedure to generate a script to fix code:
CREATE OR REPLACE PROCEDURE fix_code
(p_name IN VARCHAR2,
p_old IN VARCHAR2,
p_new IN VARCHAR2)
AS
BEGIN
FOR r IN
(SELECT LISTAGG (text, ' ') WITHIN GROUP (ORDER BY line) || CHR(10) || '/' code
FROM user_source
WHERE name LIKE p_name
GROUP BY name)
LOOP
DBMS_OUTPUT.PUT_LINE ('CREATE OR REPLACE ' || REPLACE (r.code, p_old, p_new));
END LOOP;
END fix_code;
/
-- sample execution of procedure to create script named fix_code.sql
-- to change all source code beginning with "DO"
-- by replacing every "this" with "that":
STORE SET saved_settings REPLACE
SET ECHO OFF FEEDBACK OFF HEADING OFF PAGESIZE 0 VERIFY OFF
spool fix_code.sql
EXEC fix_code ('DO%', 'this', 'that')
spool off
START saved_settings
You would then check and run the script fix_code.sql.
|
|
|
Goto Forum:
Current Time: Tue Apr 23 08:00:27 CDT 2024
|