本例子是为了把数据库中所有的日期时间类型的毫秒该为0,也可以用于其它比如改变值
-- 第一种方法:***********************************************************
--1.所有包含日期时间型字段表
select identity(int,1,1) as lsh,* into #a from sysobjects where type = 'U' --所有用户表
--变量存储表数量,循环变量,更新时间语句
declare @tcount int,@x int,@datetimecolumnscount int,@y int
declare @tablename varchar(50),@columnname varchar(50),@updatesql varchar(1000)
select @tcount = count(1) from #a
set @x = 1
set @y = 1
--循环控制
while(@x <= @tcount)
begin
select @tablename = name from #a where lsh = @x
--2.-----------------------------------------------
--获取表中的所有列
select identity(int,1,1) as lsh,c.name, t.name as type,@tablename as tablename into #b
from syscolumns c join systypes t
on c.xtype=t.xtype
where t.name <> 'sysname' and c.id=object_id(@tablename)
and t.name = 'datetime'
--看该表中是否有日期类型
select @datetimecolumnscount = count(1) from #b
insert into a select * from #b
--3.如果有日期类型
if(@datetimecolumnscount > 0)
begin
while(@y <= @datetimecolumnscount)
begin
select @columnname = name from #b where lsh = @y
set @updatesql = 'update '+@tablename+' set '+@columnname + ' = dateadd(millisecond,-datepart(millisecond,'+@columnname+'),'+@columnname+')'
exec(@updatesql)
set @y=@y+1
end
set @y = 1
end
drop table #b
-------------------------------------------------
set @x = @x + 1
end
drop table #a
-- 第二种方法:***********************************************************
使用information_schema.columns 可以获取关于列的所有视图信息,当然这个之后进行过滤,然后还是要用 循环 处理的。