Sql Server中Update用法的心得
2008-02-27 14:25
369 查看
Sql Server中Update用法的心得
问题: 表中有两字段:id_no (varchar) , in_date (datetime) ,把in_date 相同的记录的in_date依次累加1秒,
使in_date没有相同的记录。
如以下原始数据:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:02
5794 2003-9-1 14:42:02
5795 2003-9-1 14:42:03
5796 2003-9-1 14:42:03
5797 2003-9-1 14:42:03
5831 2003-9-1 14:42:04
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:14
得到结果是:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:03
5794 2003-9-1 14:42:04
5795 2003-9-1 14:42:05
5796 2003-9-1 14:42:06
5797 2003-9-1 14:42:07
5831 2003-9-1 14:42:08
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:15
处理方法:
--建立测试环境
create table a(id_no varchar(8),in_date datetime)
go
insert into a select '5791','2003-9-1 14:42:02'
union all select '5792','2003-9-1 14:42:02'
union all select '5794','2003-9-1 14:42:02'
union all select '5795','2003-9-1 14:42:03'
union all select '5796','2003-9-1 14:42:03'
union all select '5797','2003-9-1 14:42:03'
union all select '5831','2003-9-1 14:42:04'
union all select '5832','2003-9-1 14:42:04'
union all select '5833','2003-9-1 14:42:04'
union all select '5734','2003-9-1 14:42:02'
union all select '6792','2003-9-1 14:42:22'
union all select '6794','2003-9-1 14:42:22'
union all select '6795','2003-9-1 14:42:23'
union all select '6796','2003-9-1 14:42:23'
union all select '6797','2003-9-1 14:42:23'
union all select '6831','2003-9-1 14:42:34'
union all select '6832','2003-9-1 14:42:34'
union all select '6833','2003-9-1 14:42:54'
union all select '6734','2003-9-1 14:42:22'
go
--生成临时表,按照in_date排序
select * into # from a order by in_date
--相同的时间,加一秒。加完了不带重复的
declare @date1 datetime,@date2 datetime,@date datetime
update #
set @date=case when @date1=in_date or @date2>=in_date then dateadd(s,1,@date2) else in_date end,
@date1=in_date,
@date2=@date,
in_date=@date
--更新到基本表中去
update a set a.in_date=b.in_date from a a join # b on a.id_no=b.id_no
select * from a
drop table #,a
问题: 表中有两字段:id_no (varchar) , in_date (datetime) ,把in_date 相同的记录的in_date依次累加1秒,
使in_date没有相同的记录。
如以下原始数据:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:02
5794 2003-9-1 14:42:02
5795 2003-9-1 14:42:03
5796 2003-9-1 14:42:03
5797 2003-9-1 14:42:03
5831 2003-9-1 14:42:04
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:14
得到结果是:
id_no in_date
5791 2003-9-1 14:42:02
5792 2003-9-1 14:42:03
5794 2003-9-1 14:42:04
5795 2003-9-1 14:42:05
5796 2003-9-1 14:42:06
5797 2003-9-1 14:42:07
5831 2003-9-1 14:42:08
5832 2003-9-1 14:42:14
5833 2003-9-1 14:42:15
处理方法:
--建立测试环境
create table a(id_no varchar(8),in_date datetime)
go
insert into a select '5791','2003-9-1 14:42:02'
union all select '5792','2003-9-1 14:42:02'
union all select '5794','2003-9-1 14:42:02'
union all select '5795','2003-9-1 14:42:03'
union all select '5796','2003-9-1 14:42:03'
union all select '5797','2003-9-1 14:42:03'
union all select '5831','2003-9-1 14:42:04'
union all select '5832','2003-9-1 14:42:04'
union all select '5833','2003-9-1 14:42:04'
union all select '5734','2003-9-1 14:42:02'
union all select '6792','2003-9-1 14:42:22'
union all select '6794','2003-9-1 14:42:22'
union all select '6795','2003-9-1 14:42:23'
union all select '6796','2003-9-1 14:42:23'
union all select '6797','2003-9-1 14:42:23'
union all select '6831','2003-9-1 14:42:34'
union all select '6832','2003-9-1 14:42:34'
union all select '6833','2003-9-1 14:42:54'
union all select '6734','2003-9-1 14:42:22'
go
--生成临时表,按照in_date排序
select * into # from a order by in_date
--相同的时间,加一秒。加完了不带重复的
declare @date1 datetime,@date2 datetime,@date datetime
update #
set @date=case when @date1=in_date or @date2>=in_date then dateadd(s,1,@date2) else in_date end,
@date1=in_date,
@date2=@date,
in_date=@date
--更新到基本表中去
update a set a.in_date=b.in_date from a a join # b on a.id_no=b.id_no
select * from a
drop table #,a
相关文章推荐
- Sql Server中Update用法的心得
- [学习SQL SERVER 2005系列]关于INSERT、UPDATE 或 DELETE的OUTPUT及OUTPUT...INTO...用法
- SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
- SQL Server--OUTPUT及OUTPUT...INTO... 在 INSERT、UPDATE 或 DELETE的用法
- [sql server] update中变量的赋值用法 实现分组更新序号
- sql server 中ntext字段的批量替换(updatetext的用法)
- SQL Server--OUTPUT及OUTPUT...INTO... 在 INSERT、UPDATE 或 DELETE的用法
- SQL Server UPDATE语句的用法详解
- sql server 2005 学习心得(select查询语句用法)
- SQL SERVER 连续两个desc 和 Update From 用法
- [SQL Server心得]DERIVEDTBL的用法
- 关于SQL server的Update用法
- 14、SQL Server:sql update set from 的用法 (转)
- SQL Server UPDATE语句的用法详解
- Update语句更新多表时在SQL Server,Oracle,MySQL三种数据库中的用法
- sql server update 与case 用法
- SQL Server 的 Merge 关键字 用法解析
- asp.net ajax1.0基础回顾(三):UpdatePanel的基本用法
- 关于SQL的partition by 字段的一些用法心得
- SQL Server中的锁类型及用法