Hello all,
I have a process where I am converting records from an old FoxPro program into MSSQL 2000. One of the FP tables has a structure similar to the one shown below in entryitems. The FP table has multiple lines used for paragraphs of information that need to be concatenated. I've also included some notes about the actual data in the records of the sample data.
I am selecting these records into an intermediate table (wrktbl) in a specific order and then updating that table with the update command shown below. So far, this works on my small example and seems to work on the actual table that has about 375k rows. The update statement relies on the records processing in the correct order (key1, key2, eline desc) to work correctly.
Can anyone tell me if I can rely on the update statement to process these rows in this order consistently?
I've also solved this problem using a stored procedure with a cursor but the cursor method takes hours to run on the 375k row table while this process takes less than a minute.
Thanks for looking at this.
Here's my sample code:
-- create sample table with data
create table entryitems (key1 char(4), key2 char(5), eline int, edate char(8), itemtext varchar(60))
insert into entryitems values ('2005','00001',1,'20050101','This is the first row of a multiline item')
insert into entryitems values ('2005','00001',2,' ','that continues on this row in paragraph form.')
insert into entryitems values ('2005','00001',3,' ','There is no fixed number of rows for any given')
insert into entryitems values ('2005','00001',4,' ','group of items.')
insert into entryitems values ('2005','00001',5,'20050101','The indication that a new group is beginning is')
insert into entryitems values ('2005','00001',6,' ','that the edate field will contain a string in')
insert into entryitems values ('2005','00001',7,' ','YYYYMMDD format.')
insert into entryitems values ('2005','00002',1,'20050101','When a new key value (key1+key2) changes or appears.')
insert into entryitems values ('2005','00002',2,' ','the first time, the eline value will start at 1 or')
insert into entryitems values ('2005','00002',2,' ','increment from the last eline value matching that key.')
insert into entryitems values ('2005','00001',8,'20050102','Some rows stand alone.')
insert into entryitems values ('2005','00002',3,'20050101','')
insert into entryitems values ('2005','00002',4,' ','')
insert into entryitems values ('2005','00003',1,'20050103','Some rows are blank.')
-- create working table with records ordered
create table wrktbl (key1 char(4), key2 char(5), eline int, edate char(8), itemtext varchar(60), combtext varchar(8000))
insert into wrktbl select key1,key2,eline,edate,itemtext,itemtext from entryitems order by key1,key2,eline desc
-- process records - concatanating the text fields on multiple lines
declare @combtext varchar(8000), @combtext2 varchar(8000)
update wrktbl
set @combtext=isnull(combtext,'')+' '+isnull(@combtext,''),
@combtext2=@combtext,
combtext=case when edate>'!' then @combtext2 else '' end,
@combtext=case when edate>'!' then '' else @combtext end
-- display results
select * from wrktbl order by key1,key2,eline
drop table wrktbl
I have a process where I am converting records from an old FoxPro program into MSSQL 2000. One of the FP tables has a structure similar to the one shown below in entryitems. The FP table has multiple lines used for paragraphs of information that need to be concatenated. I've also included some notes about the actual data in the records of the sample data.
I am selecting these records into an intermediate table (wrktbl) in a specific order and then updating that table with the update command shown below. So far, this works on my small example and seems to work on the actual table that has about 375k rows. The update statement relies on the records processing in the correct order (key1, key2, eline desc) to work correctly.
Can anyone tell me if I can rely on the update statement to process these rows in this order consistently?
I've also solved this problem using a stored procedure with a cursor but the cursor method takes hours to run on the 375k row table while this process takes less than a minute.
Thanks for looking at this.
Here's my sample code:
-- create sample table with data
create table entryitems (key1 char(4), key2 char(5), eline int, edate char(8), itemtext varchar(60))
insert into entryitems values ('2005','00001',1,'20050101','This is the first row of a multiline item')
insert into entryitems values ('2005','00001',2,' ','that continues on this row in paragraph form.')
insert into entryitems values ('2005','00001',3,' ','There is no fixed number of rows for any given')
insert into entryitems values ('2005','00001',4,' ','group of items.')
insert into entryitems values ('2005','00001',5,'20050101','The indication that a new group is beginning is')
insert into entryitems values ('2005','00001',6,' ','that the edate field will contain a string in')
insert into entryitems values ('2005','00001',7,' ','YYYYMMDD format.')
insert into entryitems values ('2005','00002',1,'20050101','When a new key value (key1+key2) changes or appears.')
insert into entryitems values ('2005','00002',2,' ','the first time, the eline value will start at 1 or')
insert into entryitems values ('2005','00002',2,' ','increment from the last eline value matching that key.')
insert into entryitems values ('2005','00001',8,'20050102','Some rows stand alone.')
insert into entryitems values ('2005','00002',3,'20050101','')
insert into entryitems values ('2005','00002',4,' ','')
insert into entryitems values ('2005','00003',1,'20050103','Some rows are blank.')
-- create working table with records ordered
create table wrktbl (key1 char(4), key2 char(5), eline int, edate char(8), itemtext varchar(60), combtext varchar(8000))
insert into wrktbl select key1,key2,eline,edate,itemtext,itemtext from entryitems order by key1,key2,eline desc
-- process records - concatanating the text fields on multiple lines
declare @combtext varchar(8000), @combtext2 varchar(8000)
update wrktbl
set @combtext=isnull(combtext,'')+' '+isnull(@combtext,''),
@combtext2=@combtext,
combtext=case when edate>'!' then @combtext2 else '' end,
@combtext=case when edate>'!' then '' else @combtext end
-- display results
select * from wrktbl order by key1,key2,eline
drop table wrktbl