manas
manas

Reputation: 1

what is wrong with the way I write procedures and functions in plsql?

1   declare
2   a number;
3   b number;
4   c number;
5   d number;
6   PROCEDURE findMin(x IN number, y IN number, z IN number , L out number) IS
7   BEGIN
8   IF x > y&& x>z then
9   L:= x;
10  ELSE if y>z&&y>x then
11  L:= y;
12  else
13  L:=z
14  END IF;
15  End if;
16  END;
17  BEGIN
18  a:= 23;
19  b:= 45;
20  c:=36;
21  findMin(a, b, c,d);
22  dbms_output.put_line(' Minimum of (23, 45,36) : ' || d);
 END;

this is the first one I couldnt unerstand what is wrong with this code it is showing * ERROR at line 1: ORA-06540: PL/SQL: compilation error ORA-06553: PLS-906: Compilation is not possible

the second one is

2.   DECLARE 
3.  num number; 
4.  c number; 
5.  PROCEDURE fact(x IN number, f out number) IS 
6.  BEGIN 
7.  IF x = 0 THEN 
8.  f:= x; 
9.  ELSE 
10. f:= x*fact(x-1); 
11. END IF; 
12. END;   
13. BEGIN 
14. c:=f;
15. num:=6
16. fact(num,c); 
17. dbms_output.put_line(' Factorial: ' ||'is'||c); 
18. END; 
19. /

i am getting output as z:= x*fact(x-1) ; * ERROR at line 9: ORA-06550: line 9, column 14: PLS-00306: wrong number or types of arguments in call to 'FACT' ORA-06550: line 9, column 8: PL/SQL: Statement ignored

this is the second procedure i wrote but i couldnt get the problem in it

  1  create or replace  function tables(n in number) return number is s number;
  2  begin
  3   i number;
  4  for i in 1...10 loop
  5  s:=n*i;
  6  end loop;
  7  return s ;
  8* end;

this is the multiplication table function what is wrong with my codes they are showing output as

Warning: Function created with compilation errors.

Upvotes: 0

Views: 285

Answers (1)

Littlefoot
Littlefoot

Reputation: 142743

As of your 1st code: you should use AND, not && and terminate statements with a colon. When fixed, it runs (and produces wrong result, though, but I'll leave it to you):

SQL> DECLARE
  2     a  NUMBER;
  3     b  NUMBER;
  4     c  NUMBER;
  5     d  NUMBER;
  6
  7     PROCEDURE findMin (x  IN     NUMBER,
  8                        y  IN     NUMBER,
  9                        z  IN     NUMBER,
 10                        L     OUT NUMBER)
 11     IS
 12     BEGIN
 13        IF     x > y
 14           AND x > z
 15        THEN
 16           L := x;
 17        ELSE
 18           IF     y > z
 19              AND y > x
 20           THEN
 21              L := y;
 22           ELSE
 23              L := z;
 24           END IF;
 25        END IF;
 26     END;
 27  BEGIN
 28     a := 23;
 29     b := 45;
 30     c := 36;
 31     findMin (a,
 32              b,
 33              c,
 34              d);
 35     DBMS_OUTPUT.put_line (' Minimum of (23, 45,36) : ' || d);
 36  END;
 37  /
Minimum of (23, 45,36) : 45

PL/SQL procedure successfully completed.

SQL>

All that (37 lines of code) could be shortened to only one (which actually works):

SQL> select least(23, 45, 36) minimum from dual;

   MINIMUM
----------
        23

SQL>

As of your 2nd code: procedure is wrong as fact can't be used that way and lacks in 2nd parameter. One option to fix it is

SQL> DECLARE
  2     num  NUMBER;
  3     c    NUMBER;
  4
  5     PROCEDURE fact (x IN NUMBER, f OUT NUMBER)
  6     IS
  7        l_var  NUMBER := 1;
  8     BEGIN
  9        FOR i IN 1 .. x
 10        LOOP
 11           l_var := l_var * i;
 12        END LOOP;
 13
 14        f := l_var;
 15     END;
 16  BEGIN
 17     num := 6;
 18     fact (num, c);
 19     DBMS_OUTPUT.put_line (' Factorial of ' || num || ' is ' || c);
 20  END;
 21  /
Factorial of 6 is 720

PL/SQL procedure successfully completed.

SQL>

Finally, the 3rd code: I have no idea what you meant to say with it, it's full of errors. I tried to salvage it, can't tell whether I succeeded.

SQL> CREATE OR REPLACE FUNCTION tables (n IN NUMBER)
  2     RETURN NUMBER
  3  IS
  4     s  NUMBER := 0;
  5  BEGIN
  6     FOR i IN 1 .. 10
  7     LOOP
  8        s := s + n * i;
  9     END LOOP;
 10
 11     RETURN s;
 12  END;
 13  /

Function created.

SQL> SELECT tables (3) FROM DUAL;

 TABLES(3)
----------
       165

SQL>

Upvotes: 1

Related Questions