遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

Insert Into ...Select 加 Update From 同步表数据

  公司站点新增加了个功能,需要对原来用户表扩展几个新字段,为了避免修改老的代码,采用加入新表的方式(建立一张ext_User(Username,UserId[pk],AccountsType..)表),原用户表sures(suid[pk],suname,aaaflag[vip用户int],advUser[高级用户bit])

 ext_user.AccountsType 取0,1,9分别表示普通用户,高级用户跟,vip用户,而在sures表中 aaaflag=1表示vip,advUser=1表示高级用户,现在需要把sures表中的数据导入到ext_user表中,并设置相应属性(AccountsType)

 考虑ext_user表有部分数据的情况,脚本如下:

 

--同步数据
Insert Into ext_User(UserId,UserName,Title,[Description],Summary,Keys,AccountsType)
Select s.suid,s.suname,s.sucompany,'','','',0
From sures s left join ext_user e On s.suid=e.userid
where e.UserId is Null
go
--更新高级用户
update ext_user Set AccountsType=1
From ext_user e inner join sures  s on e.userId=s.suid where s.advUser=1
go
--更新vip用户
update ext_user Set AccountsType=9
From ext_user e inner join sures  s on e.userId=s.suid where s.aaaflag=1
go
--同时更新高级用户跟vip用户(可以选择上面两句实现)
update ext_user Set AccountsType=
  case s.aaaflag
       when 1 Then 9
       when 0 then
                  case s.advUser
                    when 1 then 1
                    when 0 then 0

                  End
  End
From ext_user e inner join sures  s on e.userId=s.suid

 

posted on 2009-04-02 10:06  遗忘海岸  阅读(1421)  评论(0编辑  收藏  举报