Insert a record in MySQL and return the record ID.

Source: Internet
Author: User

1. Select max (ID) from user;

2. Select last_insert_id () as ID from user limit 1;
(The returned ID for this test is always 0, which is a problem)

3. Storage Process
1)
Using el
Create sequence seqid
Minvalue 1
Max value 999999999999999999999999999
Start with 1
Increment by 1
Nocache
Order;

Create or replace procedure sp_insert (aname int, RST out INT) is
Begin
Insert into tablename (ID, name) values (seqid. nextval, aname );
RST: = seqid. currval;
End;

2) implementation in MySQL
Delimiter $

Drop procedure if exists 'test' $
Create definer = 'root' @ 'localhost' procedure 'test' (in name varchar (100), out oid int)
Begin
Insert into user (loginname) values (name );
Select max (ID) from user into OID;
Select OID;
End $

Delimiter;

 

Then execute
Call test ('gg ', @ ID );

Returns the ID.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.