开发者

Returning last inserted id from MySql

开发者 https://www.devze.com 2023-03-31 17:26 出处:网络
I\'m having a bit of trouble with the following query: START TRANSACTION; SET @LASTID = 0; INSERT INTO `Accounts` (`Col1`,`col2`,`col3`,`col4`)

I'm having a bit of trouble with the following query:

START TRANSACTION;

SET @LASTID = 0;

INSERT INTO `Accounts` (`Col1`,`col2`,`col3`,`col4`)
                VALUES (@param1,@param2,@param3,@param4);

SET @LASTID = last_insert_id(); -- This is what I need

INSERT INTO `Users` (`usr1`,`usr2`,`usr3`,`usr4`)
             VALUES (@usr1,@usr2,@usr3,@usr4);

SELECT @LASTID;
COMMIT;

Basically, I need to return the last inserted ID from the accounts table, however when running SELECT @LASTID, MySql returns a blob rather than a single value, which I'm having trouble accessing in C# asp.net

Is ther开发者_运维问答e any simple way to get this value as an int / varchar? Converting from blobs in code i feel is overkill, and I'd like to leave that lifting to the Mysql server.

Thanks in advance.


use

 SELECT LAST_INSERT_ID();

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id


Change the last statement into:

SELECT CAST(@LASTID AS integer) as last_id_from_accounts;
0

精彩评论

暂无评论...
验证码 换一张
取 消