ms sql 下批量更新字段信息
2008-01-09 12:44
232 查看
2个表, ip_list 和 it_audit_checklist , 表'ip_list'中有字段:ip,user_name,dept,obu,remark,ip和obu为关键字。表'it_audit_checklist'中有字段:ip,obu,user_name,dept,pc_plus_install,ad_install等字段,ip和obu为关键字。 现在要以ip_list表中的内容更新it_audit_checklist表中的相应内容,以关键字ip和obu为基准。有2中方法,如下:
方法1:
declare @ip char(16),@obu char(4),@user_name char(30),@dept char(20),@count int
select *,identity(int,1,1) as sn into #t --创建临时表't',增加列'sn'表示行号
from ip_list
select @count=max(sn) --取出最大行号
from #t
while @count>0 --循环从临时表中取出数据并插入到表it_audit_checklist中
begin
select @ip=ip,@obu=obu,@user_name=user_name,@dept=dept
from #t
where sn=@count
update it_audit_checklist
set user_name=@user_name,dept=@dept
where ip=@ip and obu=@obu
select @count=@count-1
end
方法2:
update a
set a.user_name=b.user_name,a.dept=b.dept
from ip_list a inner join it_audit_checklist b
on a.ip=b.ip and a.obu=b.obu
方法1是比较传统的做法,方法2就更为简单,明了
方法1:
declare @ip char(16),@obu char(4),@user_name char(30),@dept char(20),@count int
select *,identity(int,1,1) as sn into #t --创建临时表't',增加列'sn'表示行号
from ip_list
select @count=max(sn) --取出最大行号
from #t
while @count>0 --循环从临时表中取出数据并插入到表it_audit_checklist中
begin
select @ip=ip,@obu=obu,@user_name=user_name,@dept=dept
from #t
where sn=@count
update it_audit_checklist
set user_name=@user_name,dept=@dept
where ip=@ip and obu=@obu
select @count=@count-1
end
方法2:
update a
set a.user_name=b.user_name,a.dept=b.dept
from ip_list a inner join it_audit_checklist b
on a.ip=b.ip and a.obu=b.obu
方法1是比较传统的做法,方法2就更为简单,明了
相关文章推荐
- 批量更新字段信息
- solrJ 1、批量更新数据时,会有部分数据没有更新上, 2.排序时使用整型、数字型字段,不要使用字符串类型
- mybatis 多字段、多表批量更新,判断条件也批量改变
- 如何批量修改sql中某个字段中的特定信息
- 批量更新与逐条更新分块提交的一次实例:修改客户信息表700万条中50万条记录
- 批量修改MS sql中一个字段值的部分内容的方法
- 更新部分字段(批量与非批量)
- Elasticsearch更新某条信息中的字段
- 批量更新表中某字段值
- 使用Ruby来实现批量更新AD中字段
- 两个表字段之间的批量更新实现的是将a表中的a1 更换成b表中的b2,条件是a1 = b1,并且a1和b1都是唯一的。
- 批量更新数据库表字段值四步走
- 如何批量修改sql中某个字段中的特定信息
- mysql和oracle增删改字段并且批量更新字段内容的操作
- 以传参方式批量更新供求信息发布时间
- SQL根据条件批量update更新字段案例
- 批量更新具有数千属性列的数据表的记录信息(SQL Server 2005)
- Kettle使用资源库情况下批量更新邮件组件相关信息
- 批量更新日期字段中的年