Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Identity columns

Author  Topic 

Tigger
Yak Posting Veteran

85 Posts

Posted - 2002-02-27 : 14:47:54
I need to create a table which has in it

party_id - identity column
branch_number
branch_id

where branch_id is an identity column within branch.

eg

party_id branch_number branch_id
1 123 1
2 456 1
3 123 2
4 123 3
5 456 2

Customer data is going to be imported into the table using DTS and will be assigned a party_id and branch_id.

Any ideas on how to generate branch_id or is this one of those cases where we will have to process the records one row at a time ?

jbkayne
Posting Yak Master

100 Posts

Posted - 2002-02-27 : 16:26:59
Try the following:

--drop table party

create table party (

party_id int identity(1,1) not null
, branch_number int not null
, branch_id int not null
)


declare @feed table
(
branch_number int not null
, randomizer int identity(1,1) not null
)

insert into @feed (branch_number)
select 123 as branch_number
union all
select 456
union all
select 123


insert into party (branch_number, branch_id)
select
branch_number
, isnull((select max(branch_id) from party as p where p.branch_number = feed.branch_number),0)
+ (select count(*) from @feed as feed2 where branch_number = feed.branch_number and randomizer <= feed.randomizer) as branch_id
from
@feed as feed

select * from party





Edited by - jbkayne on 02/27/2002 16:37:17
Go to Top of Page
   

- Advertisement -