首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>数据库>SQL数据库>Sql Server中Update新用法案例心得
Sql Server中Update新用法案例心得
来源: 发布时间:2008-02-27 发布人: 浏览: 人次   字体: [ ]  

问题: 表中有两字段: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,@date2else 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实用经验技巧集合大放送
·sql语句联合删除多个表技巧
·实现sql server数据库的自动备份功能
·sql2005判断字符出现次数代码
·SQL2000和2005下行列转换实例
·SQL数据库开发常用语句汇总
·如何修改sqlserver数据库名称
·Sql Server端口连接的问题
·SQL Server下无限级分类查询解决办法
·Sqlserver中Union与Case的使用效率案...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·SQL数据库实现用SQL语句根据...
·SqlServer 2005 Express的配...
·sql排序规则的简介及其修改操
·如何用sql语句为字段添加和去
·sqlserver数据库日志文件压缩
·sql语句获得数据库某表字段的
·ISNUMERIC函数介绍及其要注意
·SQLServer2005导入xml文件实...
·SQL Server2005中删除表中重...
·如何手工启动SQLServer的全文
·sql删除数据库中的所有表语句
·SqlServr利用触发器来控制某...
·SQLServer常用日期格式转换方
·用sql实现查询重复记录的代码
·SQLServer2005分解导入xml文...
热 门 文 章
·图解SQL2008安装向导的使用
·实现Excel数据导入到SQL2005...
·SQLServer常用日期格式转换方...
·MS sql安装时“无法在COM+目...
·sql语句联合删除多个表技巧
·sql删除数据库中的所有表语句
·Sql Server端口连接的问题
·如何在SQL Server中得到自动...
·sql排序规则的简介及其修改操...
·SQL Server2005中删除表中重...
·分析SQL2005最新高效分页方法
·小解sqlServer中临时表及表变...
·用sql实现查询重复记录的代码
·MS SQL2005无法远程连接问题...
·如何修改sqlserver数据库名称
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .