Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 14th, 2005, 09:12 AM
frrobert frrobert is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 38 frrobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 45 sec
Reputation Power: 4
Getting new Primary key to use in second table

I am writing an asp.net form that inserts data into two firebird tables. The form lists the infomation to be inserted into both tables The problem is I need the prmary key from the first table which is an auto gnerated by a generator and trigger to use in the second table.

The first table is a table that list incident details.
field include
incident_id primary key (auto generated )
date
remaining fields

The second table is a table that that list persons involved
fields include
incident_id
person_id


The form list all the information So what I'm trying to do is have the query insert the incident details in the first table, then take the primary key from the new record and insert it into the 2nd table along with the person id.

Any ideas?

Thanks,

Fr. Robert

Reply With Quote
  #2  
Old April 14th, 2005, 05:51 PM
ipaw ipaw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: NS Canada
Posts: 20 ipaw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 37 sec
Reputation Power: 0
I don't use triggers in my db for getting an autonumber field, what I do is create a generator for my table...

CREATE TABLE REGIONS(
REGION_ID INTEGER DEFAULT 0 NOT NULL,
REGION_NAME VARCHAR(60) DEFAULT '' NOT NULL,
primary key(REGION_ID));
CREATE INDEX iREGION ON REGIONS (REGION_NAME);
CREATE GENERATOR REGION_ID_GEN;

Then in my app I execute the follow SQL to get the next available REGION_ID and store it in a variable:

SELECT gen_id(REGION_ID, 1) AS NEXT_ID FROM RDB$DATABASE;

I will then use "NEXT_ID" and insert it into my REGIONS table and it is also available for other tables as well.

Not the answer you are looking for, but another way to accomplish the same thing.

Paul

Reply With Quote
  #3  
Old April 15th, 2005, 03:53 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 17 m 43 sec
Reputation Power: 8
Quote:
Originally Posted by ipaw
I don't use triggers in my db for getting an autonumber field, what I do is create a generator for my table...

CREATE TABLE REGIONS(
REGION_ID INTEGER DEFAULT 0 NOT NULL,
REGION_NAME VARCHAR(60) DEFAULT '' NOT NULL,
primary key(REGION_ID));
CREATE INDEX iREGION ON REGIONS (REGION_NAME);
CREATE GENERATOR REGION_ID_GEN;

Then in my app I execute the follow SQL to get the next available REGION_ID and store it in a variable:

SELECT gen_id(REGION_ID, 1) AS NEXT_ID FROM RDB$DATABASE;

I will then use "NEXT_ID" and insert it into my REGIONS table and it is also available for other tables as well.

Not the answer you are looking for, but another way to accomplish the same thing.

Paul



Yes, that is the way

--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer
* FirebirdSQL Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org/shownews.php

Reply With Quote
  #4  
Old April 15th, 2005, 03:38 PM
frrobert frrobert is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 38 frrobert User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 45 sec
Reputation Power: 4
Thanks,

It worked

Reply With Quote
  #5  
Old April 17th, 2005, 10:13 AM
SilverDB's Avatar
SilverDB SilverDB is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Romania
Posts: 173 SilverDB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 45 m 53 sec
Reputation Power: 4
Send a message via Yahoo to SilverDB
I'm getting the same result by doing a commit retaining in the first table and then asking for the ID when I need to do a insert in the second one.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Getting new Primary key to use in second table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway