79539208

Date: 2025-03-27 14:44:36
Score: 0.5
Natty:
Report link

I know this is an old question, but I stumbled upon it today looking for an answer myself. Here was my solution, which seems to work better than what was here provided. In my particular case, I want some ID's to be linked to each other. In my table I have a column lkMother which records the chain of events. Basically I want to record the price of a widget on such and such a date. Then I want to record the new price as another entry in the table, but I want to show that one of these items replaces the other. We may add several price changes. Then we get a notice from our supplier that they have come out with widget 2.0 to replace the original widget. Even though they aren't exactly the same thing, I want to record that they are, as far was are concerned a replacement.

So this is what I do.

  1. Original Item receives ID Number

  2. Some time later we receive a replacement. The replacement receives the Original Items ID number. The Original item is assigned the next available ID, but its Original ID is recorded in the lkMother column.

  3. Repeat ad infinitem

  4. So when a customer calls and says they need a replacement for a part they purchased on such and such a date, I look up the part, go to the lkMother column which gives me the current equivalent.

Here is stored procedure that swaps the IDs. Notice, this procedure requires that there be no item in the table with an ID = 1. It could be easily modified to fit another scenario.

'''

CREATE PROCEDURE `noe_pricebookswapids` (intid1 INT, intid2 INT)
BEGIN
    SET SQL_SAFE_UPDATES = 0;
    
    UPDATE `Order`.tblPrice
    SET ID = 1 WHERE ID = intid1;
    
    UPDATE `Order`.tblPrice
    SET ID = intid1 WHERE ID = intid2;
    
    UPDATE `Order`.tblPrice
    SET ID = intid2 WHERE ID = 1;

END
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Luke Krell