UPDATE statement using SELECT

  • I CREATED THE FOLLOWING TABLE.

    CREATE TABLE SCRAMBLE

    (FNAME VARCHAR(10),

    LNAME VARCHAR(10)

    )

    INSERT INTO SCRAMBLE VALUES('APPLE','BROCOLLI')

    INSERT INTO SCRAMBLE VALUES ('BANNANA','CARROT')

    INSERT INTO SCRAMBLE VALUES ('PUMPKIN','POTATO')

    INSERT INTO SCRAMBLE VALUES ('ORANGE','CABBAGE')

    INSERT INTO SCRAMBLE VALUES ('PINEAPPLE','SPINACH')

    INSERT INTO SCRAMBLE VALUES('PEERS','BRINJAL')

    SELECT * FROM SCRAMBLE

    Now, I would like to randomization of columns between first name and last name, i.e rows should be mis-matching.

    So, I have run the following SELECT SCRIPT and it produced the result of what I have expected.

    SELECTt1.FNAME,

    t2.LName

    FROM(

    SELECTFNAME,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROMSCRAMBLE

    ) AS t1

    INNER JOIN(

    SELECTLName,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROMSCRAMBLE

    ) AS t2 ON t2.recID = t1.recID

    o/p result.

    FNAMELName

    ===================

    PUMPKINCABBAGE

    APPLESPINACH

    PINEAPPLE CARROT

    PEERSBROCOLLI

    ORANGEPOTATO

    BANNANABRINJAL

    It basically varies according to number of executions we made.

    My question is: How can I update with this SELECT STATEMENT INTO MY TABLE.

    I Tried the following update statement, but it didn't worked.

    update scramble

    SET FNAME = T1.Fname,

    LNAME =T2.Lname

    FROM(

    SELECTFNAME,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROMSCRAMBLE

    ) AS t1

    INNER JOIN(

    SELECTLName,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROMSCRAMBLE

    ) AS t2

    ON t2.recID = t1.recID

    o/p:

    FNAMELNAME

    =====================

    PUMPKINSPINACH

    PINEAPPLECARROT

    PUMPKINSPINACH

    PINEAPPLECARROT

    PUMPKINSPINACH

    PINEAPPLECARROT

    Can you please help me with UPDATE statment,so that I can get some output similar to as mentioned with select statement. Thank you.

    Let me know if you have questions.

  • Please do not cross post, it will only fragment the answers you get.

    Please post all answers here.

  • ok..i was not aware where exactly to post..thanks for info

  • Ignoring the infinite loop about where to post.

    Your update statement doesn't specify which row in Scramble matches which row in your random select.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (5/2/2012)


    Ignoring the infinite loop about where to post.

    Your update statement doesn't specify which row in Scramble matches which row in your random select.

    Gus, use the other one.

  • What you need is something comparable to this:

    IF OBJECT_ID(N'tempdb..#SCRAMBLE') IS NOT NULL

    DROP TABLE #SCRAMBLE ;

    CREATE TABLE #SCRAMBLE

    (FNAME VARCHAR(10),

    LNAME VARCHAR(10)) ;

    INSERT INTO #SCRAMBLE

    VALUES ('APPLE', 'BROCOLLI'),

    ('BANNANA', 'CARROT'),

    ('PUMPKIN', 'POTATO'),

    ('ORANGE', 'CABBAGE'),

    ('PINEAPPLE', 'SPINACH'),

    ('PEERS', 'BRINJAL')

    SELECT *

    FROM #SCRAMBLE ;

    ;

    WITH CTE

    AS (SELECT ROW_NUMBER() OVER (ORDER BY FNAME) AS R,

    *

    FROM #SCRAMBLE),

    CTE2

    AS (SELECT t1.FNAME,

    t2.LName,

    ROW_NUMBER() OVER (ORDER BY t1.FNAME) AS R2

    FROM (SELECT FNAME,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROM #SCRAMBLE) AS t1

    INNER JOIN (SELECT LName,

    ROW_NUMBER() OVER (ORDER BY NEWID()) AS recID

    FROM #SCRAMBLE) AS t2

    ON t2.recID = t1.recID)

    UPDATE CTE

    SET LNAME = CTE2.LName, FNAME = CTE2.FNAME

    FROM CTE

    INNER JOIN CTE2

    ON R = R2 ;

    SELECT *

    FROM #SCRAMBLE ;

    It could be simplified. You don't actually need to do anything with the FNAME column, just the LNAME. You'll get the same level of randomization, unless there are other columns that need to stay static while the FNAME changes. But based on this two-column design, changing either one gives you a fully random mix.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Lynn Pettis (5/2/2012)


    GSquared (5/2/2012)


    Ignoring the infinite loop about where to post.

    Your update statement doesn't specify which row in Scramble matches which row in your random select.

    Gus, use the other one.

    Too late.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • And this is why you shouldn't cross post. We have discussions and answers split across threads.

  • Thank you so much. I actually need to mask the data. so i found this would be good option.

  • DBA_SQL (5/2/2012)


    Thank you so much. I actually need to mask the data. so i found this would be good option.

    Figured as much.

    Does the reason and solution make sense?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Yes it makes sense. Thank you

  • Why are we messing around with real names? Single update with random length GUIDs will mask the data with guaranteed no way back.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 12 posts - 1 through 11 (of 11 total)

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