Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:53:08+00:00 2026-05-23T21:53:08+00:00

I have a T-SQL routine that copies user information from one table ‘Radius’ to

  • 0

I have a T-SQL routine that copies user information from one table ‘Radius’ to another ‘Tags’. However, as the rows are transfered, I would also like to include a unique randomly generated code in the INSERT (3 chars long). The code is generated by the WHILE loop below. Any way to do this?

INSERT Tags (UserID, JobID, Code) 
SELECT UserID, @JobID,  ?????
FROM Radius

Unique random code generator:

WHILE EXISTS (SELECT * FROM Tags WHERE Code = @code)
BEGIN

select @code=@code+char(n) from
(
select top 3 number as n from master..spt_values 
where type='p' and number between 48 and 57 or number between 65 and 90
order by newid()
) 
END

CLARIFICATION: The reason for doing this is that I want to keep the random code generation logic at the level of the SQL stack. Implementing this in the app code would require me to check the db everytime a potential random code is generated to see if it is unique. As the number of code records increases so will the number of calls to the db as probability increases that there will be more duplicate codes generated before a unique one is generated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:53:08+00:00Added an answer on May 23, 2026 at 9:53 pm

    Part One, Generate a table with all possible values

    DECLARE @i int
    
    CREATE TABLE #AllChars(value CHAR(1))
    
    SET @i=48
    
    WHILE @i<=57
    BEGIN
        INSERT INTO #Allchars(value) VALUES(CHAR(@i))
        SET @i=@i+1
    END
    
    SET @i=65
    
    WHILE @i<=90
    BEGIN
        INSERT INTO #Allchars(value) VALUES(CHAR(@i))
        SET @i=@i+1
    END
    
    CREATE TABLE AllCodes(value CHAR(3),
              CONSTRAINT PK_AllChars PRIMARY KEY CLUSTERED(value))
    
    INSERT INTO AllCodes(value)
    SELECT AllChars1.Value+AllChars2.Value+AllChars3.Value
    FROM #AllChars AS AllChars1,#AllChars AS AllChars2,#AllChars AS AllChars3
    

    This is a one off operation and takes around 1 second to run on SQL Azure. Now that you have all possible values in a table any future inserts become, something along the lines of

    SELECT
    RadiusTable.UserID,
    RadiusTable.JobID,
    IDTable.Value
    FROM 
    (
      SELECT ROW_NUMBER() OVER (ORDER BY UserID,JobID) As RadiusRow,
      UserID,JobID
      FROM Radius
    ) AS RadiusTable INNER JOIN  
    
    (
        SELECT ROW_NUMBER() OVER (ORDER BY newID()) As IDRow,
        Value
        FROM AllCodes
    ) AS IDTable ON RadiusTable.RadiusRow = IDTable.IDRow
    

    Before going with any of these schemes you had better be certain that you are not going to have more than 46656 rows in your table otherwise you will run out of unique ID Values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in a SQL Server 2005 Database that is used a
I'm doing a one-time import routine from one system to another. The first step
I have inherited a database that has a table (300gb) full of SQL datatype
I have an sql server 2008 db table that holds links to articles. My
I have a routine that will be creating individual tables (Sql Server 2008) to
I have problem with Oracle 9.2 and JMS. I created PL/SQL routine to send
I have SQL data that looks like this: events id name capacity 1 Cooking
i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have SQL Server 2008 with a table called ProductCategories designed like this: Id
I have identified a problem within my application; basically, one sub-routine prepares (lots) of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.