Hi I have the oracle data table like that
seq_no
name
place
1
Rian
Us
1
Moli
Us
1
Molina
Us
and i want to update automaticly the seq_no to be like that
seq_no
name
place
1
Rian
Us
2
Moli
Us
3
Molina
Us
If you have a table:
CREATE TABLE table_name (seq_no, name, place) AS
SELECT 1, 'Rian', 'Us' FROM DUAL UNION ALL
SELECT 1, 'Moli', 'Us' FROM DUAL UNION ALL
SELECT 1, 'Molina', 'Us' FROM DUAL;
and a sequence:
CREATE SEQUENCE your_sequence;
Then you can update the existing rows to the sequence values using:
UPDATE table_name
SET seq_no = your_sequence.NEXTVAL;
Then the table would contain:
SEQ_NO
NAME
PLACE
1
Rian
Us
2
Moli
Us
3
Molina
Us
Then when you want to INSERT more rows, you can use:
INSERT INTO table_name (seq_no, name, place)
VALUES (your_sequence.NEXTVAL, 'New Name', 'New Place');
and the row:
SEQ_NO
NAME
PLACE
4
New Name
New Place
Would be added with the next sequence number.
Alternatively, you could write a trigger to get the next sequence number or, from Oracle 12, use an IDENTITY column.
db<>fiddle here
What does "automatically" mean?
You could have created table so that SEQ_NO is automatically populated by a database trigger (can't use an identity column as you're on 11g).
Or, if you want to update the table, a simple option is
update your_table set seq_no = rownum;
Related
I need SQL command that will insert a row after specific row. Example:-
Before table
Id. Name.
1. Xyz.
2. Xyz
3. Xyz
Want result need to add 'Abc' data after each 'xyz' having same id like:-
Id. Name.
1. Xyz.
2. Xyz
3. Xyz
1. Abc
2. Abc
3. Abc
Note this command work on 1000 data
Try using an INSERT INTO ... SELECT:
INSERT INTO yourTable (id, name)
SELECT id, 'Abc'
FROM yourTable
WHERE name = 'Xyz';
This assumes that you only want to duplicate rows having Xyz as the name. If you instead want to duplicate every record with an Abc version, then just remove the WHERE clause.
I have a shuttle control with a list of countries. So the user can select multiple countries, example: UK, France, Portugal. The order depends on how the user selects or moves the choices (see pic)
.
These are stored as UK:France:Portugal in the underlying table.
The problem is that I need these countries to be stored alphabetically because UK:France:Portugal is not the same as France:Portugal:UK. I know that in an ideal world these are saved in separate rows, but unfortunately this is not an option for me.
Is there a way to sort the selected values within the shuttle (on the right) alphabetically, maybe through a dynamic action when selecting countries?
If not, as an alternative, can we have a Post Calculation Computation to sort and store these values?
Thanks!
I don't know Apex-solution, but I can suggest Oracle-solution.
Here's an example: table test stores information about countries:
SQL> create table test (id number, countries varchar2(30));
Table created.
A database trigger sorts values in the countries column. How? It splits colon-separated values string into rows (that's what the regexp and connect by do), and then merges them back into another colon-separated values string (using listagg), but this time sorted (order by 1):
SQL> create or replace trigger trg_biu_cou
2 before insert or update on test
3 for each row
4 begin
5 select listagg(regexp_substr(:new.countries, '[^:]+', 1, level), ':') within group (order by 1)
6 into :new.countries
7 from dual
8 connect by level <= regexp_count(:new.countries, ':') + 1;
9 end;
10 /
Trigger created.
OK, let's see it work:
SQL> insert into test (id, countries) values (1, 'UK:France:Portugal');
1 row created.
SQL> select * from test;
ID COUNTRIES
---------- ------------------------------
1 France:Portugal:UK
SQL> update test set countries = 'New Zealand:Croatia:Hungary' where id = 1;
1 row updated.
SQL> select * from test;
ID COUNTRIES
---------- ------------------------------
1 Croatia:Hungary:New Zealand
SQL>
Might be OK; give it a try.
Postgresql:
I have two tables 'abc' and 'xyz' in postgresql. Both tables have same 'id' columns which type is 'serial primary key;'.
abc table id column values are 1,2,3 and also xyz id column containing same values 1,2,3,4
I want to union both tables with 'union all' constraint. But I want to change 'xyz' id column values to next value of 'abc' id column last value as 1,2,3,4,5,6,7
select id from abc
union all
select id from xyz
|id|
1
2
3
1
2
3
4
my wanted resuls as
|id|
1
2
3
4
5
6
7
BETTER - Thanks to #CaiusJard
This should do it for you
select id FROM abc
UNION ALL select x.id + a.maxid FROM xyz x,
(SELECT MAX(id) as maxid from abc) a
ORDER BY id
For anyone who's doing something like this:
I had a similar problem to this, I had table A and table B which had two different serials. My solution was to create a new table C which was identical to table B except it had an "oldid" column, and the id column was set to use the same sequence as table A. I then inserted all the data from table B into table C (putting the id in the oldid field). Once I fixed the refernces to point to from the oldid to the (new)id I was able to drop the oldid column.
In my case I needed to fix the old relations, and needed it to remain unique in the future (but I don't care that the ids from table A HAVE to all be before those from table C). Depending on what your trying to accomplish, this approach may be useful.
If anyone is going to use this approach, strictly speaking, there should be a trigger to prevent someone from manually setting an id in one table to match another. You should also alter the sequence to be owned by NONE so it's not dropped with table A, if table A is ever dropped.
Let's say I have a table Geometry and another table Customer like this:
Geometry Customer
City ID Location
---- -- --------
Berlin 1 (null)
Paris 2 (null)
London
Now I'd like to fill the column Location with data from the column City. "Randomly" would be nice but it doesn't matter at all.
I've tried
update Customer set Location = (select City from Geometry where rownum < 3);
but still getting this error: single-row subquery returns more than one row
UPDATE: I'd like to fill the whole column Location with one update statement. I'm using ORACLE. The result should look like this:
Customer
ID Location
-- -------
1 Berlin
2 London
Does someone have any better idea?
Thank you very much!
SQL Server:
UPDATE
Customer
SET
Location = (SELECT TOP 1 City FROM Geometry ORDER BY NEWID());
Since you just want it to pick one record at "random", you need to specify the correct number of rows:
update Customer set Location = (select City from Geometry where rownum = 1);
But note that since the subquery is not correlated to the Customer at all, the subquery may be optimised to only run once, and the same (randomly chosen) City will probably be used to update all Locations.
I would do the following, create a trigger on customer:
CREATE OR REPLACE TRIGGER customer_location
BEFORE INSERT OR UPDATE OF location ON customer
FOR EACH ROW
WHEN (NVL(new.location, 'X') = 'X')
BEGIN
SELECT city INTO :new.location
FROM (
SELECT city FROM geometry
ORDER BY DBMS_RANDOM.VALUE
) WHERE rownum = 1;
END;
/
UPDATE customer SET location = 'X';
This will update the customers table with a matching, "random" location. The trigger will also produce a new "random" location when a record is INSERTed into customers - as long as the location to be inserted is 'X' or NULL. (This wouldn't be wise if you actually have a location X - plug in some other value there!)
It is very possible to write a stored procedure to do the same thing, but you would have to create a cursor to loop over all the rows of customers where location is NULL.
update customer set location =
(select city from (
select distinct c.id, g.city, dbms_random.value from customer c, geometry g
order by value, city, id
) randoms where randoms.id = customer.id and rownum=1);
distinct necessary if there were two equal random values for one id
I have this situation in a certain table:
id | name
1 'Test'
2 'Test'
3 'Test'
How can I make a query to SELECT by distinct the name? I also need the ID column, even if I get the first occurrence of the element, e.g. "if the name column repeats, give me the first record with this repetition."
select name, MIN(ID)
from aCertainTable
group by name