Update SQL table with random value from other table

后端 未结 6 1788
挽巷
挽巷 2020-12-31 06:56

On Microsoft SQL Server 2008, I have a table with Products:

Id | Name | DefaultImageId

And one with Images:

Id | ProductId |

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 07:40

    declare @carg_id as int
    DECLARE cursor_cargo CURSOR FOR 
        SELECT carg_id FROM cargo
    
    OPEN cursor_cargo
    
    FETCH NEXT FROM cursor_cargo
    INTO @carg_id
    WHILE  @@FETCH_STATUS =0
    BEGIN
    
        update cargo set ciud_id = (
        select top 1 ciud_id from ciudad 
        inner join Pais on Pais.pais_id = ciudad.pais_id
        where ciudad.pais_id is not null and empr_id is  null and pais_status =1 order by  NEWID()) WHERE CARG_ID = @CARG_ID
    
        FETCH NEXT FROM cursor_cargo
            INTO @carg_id
    END
    CLOSE cursor_cargo
    DEALLOCATE cursor_cargo
    

提交回复
热议问题