473,509 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

select last_insert_id()

201 New Member
Hello -

I'm recently a newbie to mysql, but have experiance in oracle, mssql, access, etc...

Here is my problem, i've created the table needed to test the audit procedures. the data element that the id is going to be inserted into is auto_increment.

I don't know what the problem could be.

here is the table code:
Expand|Select|Wrap|Line Numbers
  1. -- Table: tblaudit 
  2.  
  3. -- DROP TABLE IF EXISTS `tblaudit`; 
  4.  
  5. CREATE TABLE `tblaudit` ( 
  6. `AuditId` int(8) NOT NULL, 
  7. `UserName` varchar(50) NOT NULL, 
  8. `TableName` varchar(50) NOT NULL, 
  9. `FieldName` varchar(50) NOT NULL, 
  10. `OldValue` longtext , 
  11. `NewValue` longtext , 
  12. `DTAdded` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
  13. `TestId` bigint AUTO_INCREMENT NOT NULL, 
  14. /* Keys */ 
  15. PRIMARY KEY (`TestId`) 
  16. ) ENGINE = InnoDB 
  17. CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; 
  18.  
here is the procedure code
Expand|Select|Wrap|Line Numbers
  1. DELIMITER | 
  2.  
  3. CREATE PROCEDURE `add_tblAudit` 
  4. IN `pUserName` varchar(50), 
  5. IN `pTableName` varchar(50), 
  6. IN `pFieldName` varchar(50), 
  7. IN `pOldValue` longtext, 
  8. IN `pNewValue` longtext 
  9. BEGIN 
  10. DECLARE insertId; 
  11. INSERT INTO `tblAudit` (`UserName`, `TableName`, `FieldName`, `OldValue`, `NewValue`) 
  12. VALUES (pUserName, pTableName, pFieldName, pOldValue, pNewValue); 
  13. SELECT last_insert_id() INTO insertId; 
  14. INSERT INTO `tblAudit`(`TestId`) VALUES (insertId); 
  15. END| 
  16.  
  17. DELIMITER ; 
  18.  
thanks for your help in advance!
Jan 24 '09 #1
2 4364
wizardry
201 New Member
disregard this request!

the solution is as follows:

in the insert into values place last_insert_id() where the field should go for the value that your inserting. also ensure that the audit table you are inserting to has the primary key on this field and is auto_inc.

I've found what i want to acomplish here!

http://www.paragon-cs.com/mag/issue6.pdf

Thanks,

Wiz
Jan 25 '09 #2
Atli
5,058 Recognized Expert Expert
Hi Wiz.

Glad you found a solution.
Thanks for sharing it.
Jan 25 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3600
by: mr_burns | last post by:
hi, how do i do a SELECT statment that will just query a table to find the row with the maximum of a declared field. for example, if i have a table of customers and i would like to get the value...
0
7300
by: Irene Mettias | last post by:
Hello, I've just installed perl DBI v1.38 which according to the documentation supports the method last_insert_id(). For some reason the return value I get is undef. Here is my source code :...
1
4258
by: Michal Adamczakk | last post by:
hi, how to implement mysql's last_insert_id() ? i know that seqences and oids are great. the one thing i miss is that they are not session specific. i mean selecting last_value from seqence...
6
12664
by: jim | last post by:
Hi, I have a site with various parts which allow users to enter info via forms which gets entered into various tables in a MySQL db (I'm using ASP, rather than PHP). I have an ecards bit, and...
10
5586
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
2959
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the...
1
4101
by: simbarashe | last post by:
I am trying to insert an auto_increment value into multiple tables but its seems to lose its value after the the second insert statement and my code is as follows can any1 please help. The...
1
9590
seshu
by: seshu | last post by:
thank sir now one more dubt how to use last_insert_id( ) because i want get all the last inserted and then update only those values can you say me how? regards seshu
5
3075
Markus
by: Markus | last post by:
The MySQL manual states: I wonder what is considered a 'client'. Is that simply just different connections to the database?
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.