ROWID and rownum
Hello World!
I have a large table I want to trancate to 10 rows.
I use the staement:
delete from aa where rowid in (select rowid from (select rowid from aa) where rownum > 11);
This result in 0 records deleted due to select rowid from (select rowid from aa) where rownum > 11; results in 0 records selected.
At the same time select rowid from (select rowid from aa) where rownum < 17;
produce correct result.
Any suugestions?
TIA
You should NOT use ";" symbol at the end of sql statement you call using
EXECUTE IMMEDIATE.
Change your function (and use binding variable whereever it's possible):
SQL> CREATE or replace function delete_rows (
2 table_name IN VARCHAR2,
3 nrows IN NUMBER DEFAULT NULL) return NUMBER IS
4 l_sql VARCHAR2(32767);
5 BEGIN
6
7 IF TABLE_NAME IS NOT NULL and nrows IS NOT NULL THEN
8 l_sql := 'delete from '||table_name ||
9 ' where rowid not in (select rowid from (select rowid from '|
10 table_name||') where rownum < :1)';
11 EXECUTE IMMEDIATE l_sql using nrows;
12 END IF;
13
14 commit;
15
16 return null;
17
18 EXCEPTION
19 WHEN OTHERS THEN
20 DBMS_OUTPUT.PUT_LINE(SQLERRM);
21 return null;
22 END delete_rows;
23 /
Function created.
SQL> select delete_rows('emp',11) from dual;
DELETE_ROWS('EMP',11)
ORA-14551: cannot perform a DML operation inside a query
SQL> begin
2 dbms_output.put_line(delete_rows('emp',11));
3 end;
4 /
PL/SQL procedure successfully completed.Rgds.
Similar Messages
-
What are ROWID and ROWNUM? Are they stored in database and where?
Hi All,
can anybody please answer this question
What are ROWID and ROWNUM? Are they stored in database and where?
Thanks,
SriniROWID can be thought of as a pointer to the physical location (on disk) of the (table) row.
From a ROWID value, Oracle can extract the file, block-within-that-file and offset-of-the-row-within-that-block. Using these, Oracle can directly access a disk block to retrieve a row.
ROWNUM is a just sequence number of a row within a result set of a query.
As said by other repliers, both are not stored. They are 'constructed' when you reference them inside a query. -
I'm writing an exercise to learn more about different datatypes and am confused about the use of and difference between rowid and urowid. As I understand it using rowid should only be done when writing for backwards compatibility, and urowid should be used for new coding. I'm working in 9.2.0.1.0, so I assume I should use urowid.
The following code returns an error (shown below). Anyone know why this is happenning?
create or replace function lrnvrbls
return varchar2
is
v_rowid urowid;
v_data varchar2(100);
select rowid
into v_rowid
from mytable
where rownum = 10 -- 10 is just a number I chose
execute immediate
'select mycolumn ' ||
'from mytable ' ||
'where rowid = ' ||
v_rowid
into v_data;
return v_data;
end;
SQL>select lrnvrbls from dual;
select lrnvrbls from dual
ORA-00904: "AAAHCAAAMAAAADVAAI": invalid identifier
I've tried using both ROWID and UROWID datatypes with no luck. Also tried CHARTOROWID and ROWIDTOCHAR and failed the same way.
Anybody understand how to use this?A single datatype called the universal rowid, or UROWID, supports both logical
and physical rowids, as well as rowids of foreign tables such as non-Oracle
tables accessed through a gateway.
A column of the UROWID datatype can store all kinds of rowids. The value of the
COMPATIBLE initialization parameter must be set to 8.1 or higher to use UROWID
columns.
DROP TABLE mytable
CREATE TABLE mytable
AS SELECT owner mycolumn FROM all_tables
WHERE rownum < 100
CREATE OR REPLACE
FUNCTION lrnvrbls
RETURN varchar2
IS
v_rowid urowid;
v_data varchar2(100);
strSQL varchar2(255);
BEGIN
select rowid
into v_rowid
from mytable
where rownum = 1;
strSQL := 'select mycolumn from mytable where rowid = '''||v_rowid ||'''';
execute immediate strSQL into v_data;
return v_data;
END lrnvrbls;
SELECT lrnvrbls FROM DUAL
DROP FUNCTION lrnvrbls
DROP TABLE mytable
COMMIT
16:39:37 SQL> DROP TABLE mytable
16:39:39 2 /
Table dropped.
Elapsed: 00:00:00.00
16:39:39 SQL> --
16:39:39 SQL> CREATE TABLE mytable
16:39:39 2 AS SELECT owner mycolumn FROM all_tables
16:39:39 3 WHERE rownum < 100
16:39:39 4 /
Table created.
Elapsed: 00:00:00.00
16:39:39 SQL> --
16:39:39 SQL> CREATE OR REPLACE
16:39:39 2 FUNCTION lrnvrbls
16:39:39 3 RETURN varchar2
16:39:39 4 IS
16:39:39 5 --
16:39:39 6 v_rowid urowid;
16:39:39 7 v_data varchar2(100);
16:39:39 8 strSQL varchar2(255);
16:39:39 9 --
16:39:39 10 BEGIN
16:39:39 11 --
16:39:39 12 select rowid
16:39:39 13 into v_rowid
16:39:39 14 from mytable
16:39:39 15 where rownum = 1;
16:39:39 16 --
16:39:39 17 strSQL := 'select mycolumn from mytable where rowid = '''||v_rowid ||'''';
16:39:39 18 --
16:39:39 19 execute immediate strSQL into v_data;
16:39:39 20 --
16:39:39 21 return v_data;
16:39:39 22 --
16:39:39 23 END lrnvrbls;
16:39:39 24 /
Function created.
Elapsed: 00:00:00.00
16:39:39 SQL> --
16:39:39 SQL> SELECT lrnvrbls FROM DUAL
16:39:39 2 /
LRNVRBLS
SYS
Elapsed: 00:00:00.00
16:39:39 SQL> --
16:39:39 SQL> DROP FUNCTION lrnvrbls
16:39:39 2 /
Function dropped.
Elapsed: 00:00:00.01
16:39:40 SQL> DROP TABLE mytable
16:39:40 2 /
Table dropped.
Elapsed: 00:00:00.00
16:39:40 SQL> COMMIT
16:39:40 2 /
Commit complete.
Elapsed: 00:00:00.00
16:39:40 SQL>
*/ -
[b]how to use EJB QL for LIKE and ROWNUM[/b]
Hi,
i am using CMP EJB with OC4J server
i face two problems in it when i make any query like
select object(o) from Partymas o where o.stationCode like '?1' and o.rownum >='?2' and o.rownum <='?3' order by party_name
then it shows error on rownum so i cant understand how to solve it
second is when i use LIKE statment in EJB QL like this:-
select object(o) from Partymas o where o.pinCode like '%?%'
then it shows error so can any one help me
thanks
warm regards
vikassheelgupta<ejb-ql>select object(o) from Partymas o where o.stationCode like '?1' and
rownum >='?2' and rownum <='?3' order by party_name</ejb-ql>Try this:
rownum >= ?2 and rownum <= ?3
<ejb-ql>select object(o) from Partymas o where o.partyName like '%?%'</ejb-ql>Try this:
like ?1 -
What is the difference between logical ROWID and physical ROWID
hello
can u please explain the difference between logical ROWID and physical ROWID
regards,from the docs (a 30 sec search)
Physical rowids store the addresses of rows in ordinary tables (excluding index-organized tables), clustered tables, table partitions and subpartitions, indexes, and index partitions and subpartitions.
Logical rowids store the addresses of rows in index-organized tables. -
Dynamically pass Table name, Column Name and rownum to function
Hi Guys
I wanted to pass the table name, column name and rownum to function and get the relevant value for it. Please guide me to achieve this task
Thanking You
Regards
LakmalThanks,
Here is my test function
CREATE or replace FUNCTION GET_COLUMN_VALUE (tab_name VARCHAR2,column_name VARCHAR2) RETURN varchar2 AS
strsql varchar2 (500);
ColVal varchar2;
BEGIN
strsql:='select '||column_name||' from '||tab_name|| ' Where rownum = 1' ;
EXECUTE IMMEDIATE strsql into ColVal;
RETURN ColVal ;
END;
Message was edited by:
Lakmal Marasinghe -
Trouble with subquery and rownum and ordering
I'm having trouble making this subquery work. The basic idea is that the web app will show only so many rows of results on the page, but right now I'm just playing around in SQL*Plus. The address book holds mixed case, so in order to sort by name properly I need to use UPPER to ignore case sensitivity. This SQL statement works fine for me:
select addressbookid from addressbook where addressbookid like '905430931|%' order by upper(addressbookid);I know that if you mention ROWNUM in the WHERE clause, you're referring to the row numbers of the ResultSet that Oracle returns. How do I use both ORDER BY UPPER(ADDRESSBOOKID) and WHERE ROWNUM > 25 AND ROWNUM <= 50? I figured a subquery would do it, but I can't write a correct one that does it! Below are my 2 attempts with the errors, and then I just tried to play with rownum:
SQL> select addressbookid from addressbook where addressbookid in (select addressbookid from address book where addressbookid like '905430931|%' order by upper(addressbookid));
select addressbookid from addressbook where addressbookid in (select addressbookid from addressbook
ERROR at line 1:
ORA-00907: missing right parenthesis
SQL> select addressbookid from addressbook where addressbookid in upper(select addressbookid from addressbook where addressbookid like '905430931|%');
select addressbookid from addressbook where addressbookid in upper(select addressbookid from address
ERROR at line 1:
ORA-00936: missing expression
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50 order by upper(addressbookid);
no rows selected
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50;
no rows selectedLike I said, if I can get a working subquery, then I'd like to attach that restriction on the rownum stuff. I'm wondering if it will be a problem trying to get the ordering to happen and then the rownum restriction after that, and all in the same query...
Btw, we've made all the table and column names in our database uppercase, so that shouldn't matter.This is probably the most efficient way ...
select addressbookid
from
select addressbookid,
rownum rn
from
select addressbookid
from addressbook
where addressbookid like '905430931|%'
order by addressbookid
where rownum <= 50
where rn > 25 -
VALIDATE_LAYER_WITH_CONTEXT - Rowid and NULL in result table
Hello,
Oracle 10g R2
I performed validity test using VALIDATE_LAYER_WITH_CONTEXT as follow:
CREATE TABLE RESULT_TABLE_CP(SDO_ROWID ROWID, RESULT VARCHAR2(2000));
CALL SDO_GEOM.VALIDATE_LAYER_WITH_CONTEXT('VESSEL_ZONE_GEOMETRY', 'POLYGON', 'RESULT_TABLE_CP');
What puzzled me was the result table contained the rowid and NULL as the result for each geometry. I was expecting to see what the error is, not just NULL.
However when I use VALIDATE_GEOMETRY_WITH_CONTEXT to test the same layer, I got TRUE for all of them, as:
select DISTINCT SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(a.POLYGON,b.DIMINFO)
from VESSEL_ZONE_GEOMETRY a,
USER_SDO_GEOM_METADATA b
where b.TABLE_NAME = 'VESSEL_ZONE_GEOMETRY'
and b.COLUMN_NAME = 'POLYGON'
and a.POLYGON is not null;
SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(A.POLYGON,B.DIMINFO) ------------------------------------------------------------------------------------
TRUE
Any suggestion would be much appreciated!
Thanks, CPCan you get the geometry by running
select polygon from VESSEL_ZONE_GEOMETRY where rowid = '....'; -
How to get the rowid and the entire record selected in PLSQL?
The code given below does not work.
You cannot select the record and the rowid using a cursor in one-shot.
But you could do that in a direct SELECT in "SQL Plus" when you select records for display. Is this a bug in ORACLE PLSQL? Or is there another way to do this?
DECLARE
objid_ VARCHAR2(200);
rec_ xxx_tab%ROWTYPE;
CURSOR get_rec IS
SELECT t.*, t.rowid
FROM xxx_tab t;
BEGIN
OPEN get_rec;
FETCH get_rec INTO rec_, objid_;
CLOSE get_rec;
END;
-----------------------------------You cannot fetch into both a record type and a variable. You have a few options, you can declare the record a s a rowtype of the cursor like this:
DECLARE
CURSOR c IS
SELECT t.*, rowid rid
FROM t;
l_rec c%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO l_rec;
CLOSE c;
END;You could use an implicit cursor and let Oracle deal with the record type internally (not to mention the open fetch and close) like this:
BEGIN
FOR rec in (SELECT t.*, rowid rid FROM t) LOOP
do_stuff with rec.col_name
END LOOP;
END;Note that in both of these you must alias the rowid column to some other name, you could also manually construct the record type to match the table and add a column od ROWID datatype to hold the rowid.
Finally, I think, depending on what you are actually going to do with the rowid, and how you feel about having records locked, you could look at declaring the cursor as FOR UPDATE and get rhe rowid for free.. This would be most appropriate if you are planning to update the table in the cursor (a bad practce by the way). Something like:
DECLARE
l_rec t%ROWTYPE;
CURSOR c IS
SELECT t.*, rowid
FROM t;
BEGIN
OPEN c;
FETCH c INTO l_rec;
do_whatever with l_rec
UPDATE t
SET whatever
WHERE current of c;
END;John -
What's instance, ROWID and why we use cursor?
Dear memebers,
yesterday i have an interview there i faced the following questions.
1. what is instance?
2. what is ROWID?
3. why we use cursor?
i can give only the last one.
i am waiting for your answer.
thanks
Regards:
Muhammad Nadeem
[email protected]HY
Row Identifiers
Each row in an Oracle database has a unique row identifier, or rowid, which is used internally by the Oracle database to access the row. A rowid is an 18-digit number that is represented as a base-64 number, and it contains the physical address of a row in an Oracle database. You can view the rowid value for rows in a table by specifying the ROWID column in the select list of a query. The query in the following example retrieves the ROWID and customer_id columns from the customers table:
SELECT ROWID, customer_id
FROM customers;
Oracle instance:
An Oracle instance is the combination of the background processes and
memory structures. The instance must be started to access the data in the database. Every
time an instance is started, a System Global Area (SGA) is allocated and Oracle
background processes are started. Background processes perform functions on behalf of
the invoking process. They consolidate functions that would otherwise be handled by
multiple Oracle programs running for each user. The background processes perform
input/output (I/O) and monitor other Oracle processes to provide increased parallelism
for better performance and reliability.
REGARDS -
Greetings,
I've got some dynamic SQL that selects based on ROWID. Works OK until the table argument is an IOT.
ORA-01410: invalid ROWIDI have been made aware that IOTs do not carry valid ROWIDs, however, when a ROWID is selected from an IOT, values that can be handled as VARCHAR2 are returned.
SQL> select rowid from my_iot_table;
ROWID
*BAgA7EYCwgsCwQX+
*BAgA7EYCwgsCwQ7+
*BAgA7EYCwgsCwRX+If I handle these ROWIDs as character data, is there any guarantee of uniqueness?
-KevinIn a single transaction, I've got a bit of (recursive) code that traverses a tree of related rows in various tables. At each node, it caches the current rowid and compares it to those of all visited nodes to determine if any cycles exist in the data.
Rowid seemed to be a convenient test for uniqueness.
-K -
I have created a table via a select statement, and am working on narrowing down my critera, but the question becomes if ROWID is sequentially assigned.
create table1 as (
select empno, deptno, sequentialcode
from (
select empno, deptno, sequentialcode
from temptable
order by sequentialcode
This simply, grabs three columns from temp table and inserts them into a new table in ascending order of the sequentialcode column value.
select empno
from table1 a
where rowid = (
select min(rowid)
from table1 b
where a.deptno = b.deptno
group by deptno
the idea here, is that for each department, i want only a singular employee, but the one i select should be selected in priority sequence based on the sequentialcode column. So if I have three employees in department 1, with sequentialcode's of 1,3,4, this statement should select the employee with the sequential code of 1.
similarly if there is another department whose employee's sequentialcodes are 2,5,7, it would select the 2.
HOwever, if ROWID is not assigned sequentially in the create statement, this theory is debunct. I've tried to do the same thing using "rownum" but it runs forever and I usually end up breaking the execution before it completes.
Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner1) ROWID is just a physical address of a row. There is absolutely no guarantee that it is going to be at all sequential.
2) It sounds like you just want an analytic function here
SELECT empno, deptno, sequentialcode
FROM (
SELECT empno, deptno, sequentialcode,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sequentialCode) rn
FROM yourTable
WHERE rn = 1If you have ties, this will pick an arbitrary row. You can use the RANK() or DENSE_RANK() function if you want ties to be handled differently.
Justin -
Using order by and rownum in massive tables
Hi,
I need to retreive paginated and ordered data from a big table.
I know some different tips using rownum or the RANK() function and subqueries, these tips work well for small amount of data, but I need to get the data from a table with 200.000 entries (not under my control), and with all those methods is necessary to order first the data and then select your range, so performance is extremely poor.
Anybody knows a better solution? it doesn't matter if is plain SQL(better) or PL/SQL.
Thanks in advancebut I was looking for something like the LIMIT in MySQL or TOP in msSQL where all the sorting is made internally and it's really fast If the data needs sorting, I do not think Oracle would take any longer to do that than the others mentioned, unless the other DBs mentioned, only sort the rows that are actually being returned.
As for the LIMIT clause, Oracle already has that clause available when FETCHing from a CURSOR:
SQL> DECLARE
2 CURSOR emp_cur IS
3 SELECT * FROM scott.emp;
4 TYPE emp_rec_table IS TABLE OF emp_cur%ROWTYPE INDEX BY BINARY_INTEGER;
5 emp_tbl emp_rec_table;
6 BEGIN
7 OPEN emp_cur;
8 LOOP
9 emp_tbl.DELETE;
10 FETCH emp_cur BULK COLLECT
11 INTO emp_tbl LIMIT 5;
12 EXIT WHEN emp_tbl.COUNT <= 0;
13 dbms_output.put_line(emp_tbl.COUNT || ' rows fetched using LIMIT clause.');
14 END LOOP;
15 CLOSE emp_cur;
16 END;
17 /
5 rows fetched using LIMIT clause.
5 rows fetched using LIMIT clause.
4 rows fetched using LIMIT clause.
PL/SQL procedure successfully completed.
SQL> disconnect
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
SQL> -
Hello again.
Another question.
Can I query with union all and stop it when I get N rows.
For example:
select 1 from dba_segments
union all
select 2 from dba_segments where
union all
select 3 from dba_segments where;
and get the 100 first rows without doing the whole query:(not like that-->)
select * from (
select 1 from dba_segments
union all
select 2 from dba_segments where
union all
select 3 from dba_segments where)
where rownum < 100);
I want the query will stop when there are 100 rows in the result set.
thank you!You already posted your own answer. It just seems you don't want to use it.
ROWNUM is NOT assigned until the rows are selected to be returned. So you need to wrap the three inner queries into a query that uses ROWNUM. -
Order by AND rownum in a function?
I have a table with articles sorted by an id. In the table is also a date field to tell when the article is written.
I want a query that returns the 20 latest articles, and this is what i have come up with:
SELECT * FROM (
SELECT articleid,writtendate
FROM articles
ORDER BY writtendate desc)
WHERE ROWNUM < 21;
This works alright, BUT I want it in a function! There it doesnt compile!!! Anyone got any suggestions?
I have tried to separate the stuff, like first sorting the records in a view and then selecting the top 20 after that. I could try a cursor and fetching out of that, but havent suceeded with that either.
Bye!ok, the sql statement works perfectly in an sql editor, but wont compile in a function.
FUNCTION fnc_getLatest20 (
cur IN OUT pkg_cursor.refcur
)RETURN pkg_exception.return_type IS
BEGIN
OPEN cur FOR
SELECT * FROM (
SELECT articleid,writtendate
FROM articles
ORDER BY writtendate DESC)
WHERE ROWNUM < 21;
RETURN pkg_exception.err_none;
END;
The best I have is that either the rows returned in the cursor is all rows, ordered correctly or the top twenty in some other order.
I most be on the wrong track here, there has to be some easier way of doing this...
BTW, thanks for the help so far.
Maybe you are looking for
-
Adobe Acrobat 5.0 and Windows 7 Problem
How do I get Adobe Acrobat 5.0 and Windows 7 compatible so I can access my attachments to e-mails sent to me?
-
Dbpoweramp song name issue/free program that rips MP3s with song titl
Hi, I was just wondering if there is a way around paying for the DbpowerAmp in order to get it to provide the song titles when you rip to MP3 format. I've ripped around 20 Cds - am I going to have to go in manually and add track numbers and titles? I
-
hi there currently i am creating a web service running on Tomcat server. but it does not support the SOAP services. do i need a seperate package, if i need to send message using SOAP? if so, where can i get it.
-
Do i need to create dimensions on mp using only ods?
hi all, Do i need to create dimensions for multi provider where in all my info providers are only from ods? thanxs haritha
-
Error : Delivery does not contain items to be picked
Hi Exps I have created Stock Transport Order (STO) and then Delivery Note with VL10g. In Delivery Output EK00 (Picking list Printing ) gets populated along with EKSU (Picking List EDI output). But EK00 only turns green and EKSU turns Red saying that