1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Function returning a boolean value

Discussion in 'SQL PL/SQL' started by mukulverma2408, Jul 19, 2015.

  1. mukulverma2408

    mukulverma2408 Active Member

    Messages:
    38
    Likes Received:
    1
    Trophy Points:
    90
    Location:
    New delhi
    I have a requirement where i have to check if a department_id exist in my table, here is what I'd tried :
    Code (Text):

    create or replace
    FUNCTION VALID_DEPTID(ID DEPT.DEPARTMENT_ID%TYPE) RETURN BOOLEAN IS
    DNAME DEPT.DEPARTMENT_NAME%TYPE;
    BEGIN
    SELECT DEPARTMENT_NAME INTO DNAME FROM DEPT WHERE DEPARTMENT_ID=ID;
    IF DNAME IS NOT NULL THEN
    RETURN TRUE;
    ELSE
    RETURN FALSE;
    END IF;
    END;
     
    it seems to me that it is not the standard way of doing this,could you please help me to improve the code.
    i am using below code while calling this function.
    Code (Text):

    DECLARE
    FLAG BOOLEAN;
    BEGIN
    FLAG:=VALID_DEPTID(1000);
    END;
     
     
  2. ocprep

    ocprep Forum Advisor

    Messages:
    277
    Likes Received:
    76
    Trophy Points:
    410
    Location:
    Orlando, Florida
    Other than indenting the code to make it more readable, I don't see any obvious improvements to make. It looks like it will work and the sample you gave is not very complex, so there's not really much to be done to simplify it further.