?
Solved

Update Table With Random Strings

Posted on 2008-10-17
3
Medium Priority
?
780 Views
Last Modified: 2012-06-27
Dear Experts,

I have a table with 1,000,000 rows.  I have now created a new column in this table called random_strings where I somehow need to update the whole table so each of these 1,000,000 records has a random string in the new column.  How would I go about doing this?

The strings must be upper case and 5 characters in length.  However, the number of these 5 character strings needs to be random to per row separated by a space character, and there must be no more than 10 of these 5 character strings per row and no less than 1 of these 5 character strings per row.  For example

row1 = asdfg xcvbn
row2 = lkjdk eidkf dkeld ghdke slwyd
row3 = utire
...
row999998 =qqqqq asasa gfgre hggff nnnmm qqqqq asasa gfgre hggff nnnmm
row999999 = nnnnn aaaaa fffff
row1000000 = jfkdh eirut qywir fkdld ghfnd ppooi xcsas

Can anyone help me create a stored procedure to do this?
0
Comment
Question by:narmi2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
jfmador earned 2000 total points
ID: 22746993
Hello to build your 5char value you can use

DECLARE @text as varchar(5)
SET @text = char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer))

To add a maximum of 10 string you store a random number in a Integer and use a while decreasing this value of one at each loop until it is equal to 0

DECLARE @count as integer
DECLARE @text as varchar(60)
SET @count = 1 + cast(rand() * 9 as integer)
while @count > 0
begin
SET @text = @Text + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + char(65 + cast(rand() * 26 as integer)) + ' '
SET @Count = @Count - 1
end


You can put this code in a function and use an update statement to update all your row
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question