Selecting a small number of rows

Hello,
I am looking for something in PL/SQL that will allow me to return a limited number of rows. In other SQL's this might be something like:
set row count=100
select * from tab1 --- returns first 100 rows found

SQL> select * from emp;
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
      7566 JONES      MANAGER         7839 02-APR-81       2975       1000         20
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      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
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 12-JAN-83       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
14 rows selected.
SQL> select e.empno, e.ename, e.job, e.hiredate
  2    from (select rownum rn, empno, ename, job, hiredate
  3            from emp) e
  4   where e.rn <= 10;
     EMPNO ENAME      JOB       HIREDATE
      7566 JONES      MANAGER   02-APR-81
      7902 FORD       ANALYST   03-DEC-81
      7839 KING       PRESIDENT 17-NOV-81
      7698 BLAKE      MANAGER   01-MAY-81
      7782 CLARK      MANAGER   09-JUN-81
      7369 SMITH      CLERK     17-DEC-80
      7499 ALLEN      SALESMAN  20-FEB-81
      7521 WARD       SALESMAN  22-FEB-81
      7654 MARTIN     SALESMAN  28-SEP-81
      7788 SCOTT      ANALYST   09-DEC-82
10 rows selected.
SQL>

Similar Messages

  • Select  a specific  number of rows in query

    How can a specific number of rows be selected in a query? For example, a query retrieves 30,000 records and I want to retrieve the output by groups of 5,000. I want the query to retrieve the first 5,000 records, then the next 5,000 records etc. I tried rownum but that does not work.
    Thanks,
    PANY

    Not AGAIN. Please...........
    Do you know how to Google? Search forum?
    Why do you ask this boring FAQ AGAIN?
    Sybrand Bakker
    Senior Oracle DBA
    Experts: Those who know how to search.

  • Dynamic Select with different number of rows

    Hi all,
    I have a procedure who receives a table name and array of column names of that table.
    I need to create a specific select statement with this parameters. Of course, this could change because the number of column names may differ from each invocation the this procedure.
    Here's my procedure:
    PROCEDURE column_scramble (pv_table_name
    VARCHAR2,
    ptbl_col_names
    tbl_col_names)
    IS
    lv_query
    VARCHAR2 (32000);
    cur_handle   INTEGER;
    temp    
    NUMBER;
    BEGIN
    IF ptbl_col_names.COUNT > 0
    THEN
    lv_query := 'SELECT old.id, ';
    FOR i IN ptbl_col_names.FIRST .. ptbl_col_names.LAST
    LOOP
    lv_query :=
    lv_query
    || 'new.'
    || ptbl_col_names
    || ' as new_'
    || ptbl_col_names (i);
    IF i < ptbl_col_names.LAST
    THEN
    lv_query := lv_query || ',';
    END IF;
    END LOOP;
    lv_query :=
    lv_query
    || ' FROM (  SELECT ROWNUM rr, '
    || pv_table_name
    || '.* FROM '
    || pv_table_name
    || ' ORDER BY '
    || pv_table_name
    || '.id) old, (SELECT ROWNUM rr, a.* FROM (  SELECT id,';
    FOR j IN ptbl_col_names.FIRST .. ptbl_col_names.LAST
    LOOP
    lv_query := lv_query || ptbl_col_names (j);
    IF j < ptbl_col_names.LAST
    THEN
    lv_query := lv_query || ',';
    END IF;
    END LOOP;
    lv_query :=
    lv_query
    || ' FROM '
    || pv_table_name
    || ' ORDER BY DBMS_RANDOM.VALUE) a) new WHERE old.rr = new.rr';
    END IF;
    DBMS_OUTPUT.put_line ('LV_QUERY: ' || lv_query);
    cur_handle := DBMS_SQL.open_cursor;
    DBMS_SQL.parse (cur_handle, lv_query, DBMS_SQL.native);
    temp := DBMS_SQL.execute (cur_handle);
    LOOP
    IF DBMS_SQL.fetch_rows (cur_handle) = 0
    THEN
    EXIT;
    ELSE
    lv_query := 'UPDATE ' || pv_table_name || 'SET ';
    FOR s IN 1 .. ptbl_col_names.COUNT
    LOOP
    lv_query :=
    lv_query
    || ptbl_col_names (s)
    || '='
    || <return_value(s)>;
    IF s < ptbl_col_names.COUNT
    THEN
    lv_query := lv_query || ',';
    END IF;
    END LOOP;
    lv_query := lv_query || ' WHERE id = ' || <return_value_id>;
    END IF;
    END LOOP;
    DBMS_SQL.close_cursor (cur_handle);
    END column_scramble;
    Thanks in advance,
    Filipe

    Avoid using the "cumbersome" DBMS_SQL, try this:
    CREATE OR REPLACE TYPE tbl_col_names IS VARRAY (100) OF VARCHAR2 (40);
    CREATE OR REPLACE PROCEDURE column_scramble
        ( pv_table_name VARCHAR2, ptbl_col_names tbl_col_names
        , pmy_cursor OUT SYS_REFCURSOR)  --<<< NOTE: I added this parameter
    IS
      lv_query     VARCHAR2 (32000);
      cur_handle   INTEGER;
      temp         NUMBER;
    BEGIN
      IF ptbl_col_names.COUNT > 0
      THEN
        lv_query   := 'SELECT old.id, ';
        FOR i IN ptbl_col_names.FIRST .. ptbl_col_names.LAST
        LOOP
          lv_query      := lv_query || 'new.' ||  ptbl_col_names ( i)
                        ||  ' as new_' || ptbl_col_names ( i);
          IF i < ptbl_col_names.LAST
          THEN
            lv_query   := lv_query || ',';
          END IF;
        END LOOP;
        lv_query      := lv_query ||
          ' FROM (  SELECT ROWNUM rr, ' ||  pv_table_name || 
                  '.* FROM ' ||  pv_table_name ||
                   ' ORDER BY ' || pv_table_name ||'.id) old
                 , (SELECT ROWNUM rr, a.* FROM (  SELECT id,';
        FOR j IN ptbl_col_names.FIRST .. ptbl_col_names.LAST
        LOOP
          lv_query   := lv_query || ptbl_col_names ( j);
          IF j < ptbl_col_names.LAST
          THEN
            lv_query   := lv_query || ',';
          END IF;
        END LOOP;
        lv_query      := lv_query || ' FROM ' || pv_table_name ||
                       ' ORDER BY DBMS_RANDOM.VALUE) a) new WHERE old.rr = new.rr';
      END IF;
      DBMS_OUTPUT.put_line ( 'LV_QUERY: ' || lv_query);
      OPEN pmy_cursor FOR lv_query;
    END;
    -- And to execute do this:
    VAR my_cursor REFCURSOR;
    DECLARE
      emp_col   tbl_col_names;
    BEGIN
      emp_col      :=
        tbl_col_names (
                       'DEPTNO'
                     , 'MGR'
                     , 'EMPNO'
                     , 'ENAME'
      column_scramble ( 'EMP3', emp_col, :my_cursor);
    END;
    PRINT my_cursor;
    PL/SQL procedure successfully completed.
            ID NEW_DEPTNO    NEW_MGR  NEW_EMPNO NEW_ENAME
             1         10                  7839 KING     
             2         20       7566       7788 TIGER    
             3         30       7698       7499 ALLEN    
             4         10       7839       7782 CLARK    
             5         20       7566       7502 TIGER    
             6         30       7698       7654 MARTIN   
             7         30       7839       7698 BLAKE    
             8         20       7839       7566 JONES    
             9         20       7566       7788 SCOTT    
            10         20       7788       7876 ADAMS    
            11         30       7698       7521 WARD     
            12         20       7902       7369 SMITH    
            13         30       7698       7844 TURNER   
            14         30       7698       7501 BRWN     
    14 rows selected.

  • Selecting only required number of rows in an external table

    Hi,
    I have an external table with many flat files as source.. I know I can skip rows using the keyword 'SKIP'. I want to select only first 'n' rows from all the flat files as the rows available in my oracle external table. Is this Possible?...
    eg..
    Flat file 1
    c1,c2
    123,45
    132,56
    'ahgh',34
    'dfd',22
    Flat file 2
    c1,c2
    56,1212
    545,45
    'ahcsd',4
    'dds',24
    I want to create a file which contains the rows, means only the 2nd and 3rd rows from each flat files
    123,45
    132,56
    56,1212
    545,45
    Thanks in Advance
    Poulose

    If this means selecting from all these flat files at the same time then it might get a bit cumbersome. Basically you will have to create an external table for each flat file and then do something like this:
    SELECT * FROM
      ( select c1, c2 from
         ( select c1, c2, rownum as rn from ext_table_1 )
        where rn in (2, 3)
       union all
        select c1, c2 from
         ( select c1, c2, rownum as rn from ext_table_2 )
        where rn in (2, 3)
       union all
      select c1, c2 from
         ( select c1, c2, rownum as rn from ext_table_3 )
        where rn in (2, 3)
       union all
    /Cheers, APC

  • How to control the number of rows selected from a sybase database table?

    Hi, JDBC guru,
    I'm working on project using Sybase 11.9.x database.We need to process a record in a table,first select it from the table,then do some computing,finally write it to a dbf file, if the above steps succeed,remove the original row in the table.Quite evidently, all these operation should be put into one transaction. But how can I select just a number of rows from the sybase database table?
    Any hints?
    Thank you in advance.
    Regards,
    Jusitne

    Statement stmt...
    stmt.setMaxRows(20);

  • How to control number of rows displayed in a report  by user?

    Hi All!
    I have provided a select list called 'Number of rows' in my report page.User can choose any number in the list, and accordingly he should be able to see so many number of rows in that report page.
    For example: if user selects 50 from the list, he will be able to see 50 records at a time out of say 2000 records retrieved. I want to control number of rows displayed per page dynamically. And if he selects 25 in the select list, he should be able to see 25 records at a time out of 2000.
    How to do it? Please give me ideas.
    Thanking in advance.
    Regards,
    Deepika.

    Hi Deepika,
    If you have a Select List with Submit called, say "P1_ROWS", you just enter P1_ROWS into the Report Attribute's "Number of Rows (Item)" setting.
    Regards
    Andy

  • Select a set of random rows

    I don't understand how the sample clause works on the select statement. Eg. SELECT * FROM clients SAMPLE(50) should return half of all rows in the table, but it's unstable. Sometimes it returns 4, sometimes 3 and even 2 (SELECT COUNT(*) FROM clients returns 6). Why???
    So, if it's really unstable, how do I select a random number of rows from a query? Is there other way?

    As noted, the number of rows returned by SAMPLE can vary. The approach I use is to set the sample number high enough so that the minimum number of rows it returns is always higher than what you want, and then chop off the excess with a rownum filter:
    sql>select empno, ename, job
      2    from emp sample (50)
      3   where rownum <= 3;
        EMPNO ENAME      JOB
         7499 ALLEN      SALESMAN
         7654 MARTIN     SALESMAN
         7839 KING       PRESIDENT
    3 rows selected.
    sql>/
        EMPNO ENAME      JOB
         7566 JONES      MANAGER
         7698 BLAKE      MANAGER
         7782 CLARK      MANAGER
    3 rows selected.
    sql>/
        EMPNO ENAME      JOB
         7499 ALLEN      SALESMAN
         7566 JONES      MANAGER
         7782 CLARK      MANAGER
    3 rows selected.
    sql>/
        EMPNO ENAME      JOB
         7369 SMITH      CLERK
         7521 WARD       SALESMAN
         7839 KING       PRESIDENT
    3 rows selected.

  • How to set total number of rows of table control

    Hi All,
    I want to set the total number of rows of table control. I don't want to display the blank rows in table.
    Regards,
    Shrinivas

    Need some fine tuning regarding the scrollbar height but it's a start
    Unless this isn't what you asked for
    Small number of rows
    Large number of rows
    Attachments:
    PlayingWithTable.vi ‏17 KB

  • How to get number of rows return in SELECT query

    i'm very new in java, i have a question:
    - How to get number of rows return in SELECT query?
    (i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
    Thanks.

    make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
             String sql = "select * from testing";
             PreparedStatement ps =
              con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
             ResultSet rs = ps.executeQuery();
             rs.last();
             System.out.println("Row count = " + rs.getRow());
             rs.beforeFirst();~Tim
    NOTE: Ugly, but does the trick.

  • How can I limit the number of rows returned by a select stat

    How can I limit the number of rows returned by a select
    statement. I have a query where I return the number of stores
    that are located in a given area.. I only want to return the
    first twenty-five stores. In some instances there may be over
    200 stores in a given location.
    I know is SQL 7 that I can set the pagesize to be 25....
    Anything similiar in Oracle 8i?
    null

    Debbie (guest) wrote:
    : Chad Nale (guest) wrote:
    : : How can I limit the number of rows returned by a select
    : : statement. I have a query where I return the number of
    : stores
    : : that are located in a given area.. I only want to return the
    : : first twenty-five stores. In some instances there may be
    : over
    : : 200 stores in a given location.
    : : I know is SQL 7 that I can set the pagesize to be 25....
    : : Anything similiar in Oracle 8i?
    : If you are in Sql*Plus, you could add the statement
    : WHERE rownum <= 25
    : Used together with an appropriate ORDER BY you
    : could get the first 25 stores.
    Watch out. ROWNUM is run before ORDER BY so this would only
    order the 25 selected
    null

  • Can i get a number of rows in the select list??

    Hello~
    I am studying the "oracle call interface". but i don't know much about oci.
    Anyway i want to know that how can i get a number of rows.
    for example, there is a source code of the number of colums in the select list.
    The following is a list of DB(example)
    /*DB table*/
    ID NAME CODE ID
    1 A 1 A
    2 B 2 B
    /*source*/
    err = OCIAttrGet ((dvoid *) stmhp, (ub4)OCI_HTYPE_STMT, (dvoid*)
    &parmcnt, (ub4 *) 0, (ub4)OCI_ATTR_PARAM_COUNT, errhp);
    /*result*/
    The Column is 4. (ID, NAME, CODE, ID)
    So, i think that the row is 3 (ID, 1, 2) in the DB table.
    Simply, Can i get a number of rows in the select list??
    for example,
    err = OCIAttrGet ((dvoid *) stmhp, (ub4)OCI_HTYPE_STMT, (dvoid*)
    &parmcnt, (ub4 *) 0, (ub4)OCI_ATTR_RAW_COUNT, errhp);
    I'm trying to get the number of row count. but i can't find.
    Please, could you let me know that how can i get the number of row count.
    Thank you.

    Thank you for your reply.
    we are tested the source code by reply.
    The following is the test source code.
    /*source code*/
    strcpy (szStatement, "SELECT * from DB_TABLE");
    OCIStmtPrepare(gpOCIReadStmHandle, gpOCIErrHandle, szStatement,
         strlen(szStatement), OCI_NTV_SYNTAX, OCI_DEFAULT);
    OCIStmtExecute(gpOCISvcHandle, gpOCIReadStmHandle, gpOCIErrHandle,
         0, 0, 0, 0, OCI_DEFAULT);
    /* get a number of rows count , Reply : prajithparan*/
    OCIAttrGet((dvoid *)gpOCIReadStmHandle, OCI_HTYPE_STMT, (dvoid *)&nRowCount, NULL, OCI_ATTR_ROW_COUNT, gpOCIErrHandle))
    But There is a problem of the result value.
    The result value is 0. It's mean that the row count is 0. and then we are using the all of fuction is succeed.(return value)
    I don't know what is problem. Please let me know about the problem and solution.
    Thank you.

  • Report Script - How to get the total on the any number of rows selected

    Hi,<BR>I am using Essase and report script for the reporting purpose. In this i would like to know how to get the sum of all the rows selected. I know we can use the calculate row, but it has to be done with print row to display the newly created row. But i am using alphablox where this print row is not working. Is there any other alternative is there to get the sum of any number of rows, and displaying them??<BR>Please help me.<BR><BR>Regards<BR>R.Prasanna

    Pls check the structure and index.
    EMP_ID NUMBER(9) NOT NULL,
    EMP_CD           NUMBER(3),
    EMP_NO VARCHAR2(250 BYTE) NOT NULL,
    FROM_EFF_DT DATE NOT NULL,
    TO_EFF_DT DATE,
    CREATE INDEX IDX_EMP_NO ON EMP
    (EMP_NO)
    NOLOGGING
    TABLESPACE INDEXES
    PCTFREE 20
    INITRANS 10
    MAXTRANS 255
    STORAGE (
    INITIAL 56K
    NEXT 1784K
    MINEXTENTS 1
    MAXEXTENTS 2147483645
    PCTINCREASE 0
    FREELISTS 4
    FREELIST GROUPS 1
    BUFFER_POOL DEFAULT
    NOPARALLEL;

  • Number of rows inserted is different in bulk insert using select statement

    I am facing a problem in bulk insert using SELECT statement.
    My sql statement is like below.
    strQuery :='INSERT INTO TAB3
    (SELECT t1.c1,t2.c2
    FROM TAB1 t1, TAB2 t2
    WHERE t1.c1 = t2.c1
    AND t1.c3 between 10 and 15 AND)' ....... some other conditions.
    EXECUTE IMMEDIATE strQuery ;
    These SQL statements are inside a procedure. And this procedure is called from C#.
    The number of rows returned by the "SELECT" query is 70.
    On the very first time call of this procedure, the number rows inserted using strQuery is *70*.
    But in the next time call (in the same transaction) of the procedure, the number rows inserted is only *50*.
    And further if we are repeating calling this procedure, it will insert sometimes 70 or 50 etc. It is showing some inconsistency.
    On my initial analysis it is found that, the default optimizer is "ALL_ROWS". When i changed the optimizer mode to "rule", this issue is not coming.
    Anybody faced these kind of issues?
    Can anyone tell what would be the reason of this issue..? any other work around for this...?
    I am using Oracle 10g R2 version.
    Edited by: user13339527 on Jun 29, 2010 3:55 AM
    Edited by: user13339527 on Jun 29, 2010 3:56 AM

    You have very likely concurrent transactions on the database:
    >
    By default, Oracle Database permits concurrently running transactions to modify, add, or delete rows in the same table, and in the same data block. Changes made by one transaction are not seen by another concurrent transaction until the transaction that made the changes commits.
    >
    If you want to make sure that the same query always retrieves the same rows in a given transaction you need to use transaction isolation level serializable instead of read committed which is the default in Oracle.
    Please read http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10471/adfns_sqlproc.htm#ADFNS00204.
    You can try to run your test with:
    set  transaction isolation level  serializable;If the problem is not solved, you need to search possible Oracle bugs on My Oracle Support with keywords
    like:
    wrong results 10.2Edited by: P. Forstmann on 29 juin 2010 13:46

  • How display number of row in select statement

    How can I display number of row in select statement?
    Table
    data1 data2
    xxx ccd
    wss qwe
    qws uij
    I need get from SELECT statement:
    1 xxx ccd
    2 wss qwe
    3 qws uij

    user13734495 wrote:
    Thank you from answer.
    Statement
    select rownum rn, data1, data2 from table
    is good.
    And what have I do went I use
    select rownum rn, data1, data2 from table order by data1
    and I get
    3 qws uij
    2 wss qwe
    1 xxx ccd
    I need
    1 qws uij
    2 wss qwe
    3 xxx ccdhence the importance of describing the complete problem.
    select
      rownum,
      data1,
      data2
    from(
      select
        data1,
        data2
      from
        table
      order by
        data1)

  • How can I get a fixed number of rows on a SELECT?

    I'm interested on get the last xx records of one specific query that returns an higher number of rows than xx. How can I do this?

    you can use "where rownum < xx" if you
    write "select * from (select ... from ... order by ...) where rownum < 10"
    but you can't write "rownum > 10" because no record will be returned.
    therefor you have to do like this:
    select * from (select ..., rownum as nummer from (select ... from ... order by ...)) where nummer > 10
    ~
    pascal

Maybe you are looking for

  • Regarding SD MAJOR REPORTS

    HI, i want make reports in SD module and i want to know what are d major reports to created?  can anbody provide me examples or reports tcodes of sd module,so dat i can work on it. if find useful<b>,<POINTS OFFER REMOVED></b>

  • How to convert a string to visa resource name

    I have a string , like "visa://192.168.123.212/RIO0::INSTR", how can I convert it to a Labview visa resource name? The resource name is going to be used for "Open VI reference" control. Also, a related question is , is there a special vi control for

  • Crystal Reports version 13 managed api's run slower than native version 11

    Hello, I am a senior engineer, working in a project of porting client code written in C++ using Crystal 11. It uses the native COM api like shown approximately in the following code fragment:- IApplicationPtr pApplication; pApplication.CreateInstance

  • Best approach to reduce size of 400GB PO (yikes!)

    Hi fellow Groupwise gurus, Am taking a position with a new company that has just informed me they have a 400GB PO message store. I informed them that, uh yea, this is a bit of a problem. So, I am starting to consider best way(s) to deal with this. Th

  • Safari crashing constantly in snow leopard, esp when loading yahoo mail

    Hey Everyone, Suddenly safari is crashing in snow leopard... here is my crash log please help! I love safari but i dunno how much more of this issue i can take. Thanks in advance! Process: Safari [806] Path: /Applications/Safari.app/Contents/MacOS/Sa