简体   繁体   中英

Returning last inserted id from MySql

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 there 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.

将最后一条语句更改为:

SELECT CAST(@LASTID AS integer) as last_id_from_accounts;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM