Randomly Assingning Values to Rows in a table

  • Hi,

    I need to randomly assign values to a column in a table. I need to update a column in a table

    with handful of values from another table.

    Following is the scenario:

    T1 is the Table to Update: I am just providing the relevant columns.

    The values in TargetID column in table T1 is initially 0, I need to randomly assign them with the TargetID values from table T2

    T1:

    ID(PK int) TargetID(smallint)

    111 0

    222 0

    333 0

    444 0

    T2 is the Table with values to be picked for assignment:It can have any number of values. I am just showing upto 4 for example

    T2:

    TargetID(smallint)

    1

    2

    3

    4

    I want to update the table T1 so that it looks like this.The values from table T2 can be assigned to any ID values in table T1.

    Note: I do not want to assign TargetID = 1 from T2 to any IDs in T1

    Example resulting table T1 after update:

    ID(PK int) TargetID(smallint)

    111 2

    222 3

    333 4

    444 3

    I am currently trying to accomplish by selecting top n from the table T1 and ordering it by NewId(), but can't figure how to assign the values from T2.

    Any thoughts would be helpful.

  • Please don't cross-post. It is really annoying.

    http://www.sqlservercentral.com/Forums/Topic565232-338-1.aspx

  • Sorry Michael,I tried to delete this last night itself,but its not removing it.

    I tried again and its still not removing it.

    thanks

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply