• spaghettidba (4/18/2014)


    Well, it worked on the sample data you provided.

    If you gave us more significant sample data, maybe someone could come up with something better.

    Both tables where of the same size. I should have provided larger sample tables and of different sizes.

    But even with the tables of the same size your algoritm always produces a table were each value from the use_value table is used exactly once.

    If the target table would be 3 rows, only the first three rows from the use_value table would be used one time each.

    So this is not very random.

    Had I used tables of different sizes his effect would have been more visible.

    Sorry to have provided tables of the same sizes.

    See the code below.

    (Only the first rows of use_value are used and each only once).

    Ben

    If exists(select * from information_schema.TABLES where TABLE_NAME = 'Target') drop table Target

    If exists(select * from information_schema.TABLES where TABLE_NAME = 'Use_value') drop table Use_value

    Create table Target

    (

    something varchar(30),

    SomethingElse varchar(30),

    full_name_txt varchar(30)

    )

    insert into Target values('a','g','m')

    insert into Target values('b','h','n')

    insert into Target values('c','i','o')

    insert into Target values('d','j','p')

    -- insert into Target values('d','k','q')

    -- insert into Target values('f','l','r')

    -- insert into Target values('f2','l3','r3')

    -- insert into Target values('f1','l2','r3')

    Create table Use_value

    (

    number int identity,

    column1 varchar(30),

    column2 varchar(30)

    )

    insert into Use_value values('111','r1')

    insert into Use_value values('222','r2')

    insert into Use_value values('333','r3')

    insert into Use_value values('444','r4')

    insert into Use_value values('555','r5')

    insert into Use_value values('666','r6')

    insert into Use_value values('777','r6')

    insert into Use_value values('888','r6')

    insert into Use_value values('999','r6')

    ;

    WITH T1 AS (

    SELECT *, randowCol = NEWID()

    FROM Target

    ),

    T2 AS (

    SELECT *, RN = ROW_NUMBER() OVER (ORDER BY randowCol)

    FROM T1

    )

    UPDATE T2

    SET full_name_txt = (SELECT column1 FROM Use_value V WHERE RN = V.number)

    select * from Target

    If exists(select * from information_schema.TABLES where TABLE_NAME = 'Target') drop table Target

    If exists(select * from information_schema.TABLES where TABLE_NAME = 'Use_value') drop table Use_value