pl/sql if statement failing on incorrect if condition - oracle

apologies if this is covered elsewhere, i'm not sure what to search for. I'm having trouble with a pl/sql block.
The issue is with the IF statement below. Even when the condition (r) is not met, the script bottoms out as it attempts to run the block anyway.
I have encountered similar issues before, and resolved by putting select statements into variables and then executing the variables, however i can't get that to work in this instance. Has anyone seen this before, and any idea on how to resolve?
sqlplus -s xxx << EOSQL
set serveroutput on feedback on termout on
DECLARE
r number;
BEGIN
SELECT count(*) INTO r from sys.dba_audit_mgmt_config_params WHERE AUDIT_TRAIL = 'UNIFIED AUDIT TRAIL';
IF (r > 0) THEN
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL is in use, setting timestamp and purging');
DBMS_AUDIT_MGMT.set_last_archive_timestamp(
audit_trail_type => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
last_archive_time => SYSTIMESTAMP-30);
DBMS_AUDIT_MGMT.clean_audit_trail(
audit_trail_type => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
use_last_arch_timestamp => TRUE);
ELSE
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL not in use, skipping');
END IF;
END;
/
EOSQL
Another example of this can be seen below, this time using a SELECT statement to highlight the issue, the table RANDOMTABLE does not exist purposfully to highlight the issue.
sqlplus -s xxx << EOSQL
set serveroutput on feedback on termout on
DECLARE
r number := 0;
s varchar(200);
BEGIN
IF (r > 0) THEN
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL is in use, setting timestamp and purging');
select count(*) into s from randomtable where object_name = 'BANANAS';
dbms_output.put_line(s);
ELSE
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL not in use, skipping');
END IF;
END;
/
EOSQL
Which fails with:
DATABASE:/apps/ora/home> ./test1.sh
select count(*) into s from randomtable where object_name = 'BANANAS';
*
ERROR at line 7:
ORA-06550: line 7, column 41:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 7, column 13:
PL/SQL: SQL Statement ignored
And finally, the below works as expected, returning the ELSE block output. This time the table definitely DOES exist.
sqlplus -s '/ as sysdba' << EOSQL
set serveroutput on feedback on termout on
DECLARE
r number := 0;
s varchar(200);
BEGIN
IF (r > 0) THEN
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL is in use, setting timestamp and purging');
select count(*) into s from dba_objects where object_name = 'BANANAS';
dbms_output.put_line(s);
ELSE
dbms_output.put_line('-- UNIFIED_AUDIT_TRAIL not in use, skipping');
END IF;
END;
/
EOSQL

Related

ORA-00922: missing or invalid option when trying to create column in table

I'm using the following code for create a column in an existing table, but, I'm getting this error:
ORA-00922: missing or invalid option
I've tried get the desired result (create column in table "only if this column does not exists") without the EXECUTE IMMEDIATE instruction, but, PL/SQL doesn't allow use the ALTER TABLE [...] in an IF [] THEN structure.
Is there something I'm missing?
This is the db<>fiddle sample:
CREATE TABLE "TMP_TABLE_SAMPLE"
( "ID_TABLE" NUMBER(9,0)
) ;
✓
SET SERVEROUTPUT ON;
CLEAR SCREEN;
DECLARE
V_COLUMN_EXISTS NUMBER := 0;
BEGIN
SELECT COUNT(1) CONTEO
INTO V_COLUMN_EXISTS
FROM USER_TAB_COLS
WHERE UPPER(COLUMN_NAME) = 'PNT_NCODE'
AND UPPER(TABLE_NAME) = 'TMP_TABLE_SAMPLE';
IF V_COLUMN_EXISTS = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE TMP_TABLE_SAMPLE ADD PNT_NCODE NUMBER (9,0) ' ||
' COMMENT ON COLUMN TMP_TABLE_SAMPLE.PNT_NCODE IS ''Stores ID from TMP_TABLE_SAMPLE_2.''';
ELSE
DBMS_OUTPUT.PUT_LINE('Column already exists');
END IF;
END;
ORA-00922: missing or invalid option
The error you're getting is because you have set serveroutput on and clear screen in your script. db<>fiddle knows how to interpret SQL and PL/SQL. It doesn't support SQL*Plus commands.
If you remove those, the next error you'll get is that you have a single execute immediate statement that is trying to execute two separate statements. Creating the column and adding a comment on the column are separate operations so you need separate statements.
If I change your fiddle to this, it works the way you want
DECLARE
V_COLUMN_EXISTS NUMBER := 0;
BEGIN
SELECT COUNT(1) CONTEO
INTO V_COLUMN_EXISTS
FROM USER_TAB_COLS
WHERE UPPER(COLUMN_NAME) = 'PNT_NCODE'
AND UPPER(TABLE_NAME) = 'TMP_TABLE_SAMPLE';
IF V_COLUMN_EXISTS = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE TMP_TABLE_SAMPLE ADD PNT_NCODE NUMBER (9,0) ';
EXECUTE IMMEDIATE 'COMMENT ON COLUMN TMP_TABLE_SAMPLE.PNT_NCODE IS ''Stores ID from TMP_TABLE_SAMPLE_2.''';
ELSE
DBMS_OUTPUT.PUT_LINE('Column already exists');
END IF;
END;
/

Trigger, at line 1 ORA 04098

Hi im setting up some triggers and i cant get past this error
SET SERVEROUTPUT ON
CREATE OR REPLACE TRIGGER trigger2
BEFORE INSERT ON new_donation
FOR EACH ROW
--WHEN (new.contamt <= 10)
DECLARE
v_idno VARCHAR2(5);
v_driveno VARCHAR2(3);
v_contdate DATE;
v_contamt NUMBER(6,2);
BEGIN
SELECT IDNO, DRIVENO, CONTDATE, CONTAMT INTO v_idno, v_driveno, v_contdate, v_contamt
FROM OLD_DONATION2
WHERE IDNO = :new.idno;
IF :new.contamt < 50 THEN
RAISE_APPLICATION_ERROR (-20001, 'CONTRIBUTION TOO LOW FOR '
|| :new.idno || ' ' || :new.contamt);
END IF;
END;
/
SET SERVEROUTPUT OFF
another part
DECLARE
v_idno new_donation.idno%TYPE := '&in_idno';
v_driveno new_donation.driveno%TYPE := '&in_driveno';
v_contdate new_donation.contdate%TYPE := '&in_contdate';
v_contamt new_donation.contamt%TYPE := &in_contamt;
BEGIN
INSERT INTO new_donation
VALUES (v_idno, v_driveno, v_contdate, v_contamt);
END;
/
Im getting this error when I insert values at trigger2.
ERROR at line 1: ORA-04098: trigger 'XXXXXXXXX.NEW_DONATION' is
invalid and failed re-validation ORA-06512: at line 7
All im trying to do is insert some values input by the user to this new table, which is empty.
Also when donation amount is < 10 I want a error out.
Follow these steps.
Run this query select status from all_objects where object_name = 'TBL_USER_TRIGGER' and object_type = 'TRIGGER';
If the status is invalid run alter trigger trigger2 compile;
Then run show errors
It will throw the errors for you. You need to fix those.

PL/SQL errors; ORA-00933: SQL command not properly ended

I'm new to pl/SQL. The error I'm getting is :
ORA-06550: line 3, column 2: PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 2, column 1: PL/SQL: SQL Statement ignored
So far I have tried adding parentheses, but no luck. I know its a syntax error, but I'm not sure where my mistake is. I tested each line , line by line and I believe there is a problem with my if statement. Does anyone know how I can fix the error for the following code?
SELECT CITRIX from SWLR_ASSET where swlr_key = :p26_swlr_id
if CITRIX=1 then --If Citrix installation is required
UPDATE SOFTWARE_REPORT_DASHBOARD
SET TOTAL_CITRIX_LICENSES=TOTAL_CITRIX_LICENSES+1
WHERE SOFTWARE_NAME=select (Software_name||' ,'||software_version) from swlr_software where software_id = (select software from swlrequest where swlr_id = :p26_swlr_id)
else --If manual installation is required
UPDATE SOFTWARE_REPORT_DASHBOARD
SET TOTAL_PHYSICAL_LICENSES=TOTAL_PHYSICAL_LICENSES+1
WHERE SOFTWARE_NAME= select (Software_name||' ,'||software_version) from swlr_software where software_id = (select software from swlrequest where swlr_id = :p26_swlr_id)
end if;
when you want to a write in plsql a block statement you have to include begin and end; and you have to decalre your variables
declare p26_swlr_id number(specify a number);
v_name varchar2(500);
v_name1 varchar2(500);
begin
p26_swlr_id :=33;( or if its from a table select swrl_id into p26_swlr_id from table where something);
-- this to give you the values for your conditions , its not tested though, and from your condition it should return one record.
select (Software_name||' ,'||software_version) into V_name from swlr_software where software_id = (select software from swlrequest where swlr_id = p26_swlr_id)
select (Software_name||' ,'||software_version into V_name1 from swlr_software where software_id = (select software from swlrequest where swlr_id = :p26_swlr_id)
-- you have to put `;` when you end select
SELECT CITRIX from SWLR_ASSET where swlr_key = p26_swlr_id;
if CITRIX=1 then --If Citrix installation is required
UPDATE SOFTWARE_REPORT_DASHBOARD
SET TOTAL_CITRIX_LICENSES=TOTAL_CITRIX_LICENSES+1
WHERE SOFTWARE_NAME=V_name;
else --If manual installation is required
UPDATE SOFTWARE_REPORT_DASHBOARD
SET TOTAL_PHYSICAL_LICENSES=TOTAL_PHYSICAL_LICENSES+1
WHERE SOFTWARE_NAME= V_name2;
end if;
end;
/
Try to declare a variable and then use the select into statement.
SELECT CITRIX into V_CITRIX from SWLR_ASSET where swlr_key...
Then where you compare, use the variable.
if V_CITRIX=1 then
...
And why aren't you using declare/begin/end? From where are you getting the parameters?

How to put a SELECT from Oracle into a bash variable?

I try to save the output of an Oracle SELECT command into a bash variable.
I tried the following lines but it didn't work really well...
ACCESS_SQL=`{
sqlplus << EOF
${USER}/${PASSWORD}#DB
set head off;
set feedback off;
set pagesize 5000;
set linesize 30000;
set serveroutput on;
DECLARE
data varchar(5000);
BEGIN
select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='${USER_ID}';
dbms_output.put_line(data);
END;
/
exit;
EOF
}`
The error statement I get is :
SQL> SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7 select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG into data from uam.access_list where USER_ID='PZ230';
*
ERROR at line 4:
ORA-06550: line 4, column 110:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 4, column 2:
PL/SQL: SQL Statement ignored
I was wondering if using a varchar is the right thing to do...
You don't need to select into a variable and then use dbms_output.put_line to print it out. (Your select into statement won't work anyway, because you can't select multiple columns into a single data variable.)
Instead, do it like this:
data=$(sqlplus -S ${USER}/${PASSWORD} << EOF
set head off
set feedback off
set pagesize 5000
set linesize 30000
select ACCESS_ID, PROFILE_ID, START_DATE, END_DATE, PLATFORM, ACCESS_TYPE, PERM_FLAG, ACTIVE_FLAG from uam.access_list where USER_ID='${USER_ID}';
exit
EOF)
echo "$data"

stored procedure always returns "Process exited" error always

I am working on a table structure similar to the below and trying to get the output as mentioned.
To acheive this, I written the below PL SQL procedure:
CREATE OR replace PROCEDURE Sample_procedure
AS
TYPE list_of_names_t
IS TABLE OF emp.emp_index%TYPE;
processedindexes LIST_OF_NAMES_T := List_of_names_t();
flag emp.emp_index%TYPE;
CURSOR c1 IS
SELECT *
FROM emp
WHERE Trim(emp_id) = 'AAAAA'
ORDER BY last_maint_ts ASC;
BEGIN
dbms_output.Put('Entered the loop');
FOR rec IN c1 LOOP
SELECT emp_index
INTO flag
FROM emp
WHERE emp_id = rec.emp_id
AND last_maint_ts > rec.last_maint_ts;
IF flag IS NOT NULL THEN
processedindexes.extend;
Processedindexes(processedindexes.last) := flag;
processedindexes.extend;
Processedindexes(processedindexes.last) := rec.emp_index;
dbms_output.Put('The indexes'
||rec.emp_index
||' & '
||flag
||'refer to same emp ID');
exit;
dbms_output.Put('received NULL');
END IF;
END LOOP;
END;
1) Everytime, I run this i get the output as Process exited in sql developer, Any suggestion on this?
Note: I am new to PL SQL programming, please correct me if my approach of solving this is not right.
open sqlplus and execute the following :-
set serveroutput on
exec Sample_procedure;
provided your procedure is compiling fine, it should display the output in dbms_output.put

Resources