求一个自动添加备注的触发器......(更新)

littlefat 2004-07-05 10:26:20
表结构:
单位ID UnitID
单位名称 UnitName
联系电话 Phone
联系人 ContName
备注 Remark(Text字段)

要求写一触发器,当更新UnitName字段的时候,自动添加备注到Remark字段(累加方式)

例如有某行初始数据:
UnitID UnitName Phone ContName Remark
10 单位甲 11223344 某甲 (Null)

当执行:Update TableA set UnitName='单位乙',Phone='22334455',ContName='某乙' where UnitID=10后,自动添加备注到Remark字段,更新后的该行数据为:

UnitID UnitName Phone ContName Remark
10 单位乙 22334455 某乙 '2004-7-5:更新单位名称,原单位名称是单位甲,电话11223344;更新后单位名称单位乙,电话22334455'

再次更新:Update TableA set UnitName='单位丙',Phone='33445566',ContName='某乙' where UnitID=10后,再次自动添加备注到Remark字段,更新后的该行数据为:

UnitID UnitName Phone ContName Remark
10 单位丙 33445566 某乙 '2004-7-5:更新单位名称,原单位名称是单位甲,电话11223344;更新后单位名称单位乙,电话22334455<cr>2004-7-6:更新单位名称,原单位名称是单位乙,电话22334455,更新后单位名称单位丙,电话33445566'

即每次更新时,Remark的内容是累加的。。。

谢谢!
...全文
161 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2004-07-06
  • 打赏
  • 举报
回复
因为你的表中,remark 可以是NULL,所以你的方法会有问题.

另外,remark 没有值的时候,不应该写回车换行符吧?
littlefat 2004-07-06
  • 打赏
  • 举报
回复
感谢zjcxc(邹建)兄。。。

学习啊。。。

研究了一个半小时,我也自己写了一个,不过是参考zjcxc(邹建)兄的:

(最新添加的备注在最上面)
create trigger tr_update on tablea for update
as
if update(phone)
begin
declare @oldvalue varchar(8000),@newvalue varchar(8000)
declare @unitid int

select @oldvalue=phone,@unitid=unitid from deleted
select @newvalue=phone from inserted

declare @insertedremark varchar(8000)

Set @insertedremark='New:'+@newvalue+'Old:'+@oldvalue+char(13)+char(10)

declare @ptr varbinary(16)

select @ptr=textptr(remark) from tablea where unitid=@unitid

updatetext tablea.remark @ptr 0 0 @insertedremark
end
go

--测试
declare @a varchar(8000)

update tablea set unitname='unitb',phone='000' where unitid=10
select @a=remark from tablea where unitid=10
print @a

update tablea set unitname='unitb',phone='111' where unitid=10
select @a=remark from tablea where unitid=10
print @a

所影响的行数为 1 行)

New:000 Old:111
New:111 Old:000
New:000 Old:111

(所影响的行数为 1 行)

New:111 Old:000
New:000 Old:111
New:111 Old:000
New:000 Old:111
zjcxc 2004-07-05
  • 打赏
  • 举报
回复
--测试

--测试数据
create table TableA(UnitID int,UnitName varchar(10),Phone varchar(10),ContName varchar(10),Remark Text)
insert TableA select 10,'单位甲','11223344','某甲',null
go

--触发器
create trigger tr_update on TableA
for update
as
if update(UnitName) or update(Phone)
begin
declare tb cursor local for
select i.UnitID,Remark=convert(char(10),getdate(),120)
+':更新单位名称,原单位名称是'+d.UnitName+',电话'
+d.Phone+',更新后单位名称'+i.UnitName+',电话'+i.Phone
from inserted i join deleted d on i.UnitID=d.UnitID

declare @ptr varbinary(16),@s varchar(8000),@UnitID int
open tb
fetch tb into @UnitID,@s
while @@fetch_status=0
begin
update TableA set Remark=@s
where UnitID=@UnitID
and Remark is null
if @@rowcount=0
begin
select @ptr=textptr(Remark),@s=char(13)+char(10)+@s
from TableA
where UnitID=@UnitID
updatetext TableA.Remark @ptr null null @s
end
fetch tb into @UnitID,@s
end
close tb
deallocate tb
end
go

--更新记录
Update TableA set UnitName='单位乙',Phone='22334455',ContName='某乙'
where UnitID=10

Update TableA set UnitName='单位丙',Phone='33445566',ContName='某乙'
where UnitID=10
go

--显示更新结果
select * from TableA
go

--删除测试
drop table TableA

/*--测试结果

UnitID UnitName Phone ContName Remark
----------- ---------- ---------- ---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
10 单位丙 33445566 某乙 2004-07-05:更新单位名称,原单位名称是单位甲,电话11223344,更新后单位名称单位乙,电话22334455
2004-07-05:更新单位名称,原单位名称是单位乙,电话22334455,更新后单位名称单位丙,电话33445566

(所影响的行数为 1 行)
--*/
zheninchangjiang 2004-07-05
  • 打赏
  • 举报
回复
UP

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧