当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL Server中Update的用法

MSSQL
SQL Server:SQL Server Maintenance Plan Wizard
SQL Server:SQL mai的配置和使用,你有所研究吗?
SQLServer:数据库的定时作业设置(经典)
SQL Server:小编浅谈SQL全文本检索方法
SQL Server:小编经验谈设计数据库表和字段
SQL Server:小编浅谈数据库完整性之约束
SQL Server:容易忽视的动态约束
SQL Server:时态数据库的时间间隔
SQL Server:浅谈数据库的安全性
SQL Server:不得不看的数据库设计技巧(看了终身受益)
SQL Server:数据库中的快照
SQL Server:数据库管理中关系代数的语法
SQL Server:关系代数中的语义
验证SQL保留字
精妙的SQL语句
SQL Server 数据库管理常用的SQL和T-SQL语句
SQL SERVER 与ACCESS、EXCEL的数据转换
SQL SERVER的数据类型
未公开的SQL Server口令的加密函数
SQl 语句(常见)

MSSQL 中的 SQL Server中Update的用法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 107 ::
收藏到网摘: n/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