Cannot find the error in my code: ";expected" - syntax

When i'm running the code it says that there's an syntax error and it marks me the else: Fatal: Syntax Error, ;expected but ELSE found.(btw this is just one part from my program) can anyone tell me what am i doing wrong?
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;

try
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
end
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;

I believe you need one more end; tag as you have two if begin statements
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
end
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;

Related

using date type with REGEXP in oracle

I would like to know if someone could please tell me why the first one does not work and the second one does? Thanks in advance :)
DECLARE
fecha DATE:= TO_DATE('20/04/1996', 'DD/MM/YYYY');
BEGIN
IF NOT REGEXP_LIKE(TO_CHAR(fecha), '^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}') THEN
DBMS_OUTPUT.PUT_LINE('DATE IS NOT CORRECTLY');
ELSE
DBMS_OUTPUT.PUT_LINE('DATE CORRECTLY');
END IF;
END;
-------------------------
DECLARE
fecha varchar2(10):= '20/04/1996';
BEGIN
IF NOT REGEXP_LIKE(fecha, '^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}') THEN
DBMS_OUTPUT.PUT_LINE('FECHA INGRESADA INCORRECTAMENTE');
ELSE
DBMS_OUTPUT.PUT_LINE('FECHA INGRESADA CORRECTAMENTE');
END IF;
END;
How should I write inside a trigger? i have doubt about :NEW.date_start, should I put TO_CHAR(:NEW.fecha_ingreso)?
IF NOT REGEXP_LIKE(:NEW.date_start, '^[0-9]{1,2}/[0-9]{2}/[0-9]{4}') THEN
RAISE error_formato_fecha;
END IF;

PL/SQL - how to repeat alternating text for a given number of times with a final output?

I want to make a procedure called OE that will have text output based on the number that I define.
For example, inputting the number 6 will give the following output:
odd
even
odd
even
odd
even
= even steven!
and inputting the number 5 will give the following output:
odd
even
odd
even
odd
= you oddball!
I'm completely new at this and have been struggling to get the odd number to load correctly (for some reason, it gets stuck in an infinite loop). Any help would be appreciated! Here is what I got so far:
CREATE OR REPLACE procedure oe
(p_n IN number)
AS
v_n number;
v_on number;
BEGIN
v_n := p_n;
v_on := p_n;
IF v_n>0 THEN LOOP
dbms_output.put_line('odd');
v_n := v_n-1;
dbms_output.put_line('even');
v_n := v_n-1;
If v_n=0 then
exit;
if v_on mod 2 > 0 then dbms_output.put_line('=' || ' you oddball!');
exit;
else
dbms_output.put_line('=' || ' even steven!');
exit;
end if;
end if;
end loop;
end if;
END;
/
You are not using exit conditions properly hence your code is going in infinite loop. You simplify your logic as below. Let me know it it works for you.
You may add few validations to make sure you get proper input parameters such as p_n > 0 and other.
CREATE OR REPLACE procedure oe
(p_n IN number)
AS
begin
for i in 1..p_n
loop
if mod(i,2)=1 then dbms_output.put_line('odd');
else dbms_output.put_line('even');
end if;
end loop;
if mod(p_n,2)=1 then dbms_output.put_line('= you oddball!');
else dbms_output.put_line('= even steven!');
end if;
end;
hemalp108 has already answered this, but I just wanted to add that you don't even need the if/else logic that fills the procedure (except perhaps for handling values less than 1, which I'll leave as an exercise), because we have case:
create or replace procedure oe
( p_n in number )
as
begin
for i in 1 .. p_n loop
dbms_output.put_line(case mod(i,2) when 1 then 'odd' else 'even' end);
end loop;
dbms_output.put_line(case mod(p_n,2) when 1 then '= you oddball!' else '= even steven!' end);
end;
(You may also notice how laying your code out neatly is half the way towards debugging it.)

Backtracking not showing anything

I want to find all the posibilities of moving around these numbers 1,2,3,4,5 with the conditions that "4,3" is not possible and 1 and 5 can't be in the same solution. I have this code but when I compile it it doesn't show me anything.
var x:array[1..50] of integer;
i,k:integer;
avems,evalid:boolean;
procedure init;
begin
x[k]:=0;
end;
procedure succesor;
begin
if x[k]<5 then avems:=true
else avems:=false;
if avems=true then x[k]:=x[k]+1;
end;
function solutie:boolean;
begin
if k=3 then solutie:=true
else solutie:=false;
end;
procedure valid;
begin
evalid:=true;
for i:=1 to k-1 do
if (x[i]=1) or (x[i]=5) and (x[k]=1) or (x[k]=5) then evalid:=false;
if (k>1) and (x[k-1]=4) and (x[k]=3) then evalid:=false;
end;
procedure tipar;
begin
for i:=1 to 3 do
write(x[i],' ');
writeln;
end;
begin
k:=1;
init;
while k>0 do
begin
repeat
succesor;
until not(avems) or (avems and evalid);
if avems then
if solutie then tipar
else begin
k:=k+1;
init;
end
else k:=k-1;
end;
end.
What do you expect? Your code increments x[1] until it reaches 5, then sets avems:=false; and then decrements k to zero and exits the while loop.
Remember that Pascal does not use indentation for blocks like Python, so your
last steps are written more clearly as
if avems then begin
if solutie then tipar
else begin
k:=k+1;
init;
end
end
else k:=k-1;
If this is not what you want, you have to code some begin/end.

Oracle Forms - Error 103, Encountered the symbol "END"

I'm using Oracle Forms Builder 11.
My code:
declare
type myType is varray(3000) of my_table%rowtype;
myAsset myType:=myType();
i number;
n number;
exNoInvNum exception;
begin
go_block('my_block');
first_record;
i:=1;
loop
myAsset.extend();
myAsset(i).hqId:=:my_block.hqId;
myAsset(i).deptId:=:my_block.deptId;
myAsset(i).invNum:=:my_block.invNum;
exit when :system.last_record='TRUE';
i:=i+1;
next_record;
end loop;
go_block('my_block');
first_record;
loop
if (:my_block.linkedInvNum is not null) then
n:=0;
select count(*) into n
from my_table s
where s.invNum=:my_block.linkedInvNum
and s.hqId=:my_block.hqId
and (s.deptId=:my_block.deptId
or (s.deptId is null and :my_block.deptId is null));
if (n=0) then
for i in myAsset.first .. myAsset.last loop
if (myAsset(i).invNum=:my_block.linkedInvNum
and myAsset(i).hqId=:my_block.hqId
and (myAsset(i).deptId=:my_block.deptId
or (myAsset(i).deptId is null and :my_block.deptId is null))) then
n:=1;
end if;
end loop;
end if;
if (n=0) then
raise exNoInvNum;
else
commit_form;
go_block('my_table');
clear_block(no_validate); set_item_property('my_block.generate_excel',ENABLED,property_true); set_item_property('my_block.process_data',ENABLED,property_false);
end if;
end if;
exit when :system.last_record='TRUE';
next_record;
end loop;
exception
when exNoInvNum then
message('No existing inventory number!');
when others then
null;
end;
end;
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
I checked my code for typo errors, missing semicolumns and similar stuff, but it looks like everything is fine.
Any ideas?
I get Error 103 ad line 2, column 1: Encountered the symbol "END"
Well, you do have an extra END keyword in your PL/SQL block.
end;
end;
The syntax for an anonymous PL/SQL block is:
DECLARE
...
BEGIN
...
EXCEPTION
...
END;
And, this:
when others then
null;
is itself a bug in your code.
A when others is almost always a BUG unless it is immediately followed by a RAISE. Remember, for errors, RAISE –> CATCH –> HANDLE. why do we need an exception handler? To catch the errors, log them(optional), and finally do something about them.
Read WHEN OTHERS THEN NULL – A bug

How do i use multiple statements to one case statement?

So i'm using pascal, and i want to add multiple statements to one case. I tried this code but i get the error:
"Error: Constant and CASE types do not match"
procedure pay;
begin
loop:=loop+1;
CASE loop OF
1:
writeln('E-Mail: ');
readln(mailO[1]);
writeln('amount: ');
readln(amount[1]);
end;
Wrap compound statements in a begin and end:
procedure pay;
begin
loop:=loop+1;
CASE loop OF
1:
begin
writeln('E-Mail: ');
readln(mailO[1]);
writeln('amount: ');
readln(amount[1]);
end;
2: writeln('simple statement');
3: begin
writeln('something else');
writeln('etc.');
end;
end;
end;

Resources