Use of FIRST_VALUE OVER in a PL/SQL query

Hello,
Here is my problem:
I'm trying to execute a query using FIRST_VALUE OVER in a PL/SQL procedure, e.g.
SELECT FIRST_VALUE (name) OVER (order by birthdate)
FROM birthday_table
WHERE location = 'HOME';
I need to get the value returned. I tried to do it using an INTO clause and
also with EXECUTE IMMEDIATE, but I get an error like "invalid column name".
Thank you,
Olivier.

Assuming the query runs successfully outside of PL/SQL, the execute immediate construct would be:
execute immediate 'select first_value ... where location = :loc' into v_some_variable using 'HOME';

Similar Messages

  • Can we use formula column in lexical parameter in sql query ...

    hi
    can we use formula column in lexical parameter in sql query ...
    as example
    i want to give
    select * from & c_table
    forumula
    function c_table
    if :p_sort = 1 then
    return 'dept'
    else
    return 'emp'
    end;
    c_table formula column
    is this possible ...
    i have such example in oracle apps reports
    if i try in ordinary report usinf emp table it show error ..
    how we can give formula column...
    please help me in this regard...
    Edited by: 797525 on Feb 20, 2012 9:31 PM

    thanks sir,
    iam not exactly saying select * from &c_table but some thing that like columns in select stmt also will be populated in user_parameters ,there are lot of table select.......from     mtl_demand md,     mtl_system_items msi,     mtl_txn_source_types     mtst,     mtl_item_locations loc     &C_source_from &C_from_cat
    &c_source_from and &c_from_cat formula column and there are defined at report level only ......
    pl/sql code &c_source_from is
    function C_source_fromFormula return VARCHAR2 is
    begin
    if :P_source_type_id = 2 then return(',MTL_SALES_ORDERS mkts');
    else if :P_source_type_id = 3 then return(',GL_CODE_COMBINATIONS gl');
    else if :P_source_type_id = 6 then return(',MTL_GENERIC_DISPOSITIONS mdsp');
    else if :P_source_type_id = 5 then
         if :C_source_where is null then
              return NULL;
         else
              return(',WIP_ENTITIES wip');
         end if;
    else if :P_source_type_id = 8 then return(',MTL_SALES_ORDERS mkts');
    else if :P_source_type_id is null then
    return(',MTL_SALES_ORDERS      mkts,
    MTL_GENERIC_DISPOSITIONS mdsp,
    GL_CODE_COMBINATIONS gl ');
    else null;
    end if; end if; end if; end if; end if; end if;
    RETURN NULL; end;
    this is forumula column i hope that you understand what iam saying
    please help me in this regard....
    thanking you...

  • How to get cm:search to use the max attribute when creating the SQL query?

    When we use the max attribute in the cm:search tag, it does not seem to honor the max attribute when creating the SQL query. However, the result returned from the tag is limited to the number specified by the max attribute. Then the tag seems to work as intended, but the performance will be sub optimal when the SQL query returns unnecessary rows to the application.
    We use the cm:search tag to list the latest news (ordered by date), and with the current implementation we have to expect a decrease in performance over time as more news is published. But we can’t live with that. We need to do the constraint in the SQL query, not in the application.
    The sortBy attribute of cm:search is translated to “order by” in the SQL query, as expected.
    Is it possible to get cm:search to generate the SQL query with an addition of “where rownum <= maxRows”?

    Hi Erik,
    The behavior of a repository in regards to the search tag's max results parameter is dependent on the underlying repository's implementation. That said, the OOTB repository in WLP does augment the generated SQL to limit the number of rows returned from the database. This is done in the parsing logic. This behavior may differ with other repository implementations.
    -Ryan

  • Clarification on using function in where clause of oracle sql query

    I have an issue in regarding function using where clause of sql query..
    We are facing performance issue while executing query, so in what ways to improve the performance of the query which i have posted below.
    select col ,case when my_function(parameter)
    from tab1 a ,tab2 b,tabl3 c
    where a.column1=b.column2
    and b.column3 =c.column6
    and my_function(parameter)>0
    Regards
    Dinesh
    Edited by: wild fire on May 18, 2012 4:15 PM

    Dinesh,
    remind that when you use a function in the where clause it normally will get started for each record in the table.
    So your answer is two-fold:
    1. make the function only start when needed by adding a function based index on the table (this will make inserts and updates slower)
    2. make the function faster by adding the DETERMINISTIC clause if possible. this will make Oracle "cache" the result of the function.
    Regards,
    Richard
    blog: http://blog.warp11.nl
    twitter: @rhjmartens
    If this question is answered, please mark the thread as closed and assign points where earned..

  • Using variable as table name in pl/sql query - Is possible?

    I am relatively new to PL/SQL and I am trying to create a function that accepts a table name and a rowid as arguments and returns a comma-delimited record string of the values of the table/rowid being passed. The problem is , I cannot code a select stmt as follows
    SELECT * FROM v_table_name
    WHERE rowid = v_row_id
    in PL/SQL. There must be a easy way to approach this.
    Thanks for any and all advice.
    GC

    I don't understand the use of the concat symbol along with the commas and field namesYou just need (if you really need it) to create variable which contains you column separated by comma:
    Simple example (in the second case separate variables are used to create
    the list of columns):
    SQL> declare
      2   rc sys_refcursor;
      3   cols varchar2(200) := 'ename, empno, sal';
      4   tab varchar2(30) := 'emp';
      5  begin
      6   open rc for 'select ' || cols || ' from ' || tab;
      7   close rc;
      8  end;
      9  /
    PL/SQL procedure successfully completed.
    SQL> declare
      2   rc sys_refcursor;
      3 
      4   ename_col varchar2(10) := 'ename';
      5 
      6   empno_col varchar2(10) := 'empno';
      7 
      8   sal_col varchar2(10) := 'sal';
      9 
    10   tab varchar2(30) := 'emp';
    11 
    12  begin
    13 
    14   open rc for 'select ' || ename_col || ',' || empno_col || ',' || sal_col ||
    15   ' from ' || tab;
    16 
    17   close rc;
    18  end;
    19  /
    PL/SQL procedure successfully completed.Rgds.

  • Guidance on use of "COUNT(*) OVER () * 5" in a select query.

    Hello Friends,
    I was reading one article, in which one table was created for demo. Following was the statements for there.
    CREATE TABLE source_table
    NOLOGGING
    AS
    SELECT ROWNUM AS object_id
    , object_name
    , object_type
    FROM all_objects;
    INSERT /*+ APPEND */ INTO source_table
    SELECT ROWNUM (COUNT(*) OVER () * 5)+ AS object_id
    , LOWER(object_name) AS object_name
    , SUBSTR(object_type,1,1) AS object_type
    FROM all_objects;
    INSERT /*+ APPEND */ INTO source_table
    SELECT ROWNUM (COUNT(*) OVER() * 10)+ AS object_id
    , INITCAP(object_name) AS object_name
    , SUBSTR(object_type,-1) AS object_type
    FROM all_objects;
    Can anyone please tell me the purpose of *"ROWNUM + (COUNT(*) OVER () * 5)"* in above 2 insert statements, or suggest me some document on that.
    I don't know about its usage, and want to learn that..
    Regards,
    Dipali..

    The insert statements that you have listed are using Oracle Analytic Functions. Some examples of these functions can be found here: [Oracle Analytic Functions|http://www.psoug.org/reference/analytic_functions.html|Oracle Analytic Functions]
    Effectively what that says is the following:
    1. "COUNT(*) OVER ()" = return the number of rows in the entire result set
    2. Multiply that by 5 (or 10 depending on the insert)
    3. Add the current ROWNUM value to it.
    This can be shown with a simple example:
    SQL&gt; edit
    Wrote file sqlplus_buffer.sql
      1  SELECT *
      2  FROM
      3  (
      4     SELECT ROWNUM r,
      5             (COUNT(*) OVER ()) AS ANALYTIC_COUNT,
      6             5,
      7             ROWNUM + (COUNT(*) OVER () * 5) AS RESULT
      8     FROM all_objects
      9  )
    10* WHERE r &lt;= 10
    SQL&gt; /
             R ANALYTIC_COUNT          5     RESULT
             1          14795          5      73976
             2          14795          5      73977
             3          14795          5      73978
             4          14795          5      73979
             5          14795          5      73980
             6          14795          5      73981
             7          14795          5      73982
             8          14795          5      73983
             9          14795          5      73984
            10          14795          5      73985
    10 rows selected.
    SQL&gt; SELECT COUNT(*) from all_objects;
      COUNT(*)
         14795Hope this helps!
    Note the the statements you provided will not actually execute because of the extra "+" signs on either side. I have removed them.

  • How to use property file - sql query define in property file

    Hi All,
    Anybody please tell me how to use property file.
    I have placed sql query in propery file and I have to access this in my file.
    well so far this is my code but don't know how to implement in the following ...
    pstmt = con.prepareStatement("select * from registration where username=?");
    instead of writting the query I want to use the property file.
    so far I have developed the following code...
    FileInputStream fis = new FileInputStream("querysql.property");
    Properties dbProp = new Properties();
    dbProp.load(fis);is the code correct... or is there another way to access property file
    Please help.
    please reply soon....
    Thanks

    Before answering, check if it's already been done here http://www.jguru.com/forums/view.jsp?EID=1304182

  • Using System Variables in a SQL Query

    Hi!
    I´m new to Oracle and SQL so i don´t know very much about it.
    There is miy Problem:
    Is there a way to Use the System Variable %USERNAME% in an SQL Query?
    I tried .... where table.shorttag = '%USERNAME%' ;
    but this doesnt work.
    Is Oracle able to handle Systemvariables? Or is there another way to use the current Windows User in an SQL Query.
    It is very Important to use the current Windows user and not the Oracle user.
    Thank you,
    Mfg

    Are you looking for something like this ?
    SQL> SELECT sys_context('USERENV', 'OS_USER') FROM dual;
    SYS_CONTEXT('USERENV','OS_USER
    SAUBBANE\IBM
    SQL> SELECT sys_context('USERENV', 'TERMINAL') FROM dual;
    SYS_CONTEXT('USERENV','TERMINA
    SAUBBANEAlso you can look at the dbms_application_info package.

  • How to replace variable value of a sql query dynamically

    Hi all,
    Trying to execute sql queries present in Oracle table and the query it self contains the ODI variable #EMP_NO.
    At the run time we thought of passing the variable values.
    table contains query like
    update table <table name> set <column name>= 5000 where emp_no = #EMP_NO
    We need to execute the sql query using a ODI procedure, to pick the sql query from table we use another variable #varSQLID and in the command on source tab wrote like
    select sql_query varSQL from emp_sql where query_id=#varSQLID
    Command on target
    #varSQL
    when ever I tried to execute I am getting error please let me know what changes are required .
    The question is whether it's possible to replace the variable value present in the above sql ?
    regards,
    Palash Chatterjee

    Thanks Siddhartha,
    Actually we have been doing the same, means half of the query has been kept in the table and rest is being added in the " command on source " tab of the procedure .
    But the problem is , the "where" condition is not same for all SQL queries,
    to provide 27 different sqls queries for 27 packages we have to create 27 procedures (as "where" condition may take other column values along with "EMPNO" for the JOIN condition ) .
    Any suggestion of keeing SQL along with the variable and storing it in the table may resolve our problem and work of 27 procedures can be performed by a single procedure .
    regards,
    Palash Chatterjee

  • SQL Subscription field shows * only with Dynamic Text Label in SQL query

    We are using Hyperion Analyzer 7.2.x for showing budget and actual data. I have to show this financial data based on the security e.g. person in IT can see only IT dept. data. Hence I want to use dynamic text label <<userid>> for the security based on the person logging in to Analyzer.
    But when I use dynamic text label <<userid>> in the SQL query in SQL Spreadsheet, SQL Subscription field shows * only selection option. Does anyone have idea how to solve this problem?
    Thanks in advance for your help.
    -SV

    Hi
    Okay i know this is a bit crazy way.....but i think this is the solution for your issue.
    Create a report without the where clause (<<useris>>) then add a filter (sql subscription) then you can find all the values that are there in the SQL field (try to increase the query limit it is set to 250 as default) then edit the spreadsheet and add the where clause (<<userid>>).
    This will help you having the filter and the dynamic text label. I think there is an issue when you try to filter it with a where clause.
    Hope it helps.
    CK

  • Error in executing SQL query

    Hi All,
    I am working with labview application.
    The database being used is Access.
    The tables i have to use contains the special symbol -   (not underscore.)
    Let the table name be user-data
    When i am executing the query select * from user-data its showing
    error message syntax error in from clause.
    I tried just typing the query in access also and got the same error.
    I believe its because of the usage of the special character other than underscore in table name
    Many applications using the same database is running in VB already.So there is no way for me to rename the table.
    I know i am asking an invalid question.But still....
    Is there any way for me to use the same table name and execute sql query.
    Thanks in advance

    There should be. Try putting the table name in quotes - some DBMS use single quotes, some use double quotes. For example say the table name has a space in it. Try something like:
    select * from 'a funny table';
    or
    select * from "a funny table";   (Actually, I think I'd try this one first...)
    Failing this, you have two choices: change the name of the table, or dump Access and go with a real DBMS - which you will probibly want to do anyway because it will only be a matter of time before you start hitting a performance wall. Access is particularly bad if you have more than one process making a connection to it at a time. Interms of alternatives, there are a couple really good open-source products out there, as well as no-cost versions of Oracle and SQL-Server.
    Mike...
    Certified Professional Instructor
    Certified LabVIEW Architect
    LabVIEW Champion
    "... after all, He's not a tame lion..."
    Be thinking ahead and mark your dance card for NI Week 2015 now: TS 6139 - Object Oriented First Steps

  • SEQUENCE Select within an SQL Query with an ORDER BY statement

    Does anyone know why you cannot use an ORDER BY statement within an SQL query when also selecting from a SEQUENCE? My query was selecting production data as a result of a filtered search. I want to take the results of the filtered search and create a transaction. I have a sequence for generating transaction numbers where I select NEXTVAL. Since I could possibly obtain multiple, yet distinct, rows based upon my search criteria, I wanted to use an ORDER BY statement to simplify and order the resulting row(s).
    I was able to get the SQL select with SEQUENCE.NEXTVAL to work without the ORDER BY, so I know that my SQL is correct. Thanks in-advance.

    Okay,
    I understand. You want the sequence assigned first and the you want to ORDER BY. You
    can do this using pipelined functions. See here:
    CREATE OR REPLACE TYPE emp_rec_seq AS OBJECT (
       seq     NUMBER,
       ename   VARCHAR2 (20),
       job     VARCHAR2 (20),
       sal     NUMBER
    CREATE OR REPLACE TYPE emp_tab_seq AS TABLE OF emp_rec_seq;
    CREATE OR REPLACE FUNCTION get_emp_with_sequence
       RETURN emp_tab_seq PIPELINED
    IS
       my_record   emp_rec_seq := emp_rec_seq (NULL, NULL, NULL, NULL);
    BEGIN
       FOR c IN (SELECT dummy.NEXTVAL seq, ename, job, sal
                   FROM emp)
       LOOP
          my_record.seq := c.seq;
          my_record.ename := c.ename;
          my_record.job := c.job;
          my_record.sal := c.sal;
          PIPE ROW (my_record);
       END LOOP;
       RETURN;
    END get_emp_with_sequence;after that, you can do a select like this:
    SELECT seq, ename, job, sal
      FROM TABLE (get_emp_with_sequence)
      order by enamewhich will get you this:
           SEQ ENAME                JOB                         SAL
          1053 BLAKE                MANAGER                    2850
          1054 CLARK                MANAGER                    2450
          1057 FORD                 ANALYST                    3000
          1062 JAMES                CLERK                       950
          1055 JONES                MANAGER                    2975
          1052 KING                 MANAGER                   20000
          1060 MARTIN               SALESMAN                   1250
          1063 MILLER               CLERK                      1300
          1064 DKUBICEK             MANAGER                   12000
          1056 SCOTT                ANALYST                    3000
          1058 SMITH                CLERK                       800
          1061 TURNER               SALESMAN                   1500
          1059 WARD                 SALESMAN                   1250Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://htmldb.oracle.com/pls/otn/f?p=31517:1
    -------------------------------------------------------------------

  • APEX 4.1, SQL Query(Updateable report), Validation issue.

    Hi,
    I am using APEX 4.1.
    I have SQL Query(Updateable report), we have created validation for the columns in this report.
    The validations are working properly only for the first row of the report on submitting, the remaining rows are not getting validated.
    If we check mark the rows it will get validated, but we want the validation to happen without checkmarking, on all the rows on clicking submit button.
    Can someone help me to fix this issue?
    Thanks in advance.
    Thanks & regards,
    Ravi.

    Hi Ravi,
    Welcome to Oracle Forums!
    Please acquaint yourself with the FAQ and forum etiquette if you haven't already done so.
    Always state
    <ul>
    <li>Apex Version</li>
    <li>DB Version and edition</li>
    <li>Web server used.I.e. EPG, OHS, ApexListner Standalone or with J2EE container</li>
    <li>When asking about forms always state tabular form if it is a tabular form</li>
    <li>When asking about reports always state Classic / IR</li>
    <li>Always post code snippets enclosed in a pair of &#123;code&#125; tags as explained in FAQ</li>
    </ul>
    I am using APEX 4.1.I have SQL Query(Updateable report), we have created validation for the columns in this report.
    The validations are working properly only for the first row of the report on submitting, the remaining rows are not getting validated.
    If we check mark the rows it will get validated, but we want the validation to happen without checkmarking, on all the rows on clicking submit button.
    Can someone help me to fix this issue?
    >
    Post your validation code with some explanations of what the g_fnn are.
    Cheers,

  • Import oracle.xml.sql.query

    what file do I need to use this class?
    Thanks.

    oracle.xml.sql.query is a package.
    Please refer
    http://www.devx.com/xml/Article/32046

  • Trouble referencing BI Publisher parameters in SQL Query (nQSError: 46033)

    Hello everyone,
    I have a query that references some Parameters I created in BI Publisher. They are:
    grade_var of type String
    school_var of type String
    school_year of type String
    The SQL used to for the data model of the report is:
    SELECT "KW School Year"."School Year", "KW Grade"."Grade Seq Nbr", "KW Grade"."Grade Desc", "KWC Enrolment"."Student Count", +(case WHEN "KW School"."School Name" = 'Campbell Collegiate' then "KW School"."School Name" else 'Z - Other School' end)+
    FROM "Sask DW"
    WHERE ("KW Student"."Student Nbr" IN
    (SELECT "KW Student"."Student Nbr"
    FROM "Sask DW"
    WHERE ("KW School"."Post Secondary Institute Ind" = 'N')
    AND ("KW Enrolment Outcome"."Enrolment Outcome" NOT IN ('Deceased', 'Transferred to out-of-country schooling', 'Transferred to out-of-province schooling'))
    AND ("KW Grade"."Grade Desc" = :grade_var)
    AND ("KW School"."School Name" = :school_var)
    AND ("KW School Year"."School Year" = :school_year_var)) )
    AND ("KW School"."Post Secondary Institute Ind" = 'N')
    AND ("KW Enrolment Outcome"."Enrolment Outcome" NOT IN ('Deceased', 'Transferred to out-of-country schooling', 'Transferred to out-of-province schooling'))
    AND +("KW School Year"."School Year" >= '2005/2006')+
    The above query works great and I can get back results and throw them into a template and it works great. The problem comes when I want to change what is highlighted in italics text. School Name and School Year values are hard coded into the SQL but I want those to reference the value of the BI Publisher parameter. It seems to work fine in the bolded part but when I put the paramater name into the italics part, I get the error message:
    java.io.IOException: prepare query failed[nQSError: 46033] Datatype: 25 is not supported.
    The query I am trying to submit is (changes highlighted in bold):
    SELECT "KW School Year"."School Year", "KW Grade"."Grade Seq Nbr", "KW Grade"."Grade Desc", "KWC Enrolment"."Student Count", *(case WHEN "KW School"."School Name"=:school_var then "KW School"."School Name" else 'Z - Other School' end)*
    FROM "Sask DW"
    WHERE ("KW Student"."Student Nbr" IN
    (SELECT "KW Student"."Student Nbr"
    FROM "Sask DW"
    WHERE ("KW School"."Post Secondary Institute Ind" = 'N')
    AND ("KW Enrolment Outcome"."Enrolment Outcome" NOT IN ('Deceased', 'Transferred to out-of-country schooling', 'Transferred to out-of-province schooling'))
    AND ("KW Grade"."Grade Desc" = :grade_var)
    AND ("KW School"."School Name" = :school_var)
    AND ("KW School Year"."School Year" = :school_year_var)) )
    AND ("KW School"."Post Secondary Institute Ind" = 'N')
    AND ("KW Enrolment Outcome"."Enrolment Outcome" NOT IN ('Deceased', 'Transferred to out-of-country schooling', 'Transferred to out-of-province schooling'))
    AND ("KW School Year"."School Year" >= :school_year_var)
    The parameters are all of type string and all the columns I am comparing them to are VARCHAR. What doesn't make sense is that the parameters can be compared to the columns in the middle of the SQL but when I try a similar comparison near the beginning and at the end, I get the error shown above.
    Any thoughts/tips?
    Thanks!
    Edited by: jmoe316 on Oct 21, 2009 9:43 AM
    Edited by: jmoe316 on Oct 21, 2009 9:49 AM

    Hi Jared,
    Did you have any success with the syntax suggested? I am having the same issue and would like to know if using the parameter as part of the SQL query select statement is feasible. The goal is to have a column dynamically selected based on a parameter value. My example:
    SELECT
    CASE
    WHEN :p_name = 'Joe' THEN table1.column_a
    WHEN :p_name = 'Tom' THEN table1.column_b
    ELSE table1.column_c
    END
    FROM ...
    Regards
    Edited by: user11929404 on Mar 14, 2011 1:18 PM
    Edited by: user11929404 on Mar 14, 2011 1:20 PM

Maybe you are looking for