Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CURSORS AND STRING MANIPULATION

Status
Not open for further replies.

GShen

MIS
Sep 26, 2002
561
0
0
US
Hi,
Sorry to get wordy but I needed to in order to explain.
I have a flat file which needs to be parsed. The file gets created from someplace which cannot be changed. It is 80 bytes long fixed length. The data wraps, so I can have part of my data on 1 line and continue on the next. Also I only need data from 1-70. 71-80 is not used, so my data actually wraps after char 70. I created a DTS to pull the data into an ACCESS table (ADP). I broke it into 2 fields, 1 of 70 and the other of 10. All relative simple stuff. Now the good part. I wrote a cursor using a ton of SUBSTRING verbs in order to parse the data. There are multi. field lengths, records types, etc. It is now working with the output being inserted into 2 tables. I basically fetch 1 record (of 70) and substring 8 or 13 depending upon a few rules. If the data is an order, I basically have an item and a qty. If it is a retail, I have 3 pieces of info. Orders create 1 table the retails another.
HERE IS THE PROBLEM. SPEED!! I runs like a dog. 297 input records creating 2227 item records takes 36 seconds.
No good. I can't imagine how long 80,000 item records would take. This is currently being done with a PC version of Cobol. It eats this stuff up and runs a 80,000 item in about 3 seconds.
I was thinking of trying to put the 70 char records together using a few DTS's and somehow get the data in parsed using the delimeter (+ sign). Is this feasible? Is the SUBSTRING a hog? I was thinking of an array, not sure if that would speed it up because eventually the record has to be written somewhere, whether it be a flat file or an ACCESS table. I was trying to get the each delimited piece into an actual record but DTS gets a little funky when trying to do this.
I am open to any suggestions..........
thanks,

Remember when... everything worked and there was a reason for it?
 
I had a similar situation. I approached it in 3 steps.

1) Write the file to a new file with a useful format.
Best speed is obtained by reading in 64K chunks. You can find help on this by searching on large file processing. Write out the data to a new file in 80 char lines.

2) Import the new file into a temp table with one long char column.
Once you have a good file layout use BULK INSERT to import the entire file at once.

3) Parse the temp table into destination tables.
Now you should be able to use 2 queries (1 for orders and 1 for retail) using ubstring to select records and pieces of records you want. As in:
Code:
insert into orders_table
select substring(col, 2, 9)
      ,substring(col,11,10)
from temp_table
where substring(col,1,1) = '1'  -- orders

insert into retail_table
select substring(col,30,10)
      ,substring(col,40,10)
from temp_table
where substring(col,1,1) = '2'  -- retail

You could use a DTS with an ActiveX task and a SQL task. Or you could just use Access VBA.
 
ok your files looks likes this:
this is the data from line one to 70 of the record
and this is the wrapped up line.

ok, go ahead and load "all" your data into a table
the first line will have columns 1-70 filled ok in the row
however, the second record will have columns 11-xx blank
after you load your flat file, write a delete query and delete all records whose columns 11-xx are blank
like this
delete * from mytable where mid(col_name,11,70) is null;
 
alidabiri - you sort of addressed a problem of removing 71-80 but that is not the problem I am having.
thanks anyway.

cbhnp - Ok, you gave me food for thought. I looked up large file processing and found 41 entries. Not sure what I was looking for.
I am not sure as to what you are saying in step 1. Do you want me to create a DTS and load it to another flat file or an ACCESS file like I am doing?
You said using a useful format. Not sure what you mean there. Currently loading to ACCess table 1-70 (field 1) and 71-80 field 2. I can take that table and reload another flat file using field 1 and get 1-70 only using a stored procedure.
In step 2, take the file above(not sure which one yet) and do a BULK INSERT to a temp table. Haven't used bulk insert..reading up on it now. Do I run that from a stored procedure or from a DTS? Again, not sure what A GOOD file layout means and exactly how I get it into a 1 char field.
Do I define a table with 1 char? So my each record would contain 1 char?
In step 3
select substring(col, 2, 9)
,substring(col,11,10)
from temp_table
where substring(col,1,1) = '1' -- orders
Assuming col is a field of 1 char. You have 2 for 9, 11 for 10). How would it know how long a string I have or how many records I have? I could have 1 item for an order or 3000 items for an order.

Here is a sample of the data:
(record 1 is always blank)
(record 2 'L' record type and any other record starting with an 'L' is the beginning of a new customer. The next record (3) is where I find out if it is a order or retail.
At the end of rec. 3 you will notice 0586 (col 67-70). The numbers after that are last 10 charecters in my 2nd field which I do not use. That is the 1st 4 positions of my item on rec. 4 you will see 0E03. That is the rest of it. You will also notice that the end of line 4 ends with an 06 (only 2 valid char. of the next record). This always changes which is why I was trying either lump all the records together or somehow put them vertical in different records. There are a bunch of other rules, but I can figure them out. I just need to get the basics down. I also need to get this all to run in under 5 seconds.
Line 20 starts a new order.
------------------------------------------------------
tblati_ID tblati_Data tblati_RestOfData
1 0000000296
2 L2 #0243801 11:34:25 D=0127 E=0000 S#31165002-02-06 AUTH#02438 OK 0243801000
3 A311650AA04+72951E03+13400E01+13280E01+52184E02+52024E01+05930E12+0586 0243801001
4 0E03+05960E03+05850E03+05880E03+05940E03+05942E03+06060E03+06062E03+06 0243801002
5 070E03+06055E03+06080E03+06100E03+06090E03+06092E03+06101E03+06105E02+ 0243801003
6 05870E01+05890E01+13230E01+13350E01+13370E01+13440E01+38520E01+38555E0 0243801004
7 1+06831E08+06832E08+06833E04+06834E04+06769E01+06771E01+06381E01+06411 0243801005
8 E01+06493E01+06491E01+06791E02+03940E01+03970E01+03930E01+07068E01+070 0243801006
9 75E01+30056E01+03310E01+03110E02+99020E03+19880E01+16880E01+45109E01+4 0243801007
10 5105E01+22810E01+22790E01+22760E01+20490E01+50530E01+50600E01+04290E01 0243801008
11 +01486E01+06390E02+50520E01+51610E01+73730E01+71600E06+71605E03+09510E 0243801009
12 03+09500E01+07361E02+09480E01+09490E01+09110E01+09610E01+09520E01+0913 0243801010
13 0E01+09630E01+09600E01+09150E01+09540E01+09060E02+09030E03+09310E01+75 0243801011
14 180E01+75178E01+75500E01+75550E01+75470E01+75580E01+75600E01+75610E01+ 0243801012
15 75620E01+75530E01+75510E01+75520E01+75480E01+75540E01+75590E01+75630E0 0243801013
16 1+50630E15+63070E15+63120E05+62640E05+61310E10+61300E02+61460E02+62490 0243801014
17 E03+62510E04+03418E08+05000E06+03500E06+19780E10+19790E03+15280E05+152 0243801015
18 70E01+03610E02+03720E02+03740E02+03520E02+03525E02+03523E03+03410E04+0 0243801016
19 3413E01+03415E03+99999+ 0243801017
20 L2 #0243901 11:38:15 D=0659 E=0000 S#02271202-02-06 AUTH#02439 OK 0243901000
21 A022712AA01+22539E02+22615E01+22607E01+22346E01+22623E01+22609E01+2235 0243901001
22 6E01+22492E01+22448E22+22718E02+22722E02+22702E01+22174E02+71050E01+71 0243901002
23 060E01+71005E01+70734E02+71094E02+38595E01+38596E01+38640E01+71701E01+ 0243901003
24 71700E01+40160E01+40164E02+40079E01+25737E02+25733E01+25733E01+25735E0 0243901004
25 3+19998E01+19997E01+20180E01+20172E01+21039E01+20986E01+23759E01+23766 0243901005
26 E01+23767E02+23773E03+23765E02+23769E02+23774E01+23762E01+23728E01+237 0243901006
27 29E01+23011E01+67657E30+67648E01+56090E01+56092E01+56015E01+56014E01+5 0243901007
28 5997E02+55998E02+56080E01+56076E01+68981E02+69008E05+69011E05+69002E01 0243901008
29 +69000E01+69004E02+68990E03+69032E01+25878E02+69995E05+70188E01+28714E 0243901009
30 01+28191E01+28840E02+28450E01+29096E01+29033E01+29064E01+29103E01+2835 0243901010
31 4E02+28441E01+28441E01+28113E02+27981E01+28043E02+28032E02+28582E01+28 0243901011
32 577E01+28583E01+28579E02+22685E01+55244E02+55587E01+55462E03+55458E02+ 0243901012
33 55455E01+55299E01+55264E01+55270E01+55292E01+55256E01+55298E01+55236E0 0243901013
34 1+55502E02+55461E02+55452E02+55548E01+55547E01+55140E01+55162E01+55545 0243901014
35 E03+55456E02+55440E03+55283E01+55242E01+55073E01+55025E01+55026E01+550 0243901015
36 65E01+41034E01+41114E01+41136E01+41038E01+39249E01+72020E01+72018E02+7 0243901016
37 2016E01+72013E01+16990E15+17050E01+57083E02+57075E04+57078E02+57082E03 0243901017
38 +57106E05+57072E04+57077E01+52462E01+52461E01+19028E01+52520E01+43323E 0243901018
39 08+43208E02+43309E02+43079E01+43084E01+43099E01+74507E03+74508E02+7470 0243901019
40 3E02+73760E01+74541E02+74561E01+74559E01+74100E01+74324E01+75203E02+75 0243901020
41 876E01+75206E03+75864E01+75865E01+73799E01+73790E01+75035E01+74494E01+ 0243901021
42 75017E01+75060E01+75407E01+75404E01+75015E02+73758E02+75023E01+73771E0 0243901022
43 1+74705E01+73239E01+73258E01+73263E01+73352E01+73236E01+73229E01+57088 0243901023
44 E02+57248E01+57250E01+75583E01+75538E01+75615E01+75617E01+76277E01+760 0243901024
45 40E02+76130E01+66180E01+66122E01+66132E02+66134E02+66155E01+66139E01+6 0243901025
46 6014E01+66317E01+66034E01+66268E01+66362E01+66359E01+66344E01+66354E01 0243901026
47 +66246E01+66355E01+66348E01+66350E01+66340E01+66287E01+66336E01+66347E 0243901027
48 01+73767E01+65354E01+65653E01+65687E01+65689E01+65679E01+65683E01+6568 0243901028
49 2E01+65092E01+95062E01+65055E01+65047E01+65038E01+64979E01+64980E01+65 0243901029
50 027E01+65085E01+65093E01+66048E01+66046E01+66045E01+66040E01+66042E01+ 0243901030
51 66114E02+66112E01+66111E01+66115E01+76041E01+76025E01+76330E01+76326E0 0243901031
52 1+75622E01+73582E01+13154E02+13122E01+13136E01+13161E01+13142E01+54199 0243901032
53 E03+54185E01+54031E01+54028E02+54182E01+54003E01+54134E01+54210E01+541 0243901033
54 03E01+24072E02+24067E02+24077E01+23623E01+75501E01+23896E01+23894E01+1 0243901034
55 4648E01+14649E01+14624E01+17779E01+14768E01+24048E01+14772E01+14659E01 0243901035
56 +15072E01+31524E01+31412E02+31411E01+31474E02+29356E01+20411E01+20416E 0243901036
57 01+24121E01+24120E01+15527E02+15475E01+15534E01+23830E01+42222E01+4221 0243901037
58 6E01+42268E01+42263E01+42204E01+42206E01+42265E01+18076E01+18292E01+18 0243901038
59 278E01+18274E02+18179E02+18074E01+18320E03+18316E02+18144E01+17784E01+ 0243901039
60 18039E02+18031E02+18045E01+18044E01+18046E01+18037E01+18040E02+18030E0 0243901040
61 2+18036E01+18041E01+17450E01+17940E01+17946E02+17949E01+17900E01+17903 0243901041
62 E01+17904E01+17574E01+17771E01+17713E01+17779E01+29320E01+29343E01+175 0243901042
63 99E01+37888E03+37924E10+37924E02+37917E05+37919E04+37936E02+37935E05+3 0243901043
64 7879E14+37920E15+37915E20+37923E10+37925E05+37922E07+10143E04+37900E03 0243901044
65 +37901E01+37939E02+38460E03+38464E01+38462E01+38461E02+38095E03+37948E 0243901045
66 01+38093E03+38103E01+38099E01+38101E01+38098E01+38145E03+11289E01+1128 0243901046
67 8E01+11287E01+11524E01+11532E01+11382E01+11385E01+11438E01+11358E01+11 0243901047
68 311E01+11328E01+10913E01+10944E01+10935E01+11005E01+10882E01+11004E01+ 0243901048
69 10989E01+10183E03+10320E01+10250E03+10293E03+10334E02+10162E02+10180E0 0243901049
70 4+10192E03+10234E03+10288E03+10316E03+10236E02+10240E03+10284E03+10099 0243901050
71 E01+10798E01+10145E02+10144E02+10158E02+10724E01+10716E01+10703E01+100 0243901051
72 06E02+10591E01+47384E03+53283E02+47240E02+47364E01+53364E04+53358E01+5 0243901052
73 3264E02+53356E01+53392E02+47229E02+48401E01+48399E01+48413E01+48396E01 0243901053
74 +48417E01+48436E01+48400E01+48414E01+48439E01+48405E01+48397E01+48408E 0243901054
75 01+48989E01+48374E01+48390E01+48750E01+48766E01+48795E01+48783E01+4871 0243901055
76 7E01+48757E01+48768E01+48751E01+48947E01+48756E01+48787E01+48742E01+48 0243901056
77 718E01+48923E03+48922E02+48843E01+48944E01+48902E01+48869E01+48601E01+ 0243901057
78 48600E01+48604E01+48605E01+48606E01+48936E01+48123E02+48181E01+48111E0 0243901058
79 1+48064E01+48188E02+47666E01+47669E01+47670E01+47566E01+47583E01+47828 0243901059
80 E01+47796E01+45279E02+45275E01+45309E01+45308E03+45404E01+45407E01+451 0243901060
81 60E01+45282E01+45307E03+45306E01+45093E01+45124E01+45231E01+45078E01+4 0243901061
82 4883E01+45080E01+45076E01+44878E01+45081E01+46019E01+46101E02+46201E02 0243901062
83 +46015E01+45984E01+45946E01+45881E01+45940E01+45967E01+45892E01+45766E 0243901063
84 01+45913E01+45521E01+45686E01+45425E01+46842E01+46803E01+46843E01+7469 0243901064
85 1E20+36028E01+36086E01+36202E02+36203E01+36219E01+36207E02+36221E01+36 0243901065
86 232E02+37127E01+37147E01+37200E01+37207E01+36189E02+37211E01+37119E01+ 0243901066
87 36741E01+36502E01+36503E02+36778E20+36374E01+36379E01+37095E01+37090E0 0243901067
88 1+37103E01+36457E02+37081E01+37080E01+37687E02+36546E01+38283E01+37481 0243901068
89 E01+37474E01+37473E01+37494E01+37480E02+37489E01+30214E01+31442E01+313 0243901069
90 41E01+31447E01+23747E01+23745E01+23743E04+23751E01+23740E01+23752E01+2 0243901070
91 3739E01+31015E02+30911E01+30655E01+30643E02+31085E02+30908E01+30885E01 0243901071
92 +30652E01+31152E01+31132E01+36035E02+36003E02+37383E02+37455E02+37886E 0243901072
93 04+38247E01+43000E01+27203E01+27760E01+76560E01+27835E02+27782E03+3923 0243901073
94 8E01+39476E01+51098E01+52015E01+52041E02+52077E02+52043E02+52076E01+52 0243901074
95 074E01+52040E01+52319E01+52326E01+52315E01+52318E01+52439E01+52441E04+ 0243901075
96 52430E01+52426E02+52386E01+52383E01+54233E01+54234E01+52440E50+63715E0 0243901076
97 3+63716E04+63722E02+63728E01+63724E03+63730E01+63726E01+63732E01+63667 0243901077
98 E01+63668E01+63673E01+63663E02+63678E02+63711E02+63699E01+63698E01+637 0243901078
99 14E02+63712E04+63649E03+63650E06+63655E15+63640E03+63645E01+63641E02+6 0243901079
100 3651E02+63664E03+63725E07+63720E08+63729E12+63633E20+63634E05+63632E15 0243901080
101 +63703E15+63681E40+63734E10+63475E04+63504E01+63584E01+63486E01+63553E 0243901081
102 01+63499E01+63497E02+63513E01+63578E01+63576E01+63576E01+62982E01+6282 0243901082
103 2E01+62810E01+62814E01+62815E01+63244E01+63594E01+62990E01+63796E01+63 0243901083
104 322E01+63069E01+63041E01+63083E01+63142E01+63053E01+63156E01+63167E01+ 0243901084
105 68993E60+68993E60+69015E50+39354E99+39354E15+99999+ 0243901085
106 L1 #0244001 11:39:36 D=0979 E=0000 S#02271802-02-06 AUTH#02440 OK 0244001000
107 A022718AA01+31446E02+31382E01+31435E01+31429E01+31432E01+31360E01+3143 0244001001
108 7E01+30181E02+31148E01+31116E01+23694E02+23750E02+23693E01+23746E02+23 0244001002
109 750E02+23745E02+23748E03+23691E03+23671E04+23672E03+23670E03+30887E03+ 0244001003
110 30913E01+30885E02+31037E01+30882E01+30643E01+38240E01+38270E01+36097E0 0244001004
111 1+36098E01+36122E01+36124E02+37452E01+37456E02+36123E03+35990E01+35992 0244001005
112 E01+37460E01+37458E03+37453E02+37455E03+37459E02+37886E03+14657E01+146 0244001006
113 69E01+14747E01+14748E01+14769E01+14895E02+14809E02+14827E01+14562E02+3 0244001007
114 6060E01+37125E01+36197E01+37132E01+37204E01+36756E01+36041E01+36042E01 0244001008
115 +36040E01+36735E02+37177E01+37202E01+36530E01+36503E02+37213E02+37212E 0244001009
116 02+36194E01+36331E01+36771E01+36175E01+36207E03+36219E01+36382E01+3675 0244001010
117 7E03+36461E03+37095E01+37081E01+37094E01+37092E01+37535E01+37846E01+38 0244001011
118 116E01+36296E03+37474E01+37480E04+37475E03+37481E01+37476E01+37488E01+ 0244001012
119 37489E01+36184E01+37494E01+38313E01+38373E01+38120E01+38126E01+28080E0 0244001013
120 1+28049E01+28047E01+28003E02+28082E03+28446E01+28452E01+28450E01+28388 0244001014
121 E01+28441E01+28760E01+28220E01+28447E01+28581E01+28600E01+29104E01+291 0244001015
122 01E01+29064E01+29100E01+29103E01+29097E01+29062E02+28704E02+12133E01+1 0244001016
123 2140E01+44808E01+12324E02+44802E01+12054E01+23765E01+23774E01+23773E01 0244001017
124 +23762E01+23114E01+23042E02+23029E02+23008E01+23014E02+23011E02+23013E 0244001018
125 02+23046E02+23142E01+23001E02+23006E02+23026E01+23035E02+23262E01+2344 0244001019
126 7E01+23442E01+23135E01+25892E01+25884E01+25890E02+25898E01+25893E01+25 0244001020
127 895E02+25878E02+69002E01+69000E02+69004E02+69040E01+69011E05+69015E10+ 0244001021
128 56015E01+56036E01+56007E01+56092E01+67612E01+67480E02+40032E01+40031E0 0244001022
129 1+25736E04+25735E04+25737E04+25733E08+40228E01+40192E01+40193E01+40195 0244001023
130 E01+40200E01+21072E02+21073E02+21078E01+21022E02+21039E02+21045E01+209 0244001024
131 86E01+20141E01+20148E05+20147E05+21041E01+21047E01+21049E05+38694E05+3 0244001025
132 8688E01+38588E01+38685E01+71138E01+71155E02+71150E02+71154E02+71156E02 0244001026
133 +71148E01+71147E01+71027E01+71012E01+71005E02+71065E01+71050E02+22707E 0244001027
134 01+22702E01+22721E01+22720E01+22669E01+22346E01+22256E01+22549E01+2225 0244001028
135 7E01+22200E01+22174E01+48134E10+48172E02+48123E01+47564E01+47687E01+47 0244001029
136 477E01+47839E02+47851E01+47810E01+47814E01+47794E01+47812E01+47795E01+ 0244001030
137 11259E02+11261E01+11286E01+11288E01+11290E01+11204E01+47184E01+47167E0 0244001031
138 1+47100E01+47167E01+47147E01+47023E01+48845E01+48918E03+48943E02+48922 0244001032
139 E02+48936E02+48913E01+48926E02+48944E01+48939E01+48935E01+22723E01+227 0244001033
140 22E01+48474E01+48469E01+48410E01+48412E01+48819E01+48738E01+48784E01+4 0244001034
141 8375E01+48378E01+48382E01+48374E01+48390E01+48680E01+48669E01+48512E01 0244001035
142 +48513E01+48682E01+48843E01+48530E01+48988E01+48983E01+33133E01+32924E 0244001036
143 02+33084E02+32900E02+32910E01+32922E02+32905E05+33089E01+32932E01+3291 0244001037
144 2E01+33391E02+33392E01+33394E01+32570E01+33328E01+33457E01+33389E01+33 0244001038
145 344E01+33335E01+33222E01+35562E01+35563E01+35539E01+60942E01+62277E01+ 0244001039
146 62273E01+62271E01+60964E01+60794E01+60852E01+60928E01+60935E01+60926E0 0244001040
147 1+60938E01+60788E02+62170E01+62195E01+60039E01+60189E01+60141E01+35625 0244001041
148 E01+35637E01+35621E01+35630E01+35687E01+35744E01+35740E01+35703E01+353 0244001042
149 75E01+35378E01+35747E01+35702E01+35389E01+35390E02+35377E01+35380E01+3 0244001043
150 4411E01+34576E01+60774E01+60775E01+60682E01+60785E01+60778E01+60850E02 0244001044
151 +60709E02+33968E01+33930E01+33932E01+33926E01+33927E01+34475E01+34472E 0244001045
152 01+33018E01+33016E01+34179E01+35831E01+34300E01+34296E02+34396E01+3438 0244001046
153 6E01+34256E01+34111E01+33634E01+33654E01+33641E01+33643E02+33582E01+34 0244001047
154 373E01+34351E01+34365E01+34346E01+33640E01+33684E02+33661E01+33596E01+ 0244001048
155 33683E01+33593E01+33553E01+33554E01+33801E01+33802E01+33794E01+34784E0 0244001049
156 1+33742E01+34748E01+34751E02+38242E02+37906E01+37888E02+38103E02+38093 0244001050
157 E01+38101E02+38095E02+37879E08+37935E09+38464E02+38462E01+38461E04+381 0244001051
158 45E03+37936E08+37936E03+37933E05+37934E01+37897E02+37922E15+37924E04+3 0244001052
159 7925E04+38159E02+37917E06+38439E09+38438E35+13607E01+67607E01+13613E01 0244001053
160 +13611E01+13662E02+13642E01+13638E01+13627E01+47304E01+47380E01+47381E 0244001054
161 01+47343E01+53392E01+53280E01+53250E01+47352E01+47362E01+47354E03+5297 0244001055
162 9E01+52975E01+53362E01+47384E03+53283E01+47365E02+38438E35+17941E01+17 0244001056
163 645E01+17703E01+17640E01+84521E02+17939E04+17940E01+17888E01+17828E01+ 0244001057
164 17813E01+17904E01+17903E01+17450E02+17560E01+17513E01+17550E01+17472E0 0244001058
165 1+17756E01+17644E01+17771E01+17740E01+17737E01+17470E02+17632E01+17686 0244001059
166 E01+17741E01+17743E01+17694E01+18271E01+18144E01+18316E02+18069E01+182 0244001060
167 81E01+18275E01+18344E01+86079E01+18062E01+18320E02+18285E01+18278E01+1 0244001061
168 8400E01+18179E01+18074E01+29351E01+29303E01+29380E01+29353E01+29308E01 0244001062
169 +29371E01+23920E01+29375E01+29363E01+29378E01+29300E01+20428E01+20413E 0244001063
170 01+31488E01+31426E01+31412E01+31512E01+31485E01+31487E01+31992E01+3198 0244001064
171 9E01+32001E01+70007E01+70020E01+28301E01+28277E01+18051E03+18052E02+18 0244001065
172 037E02+18040E02+18031E02+18035E02+18032E04+18033E03+28292E10+28294E15+ 0244001066
173 28272E10+42268E01+42204E01+42250E01+42068E01+14683E01+14649E01+15488E0 0244001067
174 1+15501E01+15500E04+15496E01+15504E02+15511E01+15515E01+15475E01+23894 0244001068
175 E01+23900E01+24165E01+42261E01+45898E01+45931E01+45960E01+45913E02+458 0244001069
176 88E01+45875E01+45751E01+45894E01+45822E01+45750E01+46328E01+46099E01+4 0244001070
177 6105E02+46101E01+45984E01+46003E02+46012E01+45660E01+45563E01+45521E01 0244001071
178 +45672E01+45712E01+45714E01+46913E01+46680E01+46650E01+46781E01+46699E 0244001072
179 01+46728E01+46876E01+46664E01+46671E01+46648E01+46720E01+46842E01+4692 0244001073
180 4E01+45074E01+45076E01+45078E01+45084E01+45083E01+44880E01+45139E01+45 0244001074
181 185E01+45135E01+45059E01+45126E01+45396E01+45383E01+45115E01+45116E01+ 0244001075
182 45387E01+45282E01+45305E01+45275E01+45306E01+45385E01+45280E01+45308E0 0244001076
183 1+45420E01+45272E01+65628E01+65679E01+65316E01+65541E01+65547E01+65593 0244001077
184 E01+65043E01+65055E01+64979E01+65036E01+65592E01+65582E01+65280E01+656 0244001078
185 83E01+65682E02+65700E01+65601E01+65702E01+65705E02+65701E01+65680E01+6 0244001079
186 5689E01+65154E01+65094E01+65136E01+65098E01+64981E01+65085E01+65097E01 0244001080
187 +65559E01+64993E01+65653E02+65651E01+65398E01+65407E01+65615E01+65690E 0244001081
188 01+65687E01+13710E01+66104E01+66099E01+66100E01+66093E01+66101E01+6609 0244001082
189 5E01+66120E01+66230E01+66238E01+76330E02+76328E02+76326E01+76325E01+76 0244001083
190 285E01+76061E01+76499E01+76008E01+76045E01+76505E04+76503E04+76498E01+ 0244001084
191 66360E01+66006E01+66050E01+66139E01+66123E01+66051E01+66336E01+66307E0 0244001085
192 1+66131E01+66134E01+66152E01+66339E01+66268E01+66267E01+76277E02+76332 0244001086
193 E02+76007E01+76250E01+52426E01+52445E01+52429E01+52394E01+52383E01+523 0244001087
194 93E01+52402E01+52366E01+52358E01+52356E01+52390E01+52441E01+52412E01+5 0244001088
195 2421E01+52422E01+52391E01+52386E01+52388E01+52406E01+52424E01+52319E01 0244001089
196 +52043E03+51098E01+52075E02+73239E01+39262E01+76560E01+27760E01+27764E 0244001090
197 01+27371E01+27370E01+27528E01+39251E01+27036E01+27055E01+27772E01+2728 0244001091
198 6E01+39238E01+73263E01+52040E01+52164E01+52140E01+52021E01+52003E01+52 0244001092
199 029E01+52044E01+52036E02+52022E01+52172E01+52165E01+52086E02+52017E01+ 0244001093
200 52041E01+79508E04+74499E02+74507E03+74542E01+74559E02+74541E03+74561E0 0244001094
201 1+73766E01+73770E01+73783E01+75019E02+73565E01+74703E01+74729E01+75203 0244001095
202 E02+74142E01+74330E01+75582E01+75583E01+19316E01+75534E01+52510E01+193 0244001096
203 18E01+57257E01+74963E01+75382E01+75876E01+75865E01+73759E01+74565E01+7 0244001097
204 4518E05+74566E05+73758E01+73774E01+74572E01+74515E01+73757E01+73787E01 0244001098
205 +73808E01+74070E01+75035E01+73558E01+73590E01+73767E01+52513E01+52506E 0244001099
206 01+51017E01+54016E01+13162E01+13136E01+13152E01+13146E01+13120E01+5419 0244001100
207 9E01+13132E01+54292E01+54290E01+54210E01+54157E01+54127E01+54147E01+54 0244001101
208 116E01+54124E01+54196E01+54175E02+54200E01+54178E01+54182E01+54013E01+ 0244001102
209 54126E01+63578E01+63475E01+63497E01+63207E01+63247E01+63518E01+63486E0 0244001103
210 1+63576E01+63504E01+63584E01+63514E01+63498E01+63518E01+63216E01+63200 0244001104
211 E01+63221E01+39137E01+63529E01+63167E01+63053E02+63007E01+63088E01+630 0244001105
212 14E01+63018E01+63017E01+63175E01+63101E01+63008E01+63016E01+63112E01+6 0244001106
213 3099E01+63071E01+63693E01+63828E01+63829E01+63830E01+63246E01+62824E01 0244001107
214 +62807E01+63613E03+63612E01+63632E20+18269E01+18320E01+55244E01+55011E 0244001108
215 01+55334E01+55272E01+55263E01+55292E01+55237E01+55179E01+55180E01+5539 0244001109
216 1E01+55457E01+55441E01+55455E01+55456E01+55440E02+55462E01+55547E01+55 0244001110
217 546E01+55461E01+55452E01+55140E01+55186E01+55260E01+55250E01+55299E01+ 0244001111
218 55025E01+55420E01+41050E01+41027E01+41039E01+55051E01+55412E01+55052E0 0244001112
219 1+55413E01+40986E01+41121E01+17031E01+17061E01+17010E01+17044E01+57107 0244001113
220 E01+57035E01+57054E01+57111E01+72013E01+72001E01+43300E01+43228E01+720 0244001114
221 04E01+72016E01+72018E01+43156E01+43022E01+42998E01+43025E01+43083E01+1 0244001115
222 1137E01+11120E02+11088E01+11080E02+11081E01+11130E01+11131E01+11084E01 0244001116
223 +11125E02+11128E01+11123E01+10886E01+10888E02+10883E03+10889E02+10144E 0244001117
224 01+11003E01+11002E01+10335E01+10346E02+10336E02+10337E01+10351E01+1034 0244001118
225 9E01+10359E01+10004E01+10352E01+10622E01+10628E01+10625E01+10592E01+10 0244001119
226 590E01+10620E01+10626E01+10143E07+10382E01+10385E01+10348E01+10338E01+ 0244001120
227 10911E01+11136E01+10777E01+10965E01+10372E01+10138E01+10362E01+10364E0 0244001121
228 1+10379E01+10381E01+10792E01+10798E01+10776E01+24065E03+24072E01+24070 0244001122
229 E01+24074E01+24067E01+23770E01+10721E01+10724E01+10706E01+10716E01+107 0244001123
230 15E01+10703E01+11004E01+43108E72+43108E72+74566E30+68993E60+69008E49+1 0244001124
231 7459E02+18013E01+17920E01+11854E01+11800E05+11784E05+11791E15+11751E02 0244001125
232 +11754E02+25872E03+25871E01+25874E02+25641E02+25715E01+99999+ 0244001126
233 L2 #0244101 11:44:49 D=0308 E=0000 S#00023202-02-06 AUTH#02441 OK 0244101000
234 A000232AA01+60305E01+60306E01+60036E01+60227E01+60874E01+60870E01+6087 0244101001
235 1E01+34407E01+35712E01+35390E01+34568E01+35612E01+45307E03+45229E01+45 0244101002
236 387E02+45401E01+45407E01+45388E01+45392E01+45404E01+45284E01+45078E01+ 0244101003
237 45126E01+46800E01+46826E01+45212E01+45652E01+47188E01+45625E01+45701E0 0244101004
238 3+45714E01+45704E02+46011E01+46010E01+46018E01+46019E01+46099E01+46000 0244101005
239 E01+46101E01+46197E01+45947E01+45913E01+45946E01+45881E01+45888E01+458 0244101006
240 75E01+45940E01+45892E01+45817E02+45819E01+33611E01+33597E01+34745E01+3 0244101007
241 4748E02+33753E01+33531E02+33558E01+33839E01+33833E01+33841E01+34221E01 0244101008
242 +34371E01+35851E01+33017E01+62197E01+60897E01+60688E01+60929E01+60926E 0244101009
243 01+60937E01+60932E02+60938E01+60927E01+60884E01+33335E01+33337E01+3331 0244101010
244 7E01+32923E01+32922E01+32905E01+33091E01+33282E01+48280E01+48271E01+48 0244101011
245 289E01+48471E01+48469E01+48766E01+48694E01+48713E01+48669E01+48364E01+ 0244101012
246 48867E01+48866E01+48820E01+48819E01+48397E01+48146E01+48180E01+48242E0 0244101013
247 1+47823E01+47834E01+47835E01+47804E01+47794E01+47824E01+47802E01+47528 0244101014
248 E01+47545E01+47544E01+47592E01+11326E01+11327E01+47343E01+47379E01+529 0244101015
249 79E01+53292E01+13510E01+51016E01+34830E01+34831E01+33532E01+33777E01+3 0244101016
250 3855E01+33823E01+33729E01+33845E01+34412E01+34510E01+34590E01+62392E01 0244101017
251 +62393E01+62395E01+60138E01+45817E01+25739E02+25738E01+25862E01+25863E 0244101018
252 01+10154E01+10155E01+10144E01+10145E01+10156E01+10335E01+10338E01+1035 0244101019
253 3E01+10346E02+10337E01+10336E01+10349E01+10578E01+10018E01+10018E01+25 0244101020
254 872E01+25874E02+11751E03+11817E02+11819E01+22256E01+22198E01+22174E01+ 0244101021
255 71150E01+20988E01+20992E01+20172E01+37232E02+37406E01+36203E01+37204E0 0244101022
256 1+37039E01+36086E02+36458E01+37092E01+37095E01+37844E01+37853E01+38286 0244101023
257 E01+37847E01+37846E01+37640E01+36061E01+37473E02+37489E01+37488E01+374 0244101024
258 94E02+37480E01+37488E01+37489E01+36124E01+37457E01+35992E01+37463E01+3 0244101025
259 7461E01+37460E01+37561E01+37383E01+38120E01+38124E01+31116E01+31114E01 0244101026
260 +23747E01+23748E01+30652E01+38092E01+38101E01+37888E05+37924E10+31357E 0244101027
261 01+31446E01+55042E01+55411E01+57106E01+57076E01+19312E01+55106E01+7227 0244101028
262 6E01+55170E01+43308E01+43311E01+55288E01+55269E01+43204E01+43081E01+43 0244101029
263 046E01+43090E01+43084E01+70001E01+70011E01+69997E01+28277E01+28043E01+ 0244101030
264 28084E01+56000E01+56004E01+56080E01+56076E01+68981E01+69004E01+68990E0 0244101031
265 1+69000E01+69007E20+68993E10+24074E01+24065E01+14751E01+17740E01+17737 0244101032
266 E01+17735E01+17743E01+17903E01+17592E01+18036E01+18033E01+18034E01+183 0244101033
267 02E01+29308E01+31502E01+31487E01+31488E01+52325E01+23873E01+23872E01+2 0244101034
268 3829E01+17473E01+17599E01+17484E01+17480E01+31556E01+31555E01+20411E01 0244101035
269 +20412E01+20416E01+28450E01+28451E01+28452E01+14875E01+17507E01+17510E 0244101036
270 01+17618E01+69993E01+43126E01+43108E05+43099E01+56032E01+39354E01+5416 0244101037
271 6E01+63720E05+63721E05+63725E05+63724E05+17037E03+65024E09+64998E02+65 0244101038
272 000E01+65088E01+65050E01+65700E01+65701E01+65702E01+65703E01+65704E01+ 0244101039
273 65705E01+65706E01+65707E01+65708E01+99999+ 0244101040
274 L1 #0244201 11:48:03 D=0164 E=0000 S#01113102-02-06 AUTH#02442 OK 0244201000
275 A011131AA01+63049E01+63011E01+63072E01+63211E01+63092E01+63355E01+6355 0244201001
276 6E01+63553E01+63584E01+63809E01+63870E01+63670E05+63669E05+63946E01+63 0244201002
277 947E01+67657E10+68993E30+17618E20+17510E05+17507E05+55244E02+55269E01+ 0244201003
278 55249E01+55263E01+55272E01+55289E01+52426E01+52429E01+52412E01+52035E2 0244201004
279 0+74556E01+74545E01+42998E01+43093E01+42996E01+66112E02+66116E04+66115 0244201005
280 E03+66113E03+66114E03+76277E01+76442E01+76005E01+76008E01+76014E02+760 0244201006
281 40E02+66180E01+66055E01+66054E01+66120E01+66126E01+66140E01+66130E01+6 0244201007
282 6134E02+66129E01+66136E01+66123E01+66096E02+66097E02+66100E02+66101E02 0244201008
283 +66099E01+66103E01+66254E02+66226E01+66268E01+64980E01+65090E02+54134E 0244201009
284 02+54124E01+54175E02+54003E01+11328E01+11389E01+11436E01+11438E01+1161 0244201010
285 4E01+11522E01+11533E01+11526E01+11573E01+11538E01+11534E01+11284E01+11 0244201011
286 201E01+11765E01+48923E01+48924E01+47147E01+48926E01+48172E04+48181E01+ 0244201012
287 48064E02+45913E02+45940E01+45888E01+46010E01+46128E01+45387E03+45407E0 0244201013
288 3+45307E01+45306E01+45308E01+45282E01+46650E01+53291E01+53250E01+44808 0244201014
289 E01+47380E01+47384E02+47302E01+47304E01+47300E01+47240E01+13499E01+134 0244201015
290 96E01+13506E01+13509E01+55047E25+13665E01+39354E05+52428E05+52418E01+4 0244201016
291 5981E30+52440E10+73240E10+65088E10+65060E10+65024E15+38609E10+17939E01 0244201017
292 +17939E04+17934E05+18033E03+18037E01+18046E02+17639E01+46973E01+46975E 0244201018
293 01+46978E01+46996E01+47220E01+11750E03+11762E01+11898E01+37496E01+3749 0244201019
294 3E01+37685E10+22257E01+22356E01+69004E01+68990E01+22353E01+28047E02+69 0244201020
295 000E01+25736E02+25735E02+25717E01+40130E01+23764E02+23761E01+69008E10+ 0244201021
296 99999+ 0244201022

Remember when... everything worked and there was a reason for it?
 
Yeah, that's ugly...
There's no way you can import that file without rewriting it into a layout that is standardized.

1) I would start with VBA or an ActiveX Task in a DTS.
Here's some psuedocode:

dim sOrder as string
Open a new file for writing, OUTPUT
Open your file for reading, INPUT

Read line from INPUT
do until EOF
select case left(line,1)
case " " 'do nothing - first line is blank
case "L"
write line to OUTPUT
if sOrder > "" then write sOrder to OUTPUT
sOrder = ""
case else
'concatenate the order record
sOrder = sOrder & left(line, 70)
end select
Read line from INPUT
loop
write sOrder to OUTPUT 'write the last order/retail
Close INPUT
Close OUTPUT

2)After (1) you should have a new file with each record separated by a CRLF. Create a temporary table with one column defined as varchar(8000). I don't know how long the order/retail records can be. But if 8000 isn't enough you could use text or more than one column and put 8000 characters in each. BULK INSERT can be used from QA, a proc or a SQL Task in a DTS.

3)After (2) you should have a table all the text for a single order or customer in one record. Using:
Code:
insert into customer_table
select substring(col, 2, 9)
      ,substring(col,11,10)
      ,....
from temp_table
where substring(col,1,1) = 'L'  -- customers
--or
insert into retail_table
select substring(col,30,10)
      ,substring(col,40,10)
      ,.... 
from temp_table
where substring(col,1,1) = 'A'  -- orders

What you have to do to parse out all that stuff in the order/retail records is up to you.
 
Your problem isn't the substring function , it's the cursor. Never ever under any circumstances use a cursor to insert large numbers of records.

Use a set-based approach where you insert all the records at once.



Questions about posting. See faq183-874
Click here to help with Hurricane Relief
 
sqlsister,
Thanks for the feedback. That is why I am trying to get the data into records (1 for each item), so I can do a insert all at once. I am caught between a rock and a hard place. The only way to get the records into that format is to create a cursor. Hence, insert records. Whether it be an ACCESS table or flat file or temp table, the records have to be inserted someplace so I can then do a select of all records and insdrt them at once.

Cbhnp - I will look into doing what you said. Bulk insert in a stored procedure seems feasbile. I haven't gotten past the syntax errors I am getting yet. Still working on it. Looks like you can create output delimited records. The records would have to into a TEXT field. 8000 would not be enough. However, looking at your insert again, I still do not see where I can set the substring to an infinite amount. I can't hard code the starting and ending for the substrings. How would I know how many substrings to create? Each order is different. Could be 1 item or 2000 items.

insert into customer_table
select substring(col, 2, 9) <-- starting and ending
,substring(col,11,10)
,....
from temp_table
where substring(col,1,1) = 'L' -- customers

I am getting to the point where I do not think I can get this thing to run fast. I need 2 or 3 seconds at most. I looks like I many need to run 4 or 5 procedures at best.

Haven't done activex in a DTS. I will look this up as well.
I want to stay away from running VBA code in the DB because this all gets run from a JOB.

Remember when... everything worked and there was a reason for it?
 


Here is what I will do given the same issue:

1. use excel to import the flat file to a spreadsheet using fixed length column so remove the two other useless columns

2. import the spreadsheet into SQL server using DTS a table named "gshen" with one column "parseCol"

3. run following to get a header table for you to identify each order or retail record:

select * into head_tab from gshen where charindex('#', parseCol) > 1

4. run following to set flag for each record

update gshen set parseCol = '?'
where charindex('#', parseCol) > 1

5. export the table to a flat file named good.txt

6. import the file good.txt into a table named good, using '?' as the row delimiter, this way you get all the related rows concated together in one record

7. add identity column for both head_tab and good table, then add a column for table good, join them two to insert related columns from head_tab to good table,

8. you will be able to parse the data using set based query

 
I can't tell you how to parse out the contents of the order/retail records. How are you doing it now?

Couple ideas though...
First, while your rewriting the file to a new import file with a consistant layout, you might try the Split function to separate the record into an array using the "+" sign as a split character. Then you could process the array from 0 to ubound(array) and do something with it prior to writing the record out to the new import file. This would need to be done in Step (1) above.

Second, I know you said the file gets created somewhere else and can't be changed, but sometimes we need to challenge our assumptions.

 
This is currently being done with a PC version of Cobol. It eats this stuff up and runs a 80,000 item in about 3 seconds.

This statement bothers me. A lot. So I spent some time working on this. Hope you don't mind. [bigsmile]

I recommend you make a nice file before importing. I wrote this in VB, but could probably be converted to VBA or event vbscript without much effort.

The VB Code
Code:
Dim FSO As Scripting.FileSystemObject
Dim sData As String
Dim arData() As String
Dim i As Long
Dim j As Long
Dim iItem As Long
Dim iCount As Long
Dim bIn As Boolean
Dim cTemp As String
Dim arRecord() As String
Dim arOut() As String
Dim arItems() As String
Dim cRecordHeader As String

Set FSO = CreateObject("Scripting.FileSystemObject")
sData = FSO.OpenTextFile("[!]C:\tektips\DataToImport.txt[/!]").ReadAll
Set FSO = Nothing

arData = Split(sData, vbCrLf)
bIn = False
iCount = 0
iItem = 0
For i = LBound(arData) To UBound(arData)
    arData(i) = Left(arData(i), 70)
    If Left(arData(i), 1) = "L" Then
        cRecordHeader = arData(i)
        If cTemp <> "" Then
            ReDim Preserve arRecord(iCount)
            arRecord(iCount) = CStr(iCount) & "," & cRecordHeader
            
            arItems = Split(cTemp, "+")
            If iCount = 0 Then
                ReDim arOut(UBound(arItems))
            Else
                ReDim Preserve arOut(UBound(arOut) + UBound(arItems) + 1)
            End If
            
            For j = LBound(arItems) To UBound(arItems)
                If Trim(arItems(j)) <> "" Then
                    arOut(iItem) = CStr(iCount) & "," & arItems(j)
                    iItem = iItem + 1
                End If
            Next
            cTemp = ""
            iCount = iCount + 1
            
        End If
        bIn = True
    Else
        If bIn Then
            cTemp = cTemp & arData(i)
        End If
    End If
Next

Set FSO = CreateObject("Scripting.FileSystemObject")
Call FSO.CreateTextFile("[!]C:\tektips\DataItems.txt[/!]").Write(Join(arOut, vbCrLf))
Call FSO.CreateTextFile("[!]C:\tektips\DataHeader.txt[/!]").Write(Join(arRecord, vbCrLf))
Set FSO = Nothing

Of course, you'll want to change the file names to suit your needs.

Then, you can bulk load the data in to SQL Server using this code.

Code:
Create Table #TempHeader(Data VarChar(100))

Bulk insert #TempHeader From '[!]C:\TekTips\DataHeader.txt[/!]'

Create Table #TempItem(Data VarChar(100))

Bulk insert #TempItem From '[!]C:\TekTips\DataItems.txt[/!]'

Select * From #TempHeader
Select * From #TempItem

On my computer, it took 20 milliseconds to make the files that are 'nice' to import. Then it took another 40 milliseconds to import the data in to the temp tables. You'll still have more work to do after this, but it should be fairly simple.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top