Some Syntax Error - Can't figure out where - sql

Here is the script that i wrote and it has weird syntax error at EXCEPTION block. If i remove exception block the script compiles properly. but no sooner i write it back it gives me error
Error(58,11): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following: ( begin case declare else elsif end exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Here is the script
LOOP
BEGIN
SAVEPOINT check_point;
EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
DBMS_SQL.COLUMN_VALUE (cursor_handle, 1, cc , col_err, actual_len);
DBMS_SQL.COLUMN_VALUE (cursor_handle, 2, di, col_err, actual_len);
IF INSTR (cc, '_') <> 0 THEN
cc := Trim (cc);
cc := Upper(cc);
cc := substr(cc,4,2);
EXECUTE IMMEDIATE 'UPDATE ' || dest || ' SET cc = :v1 WHERE di = :v2'
USING cc, di;
if SQL%ROWCOUNT > 0 THEN
inserts := inserts + 1;
counter := counter + 1;
IF counter > 500 THEN
counter := 0;
COMMIT;
END IF;
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dups := dups+1;
ROLLBACK TO check_point;
WHEN VALUE_ERROR THEN
valerr := valerr +1;
ROLLBACK TO check_point;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
otherexc := otherexc +1;
IF otherexc > 50 THEN
EXIT;
END IF;
ROLLBACK TO check_point;
END IF;
END;
END LOOP;
I know its very annoying to ask such kind a question but i am unable to figure out what error is that. I am lehman at Pl/SQL.

The error appears to be that your EXCEPTION clause is inside the IF INSTR (cc, '_') <> 0 IF statements but you appear to want to match the EXCEPTION to the BEGIN statement at the top of your loop. I believe that you want to move the END IF; for the IF INSTR (cc, '_') <> 0 before the EXCEPTION as I do here
LOOP
BEGIN
SAVEPOINT check_point;
EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
DBMS_SQL.COLUMN_VALUE (cursor_handle, 1, cc , col_err, actual_len);
DBMS_SQL.COLUMN_VALUE (cursor_handle, 2, di, col_err, actual_len);
IF INSTR (cc, '_') <> 0 THEN
cc := Trim (cc);
cc := Upper(cc);
cc := substr(cc,4,2);
EXECUTE IMMEDIATE 'UPDATE ' || dest || ' SET cc = :v1 WHERE di = :v2'
USING cc, di;
if SQL%ROWCOUNT > 0 THEN
inserts := inserts + 1;
counter := counter + 1;
IF counter > 500 THEN
counter := 0;
COMMIT;
END IF; -- IF counter > 500
END IF; -- IF SQL%ROWCOUNT > 0
END IF; -- INSTR (cc, '_') <> 0
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dups := dups+1;
ROLLBACK TO check_point;
WHEN VALUE_ERROR THEN
valerr := valerr +1;
ROLLBACK TO check_point;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
otherexc := otherexc +1;
IF otherexc > 50 THEN
EXIT;
END IF;
ROLLBACK TO check_point;
END;
END LOOP;
That being said, however, I would probably rewrite the code a bit. Committing every 500 rows is almost certainly an error. I'm very dubious of your WHEN OTHERS exception handler-- I would really think that you'd want to at least write the error to a table or populate a collection of errors rather than writing to the DBMS_OUTPUT buffer that may or may not ever be displayed.
LOOP
SAVEPOINT check_point;
EXIT WHEN DBMS_SQL.FETCH_ROWS (cursor_handle) = 0;
DBMS_SQL.COLUMN_VALUE (cursor_handle, 1, cc , col_err, actual_len);
DBMS_SQL.COLUMN_VALUE (cursor_handle, 2, di, col_err, actual_len);
IF INSTR (cc, '_') <> 0 THEN
cc := Trim (cc);
cc := Upper(cc);
cc := substr(cc,4,2);
BEGIN
EXECUTE IMMEDIATE 'UPDATE ' || dest || ' SET cc = :v1 WHERE di = :v2'
USING cc, di;
if SQL%ROWCOUNT > 0 THEN
inserts := inserts + 1;
END IF;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dups := dups+1;
ROLLBACK TO check_point;
WHEN VALUE_ERROR THEN
valerr := valerr +1;
ROLLBACK TO check_point;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('errno: ' || TO_CHAR(SQLCODE) || ' Msg: ' || SQLERRM);
otherexc := otherexc +1;
IF otherexc > 50 THEN
EXIT;
END IF;
ROLLBACK TO check_point;
END;
END IF; -- INSTR (cc, '_') <> 0
END LOOP;

Related

Make the declared variable inside a trigger append its value if more than one condition is met

Well I was writing a trigger and I wanted that the declared variable v_message to append every time the condition is met. So for example if the first two statements are met the v_message should be something like "Invalid account Invalid amount". I must also find a way to separate these two messages by a space if the v_message already contains some sort of an error message as it wont be readable if you had something like this "Invalid accountInvalid amount".
CREATE OR REPLACE TRIGGER checkPayments
BEFORE INSERT ON tbl_payments
FOR EACH ROW
DECLARE
v_message VARCHAR(100 CHAR);
v_is_valid CHAR(1) := 'N';
BEGIN
IF getActiveUser(getOrderUsername(:new.order_id)) = 0
THEN v_message := 'Invalid account';
ELSIF isValidAmount(:new.payment_amount) = 0
THEN v_message := 'Invalid amount';
ELSIF checkOrderExist(:new.order_id) = 0
THEN v_message := 'Invalid order ID';
ELSIF (getValidOrderPayments(:new.order_id) + :new.payment_amount ) > getOrderTotal(:new.order_id)
THEN v_message := 'Payment exceeds total';
ELSE v_message := 'OK' ;
v_is_valid := 'Y';
END IF;
:new.payment_id:= seq_payment_id.nextval;
:new.payment_date:= LOCALTIMESTAMP;
:new.payment_message := v_message;
:new.is_valid := v_is_valid;
END;
/
CREATE OR REPLACE TRIGGER checkPayments
BEFORE INSERT ON tbl_payments
FOR EACH ROW
DECLARE
v_message VARCHAR(100 CHAR);
v_is_valid CHAR(1) := 'N';
BEGIN
v_is_valid := 'Y';
IF getActiveUser(getOrderUsername(:new.order_id)) = 0
THEN v_message := 'Invalid account';
v_is_valid := 'N';
END IF;
IF isValidAmount(:new.payment_amount) = 0
THEN v_message := v_message || ' Invalid amount';
v_is_valid := 'N';
END IF;
IF checkOrderExist(:new.order_id) = 0
THEN v_message := v_message || ' Invalid order ID';
v_is_valid := 'N';
END IF;
IF (getValidOrderPayments(:new.order_id) + :new.payment_amount ) > getOrderTotal(:new.order_id)
THEN v_message := v_message || ' Payment exceeds total';
v_is_valid := 'N';
END IF;
IF v_is_valid = 'Y'
THEN v_message := 'OK' ;
END IF;
:new.payment_id:= seq_payment_id.nextval;
:new.payment_date:= LOCALTIMESTAMP;
:new.payment_message := trim(v_message);
:new.is_valid := v_is_valid;
END;
/

Error "PL/SQL: numeric or value error: character string buffer too small" when using optional parameter in stored procedure

I am using the following stored procedure for returning an incremented key value for a specific keyword. It takes an optional parameter of Y or N to determine if the result should be prefixed by another value.
create or replace
PROCEDURE "ASGETPRIMARYKEY"
(
p_ObjectName IN LASTPRIMARYKEY.OBJECTNAME%type ,
p_PrimaryKey OUT LASTPRIMARYKEY.NEXTKEY%type,
useSitePrefix IN NVARCHAR2 Default 'Y'
)
AS
v_transcount NUMBER := 1;
v_sys_error NUMBER := 0;
v_SitePrefix NVARCHAR2(4000);
v_LastPrimaryKey NUMBER(10,0);
-- Error Handling Variables
v_LocalTran NUMBER(1,0);
v_Error NUMBER(10,0);
BEGIN
NULL/*TODO:SET XACT_ABORT ON*/;
NULL/*TODO:SET NOCOUNT ON*/;
-- STEP 0: Start the transaction
-- If procedure doesn't need to begin a transaction this can be set to 0
IF v_transcount > 0 THEN
v_LocalTran := 0;
ELSE
v_LocalTran := 1;
END IF;
IF v_LocalTran = 1 THEN
SET TRANSACTION READ WRITE;
v_transcount := v_transcount + 1;
END IF;
-- STEP 1: Get #SitePrefix
SELECT settingtext
INTO v_SitePrefix
FROM systemsetting
WHERE settingdescription = 'Site Id Prefix';
-- Check if there were any errors
v_Error := v_sys_error;
IF v_Error != 0 THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(1a)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
-- STEP 1b: Check SitePrefix exists
-- Rollback the transaction if SitePrefix doesn't exist in systemsetting
IF v_SitePrefix IS NULL THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(1b)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
BEGIN
-- STEP 2: Set NextKey for requested ObjectName
UPDATE LastPrimaryKey
SET NextKey = (NextKey + 1)
WHERE objectname = p_ObjectName;
EXCEPTION
WHEN OTHERS THEN
v_sys_error := SQLCODE;
END;
BEGIN
-- Check if there were any errors
v_Error := v_sys_error;
EXCEPTION
WHEN OTHERS THEN
v_sys_error := SQLCODE;
END;
IF v_Error != 0 THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(2)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
-- STEP 3: Set NextKey for for requested ObjectName
SELECT NextKey - 1
INTO v_LastPrimaryKey
FROM LastPrimaryKey
WHERE objectname = p_ObjectName;
BEGIN
-- Check if there were any errors
v_Error := v_sys_error;
EXCEPTION
WHEN OTHERS THEN
v_sys_error := SQLCODE;
END;
IF v_Error != 0 THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(3)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
-- STEP 4: Check SitePrefix exists
-- Rollback the transaction if SitePrefix doesn't exist in systemsetting
IF v_LastPrimaryKey IS NULL THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(4)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
-- STEP 5: Set #p_PrimaryKey by adding prefix
IF useSitePrefix = 'y' THEN
p_PrimaryKey := (CAST(v_SitePrefix || CAST(v_LastPrimaryKey AS NVARCHAR2) AS NUMBER));
ELSE
p_PrimaryKey := v_lastprimarykey;
END IF;
-- Check if there were any errors
v_Error := v_sys_error;
IF v_Error != 0 THEN
BEGIN
raise_application_error( -20002, 'Error whilst processing GetPrimaryKey.(5)' || cast(v_error as NVARCHAR2));
GOTO ErrExit;
END;
END IF;
-- STEP 6: If we reach this point, the commands completed successfully
-- Commit the transaction....
-- Normal exit
IF v_LocalTran = 1 THEN
COMMIT;
v_transcount := v_transcount - 1;
END IF;
-- Error Exit
<<ErrExit>>
IF v_LocalTran = 1 THEN
ROLLBACK;
v_transcount := v_transcount - 1;
END IF;
END;
The stored proc is called from a enterpriselibrary using the following vb code:
Dim sqlCommand As String = "asGetPrimaryKey"
Dim _db As Database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)(ASDBMSS.clsDBMSS.ConnectionStringName)
Dim DbCommand As System.Data.Common.DbCommand = _db.GetStoredProcCommand(sqlCommand)
' Add primary keys to command wrapper.
_db.AddInParameter(DbCommand, "p_objectName", DbType.String, ObjectName)
If pbIgnoreSiteIndex Then
_db.AddInParameter(DbCommand, "useSitePrefix", DbType.String, "Y")
Else
_db.AddInParameter(DbCommand, "useSitePrefix", DbType.String, "N")
End If
_db.AddOutParameter(DbCommand, "p_PrimaryKey", DbType.Int32, 8)
_db.ExecuteNonQuery(DbCommand)
Dim _result As String = _db.GetParameterValue(DbCommand, "p_PrimaryKey").ToString
lcl_NextKey = CInt(_result)
result = 1
The problem arrises when I pass the optional parameter in.
If I skip the line with the optional parameter, it works fine.
If I use SQL server instead of Oracle, it works fine.
If I pass the parameter and use Oracle I get the following error message:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 1
Any suggestions?
Change v_LastPrimaryKey from number(10,0) to same as p_PrimaryKey OUT LASTPRIMARYKEY.NEXTKEY%type,
Secondly, you are passing Y or N but inside code comparing y. So flow is always going in "else", where you are not using cast.
Hope that helps.
Cheers
V
I have found the problem!
SQL server does not care in what order the parameters are passed as long as the names match. Oracle it seems, does.
I changed the order in which they are called to this:
_db.AddInParameter(DbCommand, "p_objectName", DbType.String, ObjectName)
_db.AddOutParameter(DbCommand, "p_PrimaryKey", DbType.Int32, 8)
If pbIgnoreSiteIndex Then
_db.AddInParameter(DbCommand, "useSitePrefix", DbType.String, "Y")
Else
_db.AddInParameter(DbCommand, "useSitePrefix", DbType.String, "N")
End If
_db.ExecuteNonQuery(DbCommand)
And it now works without error

PL/SQL CRUD matrix from a source code

I am trying to create a CRUD matrix for my function from a source code of that function, so I created a procedure that will read a source code.
create or replace procedure test_
IS
CURSOR c_text is
SELECT USER_SOURCE.TEXT
FROM USER_SOURCE
WHERE USER_SOURCE.name='TEST_FUNCTION'
AND USER_SOURCE.type='FUNCTION';
order by line;
v_single_text varchar2(4000);
v_tmp_text varchar2(10000) := ' ';
begin
open c_text;
loop
fetch c_text into v_single_text;
exit when c_text%notfound;
v_tmp_text := v_tmp_text|| chr(10) || rtrim(v_single_text);
dbms_output.put_line(v_single_text);
end loop;
close c_text;
end test_;
And that works very good for me, I get the source code of my desired function. It's a very simple function and I use this to learn PL/SQL. Output of that procedure look's like this.
function test_funkction Return varchar2
IS
kpp_value varchar2(20);
begin
select KPP
into kpp_value
from CUSTOMER
where CUSTOMER_ID = 200713;
dbms_output.put_line (kpp_value);
Return kpp_value;
end test_function;
Now, how to parse the string I've got in the output to get a desired result, my result should be like this
==TABLE_NAME==========OPERATIONS==
CUSTOMER - R - -
==================================
Now I have managed to do it.
But it will only work with my simple function, now I want to make a procedure that will work with any function.
Source code below.
create or replace procedure test_
IS
v_string_fnc varchar2(10000) := UPPER('function test_function
Return varchar2
IS
kpp_value varchar2(20);
begin
select KPP into kpp_value from CUSTOMER where CUSTOMER_ID = 200713;
dbms_output.put_line (kpp_value);
Return kpp_value;
end test_function;');
v_check PLS_INTEGER;
CURSOR c_text is
SELECT USER_SOURCE.TEXT
FROM USER_SOURCE
WHERE USER_SOURCE.name = 'TEST_FUNCTION'
AND USER_SOURCE.type = 'FUNCTION'
order by line;
v_single_text varchar2(4000);
v_tmp_text varchar2(10000) := ' ';
/*v_string varchar2(10000);*/
insert_flag char := '-';
read_flag char := '-';
update_flag char := '-';
delete_flag char := '-';
empty_space char(34) := ' ';
underline char(42) := '==========================================';
/*v_txt varchar2(10000) := ' ';*/
result_table varchar2(1000) := '/';
begin
open c_text;
loop
fetch c_text
into v_single_text;
exit when c_text%notfound;
v_tmp_text := v_tmp_text || chr(10) || rtrim(v_single_text);
/* print source code*/
/*dbms_output.put_line(v_single_text);*/
end loop;
close c_text;
/*DELETE SEARCH*/
v_check := instr(v_string_fnc, 'DELETE ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO DELETE COMMAND');
else
dbms_output.put_line('THERE IS A DELETE COMMAND');
delete_flag := 'D';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY DELETE: ' || result_table);
end if;
/*SELECT SEARCH*/
v_check := instr(v_string_fnc, 'SELECT ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO READ COMMAND');
else
dbms_output.put_line('THERE IS A READ COMMAND');
read_flag := 'R';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY READ: ' || result_table);
end if;
/*UPDATE SEARCH*/
v_check := instr(v_string_fnc, 'UPDATE ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO UPDATE COMMAND');
else
dbms_output.put_line('THERE IS A UPDATE COMMAND');
update_flag := 'U';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY UPDATE: ' || result_table);
end if;
/*INSERT SEARCH*/
v_check := instr(v_string_fnc, 'INSERT ');
if v_check < 1 then
dbms_output.put_line('THERE IS NO CREATE COMMAND');
else
dbms_output.put_line('THERE IS A CREATE COMMAND');
insert_flag := 'C';
v_check := instr(v_string_fnc, 'FROM ');
v_check := v_check + 5;
result_table := substr(v_string_fnc, v_check);
result_table := substr(result_table, 0, instr(result_table, ' '));
dbms_output.put_line('TABLE AFFECTED BY CREATE: ' || result_table);
end if;
dbms_output.put_line(' ');
dbms_output.put_line('==========' || 'TABLE_NAME' || '==========' ||
'OPERATIONS' || '==');
dbms_output.put_line(empty_space || insert_flag || read_flag ||
update_flag || delete_flag);
dbms_output.put_line(underline);
end test_;
With that procedure I can extract and output my code, dbms needs a bit clean up but it will give the result I need.
Now a few questions, how to put a source code of my function to a variable that is not predefined, here is v_string_fnc but it needs to be predefined to work.
And how to link a certain operation with the table, here in my example is easy, one SELECT and keyword FROM that gives me a name of table.
Struggling continues
The bigger part it's done, just a tuning after this.
v_check := instr2(v_string_fnc, 'DROP ');
if v_check > 0 then
delete_flag := 'D';
v_check := instr2(v_string_fnc, 'TABLE ', v_check);
v_check := v_check + 6;
result_table := substr(v_string_fnc, v_check);
rest_string := result_table;
result_table := substr(result_table, 0, instr(result_table, ' '));
result_table := rtrim(result_table);
result_table := rtrim(result_table, ';');
merge into result_set
using dual
on (tables_used = result_table)
when matched then
update set drop_operation = delete_flag
when not matched then
insert
(tables_used, drop_operation)
values
(result_table, delete_flag);
while v_check > 0 loop
v_check := instr2(rest_string, 'DROP ');
if v_check > 0 then
delete_flag := 'D';
v_check := instr2(rest_string, 'TABLE ', v_check);
v_check := v_check + 6;
result_table := substr(rest_string, v_check);
rest_string := result_table;
result_table := substr(result_table, 0, instr(result_table, ' '));
result_table := rtrim(result_table);
result_table := rtrim(result_table, ';');
merge into result_set
using dual
on (tables_used = result_table)
when matched then
update set drop_operation = delete_flag
when not matched then
insert
(tables_used, drop_operation)
values
(result_table, delete_flag);
end if;
end loop;
end if;

Skip invalid cursor and continue loop

I have the below procedure were the loop is breaking if the cursor does not have any record in it. I would like to contine the iteration even if the cursor does not have any record.
My procedure is,
CREATE OR REPLACE PROCEDURE "MAPSADMIN"."FORECAST_MAINTENANCE_SCH" (
in_carrierCode VARCHAR2,
in_WindowPeriodStr VARCHAR2,
out_forecastRecords OUT types.cursortype
)
IS
vtailAndCheckCur types.cursortype;
vForecastRecsCursor types.cursortype;
vtailNo VARCHAR2(10);
vmaintCheckType VARCHAR2(10);
vforcastRecords forecastObjectsList := forecastObjectsList();
forcastRec forecastObjectsList := forecastObjectsList();
BEGIN
dbms_output.enable(null);
OPEN vtailAndCheckCur FOR
select td.tail_number,mpm.maint_check_type
from tail_details td, maint_plan_tail_map mptm, maint_plan_master mpm
where td.tail_number = mptm.tail_number
and mpm.maint_plan_code = mptm.maint_plan_code
and mpm.carrier_code = in_carrierCode
UNION
select td.tail_number,mpm.maint_check_type
from tail_details td, maint_plan_subfleet_map mptm, maint_plan_master mpm
where td.subfleet_code = mptm.subfleet_code
and mpm.maint_plan_code = mptm.maint_plan_code
and mpm.carrier_code = in_carrierCode;
LOOP
FETCH vtailAndCheckCur INTO vtailNo, vmaintCheckType;
dbms_output.put_line( vtailNo||' '||vmaintCheckType );
FORECAST_OBJS_LIST(vtailNo,vmaintCheckType,in_WindowPeriodStr,vForecastRecsCursor);
LOOP
forcastRec.EXTEND;
forcastRec(forcastRec.COUNT) := FORECASTOBJECT(null,null,null,null,null,null,null,null,null,null);
dbms_output.put_line( 'test');
FETCH vForecastRecsCursor INTO
forcastRec(forcastRec.COUNT).maintNextPlanCode,
forcastRec(forcastRec.COUNT).tailNumber,
forcastRec(forcastRec.COUNT).maintNextCheckType,
forcastRec(forcastRec.COUNT).maintNextCycleCheckLabel,
forcastRec(forcastRec.COUNT).maintNextStartDate,
forcastRec(forcastRec.COUNT).maintNextEndDate,
forcastRec(forcastRec.COUNT).maintNextDueDate,
forcastRec(forcastRec.COUNT).maintNextCalendarDays,
forcastRec(forcastRec.COUNT).maintForecastFactor,
forcastRec(forcastRec.COUNT).maintCheckColor;
dbms_output.put_line( forcastRec(forcastRec.COUNT).maintNextPlanCode);
EXIT WHEN vForecastRecsCursor%NOTFOUND;
END LOOP;
EXIT WHEN vtailAndCheckCur%NOTFOUND;
END LOOP;
CLOSE vtailAndCheckCur;
OPEN out_forecastRecords FOR
SELECT tailNumber,maintNextCheckType,maintNextPlanCode,maintNextCycleCheckLabel,maintNextStartDate,
maintNextEndDate,maintNextDueDate,maintNextCalendarDays,maintForecastFactor,maintCheckColor
FROM TABLE(CAST(forcastRec AS forecastObjectsList));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(DBMS_UTILITY.FORMAT_ERROR_STACK);
END FORECAST_MAINTENANCE_SCH;
/
Here if vForecastRecsCursor returns without 0 records in it i am getting ORA-01001: invalid cursor. Anyone please explain how to iterate further by ignoring the error.
Thanks in advance
Try to check if vForecastRecsCursor is opened:
IF vForecastRecsCursor%ISOPEN THEN
...
END IF;
You can control the loop by the GOTO clause or using blocks of code.
DECLARE
dummy_cur SYS_REFCURSOR;
l_dummy_num NUMBER;
CURSOR dual_cur IS
SELECT 1
FROM dual
WHERE 1 = 2;
l_dual_num NUMBER;
l_counter NUMBER := 0;
BEGIN
OPEN dummy_cur FOR SELECT 1 FROM dual;
LOOP
FETCH dummy_cur INTO l_dummy_num;
IF dummy_cur%NOTFOUND THEN
GOTO exit_command;
END IF;
-- inner cursor
BEGIN
-- open inner cursor
IF l_counter = 0 THEN
OPEN dual_cur;
END IF;
-- loop inner cursor
LOOP
FETCH dual_cur INTO l_dual_num;
DBMS_OUTPUT.PUT_LINE('inner loop');
EXIT WHEN dual_cur%NOTFOUND;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('exception');
END;
<<exit_command>>
EXIT WHEN dummy_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('l_dummy_num: ' || l_dummy_num);
l_counter := l_counter + 1;
DBMS_OUTPUT.PUT_LINE('l_counter: ' || l_counter);
END LOOP;
--close inner cursor
CLOSE dual_cur;
CLOSE dummy_cur;
EXCEPTION
WHEN OTHERS THEN
CLOSE dual_cur;
CLOSE dummy_cur;
END;

Selection of columns in stored procedure based on parameters

I need to check the parameters in the stored procedure if it is entered then I need to select that
i=j=k=l=m=1;
IF (p_plant_cd IS NULL) THEN
i=0;
END IF;
IF(p_global_duns_nbr IS NULL) THEN
j=0
END IF;
IF(p_global_duns_nbr IS NULL) THEN
k=0
END IF;
IF(p_matrl_grp IS NULL) THEN
l=0
END IF;
IF (p_mrp IS NULL) THEN
m=0
END IF ;
Which ever value is 1 I need to add corresponding parameters in the variable v_select
For eg ;
if k and l are 1 then
v_select='p_global_duns_nbr,p_matrl_grp'
Pls suggest me how to do this.
You can declare a variable , holding column names , then concat to select statement and you have SQL select statement in v_select , use it as you want
declare
v_columns varchar2(255);
v_select varchar2(2000);
v_result sys_refcursor;
begin
v_columns := 'rownum';
IF p_plant_cd = 1 THEN
v_columns := v_columns || ',p_plant_cd';
END IF;
IF p_global_duns_nbr = 1 THEN
v_columns := v_columns || ',p_global_duns_nbr';
END IF;
IF p_global_duns_nbr = 1 THEN
v_columns := v_columns || ',p_global_duns_nbr';
END IF;
IF p_matrl_grp = 1 THEN
v_columns := v_columns || ',p_matrl_grp';
END IF;
IF p_mrp = 1 THEN
v_columns := v_columns || ',p_mrp';
END IF;
v_select := 'SELECT ' || v_columns || ' FROM table';
open v_result for v_select;
end;