2009-01-09 10 views
1

J'ai une procédure stockée qui est utilisée lorsqu'une nouvelle transaction est insérée. Cette procédure s'insère correctement dans la table des transactions, mais j'ai également besoin de mettre à jour une autre table liée en fonction des valeurs insérées.Ma procédure stockée SQL avec une mise à jour

Basé sur le Product_ID j'ai besoin de mettre à jour PT_Pct_to_Salon dans une table appelée 'Salon' avec une valeur d'une table appelée 'Zen_Products_Description'. Le salon correspondant peut être trouvé en utilisant 'Salon_ID' de l'insert qui est équivalent à l'ID PK de la table 'Salon'.

La valeur que j'ai besoin d'insérer se trouve dans le champ 'web_share' de la table 'Zen_Products_Description'. La ligne associée dans 'Zen_Products_Description' peut être mise en correspondance en faisant correspondre la valeur insérée 'Product_ID' avec le PK de 'Zen_Products_Description' qui s'appelle 'products_id'.

J'utilise MySQL 5.


BEGIN 
INSERT INTO Transactions 
(Cart_Trans_ID, Customer_ID, Pass_Through_Amt, Product_ID, Product_Name, Product_Qty, Salon_ID, Stylist_ID, Trans_Type, customerAddress, customerCity, customerEmail, customerFirstName, customerLastName, customerPhone, customerPostal, customerState) 
VALUES (Cart_Trans_ID, Customer_ID, Pass_Through_Amt, Product_ID, Product_Name, Product_Qty, Salon_ID, Stylist_ID, Trans_Type, customerAddress, customerCity, customerEmail, customerFirstName, customerLastName, customerPhone, customerPostal, customerState); 
Insert Into Zen_Products_Description 
(products_id, products_name) 
Values (Product_ID, Product_Name) ON DUPLICATE KEY UPDATE 
products_name = Product_Name; 

//this is where I try unsuccessfully to update 
update Salon 
set PT_Pct_to_Salon = Zen_Products_Description.web_share 
join Salon 
on Salon.Salon_ID = Transactions.Salon_ID 
join Zen_Products_Description 
on Zen_Products_Description.products_id = Transactions.Product_ID; 

END 

Répondre

1

lol - vous avez oublié de poser une question ... être succinct et je vais vous aider

BEGIN 
    INSERT INTO Transactions 
    (Cart_Trans_ID, Customer_ID, 
    Pass_Through_Amt, Product_ID, Product_Name, 
    Product_Qty, Salon_ID, Stylist_ID, 
    Trans_Type, customerAddress, customerCity, 
    customerEmail, customerFirstName, customerLastName, 
    customerPhone, customerPostal, customerState) 
    VALUES 
    (Cart_Trans_ID, Customer_ID, 
    Pass_Through_Amt, Product_ID, Product_Name, 
    Product_Qty, Salon_ID, Stylist_ID, 
    Trans_Type, customerAddress, customerCity, 
    customerEmail, customerFirstName, customerLastName, 
    customerPhone, customerPostal, customerState); 


Insert Into Zen_Products_Description 
    (products_id, products_name) 
Values 
    (Product_ID, Product_Name) 

ON DUPLICATE KEY 

UPDATE products_name = Product_Name 
## ######## tout ce qui précède est sans importance
update Salon 
    set PT_Pct_to_Salon = Zen_Products_Description.web_share 
join Salon on Salon.Salon_ID = Transactions.Salon_ID 
join Zen_Products_Description 
    on Zen_Products_Description.products_id = Transactions.Product_ID; 

############### here is update 

UPDATE salon A 
INNER JOIN Transactions B ON A.salon_ID = B.salon_ID 
INNER JOIN Zen_Products_Description C on C.Products_id = B.product_id 
SET A.PT_Pct_to_Salon = C.web_share 
##, a .x = b.x etc ... ## et par la manière - apprendre comment formater votre code afin que les gens puissent le lire ...
END 
+0

Cela devrait vraiment être un commentaire sur la question. – BobbyShaftoe

+0

je vais répondre dans cet espace ... – mson