| Author |
Topic |
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 09:31:54
|
| Hey Guys,I need to iterate through all the records in a table and modify some of the columns in a stored procedure. What would be the optimal way to go about this.Please help |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-20 : 09:39:33
|
you gotta be more specific than that modify how... based on what... how many records... why 'iterate'?Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 10:03:07
|
I need to loop thru all the rows in the table and change the values of two columns in each row with values I'll be randomly generating. The record count could potentially get into the high hundreds of thousands.PS: the main constraint is that I have to do this in a stored procedure...quote: Originally posted by Seventhnight you gotta be more specific than that modify how... based on what... how many records... why 'iterate'?Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." 
|
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-10-20 : 10:08:40
|
the thing is...why do you think you have to loop through it?have you heard of user defined functions?gives some examples.Go with the flow & have fun! Else fight the flow |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-20 : 10:10:57
|
randomly generating as in:32187623423439834i3644oraskdjgasfasorblkjvergmor298ckjbvds9trkjer9ovdi45hvhow about you give me a sample to work with... you give me some records to start from, and let me know what you want them to look like... and I'll try to fix you up Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 10:16:43
|
randomly generating as in 32187623423 for one column and askdjgasfas for the other column.I have defined a function that given a string will return the randomly generated equivalent that I need.Now I need to loop thru each row in the table, read the original columns strings, pass it to the function and recieve a new randomly generated string, replace the original string in the table with this newly generated stringquote: Originally posted by Seventhnight randomly generating as in:32187623423439834i3644oraskdjgasfasorblkjvergmor298ckjbvds9trkjer9ovdi45hvhow about you give me a sample to work with... you give me some records to start from, and let me know what you want them to look like... and I'll try to fix you up Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." 
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-20 : 10:18:17
|
| >>I need to loop thru all the rows in the table and change the values of two columns in each row with values I'll be randomly generating. The record count could potentially get into the high hundreds of thousands.Is this?Update yourTable set col1=function(col1),col2=function(col2)MadhivananFailing to plan is Planning to fail |
 |
|
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 10:25:19
|
The problem with this method is that I can't generate random numbers in the UDF [ or can I ?], I need to pass a random salt value to be used by the UDF for each row from the stored procedure when I call the function, hence I imagine I can't use the update method...am i right about thisquote: Originally posted by madhivanan >>I need to loop thru all the rows in the table and change the values of two columns in each row with values I'll be randomly generating. The record count could potentially get into the high hundreds of thousands.Is this?Update yourTable set col1=function(col1),col2=function(col2)MadhivananFailing to plan is Planning to fail
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-20 : 10:34:54
|
| Do you need something like this?Update yourTable set col1=cast(newid() as varchar(36)),col2=cast(newid() as varchar(36))MadhivananFailing to plan is Planning to fail |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 10:56:59
|
Sorry guys, I guess I havent really given all the infoanother constraint is that I need to keep the original strings lenght and format in the new string.i.e an address has to look like an address when it is replaced...Example [10 holland drive] = [96 efghall zcvef]and [11239] = [04591]something like that...I need to keep the lenght and format of the string the same when Replaced, caps for caps and numbers for numbers etcquote: Originally posted by madhivanan Do you need something like this?Update yourTable set col1=cast(newid() as varchar(36)),col2=cast(newid() as varchar(36))MadhivananFailing to plan is Planning to fail
|
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-20 : 13:24:30
|
I think this will do it... though it may not be REAL FAST...  Create View dbo.RandomDigitAs Select top 1 n From ( Select n=0 Union All Select 1 Union All Select 2 Union All Select 3 Union All Select 4 Union All Select 5 Union All Select 6 Union All Select 7 Union All Select 8 Union All Select 9 ) A Order By newId()GoCreate View dbo.RandomCharacterAs Select top 1 n From ( Select n='a' Union All Select 'b' Union All Select 'c' Union All Select 'd' Union All Select 'e' Union All Select 'f' Union All Select 'g' Union All Select 'h' Union All Select 'i' Union All Select 'j' Union All Select 'k' Union All Select 'l' Union All Select 'm' Union All Select 'n' Union All Select 'o' Union All Select 'p' Union All Select 'q' Union All Select 'r' Union All Select 's' Union All Select 't' Union All Select 'u' Union All Select 'v' Union All Select 'w' Union All Select 'x' Union All Select 'y' Union All Select 'z' ) A Order By newId()GoCreate Function getRandomDigit()Returns int AsBegin Declare @rValue int Select @rValue = n from dbo.RandomDigit Return @rValueEndGoCreate Function getRandomChar()Returns varchar(1) AsBegin Declare @rValue varchar(1) Select @rValue = n from dbo.RandomCharacter Return @rValueEndGoCreate Table #TempTable(id int identity(1,1),address varchar(100),address2 varchar(100))Insert Into #TempTableSelect '3546 Holland Drive Apt: 239', null Union AllSelect '439 Bill Gates Rd', null Union AllSelect 'Intersection of 5th & Main', nullDeclare @maxLen int, @i intSet @i = 1Set @MaxLen = (Select max(len(address)) From #TempTable)Select * From #TempTableWhile @i < @maxLenBegin Update #TempTable Set address2 = isnull(address2,'') + case when substring(address,@i,1) like '[0-9]' then convert(varchar,dbo.getRandomDigit()) when substring(address,@i,1) collate SQL_Latin1_General_CP1_CS_AS like '[abcdefghijklmnopqrstuvwxyz]' collate SQL_Latin1_General_CP1_CS_AS then dbo.getRandomChar() when substring(address,@i,1) collate SQL_Latin1_General_CP1_CS_AS like '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' collate SQL_Latin1_General_CP1_CS_AS then upper(dbo.getRandomChar()) else substring(address,@i,1) end From #TempTable Set @i = @i + 1EndSelect * From #TempTableDrop Table #TempTable GoDrop View dbo.RandomDigitDrop View dbo.RandomCharacterDrop Function dbo.getRandomDigitDrop Function dbo.getRandomChar Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-20 : 17:22:14
|
Bump... I thought this was a fun topic... and then the guy just disappears Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
tchinedu
Yak Posting Veteran
71 Posts |
Posted - 2005-10-20 : 17:23:51
|
Seventhnight - you are a genius...I am very grateful for your help.I got tied up trying to use your suggestions...I really appreciate your help.Thanks a bunch....how do I become a student of yours...I could never have dreamt this up without your help...once again, accept my heart felt gratitude..You are truly a Knightquote: Originally posted by Seventhnight Bump... I thought this was a fun topic... and then the guy just disappears Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." 
|
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-20 : 17:32:36
|
well I'm glad it worked out for you... thats all I was trying to find out thanks for the compliments... though I would say that the whole Team is quite an intelligent and crafty group  Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-21 : 08:11:45
|
quote: Originally posted by madhivanan >>how do I become a student of yours...Visit thishttp://sqlteam.com/forums/pop_profile.asp?mode=display&id=11086Then clickFind all non-archived posts by SeventhnightSee his responses to all questions. At the end you become Master MadhivananFailing to plan is Planning to fail
Clever... When your done with mine... try all of Dr. Cross Join, Kristen, ... well pretty much everyone one the 1st couple of pages Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-21 : 09:25:49
|
Yes. I will do that MadhivananFailing to plan is Planning to fail |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2005-10-21 : 09:31:52
|
Yeah, not super efficient, but a very elegant solution that I will probably use in the future. Kudos.quote: Originally posted by Seventhnight I think this will do it... though it may not be REAL FAST...  Create View dbo.RandomDigitAs Select top 1 n From ( Select n=0 Union All Select 1 Union All Select 2 Union All Select 3 Union All Select 4 Union All Select 5 Union All Select 6 Union All Select 7 Union All Select 8 Union All Select 9 ) A Order By newId()GoCreate View dbo.RandomCharacterAs Select top 1 n From ( Select n='a' Union All Select 'b' Union All Select 'c' Union All Select 'd' Union All Select 'e' Union All Select 'f' Union All Select 'g' Union All Select 'h' Union All Select 'i' Union All Select 'j' Union All Select 'k' Union All Select 'l' Union All Select 'm' Union All Select 'n' Union All Select 'o' Union All Select 'p' Union All Select 'q' Union All Select 'r' Union All Select 's' Union All Select 't' Union All Select 'u' Union All Select 'v' Union All Select 'w' Union All Select 'x' Union All Select 'y' Union All Select 'z' ) A Order By newId()GoCreate Function getRandomDigit()Returns int AsBegin Declare @rValue int Select @rValue = n from dbo.RandomDigit Return @rValueEndGoCreate Function getRandomChar()Returns varchar(1) AsBegin Declare @rValue varchar(1) Select @rValue = n from dbo.RandomCharacter Return @rValueEndGoCreate Table #TempTable(id int identity(1,1),address varchar(100),address2 varchar(100))Insert Into #TempTableSelect '3546 Holland Drive Apt: 239', null Union AllSelect '439 Bill Gates Rd', null Union AllSelect 'Intersection of 5th & Main', nullDeclare @maxLen int, @i intSet @i = 1Set @MaxLen = (Select max(len(address)) From #TempTable)Select * From #TempTableWhile @i < @maxLenBegin Update #TempTable Set address2 = isnull(address2,'') + case when substring(address,@i,1) like '[0-9]' then convert(varchar,dbo.getRandomDigit()) when substring(address,@i,1) collate SQL_Latin1_General_CP1_CS_AS like '[abcdefghijklmnopqrstuvwxyz]' collate SQL_Latin1_General_CP1_CS_AS then dbo.getRandomChar() when substring(address,@i,1) collate SQL_Latin1_General_CP1_CS_AS like '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' collate SQL_Latin1_General_CP1_CS_AS then upper(dbo.getRandomChar()) else substring(address,@i,1) end From #TempTable Set @i = @i + 1EndSelect * From #TempTableDrop Table #TempTable GoDrop View dbo.RandomDigitDrop View dbo.RandomCharacterDrop Function dbo.getRandomDigitDrop Function dbo.getRandomChar Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." 
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-10-21 : 09:47:07
|
quote: Originally posted by tchinedu PS: the main constraint is that I have to do this in a stored procedure...
If this ain't homework, I don't know what is....bcp out a view, the bcp the data in to a new table, rename the old to backup and the new to oldBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-21 : 10:21:15
|
what is the point of the bcping???i though the point was the randomization...oh well Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
Next Page
|