IF else condition in sql stored procedure - sql

I'm trying to compile the stored procedure:
create
procedure checkFroud2(code IN varchar2, p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_recordset FOR
if code='C' then
select * from emp
//dbms_output.putline('this is if block')
else if code='F' then
dbms_output.putline('this is else block')
else
dbms_output.putline('last else')
end if;
end checkFroud2;
but this is showing compile time errors. Can anybody suggest where the problem is?

CREATE
PROCEDURE checkFroud2(
code IN VARCHAR2,
p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
IF code='C' THEN
dbms_output.put_line('this is if block');
OPEN p_recordset FOR
SELECT * FROM emp;
ELSIF code='F' THEN
--you can open p_recordset with dummy as
/*open p_recordset for select * from dual where 1 = 0; */
dbms_output.put_line('this is else block');
ELSE
/*open p_recordset for select * from dual where 1 = 0; */
dbms_output.put_line('last else');
END IF;
END checkFroud2;
/
var o refcursor;
BEGIN
CHECKfroud2
('C',:o);
END;
/
PRINT O;

The correct code is as follows:
create procedure checkFroud2(code IN varchar2, p_recordset OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_recordset FOR
if code='C' then
select * from emp
//dbms_output.putline('this is if block');
elsif code='F' then
dbms_output.putline('this is else block');
else
dbms_output.putline('last else');
end if;
end checkFroud2;

ELSE IF doesn't exist use ELSIF, also remove // before dbms_output.putline('this is if block').

This is an example in oracle 11g
CREATE OR REPLACE PROCEDURE PROC_EMP_CHECK
(
EMPNO1 IN NUMBER
, EMPNO2 IN NUMBER
)
AS
EMPONE_NOT_EXISTS EXCEPTION;
EMPTWO_NOT_EXISTS EXCEPTION;
BOTHEMP_NOT_EXISTS EXCEPTION;
EMPCOUNT1 NUMBER;
EMPCOUNT2 NUMBER;
BEGIN
SELECT COUNT(1) INTO EMPCOUNT1 FROM EMPLOYEES WHERE EMPLOYEE_ID=EMPNO1;
SELECT COUNT(1) INTO EMPCOUNT2 FROM EMPLOYEES WHERE EMPLOYEE_ID=EMPNO2;
BEGIN
IF( EMPCOUNT1=0 AND EMPCOUNT2=0)THEN
RAISE BOTHEMP_NOT_EXISTS;
ELSIF ( EMPCOUNT1=0) THEN
RAISE EMPONE_NOT_EXISTS;
ELSIF ( EMPCOUNT2=0) THEN
RAISE BOTHEMP_NOT_EXISTS;
dbms_output.put_line('ELSE BLOCK');
END IF;
END;
EXCEPTION
WHEN EMPONE_NOT_EXISTS THEN
dbms_output.put_line('EMP One not exit');
WHEN EMPTWO_NOT_EXISTS THEN
dbms_output.put_line('EMP two not exit');
WHEN BOTHEMP_NOT_EXISTS THEN
dbms_output.put_line('both not exit');
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END PROC_EMP_CHECK;

Use ELSIF instead of ELSE IF. By using ELSE IF you're opening up a new nested IF-block, which you're not closing.

Couple of errors:
1. Open record set for ??? For what??
2. ELSIF and no ELSE IF
3. where is ;
CREATE PROCEDURE CHECKFROUD2 ( CODE IN VARCHAR2,
P_RECORDSET OUT SYS_REFCURSOR )
AS
BEGIN
OPEN P_RECORDSET FOR SELECT * FROM DUAL;
IF CODE = 'C'
THEN
SELECT * FROM EMP;
ELSIF CODE = 'F'
THEN
DBMS_OUTPUT.PUTLINE ( 'this is else block' );
ELSE
DBMS_OUTPUT.PUTLINE ( 'last else' );
END IF;
END CHECKFROUD2;

Related

Executing stored procedure based on table column value

I am trying to return result from table based on value returned from another table column.
I am getting error when no data is found in user_config table. I want else part to execute instead.
CREATE OR REPLACE PROCEDURE valuefinders (
in_soeid IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
group1 VARCHAR2(128 BYTE) := 'n';
BEGIN
SELECT
admin
INTO group1
FROM
user_config
WHERE
soeid = in_soeid;
IF ( group1 = 'y' ) THEN
OPEN out_cursor FOR SELECT
*
FROM
user_profile;
ELSE
OPEN out_cursor FOR SELECT
*
FROM
user_request;
END IF;
END valuefinders;```
You can also nest exceptions in your code itself and take advantage of them.
This example shows that when the query on user_config raises a NO_DATA_FOUND, the variable group1 is set to 'n' and the else block will be executed:
CREATE OR REPLACE PROCEDURE valuefinders (
in_soeid IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
group1 VARCHAR2(128 BYTE) := 'n';
BEGIN
BEGIN
SELECT
admin
INTO group1
FROM
user_config
WHERE
soeid = in_soeid;
EXCEPTION WHEN NO_DATA_FOUND THEN
group1 := 'n';
END;
IF ( group1 = 'y' ) THEN
OPEN out_cursor FOR SELECT
*
FROM
user_profile;
ELSE
OPEN out_cursor FOR SELECT
*
FROM
user_request;
END IF;
END valuefinders;
Handle the exception, somehow. The simplest option is to do nothing:
CREATE OR REPLACE PROCEDURE valuefinders (in_soeid IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR)
AS
group1 VARCHAR2 (128 BYTE) := 'n';
BEGIN
SELECT admin
INTO group1
FROM user_config
WHERE soeid = in_soeid;
IF (group1 = 'y')
THEN
OPEN out_cursor FOR SELECT * FROM user_profile;
ELSE
OPEN out_cursor FOR SELECT * FROM user_request;
END IF;
EXCEPTION --> here
WHEN NO_DATA_FOUND
THEN
NULL;
END valuefinders;
If you want to do something else, then just do it:
EXCEPTION
WHEN NO_DATA_FOUND
THEN
OPEN out_cursor FOR SELECT * FROM user_request;

PLS-00103 Encountered the symbol "END"

I'm running into the issue PL-00103: Encountered the symbol "END" on the second to last line of the following procedure. The purpose of the procedure is to see if a previous version of an email exists when a new one is sent... and expire the old email. (emails are by default set to a far away expiration date when created so I check to see if the expiration date is further than the current date).
PROCEDURE EXPIRE_STUFF_PRC
(
PI_EMAIL_NBR_STR IN VARCHAR2,
PO_SUCCESS_FLG OUT VARCHAR2,
PO_OUT_MSG OUT VARCHAR2
) AS
L_SUCCESS VARCHAR2(1) := 'N';
L_EMAIL_ID NUMBER;
L_PREV_EMAIL_VER_ID NUMBER := 0;
L_PREV_EMAIL_EXP_DT DATE;
BEGIN
BEGIN
SELECT
ITEM.EMAIL_ID
INTO L_EMAIL_ID
FROM HR_EMAIL ITEM
WHERE ITEM.EMAIL_NBR_STR = PI_EMAIL_NBR_STR;
END;
BEGIN
SELECT
VER_ID.EMAIL_VER_ID
INTO L_PREV_EMAIL_VER_ID
FROM (
SELECT
EMAIL_VER_ID
FROM HR_EMAIL_VER
WHERE EMAIL_ID = L_EMAIL_ID
ORDER BY EMAIL_VER_ID DESC
) VER_ID
WHERE ROWNUM = 2;
EXCEPTION
WHEN NO_DATA_FOUND
THEN PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'LESS THAN 2 VERSIONS';
END;
END;
BEGIN
IF (L_PREV_EMAIL_VER_ID > 0) THEN
SELECT
VER.EXP_DT
INTO L_PREV_EMAIL_EXP_DT
FROM HR_EMAIL_VER VER
WHERE VER.EMAIL_VER_ID = L_PREV_EMAIL_VER_ID;
IF (L_PREV_EMAIL_EXP_DT > SYSDATE) THEN
UPDATE HR_EMAIL_VER
SET EXP_DT = SYSDATE
WHERE EMAIL_VER_ID = L_PREV_EMAIL_VER_ID;
END IF;
PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'SUCCESS';
END IF;
END;
END;
END EXPIRE_STUFF_PRC;
I've tried to move around ENDs and BEGINs to no avail...
I've changed the name of stuff due to NDA so if there is any confusion I'll be happy to clarify as I might have missed something.
Thank you in advance for any help!
You have used unnecessary BEGIN/END in your code. There could be a necessity if you want to handle exceptions in a particular section. The best way to format and check for syntax is to use a nice code editor.
I have removed all the unwanted BEGIN/END and this should compile fine:
CREATE OR REPLACE PROCEDURE expire_stuff_prc (
pi_email_nbr_str IN VARCHAR2,
po_success_flg OUT VARCHAR2,
po_out_msg OUT VARCHAR2
) AS
l_success VARCHAR2(1) := 'N';
l_email_id NUMBER;
l_prev_email_ver_id NUMBER := 0;
l_prev_email_exp_dt DATE;
BEGIN
SELECT
item.email_id
INTO l_email_id
FROM
hr_email item
WHERE
item.email_nbr_str = pi_email_nbr_str;
BEGIN
SELECT
ver_id.email_ver_id
INTO l_prev_email_ver_id
FROM
(
SELECT
email_ver_id
FROM
hr_email_ver
WHERE
email_id = l_email_id
ORDER BY
email_ver_id DESC
) ver_id
WHERE
ROWNUM = 2;
EXCEPTION
WHEN no_data_found THEN
po_success_flg := 'Y';
po_out_msg := 'LESS THAN 2 VERSIONS';
END;
IF ( l_prev_email_ver_id > 0 ) THEN
SELECT
ver.exp_dt
INTO l_prev_email_exp_dt
FROM
hr_email_ver ver
WHERE
ver.email_ver_id = l_prev_email_ver_id;
IF ( l_prev_email_exp_dt > sysdate ) THEN
UPDATE hr_email_ver
SET
exp_dt = sysdate
WHERE
email_ver_id = l_prev_email_ver_id;
END IF;
po_success_flg := 'Y';
po_out_msg := 'SUCCESS';
END IF;
END expire_stuff_prc;
/
Ideal way to write a stored procedure is to handle all exception for each of the block/statement. You have one additional END that has to be removed, additionally i have added messages to be returned when first and last block goes into any exception.
Block is in sense logically grouped DML operation, it can be just a single select or group of DML operation.Decision to be taken based on the business logic.
PROCEDURE EXPIRE_STUFF_PRC
(
PI_EMAIL_NBR_STR IN VARCHAR2,
PO_SUCCESS_FLG OUT VARCHAR2,
PO_OUT_MSG OUT VARCHAR2
) AS
L_SUCCESS VARCHAR2(1) := 'N';
L_EMAIL_ID NUMBER;
L_PREV_EMAIL_VER_ID NUMBER := 0;
L_PREV_EMAIL_EXP_DT DATE;
BEGIN
BEGIN
SELECT
ITEM.EMAIL_ID
INTO L_EMAIL_ID
FROM HR_EMAIL ITEM
WHERE ITEM.EMAIL_NBR_STR = PI_EMAIL_NBR_STR;
EXCEPTION WHEN OTHERS THEN
PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'COULD NOT FETCH EMAIL_ID';
END;
BEGIN
SELECT
VER_ID.EMAIL_VER_ID
INTO L_PREV_EMAIL_VER_ID
FROM (
SELECT
EMAIL_VER_ID
FROM HR_EMAIL_VER
WHERE EMAIL_ID = L_EMAIL_ID
ORDER BY EMAIL_VER_ID DESC
) VER_ID
WHERE ROWNUM = 2;
EXCEPTION
WHEN NO_DATA_FOUND
THEN PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'LESS THAN 2 VERSIONS';
END;
BEGIN
IF (L_PREV_EMAIL_VER_ID > 0) THEN
SELECT
VER.EXP_DT
INTO L_PREV_EMAIL_EXP_DT
FROM HR_EMAIL_VER VER
WHERE VER.EMAIL_VER_ID = L_PREV_EMAIL_VER_ID;
IF (L_PREV_EMAIL_EXP_DT > SYSDATE) THEN
UPDATE HR_EMAIL_VER
SET EXP_DT = SYSDATE
WHERE EMAIL_VER_ID = L_PREV_EMAIL_VER_ID;
END IF;
PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'SUCCESS';
END IF;
EXCEPTION
WHEN OTHERS THEN
PO_SUCCESS_FLG := 'Y';
PO_OUT_MSG := 'UNABLE TO UPDATE HR_EMAIL_VER';
END;
END EXPIRE_STUFF_PRC;

PL/SQL if table not exist create

Hello i use oracle SQL developer
I have create a procedure, and i need to check if a table exist, if not exist i must create how can do?
I have try this
DECLARE v_emp int:=0;
BEGIN
SELECT count(*) into v_emp FROM dba_tables;
if v_emp = 0 then
EXECUTE IMMEDIATE 'create table EMPLOYEE ( ID NUMBER(3), NAME VARCHAR2(30) NOT NULL)';
end if;
END;
but give me an error 00103 because not find table
Just execute the create and watch the exception if thrown. Oracle would never replace the DDL of a table.
declare
error_code NUMBER;
begin
EXECUTE IMMEDIATE 'CREATE TABLE EMPLOYEE(AGE INT)';
exception
when others then
error_code := SQLCODE;
if(error_code = -955)
then
dbms_output.put_line('Table exists already!');
else
dbms_output.put_line('Unknown error : '||SQLERRM);
end if;
end;
You can run this for example:
if (select count(*) from all_tables where table_name = 'yourTable')>0 then
-- table exists
else
-- table doesn't exist
end if;
You should try following,
declare
nCount NUMBER;
v_sql LONG;
begin
SELECT count(*) into nCount FROM dba_tables where table_name = 'EMPLOYEE';
IF(nCount <= 0)
THEN
v_sql:='
create table EMPLOYEE
(
ID NUMBER(3),
NAME VARCHAR2(30) NOT NULL
)';
execute immediate v_sql;
END IF;
end;

Trigger compilation error

I need some help in creating a trigger.
create or replace trigger trigger_one
before insert on Funtom_timesheet
for each row
Declare
V_id number;
V_hours number;
Begin
Select max(timesheet_ID)+1 into v_id from Funtom_timesheet
:new.timesheet_ID :=v_id;
select grade_hours into V_hours
from funtom_grade join funtom_employee
on emp_grade = grade_id
where empid = :new.timesheet_emp;
if V_hours >:new.timesheet_hours
else
:new.timesheet_overtime :=
:new.timesheet_hours-V_hours
:new.timesheet_hours:= V_hours;
END IF;
END;
/
please tell me which part of my code is wrong so I could work on it,
Thanks
You have many syntax errors - missing ; and then. There can't be if only with else part and without expression on it.
CREATE OR REPLACE TRIGGER TRIGGER_ONE
BEFORE INSERT ON FUNTOM_TIMESHEET
FOR EACH ROW
DECLARE
V_ID NUMBER;
V_HOURS NUMBER;
BEGIN
SELECT MAX(TIMESHEET_ID) + 1 INTO V_ID FROM FUNTOM_TIMESHEET;
:NEW.TIMESHEET_ID := V_ID;
SELECT GRADE_HOURS
INTO V_HOURS
FROM FUNTOM_GRADE
JOIN FUNTOM_EMPLOYEE
ON EMP_GRADE = GRADE_ID
WHERE EMPID = :NEW.TIMESHEET_EMP;
IF V_HOURS > :NEW.TIMESHEET_HOURS THEN
NULL;
ELSE
:NEW.TIMESHEET_OVERTIME := :NEW.TIMESHEET_HOURS - V_HOURS;
:NEW.TIMESHEET_HOURS := V_HOURS;
END IF;
END;
/
Also better use SEQUENCES instead of:
SELECT MAX(TIMESHEET_ID) + 1 INTO V_ID FROM FUNTOM_TIMESHEET;
:NEW.TIMESHEET_ID := V_ID;
When selecting form the same table as inserting, you can get MUTATING trigger error (http://www.dba-oracle.com/t_avoiding_mutating_table_error.htm)

procedure to delete duplicate records using dynamic sql

i have successfully implemented a proc which can delete delete duplicate records from a table .
Carrying on i am further trying to generalize this procedure using Dynamic sql But i get the following error when compiling itself .
ERROR line 13, col 7, ending_line 14, ending_col 14, Found ''SELECT ROWID, ERR_MOD
FROM '', Expecting: ( SELECT -or- WITH
Here is my code :-
CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
p_tblname IN VARCHAR2,
p_cname IN varchar2,
p_cvalue IN varchar2,
p_out OUT VARCHAR2:=0
)
IS
v_cnt NUMBER;
CURSOR TMP
IS
'SELECT ROWID, ERR_MOD
FROM ' || p_tblname||
'WHERE '|| p_cname '='||''p_cvalue '';
BEGIN
BEGIN
'SELECT COUNT ( * )
INTO v_cnt
FROM ' || p_tblname||
'WHERE '|| p_cname' = '||''p_cvalue'';
EXCEPTION
WHEN OTHERS
THEN
p_out:=1;
end;
FOR REC IN tmp
LOOP
IF v_cnt = 1
THEN
EXIT;
ELSE
'DELETE FROM '|| p_tblname||
'WHERE ROWID = REC.ROWID';
END IF;
v_cnt := v_cnt - 1;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
p_out := 2;
END;
/
Hi you cannot create a dynamic query for normal cursors in such cases you should use ref cursors and follow the the steps below
--create a query string
--open the ref cursor
and please use execute immediate for execution of delete statements...
CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
p_tblname IN VARCHAR2,
p_cname IN varchar2,
p_cvalue IN varchar2,
p_out OUT VARCHAR2:=0
IS
v_cnt NUMBER;
v_string varchar2(200);
TYPE ref_cur IS REF CURSOR
ref_cursor_name ref_cur;
BEGIN
v_string:='SELECT ROWID, ERR_MOD FROM ' || p_tblname|| 'WHERE '|| p_cname
'='||''p_cvalue '';
OPEN cursor_variable_name FOR v_string;
.....
.....
EXECUTE IMMEDIATE 'DELETE FROM '|| p_tblname|| 'WHERE ROWID = REC.ROWID';--you need execute immediate for this
.....
.....