前事不忘,后事之师,不忘国耻!

 注册  找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1928|回复: 0

Sql Server中Update用法的心得

[复制链接]

Sql Server中Update用法的心得

[复制链接]
ehxz

主题

0

回帖

7159

积分

管理员

积分
7159
2008-2-29 10:53:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
问题: 表中有两字段: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
处理方法:

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif--建立测试环境
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
create
table a(id_no varchar(8),in_date datetime)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
go
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
insert
into a select
'5791','2003-9-1 14:42:02'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5792','2003-9-1 14:42:02'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5794','2003-9-1 14:42:02'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5795','2003-9-1 14:42:03'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5796','2003-9-1 14:42:03'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5797','2003-9-1 14:42:03'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5831','2003-9-1 14:42:04'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5832','2003-9-1 14:42:04'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5833','2003-9-1 14:42:04'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'5734','2003-9-1 14:42:02'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6792','2003-9-1 14:42:22'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6794','2003-9-1 14:42:22'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6795','2003-9-1 14:42:23'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6796','2003-9-1 14:42:23'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6797','2003-9-1 14:42:23'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6831','2003-9-1 14:42:34'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6832','2003-9-1 14:42:34'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6833','2003-9-1 14:42:54'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
union
all
select
'6734','2003-9-1 14:42:22'
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
go
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
--生成临时表,按照in_date排序
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
select
*
into # from a order
by in_date
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
--相同的时间,加一秒。加完了不带重复的
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
declare
@date1
datetime,@date2
datetime,@date
datetime
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
update #
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif  
set
@date=case
when
@date1=in_date or
@date2>=in_date then
dateadd(s,1,@date2) else in_date end,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif      
@date1=in_date,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif      
@date2=@date,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif      in_date
=@date
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
--更新到基本表中去
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
update a set a.in_date=b.in_date from a a join # b on a.id_no=b.id_no
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
select
*
from a
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
drop
table #,a
免责申明1、欢迎访问本站,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2、本文内容仅代表作者观点,不代表本站立场,作者自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3、本文内容,包括但不限于源码、文字、图片等,仅供参考。本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容。
4、如本帖侵犯到任何版权问题,请立即告知本站 ,本站将及时删除并致以最深的歉意!客服邮箱:admin@dbabbs.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|DBA论坛中国 ( 鲁ICP备20017503号-2 )

GMT+8, 2024-5-11 03:08 , Processed in 0.095523 second(s), 11 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表