Solved

# Update Table With Random Strings

Posted on 2008-10-17
763 Views
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
Question by:narmi2

LVL 5

Accepted Solution

jfmador earned 500 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

## Join & Write a Comment Already a member? Login.

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!