Oracle 12c: How to resolve a bad bind variable error? - oracle

I have this table named, MOTOR_PRODUCTS, where it needs to store an image. I have written a PL/SQL code and when I already compile it, an error appears that says 'bad bind variable 'MOTOR_PRODUCTS.motor_image'. How to resolve this kind of error? Thank you to people who will help me.
Here with my PL/SQL code:
Declare A number;
Begin
IF
:MOTOR_PRODUCTS.MOTOR_ID IS NULL OR
:MOTOR_PRODUCTS.CATEGORY_ID IS NULL OR
:MOTOR_PRODUCTS.MOTOR_NAME IS NULL OR
:MOTOR_PRODUCTS.MOTOR_YEAR IS NULL OR
:MOTOR_PRODUCTS.MOTOR_COLOR IS NULL OR
:MOTOR_PRODUCTS.MOTOR_PRICE IS NULL OR
:MOTOR_PRODUCTS.MOTOR_WARRANTY_PERIOD IS NULL OR
:MOTOR_PRODUCTS.MOTOR_DESCRIPTION IS NULL
THEN A:= SHOW_ALERT ('ALERT_FILLUP');
ELSE
INSERT INTO MOTOR_PRODUCTS (motor_id, motor_name, category_id, motor_year, motor_color, motor_price, motor_description, motor_features, motor_warranty_period, motor_specifications, motor_image)
VALUES (:MOTOR_PRODUCTS.category_id||:MOTOR_PRODUCTS.motor_id,
:MOTOR_PRODUCTS.motor_name,
:MOTOR_PRODUCTS.category_id,
:MOTOR_PRODUCTS.motor_year,
:MOTOR_PRODUCTS.motor_color,
:MOTOR_PRODUCTS.motor_price,
:MOTOR_PRODUCTS.motor_description,
:MOTOR_PRODUCTS.motor_features,
:MOTOR_PRODUCTS.motor_warranty_period,
:MOTOR_PRODUCTS.motor_specifications,
:MOTOR_PRODUCTS.motor_image);
FORMS_DDL('COMMIT');
A := SHOW_ALERT('ALERT_SAVE');
END IF;
End;

Related

ORACLE APEX Item values setting with PL\SQL Expression source facing PLS-00103 warning

I am trying to work on setting value to a hidden item that is running at the background of the APEX page using PL\SQL expression as there is some checking need to be done. But somehow I am having some 'PLS-00103' Encountered the symbol "Declare".... error when compile.I would like to seek for help on how to solve this. Thank you
enter image description here
DECLARE
p_batch_id_stg NUMBER;
p_batch_id_fail NUMBER;
p_final_batch_id NUMBER;
BEGIN
SELECT
BATCH_ID INTO p_batch_id_stg
FROM
NIOE_ORDER_HEADER_STG_V
WHERE
BATCH_ID !='' OR BATCH_ID IS NOT NULL
ORDER BY
BATCH_ID DESC
FETCH FIRST 1 ROWS ONLY;
SELECT
BATCH_ID INTO p_batch_id_fail
FROM
NI.NI_OM_ORDER_IMPORT
WHERE
BATCH_ID !='' OR BATCH_ID IS NOT NULL
ORDER BY
BATCH_ID DESC
FETCH FIRST 1 ROWS ONLY;
IF p_batch_id_fail > p_batch_id_stg THEN
p_final_batch_id := p_batch_id_fail;
ELSIF p_batch_id_stg > p_batch_id_fail THEN
p_final_batch_id := p_batch_id_stg;
END IF;
RETURN p_final_batch_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
/
... using PL\SQL expression
Well, this is not a PL/SQL expression. It is written in PL/SQL, but it represents a Function Body that returns some value.
It is not clear where exactly you're doing this (is it that item's "Source" or "Default" value?), but - nonetheless, I'd suggest you to change type to "Function Body".

Issue with variables and proc oracle

Im a beginner in oracle and i need help. I want to use variables in my proc but I dont know how to do it. I want to affect a string value to a variable depending to an other variable but when i lunch my proc nothing is happened. The code is not passing through the IF statement and I dont know why.
What I tried to do :
PROCEDURE contractual_control(p_id_depot IN depot.id_depot%TYPE) IS
-- Code_out proc
v_cod_out varchar(15) := null;
-- List of CODE KO_FCT
ko_fct_01 varchar(15) := 'KO_FCT_01';
ko_fct_02 varchar(15) := 'KO_FCT_02';
ko_fct_03 varchar(15) := 'KO_FCT_03';
ko_fct_04 varchar(15) := 'KO_FCT_04';
-- List of CODE KO_TEC
ko_tec_01 varchar(15) := 'KO_TEC_01';
ko_tec_02 varchar(15) := 'KO_TEC_02';
BEGIN
-----------------------------------------------------------------------------------------------------
-- CC1 - Objets interdits avec Presse Export - JIRA 968
-----------------------------------------------------------------------------------------------------
SELECT ko_fct_04 INTO v_cod_out FROM DEPOT_IMPORT
WHERE DEPOT_IMPORT.ID_ZONE IS NOT NULL
AND DEPOT_IMPORT.TYP_ELEMENT IN ('ENCART','INCARTO','INCPLUS','OPP');
IF v_cod_out is not null THEN
insert into babas_test values ('code retours = ' || v_cod_out);
ELSE
insert into babas_test values ('Pas de code retours');
END IF;
COMMIT;
END contractual_control;
Can you check your select statement ,probably it returns more than one row.
SELECT ko_fct_04 FROM DEPOT_IMPORT
WHERE DEPOT_IMPORT.ID_ZONE IS NOT NULL
AND DEPOT_IMPORT.TYP_ELEMENT IN ('ENCART','INCARTO','INCPLUS','OPP');
also you can add exception to your procedure to see the errors.
exception when others then
DBMS_OUTPUT.put_line ('ERROR :' || SQLERRM );

is there a better way to write avg procedure?

I wrote this procedure but it keeps showing "WARNING: Procedure creates with compilation errors" and I don't know why, here's the table I created
create table EnrolledInClasses (
St_Id char(9) primary key,
C_Id char(6),
GradeN Number(2),
constraint FK_StId Foreign key (St_Id) references Student (St_Id),
constraint FK_CoID Foreign key (C_Id) references course (C_Id)
constraint CheckGrade check (Grade>-1)
);
and here's the procedure:
CREATE OR REPLACE PROCEDURE Avg_grades
IS
avg_grades NUMBER := 0;
BEGIN
SELECT AVG (Grade)
INTO avg_grades
FROM EnrolledInClasses
dbms_output.put_line('The average of grades is :'||avg_grades);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.put_line ('No Data Is Found..');
END;
/
Since you are using PL/SQL Developer, make sure you are writing your code in a Procedure window. Then the compilation error will be clearly highlighted. In this case it's the missing semicolon at the end of the select into statement.
Or in SQL*Plus you can use show errors:
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE AVG_GRADES:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
9/12 PL/SQL: ORA-00933: SQL command not properly ended
or the full syntax, show errors procedure avg_grades.
Or you can directly query dba|all|cdb|user_errors:
select * from user_errors e
where e.name = 'AVG_GRADES'
and e.attribute = 'ERROR';
Also, the table definition could be written more succinctly as:
create table enrolledinclasses
( st_id primary key constraint fk_stid references student(st_id)
, c_id constraint fk_coid references course (c_id)
, grade number(2) constraint checkgrade check (grade>-1)
);
student.st_id and course.c_id should probably be numbers or integers, or at least varchar2, and not char.
It is good practice to use code indentation to align code into blocks showing their dependency structure. That would give something like this:
create or replace procedure avg_grades as
avg_grades number := 0;
begin
select avg(grade) into avg_grades
from enrolledinclasses;
dbms_output.put_line('The average grade is: ' || avg_grades);
exception
when no_data_found then
dbms_output.put_line('No Data Is Found.');
end;
You might want to round() that average before displaying it, as by default it could be displayed with a lot more decimal places than you might want.
Also, it's not possible to get a no_data_found exception from a single-group aggregate query like this. If there are no rows in the table you will get a null value as the result, not an error.

Trigger is not firing using Reference

I am writing one trigger that will fire on update and insert on one table and insert into another table. Trigger looks like this.
create or replace trigger update_test_history
before insert or update on demo.test_req REFERENCING NEW AS NEW OLD AS old
for each row
declare
v_count number(1);
begin
if :old.quote_request_id != null then
--Update History table
insert into demo.test_req_history
(history_id,
req_id)
values
(isisdba.req_hist_seq.nextval,
:old.req_id);
end if;
end update_test_history;
This trigger is not working on DML. But if i remove the if condition then it start working.
Can someone please tell me whats wrong with this.
Thanks in advance.
null is never equal to any value including null. null is also never unequal to another value including null. So the statement
if( <<some expression>> != null )
then
<<do something>>
end if;
will ever get into the <<do something>> block. Neither will
if( <<some expression>> = null )
then
<<do something>>
end if;
You have to use ternary logic where you are dealing with null values bu saying is null or is not null. It appears that you probably want
if( :old.quote_request_id is not null )
the
<<do something>>
end if;

Oracle Bind Variable giving error

SET SERVEROUTPUT ON
VARIABLE dept_id NUMBER
DECLARE
max_deptno NUMBER(3);
dept_name departments.department_name%TYPE :='Education';
BEGIN
SELECT MAX(department_id)
INTO max_deptno
FROM departments;
DBMS_OUTPUT.PUT_LINE ('The maximum department no is : ' || max_deptno);
:dept_id:=(max_deptno+10);
INSERT INTO departments (department_name, department_id,location_id)
VALUES(dept_name, :dept_id, NULL);
DBMS_OUTPUT.PUT_LINE ('The number of rows affected : ' || SQL%ROWCOUNT);
END;
/
Error report:
ORA-01400: cannot insert NULL into ("SYSTEM"."DEPARTMENTS"."DEPARTMENT_ID")
ORA-06512: at line 10
01400. 00000 - "cannot insert NULL into (%s)"
*Cause:
*Action:
The maximum department no is : 190
I am getting this error while trying to execute the bind variable in oracle statment. But if i put some value instead of bind variable, i get this insert statement right. What am I doing wrong here?
I think the value of the bind variable is only set when the pl/sql block is finished. And it probably has to terminate normally.
One solution is to use max_deptno+10 in the insert insead of :dept_if. A better solution is to create another pl/sql variable and use that in the insert statement.
new_dept_id := max_deptno+10;
:dept_id := new_dept_id;
You also have to change the INSERT statement:
INSERT INTO departments (department_name,department_id,location_id)
VALUES(dept_name, new_dept_id, NULL);
I think this error is obtained because you use bind variable without using set autoprint on in start program.

Resources