2011-12-15 8 views
0

複数のテーブルにデータを挿入し、最後に挿入されたIDを取得して別のテーブルに格納するストアドプロシージャで作業していました。私はすべての可能な回避策は、この問題のためにそこにされている問題で立ち往生LAST_INSERT_IDに変換されたMySqlバグは回避策が必要

reference

のMySQL 5.0.24にLAST_INSERT_IDバグに関連てる?

たとえば、

// if agent is new one 

INSERT INTO `agent`(`agn_name`, `agn_cus_id`) VALUES (agentName, customerId); 
SET agnId = LAST_INSERT_ID(); 

//else get agnId = existing one 

INSERT INTO `order`(`orderno`, `description`, `agent`) VALUES (orderNo, description,  agnId); 

SET orderId = LAST_INSERT_ID(); 

INSERT INTO `suborder1`(`orderid`, `description`) VALUES(orderId, subdescription1); 

INSERT INTO `suborder2`(`orderid`, `description`) VALUES(orderId, subdescription2); 

エージェントが挿入されますときに問題があり、注文IDは、エージェントテーブル

+0

のいくつかの種類でこれを書いていますか?どのくらいのDBを使用していますか? –

+0

いくつかのコード、現在のデータ、および予想されるデータを投稿してください。 – Nonym

+0

@Brianはい、データベースが古すぎます。しかし、今私はそれを更新することはできません。だから可能な解決策を探している。 – Prazanna

答えて

3

からIDを取得する基本的には、新しく挿入された行からの注文IDを取得するいくつかの方法を見つけるために持っている、いずれかのタイムスタンプによって(そのテーブルをロックしますこれがテーブルの最新の行であるかどうかを確認することができます)。この場合、エージェントが作成されたばかりで、おそらく別の順序を前もって追加することができないため、このエージェントのために1つの行しか存在しないという事実を使用するとうまくいきます。

私はあなたがバグが5年前に閉鎖されたことを知っています擬似コード

if (agent is new one) { 
    INSERT INTO agent(agn_name, agn_cus_id) VALUES (agentName, customerId); 
    SET agnId = LAST_INSERT_ID(); 
    INSERT INTO order(orderno, description, agent) VALUES (orderNo, description, agnId); 
    -- there can only be one row in order for this new agent, as the agent was just created 
    SET orderId = SELECT orderId FROM order WHERE agent = agnId; 
} 
else { 
    SET agnId = <some existing value>; 
    INSERT INTO order (orderno, description, agent) VALUES (orderNo, description, agnId); 
    -- this is the first call to LAST_INSERT_ID() since the agent wasn't created 
    SET orderId = LAST_INSERT_ID(); 
} 
+0

ありがとうございます。私はそれがうまくいくと思う。 – Prazanna

関連する問題