none
How to loop through a table and populate another table with column values in MS sql server? RRS feed

  • Question

  • I have a table where a new column is added and I need to loop through another table to get the appropriate value and populate in the newly created column.

    I'm not sure if I have explained it correctly. Below picture explains it all.

    

    In the first table, user_guid is the newly added column, for each userid in the first table I need to fetch the userguid in the second table and populate it in the first table.

    I'm not sure if we have for each loop in sql. Please help me, thanks in advance. 

    Friday, August 23, 2019 7:15 PM

Answers

  • I figured it out, I did it using the below sql statements.

    update dbo.UserActionHistory 
    set user_guid = udb.userguid
    from dbo.users as udb
    where dbo.UserActionHistory.userid= udb.userid

    Thanks.


    Email address update for alerts

    Friday, August 23, 2019 9:04 PM

All replies

  • I figured it out, I did it using the below sql statements.

    update dbo.UserActionHistory 
    set user_guid = udb.userguid
    from dbo.users as udb
    where dbo.UserActionHistory.userid= udb.userid

    Thanks.


    Email address update for alerts

    Friday, August 23, 2019 9:04 PM
  • @Anantha Asamani 

    Glad to hear that issue got fixed. Appreciate for sharing the steps which helped you, this would certainly benefit other community members

    Monday, August 26, 2019 7:06 AM
    Moderator