zoukankan      html  css  js  c++  java
  • SQL 两个表根据同一标识更改次表信息

    表结构如下:
    create table tab1(
     id int identity,
     tname varchar(50)
     )
    
    create table tab2
    (
    idd varchar(10),
    iname varchar(50)
    )
    
    
    insert into tab1(tname ) //表1数据                  
    select 'a0'
    union all select 'b1'
    union all select 'b2'
    union all select 'b3'
    union all select 'b4'
    union all select 'b5'
    union all select 'b6'
    union all select 'b7'
    
    
    insert into tab2(iname)
     //表2数据              
     select 'a0'
    union all select 'b1'
    union all select 'b2'
    union all select 'b3'
    union all select 'b4'
    union all select 'b5'
    union all select 'b6'
    union all select 'b7'
    
    
    表一与表二有个共同点,就是tname和iname一样,从而我的需求为,将表一中的id取到表2中对应iname相同值的,从而更新表2中的值
    
    解决方法两种:
    
    
    1:
    update tab2 set tab2.idd = tab1.id from tab2 left join tab1 on tab2.iname=tab1.tname



    2:
     declare @tab_Name varchar(20)
     declare @tab_Name1 varchar(20)
    
    declare @tab_Name2 varchar(20)
    declare @tab_Name3 varchar(20)
    declare @tab1 table (sid int identity ,tid varchar(20),tname varchar(20))
    declare @tcount int
    
    set @tab_Name1='USE_0088_0006'
    
    insert into @tab1 select 所属支部, 英文缩写 from [人力资源-人员-基本情况]
    
    
    
    set @tcount =1
     while @tcount<1133
       begin
          select  @tab_Name2=tid,@tab_Name3=tname from @tab1 where sid=@tcount
    
           update USE_0087 set  USE_0087_0048=@tab_Name2 where USE_0087_0049=@tab_Name3
           set @tcount =@tcount+1
       end
       select * from USE_0087
    



    方法还可以优化



    
    

  • 相关阅读:
    8皇后问题
    求1到n,n个整数的全排列
    求最小周期串
    如何用java完成一个中文词频统计程序
    蛇形矩阵
    第一个算法程序
    java 继承练习题8
    java 继承练习题7
    java 继承练习题6
    java 继承练习题5
  • 原文地址:https://www.cnblogs.com/yhongl/p/3937917.html
Copyright © 2011-2022 走看看