Why throw an exception when multiplying? - pascal

I want square ,but i dont remember how to do it becauseI multiplying is a variable, but throw exeption and i don't know why.
please help i don't know what else to do
program Project1;
uses crt;
type TSquare=class
len:integer;
place:integer;
function Perimetr:integer;
function Area:integer;
function Verify():boolean;
procedure Show(P,S:real);
constructor Create(P,l:integer);
end;
function TSquare.Perimetr:integer;
var P:integer;
begin
P:=len*4;
end;
function TSquare.Area:integer;
var S:integer;
begin
S:=len*len;
end;
function TSquare.Verify:boolean;
begin
end;
procedure TSquare.Show(P,S:real);
begin
write('Площидь=',S,'Перимитр=',P);
end;
constructor TSquare.Create(p,l:integer);
begin
len:=l;
place:=p;
end;
var r: TSquare;
a,b:integer;
begin
r.Create(1,5);
r.Show(r.Perimetr(),r.Area());
end.

SIGSEGV means access of invalid memory. Here you do not create the TSquare object correctly.
r := TSquare.Create(1,5); is the correct way to create an instance of an object.

Related

"" is an invalid integer lazarus

I have no idea how to fix it
procedure TForm1.Button1Click(Sender: TObject);
var conf, adult: Integer;
pr:real;
begin
adult:=StrToInt(Edit2.text);
conf:=StrToInt(Edit3.text);
pr:=StrToFloat(Edit4.text);
If Peak1.Checked then
pr:=(adult*8.95)+(conf*6.45)
else
pr:=(adult*7.45)+(conf*5.95);
Edit4.text:='£'+(FloatToStr(pr));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit2.clear;
edit3.clear;
edit4.clear;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close
end;
I tried to change from StrToInt to FloatToString, I have no idea how to fix it
The error "" is an invalid integer means you are trying to convert an empty string to a number which is impossible and hence the exception.
If you are not sure that the strings actually contain correctly written integers/floats, then use the TryStrToInt and TryStrToFloat functions. These functions do not throw exceptions and instead return a boolean value indicating whether the conversion succeeded — use the results to detect problems with user input.

Trying to call a Function inside a stored procedure in oracle

i am trying to call a function from stored procedure in Oracle, but not getting any idea how to do.
my function has two IN parameter and one OUT parameter.
in my procedure i am using out sys refcursor . Any refrence or example will help me a lot.
Here is a simple example for calling function inside procedure. Also as mentioned by APC using OUT in function is a bad practice. Instead you can return your required output. And I'm not sure how you are using sys_refcursor, so modify your procedure accordingly
CREATE OR REPLACE FUNCTION SUM_OF_2(NUM1 IN NUMBER,NUM2 IN NUMBER) RETURN NUMBER
IS
RESULT_SUM NUMBER;
BEGIN
RESULT_SUM:=NUM1+NUM2;
RETURN RESULT_SUM;
END;
CREATE OR REPLACE PROCEDURE CALL_FUNCTON(NUM1 NUMBER,NUM2 NUMBER)
AS
V_FINAL_RESULT NUMBER;
BEGIN
V_FINAL_RESULT:=SUM_OF_2(NUM1,NUM2);
DBMS_OUTPUT.PUT_LINE(V_FINAL_RESULT);
END;
BEGIN
CALL_FUNCTON(5,10);
END;
/
CHECK DEMO HERE
Not sure on what your requirement is , maybe you are just trying the code for education purposes. Generally I have not seen much code which uses OUT parameter with functions, in case you want to return multiple values to the caller object then you could use a procedure with more then one OUT variables. There are some limitation on how an oracle function with OUT parameter would differ from a normal function.
CREATE OR REPLACE FUNCTION temp_demo_func(out_var1 OUT NUMBER)
RETURN VARCHAR2 IS
BEGIN
out_var1 := 1;
RETURN 'T';
EXCEPTION
WHEN OTHERS THEN
RETURN 'F';
END temp_demo_func;
/
CREATE OR REPLACE PROCEDURE temp_demo_proc
(
in_var1 NUMBER
,cur_refcur_out OUT SYS_REFCURSOR
) IS
res VARCHAR2(1);
out_var1 NUMBER;
BEGIN
res := temp_demo_func(out_var1 => out_var1);
dbms_output.put_line(out_var1);
OPEN cur_refcur_out FOR
SELECT in_var1
,out_var1
,res
FROM dual;
END;
/
set serveroutput on
declare
cur_refcur_out Sys_Refcursor;
in_var1 number := 22;
begin
temp_demo_proc(in_var1 => in_var1
,cur_refcur_out => cur_refcur_out);
end;
/

Procedure checking set up parameters (PL/SQL, ORACLE)

In the package I have couple of procedure that set global variables, example below:
...
PROCEDURE setA (pp IN VARCHAR2)
IS BEGIN global_vName := pp; END;
PROCEDURE setB (qq IN VARCHAR2)
IS BEGIN global_vColor := qq; END;
FUNCTION getA RETURN VARCHAR2
IS BEGIN RETURN global_vName; END;
FUNCTION getB RETURN VARCHAR2
IS BEGIN RETURN global_vColor; END;
...
Now in the PL/SQL block I'm doing test if they are working correclty:
Begin
mypack.setA('NameA');
mypack.setB('ColorB');
End;
How to write a procedure that will check if global_vName and global_vColor are set up?
If they are null procedure should return exception. Please help.
Do you mean this?
FUNCTION getA RETURN VARCHAR2 IS
BEGIN
IF global_vName IS NULL THEN
RAISE NO_DATA_FOUND;
END IF;
RETURN global_vName;
END;
There is no possibility to execute some code after the end statement - you should write it explicitly if you need additional check. As I understand, you want be sure that global variables are always initialized, so you can use the package initialization:
create or replace package body mypack as
PROCEDURE setA (pp IN VARCHAR2)
IS BEGIN global_vName := pp; END;
PROCEDURE setB (qq IN VARCHAR2)
IS BEGIN global_vColor := qq; END;
FUNCTION getA RETURN VARCHAR2
IS BEGIN RETURN global_vName; END;
FUNCTION getB RETURN VARCHAR2
IS BEGIN RETURN global_vColor; END;
< here are your other functions and procedures >
begin
-- here is an initialization section
setA('NameA');
setB('ColorB');
end mypack;
The initialization section will be executed automatically by Oracle before the first user's call to package (function, procedure, cursor, variable, etc.). So you can be sure that your variables are always initialized.

Oracle PL/SQL: Call DML procedure from a function

I have a procedure that has DML commands. the procedure accepts a variable of type out, and it returns a value.
i need call this procedure from a function.
the goal is  that the function will return the value of the variable out returns the procedure.
(i need it for SSIS, but I believe that it is useful in other cases.)
During attempts, I got these errors:
ORA-14551: cannot perform a DML operation inside a query tips.
ORA-06519: active autonomous transaction detected and rolled back.
I'm looking for the right syntax to do it.
Example of a solution that works:
The procedure:
create or replace procedure MyProc(outRes OUT NUMBER)
is
begin
update some_table set some_description = 'abc';
commit;
if (some_condition) then
outRes := 666;
else
outRes := 999;
end if;
end MyProc;
Note: You must do commit; at the end of the DML command.
The function:
CREATE or replace FUNCTION MyFunc
RETURN int IS
PRAGMA AUTONOMOUS_TRANSACTION;
myVar number;
begin
MyProc(myVar);
return myVar;
END MyFunc;
Note that at the beginning of the function has: PRAGMA AUTONOMOUS_TRANSACTION;
And this function call:
select MyFunc() as res from dual;
Here is an example of what you need to do. Do know this is UNTESTED, but should give you a general idea of which way to go. This is known as Dynamic SQL and uses bind variables. There's a lot more I don't know such as the data type your procedure spits out and what not... so if it's not varchar2 then change it accordingly...
FUNCTION myFunc(procedure_call varchar2) RETURN VARCHAR2
IS
v_out1 varchar2(500);
BEGIN
EXECUTE IMMEDIATE 'begin '||procedure_call||'( :out1 ); end;' using v_out1;
RETURN v_out;
END;

PL/SQL call a function outside a package

I define a function outside a package, tried to call this function, failed.
how to fix it ? thanks
create or replace
package body test_erp AS
procedure init_data is
begin
logMessage('procedure init_data');
end init_data;
end test_erp;
/
show error
error is
PLS-00221: 'LOGMESSAGE' is not a procedure or is undefined
As the error suggests logmessage is not a procedure. It's a function. As functions return something you need to assign this to a variable. You know that logmessage returns a number so you need to declare a variable to put this return value into.
create or replace package body test_erp AS
procedure init_data is
l_success number;
begin
l_message := logMessage('procedure init_data');
dbms_output.put_line(to_char(l_success));
end init_data;
end test_erp;
/
However, it looks like logmessage should in fact be a procedure. I assume you're executing DML statements (update/insert) in this. A function call be used in a select statement unless this is the case, which means that there's always the possibility of an error occurring. If logmessage were a procedure you can declare an out parameter to tell the calling procedure whether everything worked or not; something like the following:
create or replace procedure logmessage( msg in varchar2, success out number) is
begin
insert into logs values(msg);
success := 1;
exception when others then
success := 0;
end logmessage;
You can then call it as follows:
create or replace package body test_erp AS
procedure init_data is
l_success number;
begin
logMessage('procedure init_data', l_success);
dbms_output.put_line(to_char(l_success));
end init_data;
end test_erp;
/
If logmessage isn't going to be used outside the package test_erp I would put it inside the package; it keeps the namespace cleaner and avoids it getting used mistakenly be another package / call etc.
Assuming that logMessage is the same function from this post:
Since logMessage is a function (and returns a number) you need to call it like this:
procedure init_data is
i number;
begin
i := logMessage('procedure init_data');
end init_data;

Resources