Oracle update table value returned 'single-row sub-query returned more than one row ' - oracle

I wanna add a column named 'Bonus_AMT' on table 'Employee'.
Here's the clause I wrote.
enter image description here
I ran the above clause, but it didn't work. It returns that 'single-row' sub-query return more than one row. How could I solve that?

You need to join table in update clause with table in select clause used in SET. Here is the example.
update employees e1 set bonus = (select salary*commission_pct from employees e2 where e1.employee_id = e2.employee_id);
You need to make sure that both versions of tables are joined on primary key.

Your select query clause used in set is returning multiple row for the applied date range. So you need to change that select clause so that at a time it will single record and update the same record using joins.

Related

How to Insert a data along with update on same duplicate record

I want to insert a one or more records in table A.
But same record already existing in records with status Active
CREATE PROCEDURE Test
AS
BEGIN
INSERT INTO A (x,y,z,status)
SELECT data FROM A WHERE some condtion;
... Here i want to update table A status column into 'N', if the inserted data already existing in table A Compared with x,y,z column.
EXCETION
---Handled
END;
You can try this:
MERGE INTO target_table t
USING source_table s
ON (t.id = s.id)
WHEN MATCHED THEN
UPDATE SET t.col1 = s.col1
WHEN NOT MATCHED THEN
INSERT (col1, col2) VALUES (s.col1, s.col2);
Documentation Link: https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606
What you probably want to do is a merge-statement. The merge statement checks if the dataset already exists. In this case you can formulate an update statement. For example, you could update the status. If not, the new dataset will be inserted.
For further information you should hava a look here.
The merge is the way to go. Another approach is first updating all the rows that match the criteria and after do the insert.
However, if your problem is how to differentiate the 2 rows, use the pseudo-column ROWID.

ORACLE SQL Query to fetch all table names IN DB whereever given value is treated as PK

Just want to know is this possible.
Say that if i have value 'X' and iam sure that this is referenced in some other tables as PK value but not sure about exactly which table is that, so i would like to know the list of those tables.
Pseudo query of above what i mentioned
SELECT TABLE_NAME FROM DBA_TABLES WHERE <<ATLEAST ONE OF THE TABLE ROW PK VALUE IS MATCHING EQUAL TO 'X'>>;

Two DMLs based on Same Subquery

I need to cancel some orders, and then insert a row into another table, both based on the same subquery.
There is a very small chance that the subquery will return different rows between the time that the 1st and 2nd DMLs are issued.
But is there a proper way to do this such the orders updated are the same orders that are inserted into the cancellations table?
I am using Oracle and JDBC. Thanks.
update orders
set status = 'cancel'
where order_number in (select order_number from some_other_table_where...)
insert into order_cancellations
select order_number select order_number from some_other_table_where...
Here are five approaches that spring to mind:
(1) Put a trigger on the orders table so whenever the status is set to 'cancel', a row is inserted into order_cancellations.
(2) Use the returning clause in insert. Do the insert first and use this information for the update.
(3) Add a creation date to order_cancellations and do this insert first. Then update orders using the just-inserted rows
(4) Wrap the two statements in a transaction (this might require locking the other tables).
(5) Load the subquery data into a temporary table and use that table for both operations.
I also wonder if you could eliminate the need for the cancellations table just by having a cancellation_date column in the orders table.

Updating a SQL table where items to change are identified in another table that is linked

Everywhere I look I can find how to update a table from data in another table but I am not looking for that. I have two tables TABLE1 and TABLE2. TABLE1 has a column PULLDATE and a column JOBNMBR. TABLE2 has a column JOBNMBR and a column PROJECT. The two tables link at the JOBNMBR column. I need to do a bulk update to TABLE1.PULLDATE per a project number, but that project number is stored in TABLE2.PROJECT.
Using VisualStudio 2005 and in VB code not C+, does anyone know the code (if there is any) that links the tables and allows me to update all TABLE1.PULLDATE records grouped by TABLE2.PROJECT? I will be providing the trigger to update using a textbox [TxtBox_Pulldate] and a nearby button [Button_UpdatePulldate].
Thanks a bunch
Chuck Vensel
I think I understand that you want to update Table1 given a matching column in Table2?
You write the SQL update just as you would the SELECT except replace the SELECT clause with the UPDATE clause.
UPDATE Table1
SET
[PULLDATE] = your_value
FROM
Table1
JOIN Table2
ON Table2.[JOBNMBR] = Table1.[JOBNMBR]
WHERE
Table2.[PROJECT] = your_project_ID

Assign auto-incrementing value to new column in Oracle

I have this table in an Oracle DB which has a primary key defined on 3 of the data columns. I want to drop the primary key constraint to allow rows with duplicate data for those columns, and create a new column, 'id', to contain an auto-incrementing integer ID for these rows. I know how to create a sequence and trigger to add an auto-incrementing ID for new rows added to the table, but is it possible to write a PL/SQL statement to add unique IDs to all the rows that are already in the table?
Once you have created the sequence:
update mytable
set id = mysequence.nextval;
If you're just using an integer for a sequence you could update the id with the rownum. e.g.
update
table
set id = rownum
You then need to reset the sequence to the next valid id.
Is this what you need?
UPDATE your_table
SET id = your_seq.nextval;
This assumes you don't care what order your primary keys are in.
First you should check your PCTFREE... is there enough room for every row to get longer?
If you chose a very small PCTFREE or your data has lots of lenght-increasing updates, you might begin chaining every row to do this as an update.
You almost certainly better to do this as a CTAS.
Create table t2 as select seq.nextval, t1.* from t1.
drop t1
rename t2 to t1.

Resources