SQL: insert records is not in order - sql

I am having a problem while a record is inserted into a table.
each parking_cost is inserted after its related record. It should be with its record. why is this happening?
Any ideas ?
Regards.

If you are doing two separate queries to populate the row in the table, the first query needs to be an INSERT, the second query needs to be an UPDATE, eg:
INSERT INTO Customers (Pre_Payed_Card, Parking_ID) VALUES ('1234', 1)
Then
UPDATE Customers SET Parking_Cost = <cost> WHERE Parking_ID = 1
An INSERT will always create a new row; two INSERT queries for the same logical entity will result in two separate rows like you have in the screenshot.
Update: re-reading that query, you're probably after something like
UPDATE Customers SET Parking_Cost = p.Parking_Cost
FROM Customers c INNER JOIN Parking p ON c.Parking_ID = p.Parking_ID
WHERE c.Parking_ID = 1

Related

How do I update every row of a particular column with values from another column?

I am trying to update a column called software_id with a possible three values from a lookup table. So update user softwares software id column with the values from the softwares table of the id column. But there are only three rows in the softwares table. In the user softwares table there are over 1000. But when I run the query below only three rows get updated and the rest are left as null.
So the softwares id column is number and goes 1,2,3 and the user_sofwares software_id column is all null.
When I run the following query.
UPDATE user_software c
SET sotware_id = (
SELECT DISTINCT id
FROM softwares
WHERE id = c.id
);
Only the first three rows of the destination table get updated. I want every row on the user_softwares column to be updated with either 1,2 or 3. So it should be 1,2,3,1,2,3 for example.
I keep getting this error whenever i tried to update all rows
Error report - ORA-01427: single-row subquery returns more than one row
You can do this with a MERGE statement:
MERGE INTO (SELECT software_id, 1, ora_hash(ROWID, 2) + 1 AS fake_id
FROM user_software) u_soft
USING (SELECT DISTINCT id
FROM softwares) sftw
ON (sftw.id = u_soft.fake_id)
WHEN MATCHED THEN UPDATE SET u_soft.software_id = sftw.id;
(considering your matches are unique)

SQL insert trigger for insert with update

I have two tables:
[1] Donations - with amount and pet_id field
[2] Pets - with id and total donations field
I'm trying to create a trigger which will update total donations field whenever
a new row is being inserted to Donations table. I tried this one:
create trigger update_donations
on sponserships
for insert
as
update dbo.Pets
set tot_donations = (
select new_val = inserted.amount + pets.tot_donations
from inserted
where inserted.[Pet-ID] = pets.[Animal-ID]
)
But of course it changes all records, whereas i want to change only records that are changed in the donations table.
It is usually not a good practice to store this type of derived information - you could have a view that computes it on the fly rather than a trigger that keeps it up to date. Also please note that if you go that way, you also need a delete and an update trigger...
That said, you can use the following query in your insert trigger to update the relevant record in the pets table:
update p
set p.total_donations = p.total_donations + i.amount
from dbo.Pets p
inner join inserted i on i.[Pet-ID] = p.[Animal-ID]

SQL-Oracle: Updating table multiple row based on values contained in the same table

I have one table named: ORDERS
this table contains OrderNumber's which belong to the same person and same address lines for that person.
However sometimes the data is inconsistent;
as example looking at the table screenshot: Orders table with bad data to fix -
you all can noticed that orderNumber 1 has a name associated to and addresses line1-2-3-4. sometimes those are all different by some character or even null.
my goal is to update all those 3 lines with one set of data that is already there and set equally all the 3 rows.
to make more clear the result expected should be like this:
enter image description here
i am currently using a MERGE statement to avoid a CURSOR (for loop )
but i am having problems to make it work
here the SQL
MERGE INTO ORDERS O USING
(SELECT
INNER.ORDERNUMBER,
INNER.NAME,
INNER.LINE1,
INNER.LINE2,
INNER.LINE3,
INNER.LINE4
FROM ORDERS INNER
) TEMP
ON( O.ORDERNUMBER = TEMP.ORDERNUMBER )
WHEN MATCHED THEN
UPDATE
SET
O.NAME = TEMP.NAME,
O.LINE1 = TEMP.LINE1,
O.LINE2 = TEMP.LINE2,
O.LINE3 = TEMP.LINE3,
O.LINE4 = TEMP.LINE4;
the biggest issues i am facing is to pick a single row out of the 3 randomly - it does not matter whihc of the data - row i pick to update the line/s
as long i make the records exaclty the same for an order number.
i also used ROWNUM =1 but it in multip[le updates will only output one row and update maybe thousand of lines with the same address and name whihch belong to an order number.
order number is the join column to use ...
kind regards
A simple correlated subquery in an update statement should work:
update orders t1
set (t1.name, t1.line1, t1.line2, t1.line3, t1.line4) =
(select t2.name, t2.line1, t2.line2, t2.line3, t2.line4
from orders t2
where t2.OrderNumber = t1.OrderNumber
and rownum < 2)

How to insert data from one table to another table in oracle

I have a two table 'Inventory' and 'Tendor' where Inventory has primary key pk_id ,i updated 'Inventory' table when Inventory.ti_name = Tendor.ki_name by using following query
Update Inventory A set (Used_NAME, ACCOUNT_NUMBER, ti_STATUS)
= (Select B.Using_NAME, B.ACCOUNT_NO, B.ki_STATUS from
Tendor B where A.ti_name = B.ki_name and a.pk_id is not null);
Anything wrong in this query or any optimized one??
After updation for those who is not satisfying the condition Inventory.ti_name = Tendor.ki_name i want to insert it as new rows in 'Inventory' table with primary key pk_id should be changed
how to do this? for pk_id do i need to do some logic like 'SEQ.NEXTVAL FROM DUAL'
Can anybody suggest a query
This query updates searches matching rows in tables inventory and tendor. When both tables contain row with the same value of ki_name, it updates row in table inventory. All rows from tendor, that was not found in inventory, will be inserted there:
merge into Inventory a
using Tendor b
on (A.ti_name = B.ki_name)
when matched then update
set a.Used_NAME = B.Using_NAME,
a.ACCOUNT_NUMBER = B.ACCOUNT_NO,
a.ti_STATUS = B.ki_STATUS
when not matched then
insert (pk_id, Used_NAME, ACCOUNT_NUMBER, ti_STATUS)
values (your_seq.nextval, B.Using_NAME, B.ACCOUNT_NO, B.ki_STATUS)
Also you can use sequence in insert statement.

Advanced sql query

I have two tables in a sql database, I am trying to update a column from my committed table (committedtbl) with values from my vendor table (vendortbl) based on a common column from both tables.
There is a column with the vendor identification number (vendorno) in both tables, I tried adding the vendor description (vendorname) column from the vendortbl to the committedtbl but there are no values in it.
I need to insert values into the vendorname based on the corresponding numbers from vendorno... How do I accomplish this?
The vendorname column already exists in my committedtbl.
I have tried this, but got an error:
update v_vendorname
set v_vendorname = v_vendorno
from vendortbl vt
where v_vendorno = vt.v_venkey
update committedtbl
set c.vendorname = v.vendorname
from committedtbl c
inner join vendortbl v on v.vendorno = c.vendorno