如何从两个插入操作获取中间LastInsertID?

时间:2018-11-07 05:40:59

标签: mysql last-insert-id

部分查询如下所示。我正在C#中使用ASP.net。我想在查询结束时将'LastRegionID'放入我的C#代码中。目前,我总是收到last_ID帖子insert_region_cost。但是我想要的是在区域插入之后的“ LastRegionID”。这可能吗?还有另一种方法可以实现我想要的吗?

                    string Query = @"INSERT INTO region (" +
                                    "REGION_USER_ID," +
                                    "REGION_NICK_NAME," +
                                    "REGION_POLYGON) " + 
                                "VALUES (" +
                                    "@Parameter1," +
                                    "@Parameter2," +
                                    "PolygonFromText(@Parameter3)); " +

                                "SET @'LastRegionID' = LAST_INSERT_ID();" +

                                "INSERT INTO region_cost (" +
                                    "REGION_ID," +
                                    "REGION_COST," +
                                    "REGION_COST_NICK_NAME," +
                                    "REGION_NOTES) " +
                                "VALUES (" +
                                    "@'LastRegionID'," +
                                    "@Parameter4," +
                                    "@Parameter5," +
                                    "@Parameter6);";

部分C#代码...

                    if (sqlSuccess > 0)
                {
                    oDBStatus.LastUpdatedKey =  @LastRegionID ???';
                    oDBStatus.Type = DBOperation.SUCCESS;
                    oDBStatus.Message.Add(DBMessageType.SUCCESSFULLY_DATA_INSERTED);
                }
                else
                {
                    oDBStatus.Type = DBOperation.ERROR;
                    oDBStatus.Message.Add(DBMessageType.ERROR_NO_RECORDS_UPDATED);
                }
                return oDBStatus;

0 个答案:

没有答案