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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Optimal update method

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 ..."
Go to Top of Page

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 ..."

Go to Top of Page

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
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-10-20 : 10:10:57
randomly generating as in:
32187623423
439834i3644
or
askdjgasfas
orblkjvergm
or
298ckjbvds9tr
kjer9ovdi45hv


how 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 ..."
Go to Top of Page

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 string

quote:
Originally posted by Seventhnight

randomly generating as in:
32187623423
439834i3644
or
askdjgasfas
orblkjvergm
or
298ckjbvds9tr
kjer9ovdi45hv


how 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 ..."

Go to Top of Page

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)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 this

quote:
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)

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

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))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-10-20 : 10:36:12
read this about random numbers in UDF's
http://weblogs.sqlteam.com/jeffs/archive/2004/11/22/2927.aspx

Go with the flow & have fun! Else fight the flow
Go to Top of Page

tchinedu
Yak Posting Veteran

71 Posts

Posted - 2005-10-20 : 10:56:59
Sorry guys, I guess I havent really given all the info

another 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 etc


quote:
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))


Madhivanan

Failing to plan is Planning to fail

Go to Top of Page

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.RandomDigit
As
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()
Go

Create View dbo.RandomCharacter
As
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()
Go
Create Function getRandomDigit()
Returns int As
Begin
Declare @rValue int

Select @rValue = n from dbo.RandomDigit

Return @rValue
End
Go
Create Function getRandomChar()
Returns varchar(1) As
Begin
Declare @rValue varchar(1)

Select @rValue = n from dbo.RandomCharacter

Return @rValue
End
Go
Create Table #TempTable
(
id int identity(1,1),
address varchar(100),
address2 varchar(100)
)

Insert Into #TempTable
Select '3546 Holland Drive Apt: 239', null Union All
Select '439 Bill Gates Rd', null Union All
Select 'Intersection of 5th & Main', null

Declare @maxLen int,
@i int

Set @i = 1
Set @MaxLen = (Select max(len(address)) From #TempTable)

Select * From #TempTable

While @i < @maxLen
Begin
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 + 1
End

Select * From #TempTable


Drop Table #TempTable

Go
Drop View dbo.RandomDigit
Drop View dbo.RandomCharacter
Drop Function dbo.getRandomDigit
Drop 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 ..."
Go to Top of Page

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 ..."
Go to Top of Page

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 Knight

quote:
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 ..."

Go to Top of Page

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 ..."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-21 : 01:00:08
>>how do I become a student of yours...

Visit this
http://sqlteam.com/forums/pop_profile.asp?mode=display&id=11086

Then click
Find all non-archived posts by Seventhnight

See his responses to all questions. At the end you become Master

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 this
http://sqlteam.com/forums/pop_profile.asp?mode=display&id=11086

Then click
Find all non-archived posts by Seventhnight

See his responses to all questions. At the end you become Master

Madhivanan

Failing 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 ..."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-21 : 09:25:49
Yes. I will do that

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.RandomDigit
As
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()
Go

Create View dbo.RandomCharacter
As
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()
Go
Create Function getRandomDigit()
Returns int As
Begin
Declare @rValue int

Select @rValue = n from dbo.RandomDigit

Return @rValue
End
Go
Create Function getRandomChar()
Returns varchar(1) As
Begin
Declare @rValue varchar(1)

Select @rValue = n from dbo.RandomCharacter

Return @rValue
End
Go
Create Table #TempTable
(
id int identity(1,1),
address varchar(100),
address2 varchar(100)
)

Insert Into #TempTable
Select '3546 Holland Drive Apt: 239', null Union All
Select '439 Bill Gates Rd', null Union All
Select 'Intersection of 5th & Main', null

Declare @maxLen int,
@i int

Set @i = 1
Set @MaxLen = (Select max(len(address)) From #TempTable)

Select * From #TempTable

While @i < @maxLen
Begin
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 + 1
End

Select * From #TempTable


Drop Table #TempTable

Go
Drop View dbo.RandomDigit
Drop View dbo.RandomCharacter
Drop Function dbo.getRandomDigit
Drop 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 ..."

Go to Top of Page

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 old



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

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 ..."
Go to Top of Page
    Next Page

- Advertisement -