[ic] Return auto_increment value from [query] INSERT

Paul Jordan interchange-users@icdevgroup.org
Wed May 7 02:11:01 2003


> Quoting Paul Jordan (paul@gishnetwork.com):
> > Hi guys      IC 4.9.7   MYSQL 3.2XXXXX
> >
> > I have a [query] INSERT statement, and the table it is on has an
> > auto_increment primary key. Is it was possible to do an
> > INSERT and also be able to grab the auto_incremented value...
> >
> > [query
> >     st=sql
> >     table="messages"
> >     type=list
> >     sql=|
> >         INSERT  messages
> >         SET     message_id = '',
> >                 call_id = '[scratch callid]',
> >                 direction = '2',
> >                 message_source = 'SeeBase',
> >    [snip "code"]
> >                 to_email = '',
> >                 subject = '[value resub]',
> >                 status = '4'
> >         |][seti new_id][sql-pos 0][/seti][/query]
>
> This is one that is best done in embedded Perl.
>
> [perl messages]
> 	my $db = $Db{messages};
> 	my %hash = (
> 		call_id => $Scratch->{callid},
> 		direction => 2,
> 		message_source => 'SeeBase',
> 		to_email => '',
> 		subject => $Values->{resub},
> 		status => '4',
> 	);
> 	$Scratch->{new_id} = $db->set_slice(undef, \%hash);
> 	return;
> [/perl]
>
> --
> Mike Heins

Ahh Thanks Mike.

Of course, now I see what to have searched for :) This works really good. I
should have been searching "retrieve autonumber" instead of "retrieve
auto_increment" and "retrieve record code..." and so on.

I noted a jump in my primary key (I emptied the tables) and suddely
realized... foo.autonumber!  This set_slice is interesting too.

Thank you very much, Paul

PS, Racke... no joy. But thanks... it help explain set_slice.