I’d like to insert a record between the records which are already in the table. There are over 40000 records, and I would like to place this new record at 19th place. How may I do so?
Let’s consider an example with 5 records:
Table data:
CREATE TABLE enum
(identifier VARCHAR2(64),
code VARCHAR2(512),
data VARCHAR2(4000))
/
INSERT ALL
INTO enum VALUES ('HR_B_A', 'Halli, L6', 'Halli, L6')
INTO enum VALUES ('HR_B_A', 'Halli, L7', 'Halli, L7')
INTO enum VALUES ('HR_B_A', 'Halli, L8', 'Halli, L8')
INTO enum VALUES ('HR_B_A', 'Halli, L9', 'Halli, L9')
INTO enum VALUES ('HR_B_A', 'Halli, P6', 'Halli, P6')
INTO enum VALUES ('HR_B_A', 'Halli, P7', 'Halli, P7')
select * from dual
/
So when we check the table, we get:
SELECT * FROM enum
IDENTIFIER CODE DATA
---------- --------- ---------
HR_B_A Halli, L6 Halli, L6
HR_B_A Halli, L7 Halli, L7
HR_B_A Halli, L8 Halli, L8
HR_B_A Halli, L9 Halli, L9
HR_B_A Halli, P6 Halli, P6
HR_B_A Halli, P7 Halli, P7
What I would like to have when I run the select query:
SELECT * from enum
IDENTIFIER CODE DATA
---------- --------- ---------
HR_B_A Halli, L6 Halli, L6
HR_B_A Halli, L7 Halli, L7
HR_B_A Halli, L8 Halli, L8
HR_B_A Halli, L9 Halli, L9
HR_B_A Halli, L10 Halli, L10
HR_B_A Halli, P6 Halli, P6
HR_B_A Halli, P7 Halli, P7
The only way I can think of is transfer the data till L9 line in a table (e.g enum_temp), insert L10 line and then transfer the remaining data in that table. But I can’t seem to figure out the syntax of the query.
Thanks in advance 🙂
Rows in a relational database are not sorted
There is no way you can ensure any desired ordering unless you use an
ORDER BYexpression in your SELECT.As you are mixing several types of “information” in your values, you will need to use something like this:
The first order by expression orders by the part of the code that does not contain numbers. That way all
Halli, Lvalues are sorted together. The second order by expression then sorts by the numeric value of the code (stripping of all non-numeric characters).