Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question, it may seems to be easy but i am having hard time to figure it out, any help is greatly appreciated.

so the problem is, once the delivery of product with full release is finished, i want it to merge or replace the existing row with the same Product_Section and Product_Name,


| Id | Product_Name | Product_Section | Product_Release_Status |Product_Delivery_Status|
--------------------------------------------------------------------------------------------------
| 1 | Paper | 101 | Full | Ongoing |
| 2 | Paper | 101 | Partial | Finished |

if i insert the values(1, 'Paper', 101, 'Full', 'Finished') again for update using the following procedure below,

and this is what i get.

| Id | Product_Name | Product_Section | Product_Release_Status |Product_Delivery_Status|
--------------------------------------------------------------------------------------------------
| 1 | Paper | 101 | Full | Finished |
| 2 | Paper | 101 | Partial | Finished |

Whereas the Expected Output should be:

| Id | Product_Name | Product_Section | Product_Release_Status |Product_Delivery_Status|
--------------------------------------------------------------------------------------------------
| 2 | Paper | 101 | Full | Finished |

i hope you'll get what i mean. im probably new to database programming, i hope someone can help me. Thank you.

What I have tried:

SQL
PROCEDURE `MY_Products_InsertUpdateByDelivery`(IN _myId INT, _myProductName VARCHAR(50), _myProductSection INT, _myProductReleaseStatus VARCHAR(50), _myProductDeliveryStatus VARCHAR(50))

BEGIN
      IF EXISTS(SELECT Product_Section from tbl_products Where Product_Name LIKE _myProductName AND 
        Product_Release_Status LIKE 'Partial') THEN
      UPDATE 
            tbl_products
      SET
            Product_Release_Status = _myProductReleaseStatus,
            Product_Delivery_Status = _myProductDeliveryStatus
      WHERE
            Product_Section = _myProductSection
      AND
            Product_Name = _myProductName;

-- Once i updated the existing partial.
-- I want to delete the origin of update to avoid duplication of row by deleting it.

    DELETE FROM
           tbl_products
    WHERE
        Id = _myId;

ELSE
      UPDATE 
            tbl_products
      SET
            Product_Release_Status = _myProductReleaseStatus,
            Product_Delivery_Status = _myProductDeliveryStatus
      WHERE
            Product_Section = _myProductSection
      AND
            Product_Name = _myProductName;
    END IF;
END
Posted
Updated 30-Nov-21 5:16am

1 solution

When I run your code I do not get the results you claim - I get (I've shortened the column names)
Id	Productname	ProductSection	Release_Status	Delivery_Status
1	Paper		101				Full			Finished
2	Paper		101				Full			Finished
Based on your description of what you are trying to do, I would first identify the Id of the "origin of update" - which seems to be this line
| 1 | Paper | 101 | Full | Ongoing |
E.g. something like this (ssql not mysql but demonstrates what I mean)
SQL
DECLARE @IDFOUND INT = (SELECT ID FROM tbl_products WHERE Product_Name LIKE @_myProductName AND 
        Product_Delivery_Status LIKE 'Ongoing')
Then you can do your update finally the delete uses the "found" id
SQL
DELETE FROM
           tbl_products
    WHERE
        Id = @IDFOUND;
To pass the Id into the SP you must first know what has prompted the update, but you can do that processing within the SP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900