scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-08-20 : 11:08:51
|
Guys,I have scenario where in a column - col004 I have following datacol001 col004______________________________________________________1 D,HAINESVILLE CEMETERY,,I,SANDYSTON TOWNSHIP,,Each time after every 3rd delimiter it is populated with 'D' or 'I', the subsequent data (for next 2 delimiters) should be inserted in another table as seperate row.col001 col004______________________________________________________1 D,HAINESVILLE CEMETERY,,1 I,SANDYSTON TOWNSHIP,,For the same scenario I have written a stored procedure which uses cursor and function - which does the logic for the inserts but I am stuck at the loop. To pinpoint I am stuck at while (dbo.gettoken(@col004, ',', ??) = 'D' OR dbo.gettoken(@col004, ',', ??) = 'I'). I do not know what value should be 'token' which should be 1, 4, 7, 10. Each time when we encouter 'D' or 'I', I insert into different table as seperate row until we find next 'D' or 'I'. The function when I pass string to be parsed, delimiter and token number it gives me the string next to the token number.For example dbo.gettoken ('D,HAINESVILLE CEMETERY,,', ',', 1) gives D dbo.gettoken ('D,HAINESVILLE CEMETERY,,', ',', 2) gives HAINESVILLE CEMETERYCREATE PROCEDURE P_party aSbegin declare C_partytwo cursor for select id, col004 from maps order by id declare @counter int, @col004 varchar(400), @col004_prev varchar(400), @id int, @id_prev int, @stmt Nvarchar(400), @fetchcount int set @counter=0 set @col004='' set @id=0 set @col004_prev=0 set @id_prev=0 set @fetchcount = 0 open C_partytwo fetch next from C_partytwo into @id, @col004 while @@fetch_status<>-1 BEGIN while while (dbo.gettoken(@col004, ',', ??) = 'D' OR dbo.gettoken(@col004, ',', ??) = 'I') -- logic for inserting into different table INSERT INTO PARTY VALUES (@ID, DBO.GETTOKEN(@COL004, ',', ??)) set @counter=1 set @fetchcount=@fetchcount+1 fetch next from C_partytwo into @id, @col004-- print 'fetchcount: ' + cast(@fetchcount as varchar)-- PRINT 'SEQ_KEY: ' + cast(@seq_key as varchar) endclose c_partytwodeallocate c_partytwoendAny suggestions and inputs would help.Thanks |
|