There is a common function:

CREATE OR REPLACE FUNCTION ISFUNC(ID IN NUMBER) RETURN VARCHAR2 IS BEGIN IF (ID = 2 OR ID = 5) THEN RETURN('TRUE'); ELSE RETURN('FALSE'); END IF; END ISFUNC; / 

Its essence seems to be clear. How can I insert a SELECT into it? Those. That in IF there were not specific numbers, and result? Suppose, for example, there is such a SELECT:

 SELECT id FROM Table 

The result of this select is two lines: 2 and 5.

How to insert this SELECT correctly into a function? I tried in the IF myself, but I realized that it was not correct.

    3 answers 3

    Try this:

     CREATE OR REPLACE FUNCTION ISFUNC(ID IN NUMBER) RETURN VARCHAR2 IS l_exst number; BEGIN SELECT count(*) INTO l_exst FROM Table t WHERE t.id = ID; IF (l_exst <> 0) THEN RETURN('TRUE'); ELSE RETURN('FALSE'); END IF; END ISFUNC; / 

    or

     CREATE OR REPLACE FUNCTION ISFUNC(ID IN NUMBER) RETURN VARCHAR2 IS BEGIN FOR x IN ( SELECT ID FROM Table ) LOOP IF (ID = x.ID) THEN RETURN('TRUE'); END IF; END LOOP; RETURN('FALSE'); END ISFUNC; / 

      Another solution to this problem:

       SQL> create table tab(id number); Table created SQL> insert into tab values (2); 1 row inserted SQL> insert into tab values (5); 1 row inserted SQL> select * from tab; ID ---------- 2 5 SQL> CREATE OR REPLACE FUNCTION ISFUNC(pID IN NUMBER) RETURN VARCHAR2 2 IS 3 result varchar2(5); 4 BEGIN 5 select decode(count(*),0,'FALSE','TRUE') into result 6 from tab 7 where tab.id=pid; 8 return(result); 9 END ISFUNC; 10 / SQL> select level, isfunc(level) from dual connect by level<=10; LEVEL ISFUNC(LEVEL) ---------- -------------------------------------------------------------------------------- 1 FALSE 2 TRUE 3 FALSE 4 FALSE 5 TRUE 6 FALSE 7 FALSE 8 FALSE 9 FALSE 10 FALSE 10 rows selected 

        You can declare the @count variable and execute the query:

         select @count = count(*) from Table where id in (2, 5) 

        And then check the number of lines:

         if (@count > 0) 

        PS I don’t remember the PL / SQL syntax, but correct the inaccuracies yourself, if that.