Results 1 to 2 of 2

Thread: Return value from a insert stored procedure

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Return value from a insert stored procedure

    Hello,

    I have a stored procedure that inserts a new passenger into a database. The fields are name and email. The passengerID is a auto increment. The database I am using is mySQL.

    The problem is that I want to return the passengerID when the stored procedure is called. I am not sure if that is possible. Here is my code:
    Store procedure in mysql
    Code:
    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `flightdb`.`spInsertNewPassenger`$$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `spInsertNewPassenger`(_name varchar(50), _email varchar(5) )
    BEGIN
    	INSERT INTO Passengers ( name, email )
    	VALUES ( _name, _email );	
    END$$
    
    DELIMITER ;
    The stored procedure is called from a java program below:
    Code:
    try
    		{
    			CallableStatement cs = conn.prepareCall( "{ spInsertNewPassenger( ?, ?) }" );
    			
    			cs.setString( 1, name );
    			cs.setString( 2, email );
    			
    			cs.executeUpdate(); //I want to return the ID here
    			
    		}
    Can anyone help change the above code so that i can return the ID number.

    Thanks in advance,

    Steve
    Last edited by steve_rm; Sep 21st, 2006 at 07:12 AM.
    steve

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Return value from a insert stored procedure

    I think you best add an OUT parameter to the procedure that receives the new ID. (The LAST_INSERT_ID MySQL function can retrieve the ID if it's an auto_increment column.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width