现在的问题是,在修改表A的birthday时表B中的age不能跟随变化,比如张三生于1970年,第一次到访日期是2000年,这时age是30;2002年到访,这时age是32岁,我现在在表A中更改张三的出生日期为1960年,但B中的年龄不会同步到40岁和42岁。
所以想写一个触发器,看了别的关于触发器提问的帖子,基本写法能明白,但是不知道SQL SERVER 2000里有没有日期间的函数,对时间要求不是很精准,比如类似 datedifference(A.birthday,B.visit_date)/365这样的就可以,但最好是根据年月日判断的:)
先谢谢大家了
6 个解决方案
#1
DATEDIFF(<datepart> ,<number> ,<date>)
--函数返回两个指定日期在datepart 方面的不同之处
例:
datediff(yy,a.birthday,b.visit_date)
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
--函数返回两个指定日期在datepart 方面的不同之处
例:
datediff(yy,a.birthday,b.visit_date)
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
#2
create trigger t_birthday
on B
for update
as
update B set B.age=datediff(yy,A.birthday,B.birthday) from B
inner join A on B.birthday=A.birthday
go
on B
for update
as
update B set B.age=datediff(yy,A.birthday,B.birthday) from B
inner join A on B.birthday=A.birthday
go
#3
上面字段名写错了:
datediff(yy,A.birthday,B.visit_date)
datediff(yy,A.birthday,B.visit_date)
#4
/* 假设A表和B表都有一个id来表示一个人 */
create trigger dbo.tri_A_Bir
on 表A for update
as
if @@rowcount=0
return
if update (birthday)
update B
set age=datediff(y,i.birthday,B.visit_date)
from 表B B join inserted i
on B.[id]=i.[id]
Go
create trigger dbo.tri_A_Bir
on 表A for update
as
if @@rowcount=0
return
if update (birthday)
update B
set age=datediff(y,i.birthday,B.visit_date)
from 表B B join inserted i
on B.[id]=i.[id]
Go
#5
create table A([name] varchar(10),birthday datetime)
insert into A
select '张三','1970-12-01 08:40:10.460'union all
select '李四','1975-01-01 08:40:10.460'union all
select '王五','1980-03-01 08:40:10.460'union all
select '赵六','1985-05-01 08:40:10.460'
create table B([name] varchar(10),visit_date datetime,age int)
insert into B
select '张三','2000-12-01 08:40:10.460',30 union all
select '张三','2002-12-01 08:40:10.460',32 union all
select '李四','2000-12-01 08:40:10.460',25 union all
select '王五','2000-12-01 08:40:10.460',20 union all
select '赵六','2000-12-01 08:40:10.460',15
--创建触发器
CREATE trigger trg_AutoReviseB on dbo.A
for update
as
select [name]from inserted
update b set age=datediff(yy,a.birthday,b.visit_date)from b b right join a a on b.[name]=a.[name]
where b.[name] in(select [name]from inserted)
go
--测试
select * from b
update a set birthday='1960-12-01 08:40:10.460'where [name]='张三'
select * from b
drop table a,b
/*
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 30
张三 2002-12-01 08:40:10.460 32
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
name
----------
张三
(所影响的行数为 1 行)
(所影响的行数为 1 行)
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 40
张三 2002-12-01 08:40:10.460 42
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
*/
insert into A
select '张三','1970-12-01 08:40:10.460'union all
select '李四','1975-01-01 08:40:10.460'union all
select '王五','1980-03-01 08:40:10.460'union all
select '赵六','1985-05-01 08:40:10.460'
create table B([name] varchar(10),visit_date datetime,age int)
insert into B
select '张三','2000-12-01 08:40:10.460',30 union all
select '张三','2002-12-01 08:40:10.460',32 union all
select '李四','2000-12-01 08:40:10.460',25 union all
select '王五','2000-12-01 08:40:10.460',20 union all
select '赵六','2000-12-01 08:40:10.460',15
--创建触发器
CREATE trigger trg_AutoReviseB on dbo.A
for update
as
select [name]from inserted
update b set age=datediff(yy,a.birthday,b.visit_date)from b b right join a a on b.[name]=a.[name]
where b.[name] in(select [name]from inserted)
go
--测试
select * from b
update a set birthday='1960-12-01 08:40:10.460'where [name]='张三'
select * from b
drop table a,b
/*
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 30
张三 2002-12-01 08:40:10.460 32
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
name
----------
张三
(所影响的行数为 1 行)
(所影响的行数为 1 行)
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 40
张三 2002-12-01 08:40:10.460 42
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
*/
#6
不好意思,老板让干活,今天才想起来,以上答案都对,给分。
谢谢各位了
谢谢各位了
#1
DATEDIFF(<datepart> ,<number> ,<date>)
--函数返回两个指定日期在datepart 方面的不同之处
例:
datediff(yy,a.birthday,b.visit_date)
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
--函数返回两个指定日期在datepart 方面的不同之处
例:
datediff(yy,a.birthday,b.visit_date)
====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
#2
create trigger t_birthday
on B
for update
as
update B set B.age=datediff(yy,A.birthday,B.birthday) from B
inner join A on B.birthday=A.birthday
go
on B
for update
as
update B set B.age=datediff(yy,A.birthday,B.birthday) from B
inner join A on B.birthday=A.birthday
go
#3
上面字段名写错了:
datediff(yy,A.birthday,B.visit_date)
datediff(yy,A.birthday,B.visit_date)
#4
/* 假设A表和B表都有一个id来表示一个人 */
create trigger dbo.tri_A_Bir
on 表A for update
as
if @@rowcount=0
return
if update (birthday)
update B
set age=datediff(y,i.birthday,B.visit_date)
from 表B B join inserted i
on B.[id]=i.[id]
Go
create trigger dbo.tri_A_Bir
on 表A for update
as
if @@rowcount=0
return
if update (birthday)
update B
set age=datediff(y,i.birthday,B.visit_date)
from 表B B join inserted i
on B.[id]=i.[id]
Go
#5
create table A([name] varchar(10),birthday datetime)
insert into A
select '张三','1970-12-01 08:40:10.460'union all
select '李四','1975-01-01 08:40:10.460'union all
select '王五','1980-03-01 08:40:10.460'union all
select '赵六','1985-05-01 08:40:10.460'
create table B([name] varchar(10),visit_date datetime,age int)
insert into B
select '张三','2000-12-01 08:40:10.460',30 union all
select '张三','2002-12-01 08:40:10.460',32 union all
select '李四','2000-12-01 08:40:10.460',25 union all
select '王五','2000-12-01 08:40:10.460',20 union all
select '赵六','2000-12-01 08:40:10.460',15
--创建触发器
CREATE trigger trg_AutoReviseB on dbo.A
for update
as
select [name]from inserted
update b set age=datediff(yy,a.birthday,b.visit_date)from b b right join a a on b.[name]=a.[name]
where b.[name] in(select [name]from inserted)
go
--测试
select * from b
update a set birthday='1960-12-01 08:40:10.460'where [name]='张三'
select * from b
drop table a,b
/*
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 30
张三 2002-12-01 08:40:10.460 32
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
name
----------
张三
(所影响的行数为 1 行)
(所影响的行数为 1 行)
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 40
张三 2002-12-01 08:40:10.460 42
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
*/
insert into A
select '张三','1970-12-01 08:40:10.460'union all
select '李四','1975-01-01 08:40:10.460'union all
select '王五','1980-03-01 08:40:10.460'union all
select '赵六','1985-05-01 08:40:10.460'
create table B([name] varchar(10),visit_date datetime,age int)
insert into B
select '张三','2000-12-01 08:40:10.460',30 union all
select '张三','2002-12-01 08:40:10.460',32 union all
select '李四','2000-12-01 08:40:10.460',25 union all
select '王五','2000-12-01 08:40:10.460',20 union all
select '赵六','2000-12-01 08:40:10.460',15
--创建触发器
CREATE trigger trg_AutoReviseB on dbo.A
for update
as
select [name]from inserted
update b set age=datediff(yy,a.birthday,b.visit_date)from b b right join a a on b.[name]=a.[name]
where b.[name] in(select [name]from inserted)
go
--测试
select * from b
update a set birthday='1960-12-01 08:40:10.460'where [name]='张三'
select * from b
drop table a,b
/*
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 30
张三 2002-12-01 08:40:10.460 32
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
name
----------
张三
(所影响的行数为 1 行)
(所影响的行数为 1 行)
name visit_date age
---------- ------------------------------------------------------ -----------
张三 2000-12-01 08:40:10.460 40
张三 2002-12-01 08:40:10.460 42
李四 2000-12-01 08:40:10.460 25
王五 2000-12-01 08:40:10.460 20
赵六 2000-12-01 08:40:10.460 15
(所影响的行数为 5 行)
*/
#6
不好意思,老板让干活,今天才想起来,以上答案都对,给分。
谢谢各位了
谢谢各位了