I have two tables.
One is SUPPLIER with SupplierID as its PK and it has a State attribute.
The other is SUPPLIEDPART. It has a UnitCost attribute. SupplierID is a FK.
I'm trying to update the UnitCost based on whether or not the Supplier is located in the state of Texas. I have tried multiple queries, but they always fail. Here are some of the latest attempts. Any help would be appreciated. I have attached a picture of the relationship between the two tables if that will help.
INSERT INTO SUPPLIEDPART (UnitCost)
SELECT UnitCost * 1.1
FROM SUPPLIEDPART SP INNER JOIN SUPPLIER S
ON SP.SupplierID = S.SupplierID
WHERE State = 'TX';
----This one results in a "cannot insert nulls into brewer34.suppliedpart.supplierid
UPDATE SUPPLIEDPART
SET UnitCost = UnitCost * 1.1
WHERE SupplierID = (SELECT SupplierID
FROM SUPPLIER
WHERE State = 'TX')
GROUP BY PartNumber;
----This query results in a command not properly ended----
I think you were close:
UPDATE SUPPLIEDPART
SET UnitCost = UnitCost * 1.1
WHERE SupplierID IN (SELECT SupplierID -- <-- IN instead of =
FROM SUPPLIER
WHERE State = 'TX')
-- no GROUP BY
On most DBMS you can do:
UPDATE SUPPLIEDPART AS a
INNER JOIN SUPPLIER AS b ON a.SupplierID = b.SupplierID
SET a.UnitCost = a.UnitCost * 1.1
WHERE b.State = 'TX';
Related
I have tables called tblPurchase and tblInvoice. When I Insert new record I want to update my TotalStock form tblInvoice. I tried following code.
Create procedure [dbo].[spUpdateTotalStock] #Stock int, #PurchaseId int
As
Begin
Begin Transaction
Update tblPurchase
Set TotalStock = #Stock
Where PurchaseId = #PurchaseId
RollBack Transaction
end
It worked as expected. But I want to update TotalStock column by many ids. It means when I insert more than one invoice at the same time, I want to update my TotalStock column. like this
tblInvoice
ItemName | Quantity
---------+----------
Phone 3
Mouse 6
tblPurchase
ItemName | TotalStock
-----------+------------
phone 3
Mouse 6
ID comes with parameter. Always more than 10 ids come with parameter.
When I Insert new record I want to update my TotalStock form tblInvoice
This would normally be handled via a trigger not a stored procedure. That would look something like this:
create trigger trig_invoice_update on tblInvoice
after insert, update, delete
begin
update p
set quantity = p.quantity + coalesce(i.quantity, 0) - coalesce(d.quantity, 0)
from tblPurchase p left join
(select i.itemName, sum(i.quantity) as quantity
from inserted i
group by i.itemName
) i
on p.itemName = i.itemName left join
(select d.itemName, sum(d.quantity) as quantity
from deleted d
group by d.itemName
) d
on p.itemName = d.itemName
where d.quantity <> 0 or i.quantity <> 0;
end;
This version handles inserts, updates, and deletes, but assumes that all items are already in the purchases table.
UPDATE Customers,OrderLine
Set customerID = "---"
Where Customers.customerID != OrderLine.CustomerID;
I'm not too sure how to make this work
What it is meant to do is change the values in customerID to --- but only if that value is not present in the CustomerID column in the OrderLine table
A column like customerID is I guess the PRIMARY KEY of the table Customers, right?
If this is the case then simply you can't update more than 1 row to '---' because this would create duplicates and this is not allowed.
If this is not the case then use NOT EXISTS:
UPDATE Customers
SET customerID = "---"
WHERE NOT EXISTS (
SELECT 1 FROM OrderLine
WHERE Customers.customerID = OrderLine.customerID
)
With NOT IN:
UPDATE Customers
SET Customers.customerID = "---"
WHERE Customers.ID NOT IN (SELECT OrderLine.CustomerID FROM OrderLine);
Everyone I am beginner in SQL.
I have two tables like first, second.
pid column of first table and pid column of second table are same. I have to get price from the first table and quantity from second table, multiply those values (final) and update that final values in second table at the price column.
I tried but it doesn't work. Can anyone help me? Thanks in advance.
FIRST TABLE
Name Null? Type
------ -------- ------------
PID NOT NULL NUMBER(38)
PNAME NOT NULL VARCHAR2(20)
PPRICE NOT NULL FLOAT(126)
PAVAIL NOT NULL NUMBER(38)
SECOND TABLE
Name Null? Type
------------ -------- ------------
ORDERID NOT NULL NUMBER(38)
PID NUMBER(38)
CUSTOMERNAME NOT NULL VARCHAR2(20)
PHNO NOT NULL NUMBER(38)
QTY NOT NULL NUMBER(38)
PRICE NOT NULL FLOAT(126)
I used this query, but it doesn't work:
update first f, second s
SET s.price = f.pprice * s.qty
where s.pid EXISTS (select second.pid from first, second
where (first.pid=second.pid);
Oracle does not support update join syntax. One workaround is to use an update with a correlated subquery.
UPDATE second s
SET s.price = s.qty * (SELECT f.pprice FROM first f WHERE f.pid = s.pid)
WHERE EXISTS (SELECT 1 FROM first f WHERE f.pid = s.pid)
The exists clause is necessary to make sure that a record in second which does not match to anything in first does not have its price update to NULL. If this can't happen, or you don't care if it does, then you may also remove the WHERE clause.
Update using a join is done with the MERGE statement in Oracle and that's what you need. In your case:
MERGE INTO second a
USING FIRST b
ON (a.pid = b.pid)
WHEN MATCHED
THEN
UPDATE SET a.price = b.price * a.qty;
Check this statement:
update second s
set s.price = s.qty * (select f.pprice from first f where f.pid = s.pid )
where exists (select f.pprice from first f where f.pid = s.pid );
you may try this also..
update s
set s.price =
s.qty * f.pprice
from second s,first f
where s.pid = f.pid
if any mistake..revert me back
I'm sure this is answered elsewhere, however I'm not sure how to search for this specific question.
Let's say I have 2 tables:
InputDataNames with columns [Prospect_Name], [Product_Name], [Market_Name]
Entity with columns [EntityId], [Name], [EntityTypeId], where [Name] is used in the InputDataNames table.
Now suppose I want to create a table equal to InputDataNames, but instead of Names, fill in with IDs from the Entity table:
InputDataIDs with columns [Prospect_ID], [Product_ID], [Market_ID]
Using the InputDataNames table to populate the appropriate combination of IDs, how can I join with the Entity table to get the desired effect?
Note: I know this is sloppy, just doing a little database cleanup.
You can join to the entity table three times. Assuming the EntityTypeId column determines whether or not the entity is a prospect, product, or market, then you should include that column in the join. If they are numbered 1,2,3:
select
pros.entityid, prod.entityid, mkt.entityid
from
inputdatanames id
inner join
entity pros on id.prospect_name = pros.name and pros.entitytypeid = 1
inner join
entity prod on id.product_name = prod.name and prod.entitytypeid = 2
inner join
entity mkt on id.market_name = mkt.name and mkt.entitytypeid = 3
insert InputDataIDs
(Prospect_ID, Product_ID, Market_ID)
select Prospect.EntityID
, Product.EntityID
, Market.EntityID
from InputDataNames
left join
Entity Prospect
on Prospect.EntityTypeId = 1 -- Type of prospect
and Prospect.Name = InputDataNames.Prospect_Name
left join
Entity Product
on Product.EntityTypeId = 2 -- Type of product
and Product.Name = InputDataNames.Product_Name
left join
Entity Market
on Market.EntityTypeId = 3 -- Type of market
and Market.Name = InputDataNames.Market_Name
I wanna update a column of a table:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
the SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
mustn't return a null result, if it's null, the update will not be made
How to do that?
You need to filter the rows to be updated in the WHERE clause as well:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
WHERE EXISTS (SELECT 42
FROM supplier s2
WHERE s2.prodno = prod.prodno);
It might be faster using a MERGE (assuming prodno is the primary key in product):
merge into product
using
(
select p.prodno,
s.prod_supplier_id
from product p
join supplier s on s.prodno = p.prodno
) t on (t.prodno = prod.prodno)
when matched then update
set prod_supplier_id = t.prod_supplier_id;
Not tested!
first of all create a back up table:
CREATE TABLE productBAK AS SELECT * FROM product;
now you can use update query like this:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno and
s.prod_supplier_id is not null
)
WHERE prod.prodno in (SELECT s1.prodno FROM supplier s1 where s1.prod_supplier_id is not null);
The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
I see prod_supplier_id being a column in both product and supplier.
Can you explain in which table the prod_supplier_id must not be null?
Looking at your query you want something like
update product
set prod_supplier_id = (select prod_supplier_id
from supplier
where prod_supplier_id is not null);
Issues you're facing:
all product records will be updated with the same prod_supplier_id
if the subselect statement returns more than one record -let's say you have 5 suppliers, so 5 prod_supplier_id's, the update statement will fail.
Can you tell us:
do you want all your products to be set to 1 supplier?
if not, do you have a list of which product should have which supplier?
R