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 8301577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:00:43+00:00 2026-06-08T17:00:43+00:00

The SO Question has lead me to the following question. If a table has

  • 0

The SO Question has lead me to the following question.

If a table has 16 rows I’d like to add a field to the table with the numbers 1,2,3,4,5,…,16 arranged randomly i.e in the ‘RndVal’ field for row 1 this could be 2, then for row 2 it could be 5 i.e each of the 16 integers needs to appear once without repetition.

Why doesn’t the following work? Ideally I’d like to see this working then to see alternative solutions.

This creates the table ok:

IF OBJECT_ID('tempdb..#A') IS NOT NULL BEGIN DROP TABLE #A END
IF OBJECT_ID('tempdb..#B') IS NOT NULL BEGIN DROP TABLE #B END
IF OBJECT_ID('tempdb..#C') IS NOT NULL BEGIN DROP TABLE #C END
IF OBJECT_ID('tempdb..#myTable') IS NOT NULL BEGIN DROP TABLE #myTable END

CREATE TABLE #B (B_ID INT)
CREATE TABLE #C (C_ID INT)

INSERT INTO #B(B_ID) VALUES
    (10),
    (20),
    (30),
    (40)
INSERT INTO #C(C_ID)VALUES
    (1),
    (2),
    (3),
    (4)

CREATE TABLE #A 
    (
    B_ID INT
    , C_ID INT
    , RndVal INT
    )

INSERT INTO #A(B_ID, C_ID, RndVal)  
SELECT 
        #B.B_ID
        , #C.C_ID
        , 0 
FROM #B CROSS JOIN #C; 

Then I’m attempting to add the random column using the following. The logic is to add random numbers between 1 and 16 > then to effectively overwrite any that are duplicated with other numbers > in a loop …

SELECT 
    ROW_NUMBER() OVER(ORDER BY B_ID) AS Row
    , B_ID
    , C_ID
    , RndVal
INTO #myTable
FROM #A

DECLARE @rowsRequired INT = (SELECT COUNT(*) CNT FROM #myTable)
DECLARE @i INT = (SELECT @rowsRequired - SUM(CASE WHEN RndVal > 0 THEN 1 ELSE 0 END) FROM #myTable)--0


DECLARE @end INT = 1
WHILE @end > 0 
    BEGIN

            SELECT @i = @rowsRequired -  SUM(CASE WHEN RndVal > 0 THEN 1 ELSE 0 END) FROM #myTable
            WHILE @i>0
                BEGIN

                    UPDATE x
                    SET x.RndVal = FLOOR(RAND()*@rowsRequired)
                    FROM #myTable x
                    WHERE x.RndVal = 0 

                    SET @i = @i-1
                END 

         --this is to remove possible duplicates
            UPDATE c 
            SET c.RndVal = 0 
            FROM 
                #myTable c
                INNER JOIN 
                    (
                    SELECT RndVal
                    FROM #myTable
                    GROUP BY RndVal 
                    HAVING COUNT(RndVal)>1
                    ) t
                    ON 
                        c.RndVal = t.RndVal

         SET @end = @@ROWCOUNT

    END

TRUNCATE TABLE #A
INSERT INTO #A
SELECT 
    B_ID
    , C_ID
    , RndVal
FROM #myTable

If the original table has 6 rows then the result should end up something like this

B_ID|C_ID|RndVal
----------------
    |    | 5 
    |    | 4
    |    | 1
    |    | 6
    |    | 3
    |    | 2
  • 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-06-08T17:00:44+00:00Added an answer on June 8, 2026 at 5:00 pm

    I don’t understand your code, frankly

    This will update each row with a random number, non-repeated number between 1 and the number of rows in the table

    UPDATE T
    SET SomeCol = T2.X
    FROM
       MyTable T
       JOIN
       (
       SELECT
          KeyCol, ROW_NUMBER() OVER (ORDER BY NEWID()) AS X
       FROM 
          MyTable
       ) T2 ON T.KeyCol = T2.KeyCol 
    

    This is more concise but can’t test to see if it works as expected

    UPDATE T
    SET SomeCol = X
    FROM
       (
       SELECT
          SomeCol, ROW_NUMBER() OVER (ORDER BY NEWID()) AS X
       FROM 
          MyTable
       ) T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has developed off an answer here . My question therefore is what
This question has two parts. Part 1. Yesterday I had some code which would
This question has been asked before but i still don't understand it fully so
this question has probably been asked before, but i'm stuck and i've tried a
This question has baffled myself and my cohorts. In the program I had written
This question has disturbed me for a long time. Sorry if it is a
This question has been bugging me for a long time now but essentially I'm
This question has frequently been asked over stackoverflow.com but none of the answers work
This question has been asked in a C++ context but I'm curious about Java.
This question has been asked before but in a slightly different way and I

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.