How to fix PLS-00103 error in case statement? - sql

I"m using oracle sql developer and I'm trying to test case statements, but I keep getting errors ORA-06550 and PLS-00103.
set v_naujas_rizikos_lygis := 100;
begin
declare v_naujas_rizikos_lygis INT;
set v_naujas_rizikos_lygis :=225;
begin
case
when v_mokejimu_suma>0 and v_mokejimu_suma<100 then 1
when v_mokejimu_suma>100 and v_mokejimu_suma<200 then 2
when v_mokejimu_suma>200 and v_mokejimu_suma<300 then 3
end v_naujas_rizikos_lygis
print(v_naujas_rizikos_lygis);
I need my case statment to set a new value for my variable v_naujas_rizikos_lygis. Any ideas?

If think you mean
v_naujas_rizikos_lygis := case
when v_mokejimu_suma>0 and v_mokejimu_suma<100 then 1
when v_mokejimu_suma>100 and v_mokejimu_suma<200 then 2
when v_mokejimu_suma>200 and v_mokejimu_suma<300 then 3
else ...
end;
(replace ... with a default value)

Related

Simple ISEMPTY() function in SQL Server throws a non-boolean type error

I'm trying to write a simple ISEMPTY function in Microsoft SQL Server:
DROP FUNCTION IF EXISTS ISEMPTY;
GO
CREATE FUNCTION ISEMPTY
(#charsequence nvarchar(max))
RETURNS BIT
AS
BEGIN
DECLARE #result BIT;
IF (#charsequence IS NULL OR LEN(#charsequence) = 0)
SET #result = 1
ELSE
SET #result = 0;
RETURN #result;
END
GO
When I want to test it with:
SELECT CASE WHEN dbo.ISEMPTY('') THEN 'REACHED!' END;
I get the following error:
[S0001][4145] Line 1: An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
What goes wrong here?
A boolean is expected after when in the case expression, but your function returns a bit.
Try this instead:
SELECT CASE WHEN dbo.ISEMPTY('') = 1 THEN 'REACHED!' END;
The function returns a number so you need a comparison with a number to get a boolean value
CREATE FUNCTION ISEMPTY( #charsequence nvarchar(max))
returns BIT AS
begin
DECLARE #result BIT;
IF (#charsequence IS NULL OR LEN(#charsequence) = 0 )
SET #result = 1;
ELSE
SET #result = 0;
RETURN #result;
end
GO
SELECT CASE WHEN dbo.ISEMPTY('') = 1 THEN 'REACHED!' END;
GO
| (No column name) |
| :--------------- |
| REACHED! |
db<>fiddle here

Error "Invalid Instruction" in PL/SQL function using Oracle

here i'm trying to create a sort of a boolean function but when i try to compile it, it says me that my instruction "truth INTEGER;" is not correct. I can't figure out where does the problem come from because every code sample that i've seen on Google is quite similar to mine. Using Oracle is totally new for me so i'm kinda lost if someone is okay to help me...
I tried a lot of things by searching but nothing works so i'm here to ask for some help...
CODE:
CREATE OR REPLACE FUNCTION HISTOTOX.matching_idipl (iDiplNumero INTEGER) RETURN INTEGER
AS
cpt INTEGER;
truth INTEGER;
BEGIN
SELECT count(*) INTO cpt FROM GARNUCHE.INSC_DIPL WHERE IDIPL_NUMERO = iDiplNumero;
IF cpt = 1 THEN
truth := 1;
ELSE
truth := 0;
END IF;
RETURN truth;
END;
What you reported isn't Oracle error. I replicated your code in my 11g and everything works just fine:
SQL> CREATE OR REPLACE FUNCTION matching_idipl (iDiplNumero INTEGER) RETURN INTEGER
2 AS
3 cpt INTEGER;
4 truth INTEGER;
5
6 BEGIN
7 SELECT count(*) INTO cpt FROM INSC_DIPL WHERE IDIPL_NUMERO = iDiplNumero;
8 IF cpt = 1 THEN
9 truth := 1;
10 ELSE
11 truth := 0;
12 END IF;
13 RETURN truth;
14 END;
15 /
Function created.
SQL> select matching_idipl(1) from dual;
MATCHING_IDIPL(1)
-----------------
1
SQL>
So: where exactly did you execute that code? Which error did you get (exact message and error code, please)?

PL/SQL: ORA-00920: invalid relational operator

I get this error
23/112 PL/SQL: ORA-00920: invalid relational operator
It's pointing to AND CURRENT OF statement..
CREATE OR replace PROCEDURE alga_uz_pasirodyma(grupe_id in grupes.id%TYPE, alga in out number)
IS
v_kliento_id nariai.asm_kodas%TYPE;
TYPE bendras IS RECORD (
alga number
);
globalus bendras;
CURSOR c_klientai IS
SELECT nariai.asm_kodas
FROM nariai
where nariai.fk_grupe = grupe_id
FOR UPDATE OF nariai.alga;
BEGIN
globalus.alga:= alga;
IF grupe_id <= 0 THEN
raise_application_error(-20101, 'Nepavyko surasti grupes');
END IF;
OPEN c_klientai;
LOOP
FETCH c_klientai INTO v_kliento_id;
EXIT WHEN c_klientai%NOTFOUND;
UPDATE nariai set nariai.alga = nariai.alga * globalus.alga where nariai.asm_kodas = v_kliento_id AND CURRENT OF c_klientai;
END LOOP;
UPDATE grupes set grupes.pasirodymu_kiekis = grupes.pasirodymu_kiekis + 1 where grupes.id = grupe_id;
SELECT max(nariai.alga) into alga from nariai where nariai.fk_grupe = grupe_id;
CLOSE c_klientai;
END alga_uz_pasirodyma;
What should I do? I believe everything is declared correctly in the where statement..
"CURRENT OF" should be by itself in the where clause. It allows you to update or delete the record at the current loop iteration of the cursor.
On a different note, I don't see you doing anything significant in the loop to warrant a cursor. Ignore this note if that will change, otherwise just run the update "where nariai.fk_grupe = grupe_id"

PL/SQL ORA06550 error in an update statement

The following is a proc code that I have written . I am getting a syntax error in the block marked from (A) to (B) , namely , ORA06550, saying SQL command not ended properly.
If I eliminate the line l.ban4_upd ='UPDATED' there are no more of nay error messages.
I don't know how to correct it . Hoping for your help and thanks in advance
DECLARE
dummy_BAN4 VARCHAR2(30);
dummy_bank_acc_num VARCHAR2(20);
CURSOR c_customers is
SELECT BAN4,Bank_acc_num FROM Test_Table ;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into dummy_BAN4 , dummy_bank_acc_num;
IF c_customers%notfound THEN
update Test_Table chs Set
chs.error_msg ='No such record found in DB '
where bank_acc_num =dummy_bank_acc_num;
END IF;
update Transact_ord2 l Set.........................(A)
l.ban4_upd ='UPDATED'
l.x_account_number =dummy_BAN4
where X_account_number =dummy_bank_acc_num; ..........(B)
you are missing , between the fields you set:
update Transact_ord2
Set ban4_upd = 'UPDATED',
x_account_number = dummy_BAN4
where X_account_number = dummy_bank_acc_num;
Check this

Recursive Stored Procedures

I found this code snipped (Source):
CREATE PROCEDURE rec_fib(n INT, OUT out_fib INT)
BEGIN
DECLARE n_1 INT;
DECLARE n_2 INT;
IF (n=0) THEN
SET out_fib=0;
ELSEIF (n=1) then
SET out_fib=1;
ELSE
CALL rec_fib(n-1,n_1);
CALL rec_fib(n-2,n_2);
SET out_fib=(n_1 + n_2);
END IF;
END
This code works with MySQL. In how far do I have to modify it to run on DB2? I cannot seem to find a running minimal example of an recursive stored procedure for DB2.
This works for me: (I haven't done more than make it work, so alternative coding could also work.)
First, add these two lines:
DECLARE n_3 INT;
DECLARE n_4 INT;
Then modify this small section:
ELSE
set n_3 = n - 1;
set n_4 = n - 2;
CALL rec_fib(n_3,n_1);
CALL rec_fib(n_4,n_2);
That's all. Runs on IBM i 6.1 DB2 UDB.
The following code is from SQL tips for DB2, written by Serge Rielau
CREATE OR REPLACE FUNCTION Fib(n INTEGER) RETURNS DECIMAL(31, 0)
BEGIN
DECLARE res DECIMAL(31, 0);
CASE WHEN n = 0 THEN
SET res = 0;
WHEN n = 1 THEN
SET res = 1;
WHEN n > 1 THEN
BEGIN
DECLARE stmt STATEMENT;
PREPARE stmt FROM 'SET ? = Fib(? - 1) + Fib(? - 2)';
EXECUTE stmt INTO res USING n, n;
END;
ELSE
SIGNAL SQLSTATE '78000' SET MESSAGE_TEXT = 'Bad input';
END CASE;
RETURN res;
END;
/
For more information, please check the source page of this code: https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/recursive_sql_pl?lang=en