Writing a function to find whether a value is present in a sql query output

Hi gurus,
I would like to write a function to which i will pass a value and a sql query as parameters.
Now the function needs to execute that sql query and find whether the given value is present in the output list
when the query is executed. If it is present it should return 'T' otherwise 'F'
My function will look like
CREATE FUNCTION CHECK_VALUE(VALUE VARCHAR2,V_SQL VARCHAR2) RETURN VARCHAR2
SELECT CHECK_VALUE('Dallas','SELECT LOCATION_CODE FROM HR_LOCATIONS')
It should check whether the value 'Dallas' is present in the output list returned by the sql query.
Any help will be appreciated.
Thank you.

CREATE OR REPLACE
  FUNCTION CHECK_VALUE(
                       VALUE VARCHAR2,
                       V_SQL VARCHAR2
    RETURN VARCHAR2
    IS
        RETVAL VARCHAR2(4000);
        REFCUR SYS_REFCURSOR;
    BEGIN
        OPEN REFCUR FOR V_SQL;
        LOOP
          FETCH REFCUR INTO RETVAL;
          EXIT WHEN REFCUR%NOTFOUND;
          IF RETVAL = VALUE
            THEN
              CLOSE REFCUR;
              RETURN 'T';
          END IF;
        END LOOP;
        CLOSE REFCUR;
        RETURN 'F';
END;
Function created.
SQL> SET SERVEROUTPUT ON
SQL> EXEC DBMS_OUTPUT.PUT_LINE(CHECK_VALUE('DALLAS','select loc from dept'));
T
PL/SQL procedure successfully completed.
SQL> EXEC DBMS_OUTPUT.PUT_LINE(CHECK_VALUE('PARIS','select loc from dept'));
F
PL/SQL procedure successfully completed.
SQL> SY.

Similar Messages

  • Passing multiple values to a parmeters in SQL Query

    Hi friends,
    I have the following requirement -
    I need to pass multiple values to the parameter 'WHERE hou.name = (:id1)' and the query is copied below for your reference .
    SELECT partno part_num,
         customer customer_name,
         hou.name op_name
         FROM hr_organization_units hou,
         oe_transaction_types_all sot,
         ra_customers rc,
         ra_addresses_all ra,
         ra_site_uses_all rsu,
         oe_order_headers_all h,
         oe_order_lines_all l,pwr_sod50 ps
         WHERE hou.name = (:id1)
    -- and hou.name = (:id4)
    --hou.name in ('CPS FRANCE','CPS GERMANY')
    --and hou.name = (:id1,hou.name)
         and trunc(ps.sch_ship_date) between nvl(:id2,trunc(ps.sch_ship_date)) and nvl(to_date(:id3)+.99999,trunc(ps.sch_ship_date))
         and ps.group_id = 9999999
         and hou.organization_id=h.org_id
         and ps.line_id =l.line_id
         and l.header_id =h.header_id
         and h.invoice_to_org_id=rsu.site_use_id
         and rsu.address_id =ra.address_id
         and ra.customer_id =rc.customer_id
         and h.order_type_id =sot.transaction_type_id
    Looking for your help on this.
    Thanks In Advance.
    Thanks & Regards
    Ramya Nomula

    Hi karthik,
    I am sorry for the wrong updation of my anonymus block.
    My requirement is to pass a multiple values to the parameter in SQL query, and here is the code which is working now for the multiple values with ourt single quotes to the values -
    SELECT partno part_num,
         customer customer_name,
         ps.customer_id customer_id,
         avail_qty avail_qty,
         sch_ship_date schedule_Ship_date,
         so_num order_no,
         h.header_id header_id,
         line_num line_no,
         l.ordered_quantity ordered_quantity,
         scd_qty qty_open,
         s_price unit_price,
         part_flag flag,
         sub_inv subinv,
         sbu,
         hold,
         qoh,
         line_detail_id detail_id,
         picking_line_id,
         picking_line_detail_id,
         rc.customer_name cust_name,
         rc.customer_number customer_number,
         rsu.location location,
         sot.attribute5 order_type,
         ps.line_id line_id,
         h.transactional_curr_code transactional_curr_code,
         hou.name op_name
         FROM hr_organization_units hou,
         oe_transaction_types_all sot,
         ra_customers rc,
         ra_addresses_all ra,
         ra_site_uses_all rsu,
         oe_order_headers_all h,
         oe_order_lines_all l,pwr_sod50 ps
         WHERE ','||:id1||',' like '%,'||hou.name||',%'
         and trunc(ps.sch_ship_date) between nvl(:id2,trunc(ps.sch_ship_date)) and nvl(to_date(:id3)+.99999,trunc(ps.sch_ship_date))
         and ps.group_id = 9999999
         and hou.organization_id=h.org_id
         and ps.line_id =l.line_id
         and l.header_id =h.header_id
         and h.invoice_to_org_id=rsu.site_use_id
         and rsu.address_id =ra.address_id
         and ra.customer_id =rc.customer_id
         and h.order_type_id =sot.transaction_type_id;
    Condition for sending multiple Oprtaing Units -
    WHERE ','||:id1||',' like '%,'||hou.name||',%'
    This above condition is working when i am passing multiple values with out single quotes...but not working for multiple values with single quotes.
    Sample queries tested -
    select 1 from dual where ',aa,bb,cc,' like '%,bb,%' (This is working)
    select 1 from dual where ','aa','bb','cc',' like '%,'bb',%'(This is not working).
    Thanks In Advance!
    Looking for Your Great Help.
    Thanks & Regards
    Ramya Nomula

  • Having troubles passing values of Shuttle control to SQL Query of Report and Chart Region

    Hello,
    I am very new to APEX and need help for one of the Pa.I have a shuttle control on my page which populates Categories. Once user selects Categories from this control, I wish to pass the values to following SQL query :
    select * from emp_class where category IN ( LIST of VALUES FROM RIGHT SIDE SHUTTLE).
    I tried various ways of doing this including writing a java scripts which reads shuttle value, converts it into below string
    'Category1',Category2',Category3'. Then I set this value to a text box. And then I was expecting that below trcik would work
    select * from emp_class where category IN (:TXT_VALUES)
    I am sure this is not right way and hence its not working. Can you please guide me here with options?
    Many Thanks,
    Tush

    b96402b4-56f7-44ba-8952-fb82a61eeb2c wrote:
    Please update your forum profile with a real handle instead of "b96402b4-56f7-44ba-8952-fb82a61eeb2c".
    I am very new to APEX and need help for one of the Pa.
    Don't understand what this means. What is "Pa"?
    select * from emp_class where category IN (:TXT_VALUES)
    I am sure this is not right way and hence its not working. Can you please guide me here with options?
    This is a common fallacy. In
    select * from table where columnvalue in (7788, 7839, 7876)
    (7788, 7839, 7876) is an expression list and the predicate is evaluated as a membership condition.
    In
    select * from table where columnvalue in :P1_X
    :P1_X is a scalar string, incapable of containing multiple values.
    In an APEX standard report, a PL/SQL function body returning an SQL query report source with lexical substitution can be used to produce a "varying IN-list":
    return 'select * from table where columnvalue in (' || :P1_X || ')';
    where P1_X contains fewer than 1000 values, has been sanitized for SQL injection, and string values are properly quoted.
    Some people suggest the following approach, which will also work in APEX interactive reports:
    select * from table where instr(':' || :P1_X || ':', ':' || columnvalue || ':') > 0
    However this is non-performant as it eliminates the possibility of the optimizer using indexes or partition pruning in the execution plan.
    See varying elements in IN list on Ask Tom, and emulating string-to-table functionality using sql for efficient solutions.

  • Retreving Hexadecimal Values from a table using SQL query.

    Hi,
    I'm looking for a query to retreive Hexadecimal values contained in a column using SQL query.
    The column contains the values of all types, alphanumeric, numeric and hexadecimal.
    I need to retreive only hexadecimal values.
    any help to me in this regard will be appreciated.
    Thanks

    Presumably, you can be sure that any valid hex value is indeed MEANT to BE a hex value. For example the value 'ACE' is meant to be a hexadecimal value and not the word.

  • Function to check whether a value is NUMERIC

    Hi
    I have a requirment, where I need to check for a field whether it contains a valid numeric value or not.
    Could you please suggest a function.
    If I use TO_NUMBER(VAL1) it throws an exception in case of an non_numeric value.
    The requirement is as follows.
    If the field 'val1' contains a numeric value then the function should return the coressponding nueric value. If it is not,then return a boolean value are a return code instead of exception.
    Is there are built in function in ORACLE to acheive the same , please advice.

    Hi,
    The best way to do it is using User Defined Function, i have written a function as per your Requirement...It is give the data if it is numeric else it will return FALSE
    create table Testdata
    as
    select '10' n from dual
    union all
    select 'xx' from dual
    union all
    select '20' from dual
    union all
    select '3YY' from dual;
    CREATE OR REPLACE FUNCTION IsNumeric
    (p_col_value IN VARCHAR2)
    RETURN     VARCHAR2
    AS
    v_val NUMBER;
    BEGIN
    v_val := TO_NUMBER (p_col_value);
    RETURN 'TRUE';
    EXCEPTION
    WHEN VALUE_ERROR THEN
              RETURN 'FALSE';
         WHEN OTHERS THEN
              RETURN 'FALSE';
    END IsNumeric;
    Select Decode(IsNumeric(n),'TRUE',n,'FALSE') from Testdata
    Regards
    kamal

  • Writing plsql function to find out repeating character in a string

    I need to write one pl sql function to compare 2 consecutive character in a word that they are same or not, if same return false otherwise return true. could any one help me on this
    what i have written pasting here code or make me correct
    CREATE OR REPLACE FUNCTION validate_password (v_string IN VARCHAR2)
    RETURN BOOLEAN
    IS
    v_number NUMBER (3) := 1;
    i_length NUMBER (3);
    BEGIN
    SELECT LENGTH (v_string) INTO i_length FROM DUAL;
    IF (i_length > 0)
    THEN
    FOR v_number IN v_number .. i_length
    LOOP
    substring (v_string, v_number, 1) :=
    substring (v_string, v_number + 1, 1);
    v_number := v_number + 1;
    END LOOP;
    END IF;
    RETURN v_string;
    EXCEPTION
    WHEN OTHERS
    THEN
    DBMS_OUTPUT.put_line ('message');
    END;
    Thanks in advance.

    no need to SELECT FROM DUAL;
    How do I ask a question on the forums?
    SQL and PL/SQL FAQ
    scroll down to #9 to see how to use tags as below
    [code]
    CREATE OR replace FUNCTION Validate_password (v_string IN VARCHAR2)
    RETURN BOOLEAN
    IS
      v_number NUMBER (3) := 1;
      i_length NUMBER (3);
    BEGIN
        i_length  := Length (v_string) ;
        IF ( i_length > 0 ) THEN
          FOR v_number IN v_number .. i_length LOOP
              Substring (v_string, v_number, 1) :=
              Substring (v_string, v_number + 1, 1)
              v_number := v_number + 1;
          END LOOP;
        END IF;
        RETURN v_string;
    EXCEPTION
      WHEN OTHERS THEN
                 dbms_output.Put_line ('message');
    END;
    [/code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Getting run time value on form field by sql query and function

    Hi All,
    we have below requirement -
    create a form with fields as below -
    KRA_Type Grade Weightage Start Date End date
    Functinal E2 30 1-Jan-2010 1-jan-2011
    Organizational E2 70 1-jan-2010 1-jan-2011
    lov of kra_type has 2 values (Functinal, Organizational). requirement is to validate Weightage field such that for same grade :- Fuctional Weightage + Organizational Weightage = 100.
    please suggest on how to approch this.
    Thanks,
    Sam D

    this is not related to FORMS at all......
    1) create a database function which take grade parameter and return the total weightage.
    2) use it on when-validate-item trigger to validate
    create or replace function get_grade_weightage(p_grade varchar2)
    return number
    is
    v_grade number;
    begin
      select sum(Weightage)
       into v_weightage
      from mytable
    where grade = p_grade;
    exception when no_data_found then
       return 0;
    end;
    select get_grade_weightage(:grade)  into v_weigtage from dual;
    if :current_weigth < v_weightage then
        -- do something
    else
      --do something
    end if;Hope it helps
    plz mark the answer as correct or helpful if it is

  • How to check whether a value is empty in ABAP Sql

    Select matnr from mara where
    MARA~LVORM is null
    Basically if LVORM is blank, i.e., not popoulated, I want to get matnr.
    Will above query accomplish this in ABAP OPEN SQL.

    Hi,
    LVORM column is defined as a NOT NULL column.. so the values will be either a initial value (space, in this case) or filled with some value
    SELECT matnr
      INTO TABLE t_matnr
      FROM mara
      WHERE LVORM = space.
    https://cw.sdn.sap.com/cw/docs/DOC-40206
    http://learnsapmdm.blogspot.com/2009/01/data-modelling-and-database-design-in.html
    regards,
    Paulo Carvalho

  • SBO SQL Query outputs zero values as nulls but need the zeros

    Hi all,
    I'm having a problem with a query I'm writing in SBO where if the field contents are zero, these are output as null.
    I have a field for the stock usage on sales orders and ont too for production orders, but if I add these together they only provide a result if both columns contain a numeric value.
    I've tried adding "0" to the column value, adding and subtracting "1" and also multiplying the value by "1" but still no joy.
    I have also tried adding a "cast" statement to the column, but that doesnt seem to have an effect either (I think as this is more an output function).
    I need to be able to use these columns as separate displays in addition to being used in a further column as part of a calculation.
    If anyone has an idea how I can output a true zero value as opposed to a null value your help would be appreciated.
    Cheers,
    J

    Hello Julian,
    You could try to use a CASE Statement as I have shown below and test this will the SQL below.  If you use the T0.LineNum without any CASE or CAST the LineNum 0 will show as blank in SAP.
    SELECT
    CASE WHEN T0.LineNum != 0 THEN CAST(T0.LineNum AS VARCHAR(10)) ELSE '0' END AS 'Row Number',
    T0.ItemCode AS 'Item No.', T0.BaseQty AS 'Base Quantity', T0.PlannedQty AS 'Planned Quantity - Rows'  FROM  [dbo\].[WOR1\] T0

  • Changing A Value Using If Statement in SQL Query

    Hello, I have a very rudimentary question that I'm stuck on.  I have a query that returns one of 3 different integers.  I want to have each integer translate to a string value depending on the number returned.  Unfortunately this is the SCCM
    database which cannot be changed to provide an additional table with the values.
    I have something like this but I'm not getting what I want out of the date - depending on what I do I either get an error, or nothing.
    Declare @NewValue nvarchar
    Declare @Value nvarchar
    If @Value = 1 Set @Value = 'String1'
    If @Value = 2 Set @Value = 'String2'
    If @Value = 3 Set @Value = 'String3'
    Select PrimaryKey,IntegerValue FROM db_Table WHERE @Value=IntegerValue
    I know this is a simple thing but I'm new to SQL and not exactly sure what terms I should even be using to search for this.  Thanks for the help!

    select PrimaryKey, IntegerValue, case IntegerValue when 1 then 'String1'
    when 2 then 'String 2' when 3 then 'String3' else 'Unknown Integer Value' end as [Description]
    from db_Table
    Take a look at
    CASE expression in BOL. You may also find the following article helpful
    http://social.technet.microsoft.com/wiki/contents/articles/21327.t-sql-simplified-case-expression.aspx
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Set Page item value when paging through an sql query report

    I have a report that includes a checkbox to indicate which rows a process is to be run on. I have a "Select All" button that sets a value to check all boxes in the report, and deselect button to turn that feature off. The page only renders one of the buttons at a time depanding on the value. If the "Select All" is turned on, I would like to set the value to "OFF" when a user goes to another page of the report.
    For example:
    A user enters the page and the report is rendered. The rows returned are greater than what is displayed. The paging options for the report are shown. 1-15 16-30 etc. It is currently showing the first 15 rows of the report.
    The user clicks the select all button, and all checkboxes are now checked. Currently it will remain this way until the deselect button is clicked.
    When the user clicks the report paging, I would like to set the value of the Select All item to off.
    Does anyone know how to do this at this point?

    Hi,
    Try Scott's suggestion in Re: How to get query from region "PL/SQL function body returning string query"?
    Andy

  • Difference between values in 2 rows in SQL query

    I have to find the difference between inventory onhand in the same store in week1 and week2.
    I tried to do it in one "select" query something like :
    SELECT store_id, ( suppose to be function that calculates difference) FROM my_table WHERE week=week1 or week=week2 GROUP BY store_id
    however I have no idea how to find the difference between the valuues in two separate rows.
    thanks in advance.

    If you have only two weeks in your data. This can work.
    SQL> select * from emp
      2  /
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          7839 KING PRESIDENT 17-NOV-81 5000 10
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
          7566 JONES      MANAGER         7839 02-APR-81       2975                    20
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
          7900 JAMES      CLERK           7698 03-DEC-81        950                    30
          7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
          7369 SMITH      CLERK           7902 17-DEC-80        800                    20
          7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
          7876 ADAMS CLERK 7788 12-JAN-83 1100 20
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
          8888 KING PRESIDENT 17-NOV-81 7000 10
          9999 ADAMS CLERK 7788 12-JAN-83 7000 20
    16 rows selected.
    SQL> select ename, max(sal) - min(sal) diff
      2  from emp
      3  group by ename
      4  /
    ENAME            DIFF
    ADAMS 5900
    ALLEN               0
    BLAKE               0
    CLARK               0
    FORD                0
    JAMES               0
    JONES               0
    KING 2000
    MARTIN              0
    MILLER              0
    SCOTT               0
    SMITH               0
    TURNER              0
    WARD                0
    14 rows selected.
    SQL> Cheers
    Sarma.

  • Get first value and then remove remaining in query output

    hi all,
    we have a query where we want to count the number of sales orders and show this count by created on month and salesperson.  trick is that sales orders have multiple line items so the same order can show in multiple months. 
    lets say order 1000 has 3 line items.  each item is for month 1,2 and 3 of 2009.  the report is showing as such:
    1.2009 1
    2.2009 1
    3.2009 1
    each value represents the same order number.  we only want to see 1.2009 and then 2 and 3 show nothing because the counting of the order should happen 1X.  so the first month it finds an order number in, it counts it for that month and if it finds the same order for a later month then don't count it.
    is there a way to accomplish this?
    thank you

    Hi there,
    If you simply want to create count on sales order, then in query designer, just create a calculated key figure in that use option of formula variable,
    create one new formula variable of processing type replacement path and assign that sales order number characteristic to reference characteristic.
    in next replacement path tab
    Replace variable with - Infoobject
    Replace with  - Attributes
    Attributes - characteristic reference.
    For calculated key figure use aggregation after option.
    Hope this will help you.
    Regards,
    Rajdeep.

  • Get maximum value in a field using SQL query

    Hi,
    I want to retrieve the row containing the maximum value for a particular field.
    select max(fieldname) from tablename
    How do I do this using database connectivity toolkit.
    Shiva

    Hello,
    I figured out the answer. It was sql syntax.
    Thanks for coming in here to help.
    I created another post earlier because I had a little different question that pertained to fetch.vi.  How do you get an array out from fetch.vi without creating error. I also made type an array also but it does not allow an array.
    Thanks for your help.
    By the way, how do I as the original poster get rid of a whole thread I created, especially if its a duplicate.

  • Fusion HCM SaaS - How to find VO's behind screens & the VO's sql query

    I believe it's not currently possible for a customer to discover this information in SaaS......is it?
    I'm wondering if there’s a way we could get this information internally for a few major HCM screens. (such as the various "Manage Person screens")? This would make it much easier for partners, OCS & customers to write new data model's in BI Publisher and meet a number of reporting needs not currently served by the delivered subject areas.

    Hi Kiran,
    the data model (tables, views) are documented in Fusion Apps OER (https://fusionappsoer.oracle.com) but that doesn't necessarily help you understand how a particular screen derives its data.
    We have some blog posts out there that explain how to identify code artifacts:
    https://blogs.oracle.com/fadevrel/entry/finding_code_artifacts_for_customization
    https://blogs.oracle.com/fadevrel/entry/finding_code_artifacts_for_customization1
    But I don't think there is a way to achieve what you are looking for if all you have is access to a SaaS instance.

Maybe you are looking for

  • Application_id column in the Table FND_DOCUMENT_SEQUENCES?

    Hi, Can anyone clarify what does application_id column stands for in the Table FND_DOCUMENT_SEQUENCES? Does application_id stands for GL, AP, FA ...etc? If yes, then is there another table that explains this column in terms of a name rather than numb

  • Page items are cleared when calling javascript function for a buton.

    Hello, Can anyone please help me out with this issue. I have a reports region with some editable items and in which I have a button enter errors which opens up an popup window, but for this button i also have save process which updates the report and

  • Talent Management - TM - Trex 7.1 - Delta Indexing

    luke.....are you out there?? congrats on your blog being featured right on the SDN homepage... very nice... http://scn.sap.com/community/erp/hcm/blog/2010/09/20/setting-up-netweaver-embedded-search-trex-for-sap-ehp4-talent-management and congrats to

  • BI Content Not Installed

    HI , In ECC i found BI Content is not installed. Can any one tell me is there any need to install ? if we wont install what are effects we will go through. Explain on this............. Regards Kavitha

  • Cancel Task in BPEL human workflow

    All, We have a requirement to cancel the task for the user using human work flow. The worklist should be deleted for the user. Let me know if oracle human workflow provides an opertaion option to cancel the task like reassign,initaiate etc. Regards,