Re: insert rows : generating sequence numbers
From: Tenaya (ct_at_ct.ct)
Date: 04/04/04
- Next message: Nigel Rivett: "RE: What makes trigger doesn't fire?"
- Previous message: Learner: "Extremely Frustrated - Still can't figure how to populate table"
- In reply to: John A Grandy: "Re: insert rows : generating sequence numbers"
- Next in thread: Tore Bostrup: "Re: insert rows : generating sequence numbers"
- Reply: Tore Bostrup: "Re: insert rows : generating sequence numbers"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 4 Apr 2004 07:55:28 -0400
John,
Well, never say die :-)
If you are willing to create an auxiliary table, then there is a
set-oriented approach.
For the sake of simplicity and clarity, I've eliminated the character
manipulation part of the code, and have simply concentrated on what I
believe is the difficult part ... namely, backfilling sequence numbers. I've
also created views to (substantially) simplify the code. Views are never
necessary, but can often times be very helpful in understanding the logic
... and, I think, particularly in this case:
create table Numbers (n int NOT NULL primary key)
go
insert into Numbers values (0)
insert into Numbers values (1)
insert into Numbers values (2)
insert into Numbers values (3)
insert into Numbers values (4)
insert into Numbers values (5)
insert into Numbers values (6)
insert into Numbers values (7)
insert into Numbers values (8)
insert into Numbers values (9)
go
create table Table1 (Col1 int NOT NULL unique)
create table Table2
(
Col1 int NOT NULL,
Col2 int NOT NULL
)
go
insert into Table2 values (2, 2)
insert into Table2 values (4, 4)
insert into Table1 values (3)
insert into Table1 values (5)
insert into Table1 values (100)
insert into Table1 values (111)
go
create view linear_Table1 as
select Col1, s = (select count(*)
from Table1 as t2
where t2.Col1 < t1.Col1)
from Table1 as t1
go
create view backfill_Numbers as
select backfill = n
from Numbers
where not exists (select *
from Table2 as t2
where n = t2.Col2)
go
create view seq_backfill_Numbers as
select backfill,
n = (select count(*)
from backfill_Numbers as b2
where b2.backfill < b1.backfill)
from backfill_Numbers as b1
go
insert into Table2
select t1.Col1, b.backfill
from linear_Table1 as t1
join seq_backfill_Numbers as b
on (t1.s = b.n)
go
select * from Table2
go
Chief Tenaya
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%23MsAnmhGEHA.712@tk2msftngp13.phx.gbl...
> Regarding generating sequence numbers ... if the table inserted into
> (Table2) has pre-existing rows , then the problem becomes more complex ...
>
> I need to insert into Table2 from Table1.
>
> Table1 contains some rows where Table1.Col1 char(7) matches the pattern
> '555mmmm' -- but 'mmmm' is not in an increment-by-1 sequence (sample
data
> : '5550001' , '5550017' , '5550100' )
>
> Table2 contains some rows where Table2.Col2 char(7) matches the pattern
> '666nnnn' -- but 'nnnn' is not in an increment-by-1 sequence (sample
data
> : '5550001' , '5550017' , '5550100' )
>
> I need to insert one Col2 = '666nnnn' row into Table2 for each Col1 =
> '555mmmm' row in Table1 -- and I need to sequentially fill the Col2
> sequence gaps , in the order given by Col1
>
> sample data :
>
> Table2.Col2
> '6660002'
> '6660004'
>
> Table1.Col1
> '5550003' -- want to translate to Table2.Col2 = '6660000'
> '5550005' -- want to translate to Table2.Col2 = '6660001'
> '5550100' -- want to translate to Table2.Col2 = '6660003'
> '5550111' -- want to translate to Table2.Col2 = '6660005'
>
>
>
- Next message: Nigel Rivett: "RE: What makes trigger doesn't fire?"
- Previous message: Learner: "Extremely Frustrated - Still can't figure how to populate table"
- In reply to: John A Grandy: "Re: insert rows : generating sequence numbers"
- Next in thread: Tore Bostrup: "Re: insert rows : generating sequence numbers"
- Reply: Tore Bostrup: "Re: insert rows : generating sequence numbers"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|