plsql procedure is not a procedure or is undefined - oracle

I want to create a plsql procedure to calculate the factorial of a given number.
This is the procedure:
CREATE OR REPLACE PROCEDURE fact(x IN number, fact OUT number)
IS
BEGIN
while x > 0 loop
fact := x*fact;
x := x-1;
END loop;
END;
/
Warning: Procedure created with compilation errors.
and this is where I'm calling the function
DECLARE
x number := &x;
fact number := 1;
BEGIN
fact(x,fact);
dbms_output.put_line('Factorial is: '||fact);
END;
and this is the error I'm getting:
Enter value for x: 5
old 2: x number := &x;
new 2: x number := 5;
fact(x,fact);
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00221: 'FACT' is not a procedure or is undefined
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored

You need to convert your creation of procedure like this :
SQL> CREATE OR REPLACE PROCEDURE fact(x IN OUT number, fact OUT number) IS
BEGIN
while x > 0 loop
fact := x * nvl(fact, 1);
x := x - 1;
END loop;
END;
/
and call :
SQL> SET SERVEROUTPUT ON;
SQL> DECLARE
x number := &x;
v_fact number := 1;
BEGIN
fact(x, v_fact);
dbms_output.put_line('Factorial is: ' || v_fact);
END;
Factorial is: 120
You can not use a variable of type IN as an assignment target
You need to initialize a null variable fact as nvl(fact,1) or fact := 1; just before the while x > 0
Actually, you even do not need an extra parameter fact OUT number for procedure named fact, and make that as local. So, your procedure may be replaced with this :
SQL> CREATE OR REPLACE PROCEDURE fact(x IN OUT number) IS
fact number := 1;
BEGIN
while x > 0 loop
fact := x * fact;
x := x - 1;
end loop;
x := fact;
END;
/
Therefore, should be invoked as :
SQL> DECLARE
x number := &x;
BEGIN
fact(x);
dbms_output.put_line('Factorial is: ' || x);
END;
Factorial is: 120

You might consider rewriting this as a function along the lines of the following:
CREATE OR REPLACE FUNCTION fact(pinX IN INT)
RETURN INT
IS
nResult INT := 1;
BEGIN
FOR i IN 2..pinX LOOP
nResult := i * nResult;
END LOOP;
RETURN nResult;
END FACT;
dbfiddle here
Best of luck.

Related

Turbo Pascal result is not visible

can you say to me, where is the error. I wanna write in console the content of the a[i] in every for run. The output could be 6 digits. There is compiler Alert:
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
...Program finished with exit code 0
program Hello;
type aha = array [1..6] of integer;
var
a: aha;
n,x,i: integer;
begin
a[1]:=2;
a[2]:=6;
a[3]:=4;
a[4]:=2;
a[5]:=4;
a[6]:=3;
n:=6;
x:=a[1];
for i:=2 to n do
begin
{
if (a[i-1]>= x) then
begin
a[i]:=a[i] - x div 2;
end;
else
begin
a[i]:=a[i] + x;
x:= x + mod x(a[i] + 1);
end;
writeln (a[i]);
}
end;
end.

PL/SQL Check Digit, luhn using MOD 11

So here is the question:
Write code to take in an id and determine if the check digit is correct
UPDATED CODE:
Set SERVEROUTPUT ON
DECLARE
val_num NUMBER := '&user_input';
holder NUMBER := 0;
y NUMBER := 0;
conv_string VARCHAR2(20);
BEGIN
conv_string := to_char(val_num*10);
for x in 1..length(conv_string) loop
y := to_number(substr(conv_string, -x, 1));
if mod(x,2) = 0 then
y := y * 2;
if y > 9 then
y := y - 9;
end if;
end if;
holder := holder + y;
end loop;
dbms_output.put_line ('Check is '||(11-Mod(holder, 11)));
END luhn;
/
SET SERVEROUTPUT ON
The return is:
SQL> # loop
Enter value for user_input: 036532
old 2: val_num NUMBER := '&user_input';
new 2: val_num NUMBER := '036532';
Check is 2
It should be 6
Before actual execution
SET SERVEROUTPUT ON
to enable SQL*Plus to fetch database output buffer.
Here is solution: https://community.oracle.com/thread/837639?start=0&tstart=0
There are lots of different variations of the luhn algorithm, so looking at these implementations and your (I think incomplete) description in the comments I think this may be fairly close to what you are looking for, and gives the correct checksum for 036532 as per your initial question.
Hope it is helpfull
Set SERVEROUTPUT ON
DECLARE
val_num number := '036532';
holder NUMBER := 0;
y NUMBER := 0;
conv_string VARCHAR2(20);
BEGIN
conv_string := to_char(val_num);
FOR X IN 1..LENGTH(CONV_STRING) LOOP
Y := TO_NUMBER(SUBSTR(CONV_STRING, -X, 1));
IF ((X+1) > 10) THEN
Y := Y * 10;
ELSE
Y := Y * (X + 1);
END IF;
IF (Y >= 10) THEN
HOLDER := HOLDER + TO_NUMBER(substr(TO_CHAR(Y), 1, 1)) + TO_NUMBER(substr(TO_CHAR(Y), 2, 1));
ELSE
HOLDER := HOLDER + Y;
END IF;
END LOOP;
HOLDER := MOD(HOLDER, 11);
Holder := 11 - mod(holder, 11);
dbms_output.put_line ('Check is '|| holder);
END luhn;
/
SET SERVEROUTPUT ON

oracle - PLS-00103 & ORA -06550 on procedures

I'm getting the above error while trying to run a simple PL/SQL program that uses procedures. I dont know what went wrong, please help out.
declare
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
end;
begin
x integer:=121;
y integer;
palindrome(x,y);
dbms_output.put_line(y);
end;
/
ERROR at line 2:
ORA-06550: line 2, column 2: PLS-00103: Encountered the symbol
"CREATE" when expecting one of the following: begin function pragma
procedure subtype type current cursor delete exists prior
Modifications to your code.
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
dbms_output.put_line(y);
end;
/
Execute that procedure
declare
y number;
begin
palindrome(133,y);
end;
You only need DECLARE when you're creating an anonymous PL/SQL block that contains variable declarations. When you're creating a named procedure/function/package, then your CREATE OR REPLACE ... statement takes the place of the DECLARE. Anything between that statement and the corresponding BEGIN is known as the declaration section.
It's not especially clear if you're trying to create a named procedure that you can then call with another PL/SQL anonymous block, in which case you'd do:
create or replace procedure palindrome (x in number,y out number) is
i integer;
j integer;
k integer:=0;
begin
i:=x;
while i>0
loop
j:=mod(i,10);
k:=k*10+j;
i:=i/10;
end loop;
y:=k;
end;
/
declare
x integer := 121;
y integer;
begin
palindrome(x,y);
dbms_output.put_line(y);
end;
/
or if you're trying to declare a procedure inside an anonymous PL/SQL, in which case you'd do:
declare
x integer := 121;
y integer;
procedure palindrome (x in number, y out number) is
i integer;
j integer;
k integer := 0;
begin
i := x;
while i > 0
loop
j := mod(i,10);
k := k*10 + j;
i := i/10;
end loop;
y := k;
end palindrome;
begin
palindrome(x,y);
dbms_output.put_line(y);
end;
/

factorial of a number in pl/sql

The following pl/sql program generates an error on execution on line the sum :=temp*sum; encountered symbol ; when expecting ( . Please explain my mistake.
declare
n number;
temp number;
sum number := 1;
begin
n := &n;
temp := n;
while temp>0 loop
sum := temp*sum;
temp := temp-1;
end loop;
dbms_output.put_line('Factorial of '||n||' is '||sum);
end;
/
Maybe not the answer to your question, but there is no need for PL/SQL here:
select round(exp(sum(ln(level))))
from dual
connect by level <= 5;
where 5 is your number (5!).
Additionally, if you like to operate faster in PL/SQL use pls_integer instead of number.
UPDATE
So according to comments I felt free to test:
create or replace package test_ is
function by_query(num number) return number deterministic;
function by_plsql(num number) return number deterministic;
end test_;
/
create or replace package body test_ is
function by_query(num number) return number deterministic
is
res number;
begin
select round(exp(sum(ln(level))))
into res
from dual
connect by level <= num;
return res;
end;
function by_plsql(num number) return number deterministic
is
n number := 0;
begin
for i in 1..num loop
n := n + ln(i);
end loop;
return round(exp(n));
end;
end test_;
So there are two functions with different content. Test query:
declare
dummy number;
begin
for i in 1..10000 loop
dummy := test_.by_query(5);
end loop;
end;
0.094 sec.
declare
dummy number;
begin
for i in 1..10000 loop
dummy := test_.by_plsql(5);
end loop;
end;
0.094 sec.
You'll say I am cheater and using deterministic keyword but here it is obvious and is needed by logic. If I remove it, the same scripts are working 1.7 sec vs 1.3 sec, so procedure is only a bit faster, there is no even double-win in performance. The totally opposite effect you will get if you use the function in a query so it is a fair trade.
Sum is reserved word in sql. Change variable name like
declare
n number;
temp number;
sum_ number := 1;
begin
n := &n;
temp := n;
while temp>0 loop
sum_ := temp*sum_;
temp := temp-1;
end loop;
dbms_output.put_line('Factorial of '||n||' is '||sum_);
end;
/
declare
n number;
i number;
sum_of_log_10s number;
exponent number;
base number;
begin
n := &n;
i := 1;
sum_of_log_10s := 0;
while i <= n loop
-- do stuff
sum_of_log_10s := sum_of_log_10s + log(10,i);
i := i + 1;
end loop;
dbms_output.put_line('sum of logs = '||sum_of_log_10s);
exponent := floor(sum_of_log_10s);
base := power(10,sum_of_log_10s - exponent);
dbms_output.put_line(n||'! = '||base||' x 10^'||exponent);
end;
I came up with this code that I like even better than #smnbbrv's answer. It's a great way to check the speed of a machine. I've been using a variation of this since my Atari 800
ALTER SESSION FORCE PARALLEL DDL PARALLEL 16;
ALTER SESSION FORCE PARALLEL DML PARALLEL 16;
ALTER SESSION FORCE PARALLEL QUERY PARALLEL 16;
with t as (
select /*+materialize*/
rownum i
from dual connect by rownum < 100000 -- put number to calculate n! here
)
,t1 as (
select /*+parallel(t,16)*/ /*+materialize*/
sum(log(10,i)) logsum
from t
)
select
trunc(power(10,(mod(logsum,1))),3) ||' x 10^'||trim(to_char(floor(logsum),'999,999,999,999')) factorial
-- logsum
from t1
;
-- returns 2.824 x 10^456,568
Here is the simple code for finding factorial of number at run time...
declare
-- it gives the final answer after computation
fac number :=1;
-- given number n
-- taking input from user
n number := &1;
-- start block
begin
-- start while loop
while n > 0 loop
-- multiple with n and decrease n's value
fac:=n*fac;
--dbms_output.put(n||'*');
n:=n-1;
end loop;
-- end loop
-- print result of fac
dbms_output.put_line(fac);
-- end the begin block
end;

PlSQL showing compilation error

PL/SQL is showing compilation error. Function is working correctly and successfully compiled, but if running pl/sql query then showing compilation.
CREATE OR REPLACE FUNCTION f(
num IN NUMBER,
num2 IN NUMBER,
ans OUT NUMBER
) RETURN number IS
BEGIN
ans := num + num2;
RETURN ans;
END;
CREATE table add1(val1 number(2),val2 number(1),ans number(3));
SET SERVEROUTPUT ON ;
DECLARE
a1 add1.val1%TYPE;
b1 add1.val2%TYPE;
sum add1.ans%TYPE;
BEGIN
WHILE a1!=-99
LOOP
a1 := &a1;
b1 := &b1;
sum := f(a1, b1, sum);
INSERT INTO add1 VALUES(a1, b1, c1);
END LOOP;
END;
You cannot use IN and OUT in functions. Only in procedures. Correct approach is:
create or replace function f(
num number,
num2 number
) return number is
ans number;
begin
ans := num + num2;
return ans;
end;
UPD: Oh, boy! There are too many errors in your code. Maybe you should describe what you are trying to do with all that stuff.

Resources