Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 80720 invoked from network); 1 Aug 2006 14:46:58 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 1 Aug 2006 14:46:58 -0000 Received: (qmail 28997 invoked by uid 500); 1 Aug 2006 14:46:53 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 28976 invoked by uid 500); 1 Aug 2006 14:46:53 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 28918 invoked by uid 99); 1 Aug 2006 14:46:53 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Aug 2006 07:46:52 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [63.247.81.241] (HELO tss8.serverconfig.com) (63.247.81.241) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Aug 2006 07:46:51 -0700 Received: from rrcs-67-52-41-92.west.biz.rr.com ([67.52.41.92] helo=[127.0.0.1]) by tss8.serverconfig.com with esmtpa (Exim 4.52) id 1G7vVu-0003xU-4t for derby-user@db.apache.org; Tue, 01 Aug 2006 10:46:30 -0400 Message-ID: <44CF6973.5090808@decoursey.net> Date: Tue, 01 Aug 2006 09:47:15 -0500 From: Paul J DeCoursey User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: Derby Discussion Subject: Re: Retrieving the identity column value after an insert References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - tss8.serverconfig.com X-AntiAbuse: Original Domain - db.apache.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - decoursey.net X-Source: X-Source-Args: X-Source-Dir: X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Mark Ashworth wrote: > Hi, > > The problem is when you want to insert rows into other tables for each of > the rows that have been inserted into to the user's table and those tables > have identity fields. In this case it seems that without a variable to hold > the userid, the IDENTITY_VAL_LOCAL() returns the new identity value from > those other tables (which is to be expected). > > Regards, > Mark P Ashworth > > -----Original Message----- > From: Kristian.Waagan@Sun.COM [mailto:Kristian.Waagan@Sun.COM] > Sent: 01 August 2006 03:27 PM > To: Derby Discussion > Subject: Re: Retrieving the identity column value after an insert > > Mark Ashworth wrote: > >> Good Day, >> >> >> >> I would like to run the following SQL in ij >> >> >> >> INSERT INTO users (name) VALUES ('test'); >> >> >> >> And after the insert statement use the inserted id value to complete >> other inserts in other tables. >> >> >> >> INSERT INTO attr (user_id, value) VALUES( INDENTITY_VAL_LOCAL(), 'test >> attr'); >> >> INSERT INTO attr (user_id, value) VALUES( INDENTITY_VAL_LOCAL(), 'test >> attr2'); > the id that was generated in the previous statement. >> >> >> >> How do I assign the identity value to a variable? >> > > Hello Mark, > > I don't know how to assign the identity value to a variable, but if you > can use multiple row INSERT statements, you should be able to do what > you want. > > INSERT INTO users (name) VALUES ('test'); > INSERT INTO attr (user_id,value) VALUES > (IDENTITY_VAL_LOCAL(), 'test attr'), > (IDENTITY_VAL_LOCAL(), 'test attr2'); > > The multiple row insert will not update the value of IDENTITY_VAL_LOCAL. > Note that not all databases support this form of the INSERT statement. > > > > > Regards, > try SELECT IDENTITY_VAL_LOCAL() FROM users, I think that the IDENTITY_VAL_LOCAL() is per table, I have some stored procedures that do this and it seems to work. I'm not for certain that it is correct. Paul