BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Identity example please

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Predrag Cabarkapa
Guest





PostPosted: Tue Sep 09, 2003 4:43 am    Post subject: Re: Identity example please Reply with quote



Hi ,
try with @@IDENTITY global variable

Predrag Cabarkapa
"john" <john (AT) publico (DOT) co.uk> wrote

Quote:

Hi i want to insert data into a master-detail, i am starting a
transaction, inserting the master, i then need to get the identity value of

the primary key (orderid, so that i can insert the detail rows.
Quote:

How do i get the value of the orderid ? so that i can use it in the
folowing detail insert ?

Thanks

John




Back to top
Predrag Cabarkapa
Guest





PostPosted: Tue Sep 09, 2003 5:40 am    Post subject: Re: Identity example please Reply with quote



Hi,
I hope selecting @@IDENTITY var. will help
you to solve the problem, but is there any
special reason to code your application in
this manner ?

Consider this:
--------------

create table table_1 (id integer,...)
create table table_2 (id integer,...)

create trigger tI_table_1 on table_1
for insert
as
update table_1
set id = select max(id)+1 from table_1 (next value for id column)
where id is null (or id=0, or other fixed
value inside transaction
if id is NOT NULL )

Now, inside each transaction

select max(id) from table_1

will return a value supplied by a tI_table_1 trigger until a
transaction is finished.
Now, you have a value for a id column in table_2.

Later, if there is any reason to change a value of id column
in table_1 it is trivial to update id column in table_2.


create trigger tU_table_1 on table_1
for update
AS
declare @new_id integer
declare @old_id integer
select @old_id = id from deleted
select @new_id = id from inserted
update table_2
set id = @new_id where id=@old_id


Or

create trigger tU_table_1 on table_1
for update
AS

update table_2
set id = (select id from inserted)
where id=(select id from deleted)


bye
"john" <john (AT) publico (DOT) co.uk> wrote

Quote:

Hi i want to insert data into a master-detail, i am starting a
transaction, inserting the master, i then need to get the identity value of

the primary key (orderid, so that i can insert the detail rows.
Quote:

How do i get the value of the orderid ? so that i can use it in the
folowing detail insert ?

Thanks

John




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.