Here's my idea ... create 5 working tables and one perm table, lets call them T1,T2,T3,T4,TMain and TDone for simplicity.
Create TMain w/ 2 columns. An ID field set as an integer and also set as an Identity field. This will give us a number we can increment thru. Then a field named RecordText set as a VarChar(500) to get all the data from the file in there. Better to have too much than too little - LOL.
Next task would be to import the fixed length file into the TMain.
Now create tables T1 thru T4. Start w/ an ID field like above, w/ an identity, and then having the correct fields that match the header number we may be processing. SO ... T1 would have and as an example ...
ID Int (= AutoIncrement)
SomeNbr Char(8) (= 99988777)
SomeCode1 Char(1) (= 'N')
SomeCode2 Char(4) (= 'AT01')
Name1 Char(25)(= 'JOHN')
TypePerson Char(25)(= 'BORROWER')
You get the idea. Since it was a fixed length file, you should have all the lengths to fill in the sizes above. Do this for T2-T4 tables based on the '02','03' or '04' header.
You should now create the TDone table, our finalized table, that includes all the fields that are in the text file.
Now with the table loaded we create a couple of working vars and use them in a WHILE loop to identify and then parse our record to the appropriate table.
SO ...
declare @TotRecs Int
declare @NextRecNbr Int
declare @RecordTxt VarChar(500)
select @TotRecs = Count(*) from TMain
set @NextRecNbr = 1
set @RecordTxt = ''
WHILE @NextRecNbr <= @TotRecs
BEGIN
SELECT @RecordTxt = RecordTxt FROM TMain
WHERE ID = @NextRecNbr
IF SUBSTRING(@RecordTxt,1,2) = '01
BEGIN
INSERT INTO T1 ( SomeNbr,
SomeCode1,
SomeCode2,
Name1,
TypePerson )
VALUES
( SUBSTRING(@RecordTxt,3,8),
SUBSTRING(@RecordTxt,12,1),
SUBSTRING(@RecordTxt,14,4),
SUBSTRING(@RecordTxt,19,25),
SUBSTRING(@RecordTxt,35,25) )
END
IF SUBSTRING(@RecordTxt,1,2) = '02
BEGIN
INSERT INTO T2 ( *Fill In fields* )
VALUES
( *File In Substring code* )
END
IF SUBSTRING(@RecordTxt,1,2) = '03
BEGIN
INSERT INTO T3 ( *Fill In fields* )
VALUES
( *File In Substring code* )
END
IF SUBSTRING(@RecordTxt,1,2) = '04
BEGIN
INSERT INTO T4 ( *Fill In fields* )
VALUES
( *File In Substring code* )
END
SET @NextRecNbr = @NextrecNbr + 1
END
The code above uses the SUNSTRING Function to parse out the desired field based on its starting position and length. So you may want to try each of the SUBSTRING commands in Query Analyzer first to get the settings right.
OK ... since your records in the file where in order and should have been loaded that way into the table, the logic follows that the first record read w/ be '01' and put in table T1 w/ and ID of 1. Next record read in the loop will be '02' and put into the T2 table w/ and ID of 1. Same for '03' & '04' going into T3 and T4 w/ an ID of 1. SO ... you may be seeing what is happening. The 5th record read is really the next TRUE record to start and should have an '01' in it. It would then be place in the T1 table w/ ID = 2 and then next 3 records will be '02','03' and '04' which get put in the T2..T4 tables w/ ID of 2
When this is all completed you can then run a query, and use it for your last import into TDone that may look like this...
SELECT *
FROM T1
JOIN T2
ON (T1.ID = T2.ID)
JOIN T3
ON (T1.ID = T3.ID)
JOIN T4
ON (T1.ID = T4.ID)
WaLa ... You are there. Hope this helps and if you need some more tips or explaination ... post again
Thanks
J. Kusch