Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have table employee(empID, fName, lName)
position(positionID, positionName)
empPosition(empID, positionID, dateChanged, INDEX(empID), INDEX(positionID))

I am doing this code
C#
cmd.CommandText =
                "INSERT INTO employee (empID, fName, lName)" +
                " VALUES('null','" + fNameTxt.Text + "','" + lNameTxt.Text + "')";"

I have to enter this empID, which is auto increment in database, to enter in empPosition table along with positionID. can you give me any idea how to get LAST_INSERT_ID() in it ?
I tried to do SET @employee = LAST_INSERT_ID(); but couldnt make the syntax correct.
Posted
Updated 11-Oct-19 0:46am
v2

You can get any table's details from the following query:
SQL
SHOW TABLE STATUS FROM 'your DB name' LIKE '%your table name%' ;

For getting the auto increment value on a table you can use the following query:
SQL
SELECT AUTO_INCREMENT
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your db name'
AND   TABLE_NAME   = 'your table name';


Good luck,
OI
 
Share this answer
 
Use "select SCOPE_IDENTITY()" to get the last successful inserted id value from database.

This works in SQL SERVER..


Best of luck.
 
Share this answer
 
SELECT LAST_INSERT_ID();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900