Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
anaylor01
Starting Member
28 Posts |
Posted - 2008-12-03 : 09:33:46
|
I have a one column table. The data in that column has 1142 characters. It is fixed delimited data. To make it more dynamic for parsing the data I have created a table that has the fieldname, fieldlength and field size. I want to create a cursor that will parse this data into the correct fields into a table. When I run this it puts the data into the first field. Lets say there are 50 records in the One column table. Well it puts the first 50 rows into the new table but it starts the second field at 51 and so on and so forth. Declare DDOKRaw cursor for Select [field name],startingposition, fieldlength from dbo.TablefieldPositionLengthDeclare @fn varchar(128)Declare @sp varchar(128)Declare @fl varchar(128)Open DDOKRAW Fetch Next From DDOKRaw into @fn, @sp, @flset @rownum = 1 While @@Fetch_Status = 0 BeginEXEC('insert into migr0173 (' + @fn + ') select top 1 substring(data,' + @sp + ', ' + @fl + ') as ' + @fn + ' from dbo.MIGR0173_20080917061003') Fetch Next From DDOKRaw into @fn, @sp, @flend Close DDOKRaw Deallocate DDOKRaw |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-03 : 09:39:01
|
cant you go for a set based approach? May be you could illustrate your reqmnt with some sample data so that we can try to give sample query. |
 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-12-03 : 09:55:59
|
Also, Why do you want to make it more dynamic for parsing ? Does the startingposition, fieldlength etc change very often ? |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-03 : 14:23:12
|
[code]Declare DDOKRaw cursor for Select [field name],startingposition, fieldlength from dbo.TablefieldPositionLengthDECLARE @insert_part varchar(2000)DECLARE @value_part varchar(2000)DECLARE @loop intDeclare @fn varchar(128)Declare @sp varchar(128)Declare @fl varchar(128)Open DDOKRAWFetch Next From DDOKRaw into @fn, @sp, @flset @loop = 1While @@Fetch_Status = 0Begin if @loop=1 begin select @insert_part='insert into migr0173 (' + @fn select @value_part='select substring(data,'+ @sp + ', ' + @fl + ') as ' + @fn end else begin select @insert_part=@insert_part + ',' + @fn select @value_part=@value_part + ',' + 'substring(data,'+ @sp + ', ' + @fl + ') as ' + @fn end set @loop=@loop+1Fetch Next From DDOKRaw into @fn, @sp, @flendselect @insert_part=@insert_part + ') 'select @value_part=@value_part + ' from dbo.MIGR0173_20080917061003'Close DDOKRawDeallocate DDOKRawprint @insert_partprint @value_partexec (@insert_part + @value_part)[/code]GreetingsWebfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
anaylor01
Starting Member
28 Posts |
Posted - 2008-12-04 : 10:35:30
|
I changed the VARCHAR(2000) to VARCHAR(4000) and it worked freaking awesome. You are the man WEBFRED. Thank you thank you thank you. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-05 : 15:02:49
|
my pleasure  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|