Calling a BOOLEAN returning function from SQL

Hello All,
I have created below function which return BOOLEAN value :
CREATE OR REPLACE FUNCTION FUNC_1
P_EMPID IN emp.empno%type
)RETURN BOOLEAN
AS
L_VAR NUMBER;
BEGIN
SELECT 1 INTO L_VAR
FROM EMP
WHERE EMPNO = P_EMPID;
IF L_VAR = 1 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END;Now I want to call this function from SELECT but I know that SQL does not support BOOLEAN value so I tried in other way i.e. via CASE
SELECT CASE FUNC_1(7788)
       WHEN TRUE THEN 'TRUE'
       WHEN FALSE THEN 'FALSE'
       ELSE 'NULL'
       END CASE
FROM DUALBut it is giving me error "ORA-00904: "FALSE": invalid identifier'
How can I achieve this ?
Thanks & Regards,
Rakesh

Hi Rakesh,
Why cant you try something like this, When BOOLEAN type is not supported in SQL.
Here I have made the return value a number. And, at case comparison, we get the BOOLEAN
result as TRUE of FALSE.
CREATE OR REPLACE FUNCTION func_1 (p_empid IN emp.empno%TYPE)
   RETURN NUMBER
AS
   l_var   NUMBER;
BEGIN
   SELECT count(*)
     INTO l_var
     FROM emp
    WHERE empno = p_empid;
   IF l_var = 1
   THEN
      RETURN 1;
   ELSE
      RETURN 0;
   END IF;
END;And,
SELECT CASE func_1 (55656)
          WHEN 1
             THEN 'TRUE'
          WHEN 0
             THEN 'FALSE'
       END
  FROM DUAL;Which return TRUE if the Employee Number Exists and FALSE if doesnot.
Thanks,
Shankar.

Similar Messages

  • Use of boolean returning functions in a project

    Hello all gurus,
    in my project, we have a fairly important packaged functions that return boolean values. Working with these ones in pl/sql is very fine. But sometimes we need to reuse these functions in SQL, but no luck because bools are usable under pl/sql only and can't interact in any way in SQL statements. So the workaround should be to use these functions to return us some Y/N or 1/0 to emulate boolean behavior in SQL statements.
    Here what i tested with not luck:
    -- not work
    select r.role, sys.diutil.bool_to_int(dbms_session.is_role_enabled(r.role)) as is_role_enabled
    from   dba_roles r;
    -- not work
    select r.role
    from   dba_roles r
    where  sys.diutil.bool_to_int(dbms_session.is_role_enabled(r.role)) = 1;
    -- not work
    select t1.id,
           bool_to_char(my_bool_func(t1.x, t1.y, ...)) as is_something
    from   t1;
    -- not work
    select t1.id,
           sys.diutil.bool_to_int(my_bool_func(t1.x, t1.y, ...)) as is_something
    from   t1;The odd wrapping trick as a last resort solution is working....
    -- Works! Seems the only way, but a lot of wrapping work...
    create or replace function my_bool_func_wrap(p_x number, p_y number, ...) return varchar2 as
    begin
       return bool_to_char(my_bool_func(p_x, p_y, ...));
    end;
    select t1.id,
           my_bool_func_wrap((t1.x, t1.y, ...)) as is_something
    from   t1;I read a lot, but no elegant and working way.
    Is there a more standard, elegant universal way to call bool functions from SQL?
    Is creating a custom type visible and usable from both pl/sql and sql, if possible, a way to go?
    Any other pointers?
    For new development, is it good to make my boolean type returning functions using SQL compatible type like CHAR (Y/N) or NUMBER (1/0) ? It will make us less wrapping job, but more and less elegant bool handling code on the pl/sql side.
    What is the goal to have bool only in pl/sql and not usable in SQL? It's kind of a feature incompatibility in the same product. Strange...
    Thanks a lot
    Bruno

    brlav wrote:
    Finally, I'll have to dump the BOOLEAN return type to all our boolean functions and return char instead. With this I will be able to call then from SQL.... From this perspective, BOOLEAN is useless.I would not say that. Let's assume that you implement boolean in SQL as a single byte character string containing either Y or N, enforce that with a constraint and also add a not-null constraint to it.
    You simply define two PL functions (not usable from SQL) that deals with the conversion. You use the one function to change boolean to char before hitting the SQL engine, and the other to convert char to boolean when getting data from the SQL engine.
    As I/O routines are modularised, it means that you need to deal with these conversions once only when writing and once only when reading (per module).
    Simple example:
    SQL> create or replace function to_bool( c varchar2 ) return boolean is
      2  begin                                                            
      3          return( c = 'Y' );                                       
      4  end;                                                             
      5  /                                                                
    Function created.
    SQL>
    SQL> create or replace function bool_to_char( b boolean ) return varchar2 is
      2  begin
      3          if b then
      4                  return( 'Y' );
      5          else
      6                  return( 'N' );
      7          end if;
      8  end;
      9  /
    Function created.
    SQL>
    SQL> declare
      2          i       integer;
      3          b       boolean;
      4          flag    all_tables.temporary%type;
      5          tab     all_tables%rowtype;
      6  begin
      7          flag := bool_to_char(true);  
      8          select count(*) into i from all_tables where temporary = flag;
      9
    10          select t.* into tab from all_tables t where rownum = 1;
    11          b := to_bool( tab.temporary );
    12  end;
    13  /
    PL/SQL procedure successfully completed.
    SQL>

  • Without calling stored procedure or functions from database

    Hi,
    I am using Jdeveloper 11.1.1.5.0.
    =>How to do PL/SQL procedures and functions in ADF without calling stored procedure or function from DB?

    S, PL/SQL procedures and functions are done in Application Module class or in managed bean..By calling the stored procedures or functions from DB.
    But I am asking how to do if DB doesn't have any procedures,triggers and functions.

  • Calling an external C function from a C file in JNI

    Hello,
    I am trying to call an external C function, from a C file which is being called by a Java file. I am getting an unresolved symbol error. I have tried many things like, making the external C function in the format of JNI, etc, etc. It is still not working. I think it has something to do with linking the two C files. If anyone can please answer this, it would greatly help me. here is the code:
    HelloWorld.c:
    #include <jni.h>
    #include <stdio.h>
    #include "MyOldHello.h"
    #include "HelloWorld.h"
    JNIEXPORT void JNICALL
    Java_HelloWorld_print(JNIEnv *env, jobject obj)
         helloPrint();
         return;
    HelloWorld.java:
    class HelloWorld
         private native void print();
         public static void main(String[] args)
              new HelloWorld().print();
         static
              System.loadLibrary("HelloWorld");
              System.loadLibrary("MyOldHello");
    MyOldHello.c:
    #include <jni.h>
    #include <stdio.h>
    #include "MyOldHello.h"
    void helloPrint()
         printf("\nHello World!\n");
    MyOldHello.h:
    void helloPrint();
    Now i use the Visual C++ command prompt to compile this by saying:
    javac HelloWorld.java
    javah -jni HelloWorld
    cl -Ic:\Java\jdk1.6.0_20\include -Ic:\Java\jdk1.6.0_20\include\win32 -MD -LD HelloWorld.c -FeHelloWorld.dll
    and now it gives me the error saying that there is an unresolved external symbol, which is the call to helloPrint in the file HelloWorld.
    If anyone knows how to solve this, or how to call external C functions from a C file that is being called from a Java file using JNI, please respond.
    Thanks
    Nick

    Hi,
    In your post on velocity review, you did not compile MyOldHello.c. You compiled a C file that included the header file for it and called a method defined in the header. The linker is never going to be able to find the code for this if you do not include the object file for this.
    Try this. You will also have to add in any JNI libraries you need to link against but I am sure you could work that out.
    cl /c MyOldHello.c
    cl /c -Ic:\Java\jdk1.6.0_20\include -Ic:\Java\jdk1.6.0_20\include\win32 -MD HelloWorld.c
    cl /LD MyOldHello.obj HelloWorld.obj /FeHelloWorld.dll
    [http://msdn.microsoft.com/en-us/library/f35ctcxw(VS.80).aspx]
    Cheers,
    Shane

  • Is it possible to call the Print Quote functionality from Custom ADF page

    Hi,
    We are researching if it is possible to call the Print Quote functionality from the Custom ADF application.
    Goal is to pop up the PDF report upon clicking the Print Quote button on the custom page. Is it possible ?
    Atleast advice on the direction to go forward is appreciated.
    Thanks
    Sai

    Hi ,
    Please check following thread on forum -
    Re: ADF: Calling OAF Page from ADF page
    Check this may also be useful-
    https://blogs.oracle.com/shay/entry/to_adf_or_oaf_or
    I have not tried yet but Steven Chan (Sr. Director OATG) suggest following methodolgy for this-
    https://blogs.oracle.com/stevenChan/entry/appsdatasource_jaas_ebs
    Thanks,
    Ashish

  • Calling task flows return activity from Jspx page.

    Hi ,
    Using JDev11.1.1.6
    I have created template for home page, drag and dropped the task flow(Main Task flow) as region.
    Main task flow contains other 2 taskflows with control flows and other 2 taskflows contains Task flow return activity to navigate back to Main task flow.
    I need to call task flow return activity from jspx page.
    How it can be acheived? can you please suggest me.
    Thanks and Regards,
    Raj Gopal K

    Hi,
    My use case is to close the child task flow and navigate back to main task flow. This should be done through homePage.jspx(whch contains main task flow).
    What i have done:
    Having page template which has logout and home page links. Use this template to create homePage.jspx, Next drag the main task flow to create a region inside the homePage.jspx.
    Thanks,
    Rajgopal K

  • Calling a pure C function from JNI method

    Is it posible to call a pure C function from a JNI method.?
    I am communicating with an external device whose API is written in C language.
    Would it work this way
    If I declare empty native methods in Java and those methods in C call pure C methods?
    Thank You...

    Hello,
    I have a similar problem and I posted it here http://www.velocityreviews.com/forums/t724826-jni-calling-an-outside-function-in-the-c-file-which-is-being-called-by-the-java-file.html. If you can answer the question, it would greatly help me.
    Thanks
    Nick

  • Calling a function from SQL prompt that returns a record

    Hi,
    I've been trying to execute a function that is present on a different database. for eg. I am loged on to a database say 'A' and trying to execute a function present in database 'B'. this function is present in a package 'X' which has 2 functions and two procedures. From the packages i am able to execute the two procedures and one of the function.
    So i guess it is not a problem with the access permissions. The function that i am trying to call say function I has got 3 OUT
    parameters and 1 IN parameter. the Function returns a record. When i try to execute this function i get an error. Can you please let me know as to how exactly i need to call this function from the SQL prompt...
    thanx in advance
    null

    Hi Anand,
    As your function has 3 OUT parameters and it returns a record you can not just call it from SQL Plus. You need to write small PL/SQL program and use variables to hold the OUT values and the returned record.
    Good Luck,
    RajKiran
    null

  • Calling a function from sql*plus

    I can call my procedure from sql *plus
    by doing
    sql>call Proc_name(x,y);
    How do you call a function?
    null

    John,
    I think moifying the statement
    CREATE OR REPLACE PROCEDURE "OGUSER"."OGX1" (user_county in integer, user_permit in integer )
    TO
    CREATE OR REPLACE FUNCTION "OGUSER"."OGX1" (user_county in integer, user_permit in integer ) return NUMBER is
    AND before end you will have to add a return statement
    (Probably
    return 0;
    exception
    when others then
    return 1;
    end;
    This will change your procedure to a function but I am not sure you'll be able to see your dbms_output's, if you call the function using select ...
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by john saucer ([email protected]):
    I want to turn my procedure into a function.
    So I can call it with a select statement.
    I'm kind of having problems with the return statement at the top and bottom.
    I don't quite understand how to declare the type in the return. My procedure calculates 2 pl/sql tables....
    My procedure looks like.
    CREATE OR REPLACE PROCEDURE "OGUSER"."OGX1" (user_county in integer, user_permit in integer )
    as
    i integer :=0;
    j integer :=0;
    type dept_table_type is table of ogxtest%rowtype
    index by binary_integer;
    type dept2_table_type is table of ogxtest%rowtype
    index by binary_integer;
    my_dept_table dept_table_type;
    my_dept2_table dept2_table_type;
    v_cotemp number := user_county;
    v_permittemp number := user_permit;
    v_origcotemp number := user_county;
    v_origpermittemp number := user_permit;
    v_count number(2) :=1;
    v_count2 number(2) := 1;
    v_oldcount number(2) :=1;
    v_oldcount2 number(2) := 1;
    begin
    select count(*) into v_count from ogxtest where oco=v_cotemp and opermit=v_permittemp;
    select count(*) into v_oldcount from ogxtest where nco=v_cotemp and npermit=v_permittemp;
    while v_count >= 1 LOOP
    i := i+1;
    v_count2 := v_count2 +1;
    select *
    into my_dept_table(i)
    from ogxtest where oco=v_cotemp and opermit=v_permittemp;
    v_cotemp := my_dept_table(i).nco;
    v_permittemp := my_dept_table(i).npermit;
    select count(*) into v_count from ogxtest where oco=v_cotemp and opermit=v_permittemp;
    end loop;
    while v_oldcount >= 1 LOOP
    j := j+1;
    v_oldcount2 := v_oldcount2 +1;
    select *
    into my_dept2_table(j)
    from ogxtest where nco=v_origcotemp and npermit=v_origpermittemp;
    v_origcotemp := my_dept2_table(j).oco;
    v_origpermittemp := my_dept2_table(j).opermit;
    select count(*) into v_oldcount from ogxtest where nco=v_origcotemp and npermit=v_origpermittemp;
    end loop;
    for i in 1..v_count2-1
    loop
    dbms_output.put_line(' reassigned to - orig county ' | |my_dept_table(i).oco | | ' orig permit ' | |my_dept_table(i).opermit| | ' new county ' | |
    my_dept_table(i).nco | | ' new permit ' | |my_dept_table(i).npermit );
    end loop;
    for j in 1..v_oldcount2-1
    loop
    dbms_output.put_line(' reassigned from - orig county ' | |my_dept2_table(j).oco | | ' orig permit ' | |my_dept2_table(j).opermit| | ' new county ' | |
    my_dept2_table(j).nco | | ' new permit ' | |my_dept2_table(j).npermit );
    end loop;
    end;
    <HR></BLOCKQUOTE>
    null

  • Calling function from sql folder vs report

    Hi,
    A report based on a custom sql folder is taking a long time to run. One of the things that I noticed is that when I run the sql in plsql it takes a long time, but if I remove the row where I call a function, it runs pretty quickly.
    In general, is there a difference between running a function from the sql custom folder and calling it from the report itself?
    Thanks.
    Leah

    Hi Tamir,
    I might check out the execution plans, but truthfully, understanding the plans and the meaning of the differences is not my strong point.
    I thought that maybe there was some general rule that, for instance, it is better to keep functions out of the sql folder and use them in a condition in the report itself, or the opposite, or that maybe it makes no difference at all.
    I appreciate the response.
    Thanks.
    Leah

  • Calling the function from SQL query

    Hi,
    I am trying to run the below statement,
    Select to_number(apps.pay_balance_pkg.get_value( 326, :paa.assignment_action_id,to_date ('31032011','ddmmyyyy'))) from dual;
    getting an error as :
    ORA-14552 cannot perform a DDL, commit or rollback inside a query or DML
    ORA - 06512 at apps.pay_balance_pkg , line 4526.
    How can I execute this funciton "apps.pay_balance_pkg.get_value" from sql query?
    Thanks in advance.

    user1175432 wrote:
    Hi,
    I am trying to run the below statement,
    Select to_number(apps.pay_balance_pkg.get_value( 326, :paa.assignment_action_id,to_date ('31032011','ddmmyyyy'))) from dual;
    getting an error as :
    ORA-14552 cannot perform a DDL, commit or rollback inside a query or DML
    ORA - 06512 at apps.pay_balance_pkg , line 4526.
    How can I execute this funciton "apps.pay_balance_pkg.get_value" from sql query?
    Thanks in advance.If the function is performing DDL, commit or rollback inside it then you will not be able to call it from an SQL statement.
    Either change the function so it doesn't perform DDL, commit or rollback, or use a different means to obtain the information you want (assuming you can't change the function)

  • Migrating Functions that return TABLE from SQL Server to Oracle

    I have some functions in SQL Server that return a TABLE datatype. When these functions are moved to Oracle 9i using Migration Workbench, they give compilation errors. In the migrated function it says that the DDL stmt is passed to the ddl file, but the table is not created. I checked the ddl stmt for temporary tables and it is wrong. Its a create table stmt with no size for varchars and we can't even edit these stmts in the workbench.
    Also the migrated function has the table name for return type, which doesn't works in Oracle. Oracle needs a datatype to be returned from Oracle.
    How do we return a table from a function?

    Yes.
    If you do not enclose the object names (table/view/index etc) in double-quotes, they are stored in uppercase format in the data dictionary.
    If you enclose them in quotes, they are stored in the same case ans you entered. As such, while accessing such objects, you need to tell Oracle not to convert the names to uppercase, hence the requirement to supply the names in quotes.

  • Call external function from SQL query

    Hi,
    I am new to PL/SQL programming.I don't think this is possible but please let me know if there is a way to achieve this. I have a function written in VB.net and I would want to call from the query.
    create table temp as select id, callvbfunction(note_text) from temp2
    Here callvbfunction is the vb.net function.I need to pass note_text field value to the function.
    Thanks..

    Yes it is possible.
    No idea how to specifically call a .Net function (from Oracle) as I do not do Windows (except for playing games ;-) ). But external procedures (extproc) and Java stored procs can be wrapped by PL/SQL wrapper functions and used in SQL statements.

  • Regarding Restrictions on calling functions from sql expressions

    Hi all,
    While going through the functions of oracle 9i plsql documentation i came across restrictions on calling a function, i that i did not understand the following statement. can anyone explain me with an example. Please.........
    Functions called from SQL statements cannot contain statements that end the transactions.
    Regards,
    Sri Ram.

    Some where from google
    http://www.ucertify.com/article/what-are-the-restrictions-on-a-user-defined-function-that-is-called-from-a-sql-expression.html
    >
    •The function cannot contain statements that end the transaction. For example, the function cannot contain transaction control statements (such as COMMIT), session control statements (such as SET ROLE), or system control statements (such as ALTER SYSTEM). Also, it cannot contain DDL statements (such as CREATE) because they are followed by an automatic COMMIT.

  • Booleans API Functions into SQL SELECT

    Hello,
    our API is using a huge set of boolean «is_{what we want to check}» functions. These simple atomic functions are used into more sophisticated PL/SQL funcs or procs.
    Sometime, we need to use these bool funcs directly into our SQL queries.
    Here is a simplified example we want:
    SELECT id1, id2, is_valid(id1, id2)
    FROM table
    WHERE ... stuff ...;
    or
    SELECT col1, col2, ...
    FROM table
    WHERE is_valid(id1, id2) IS TRUE ;
    or
    SELECT col1, col2, ...
    FROM table
    WHERE is_valid(id1, id2) = TRUE ;
    to be valid queries....
    I know that booleans aren't supported natively in the SQL, I created a function that translate bool to char:
    FUNCTION bool_to_char (p_bool IN BOOLEAN)
    RETURN VARCHAR2
    IS
    v_retour VARCHAR2 (5);
    BEGIN
    v_retour := CASE p_bool
    WHEN TRUE
    THEN 'TRUE'
    WHEN FALSE
    THEN 'FALSE'
    END;
    RETURN v_retour;
    END;
    So, when I call this func into my sql, it's not working anymore:
    SELECT id1, id2, bool_to_char(is_valid(id1, id2))
    FROM table
    WHERE ... stuff ...;
    or
    SELECT col1, col2, ...
    FROM table
    WHERE bool_to_char(is_valid(id1, id2)) = 'TRUE';
    I'm searching for a solution to make this possible... The only fast solution that came in my mind is to encapsulate every boolean functions to return a corresponding varchar value. Not that good....
    Any ideas or suggestions?

    SQL standards have been developed over the years and whilst some vendors have tweaked SQL functionality to make their database better than others, one thing that is not part of SQL standards is support for BOOLEAN. 3GL and 4GL languages most often support boolean values, which are represented internally as a binary number.
    For those interested, in some languages the BOOLEAN is represented as -1 for TRUE and 0 for FALSE. In binary, -1 is represented as all the bits set to 1 whilst 0 is all the bits set to 0. There was a nice feature in one of the BASIC versions (I think it was BBC Basic if I remember correctly) that allowed you to use booleans within assignments such as...
    x := y + (z AND (y < 100))If the "y<100" condition evaluated to TRUE, this would translate as...
    x := y + (z AND TRUE)which, in binary terms (let's assume all numbers are single bytes for illustrative purposes) would be...
    x := y + (z AND 11111111)The "z AND 11111111" would perform a logical AND between the z value and the binary 11111111 resulting in a value of z... giving...
    x := y + zIf the condition evaluated to false on the other hand it would go as follows...
    x := y + (z AND FALSE)
    x := y + (z AND 00000000)So, "z AND 00000000" would logically AND to give a result of 0 and thus giving...
    x := ySo it was useful to use in conditional arithmetic, avoiding having to put IF statements in the code.
    Getting back to the thread...
    Using PL/SQL functions so extensively in SQL is a bad idea as it will inevitably cause a lot of context switching between the SQL and PL/SQL engines. This will have a noticable effect on performance of queries. Where possible, if the test can be done within SQL, then it should be done there rather than calling PL/SQL.
    ;)

Maybe you are looking for