触发器中如何对依赖于另一字段的字段进行赋值?

huazai 2003-07-29 09:57:33
在数据库表中添加一条记录,其中表中的一个字段a为自增字段,另一个字段b的值依赖于前一个字段的值,我想在该表上建立个触发器,当添加记录时,能够把添加记录的b字段的值同时进行更改,请问怎么搞,最好在触发器中,因为很多地方有此操作。
...全文
78 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdhdy 2003-07-29
  • 打赏
  • 举报
回复
create trigger trig_table1_insert on table1
for insert
as
update table1 set b = a from inserted where table1.a = inserted.a
txlicenhe 2003-07-29
  • 打赏
  • 举报
回复
CREATE TRIGGER yourTriggerName on yourTable
For INSERT
AS
BEGIN
Update B set yourField = newValue From B join Inserted on B.KeyField = Inserted.KeyField
END
woyaofengle 2003-07-29
  • 打赏
  • 举报
回复
--create table a(CategoryID int identity(1,1),name char(20) null, SubCategory char(10))
--insert into a (name) values('sub11')
--insert into a (name) values('sub12')
--insert into a (name) values('sub13')
--insert into a (name) values('sub14')
--insert into a (name) values('sub21')
--insert into a (name) values('sub22')
go
create trigger t1 on a
instead of insert
as
begin
insert into a (name,SubCategory) select name,cast(CategoryID as char(10))+'wofengle' from inserted
end
--select * from a
--insert into a (name) values('sub23')
--drop trigger t1
但执行时有问题,还望大侠们多多指点
woyaofengle 2003-07-29
  • 打赏
  • 举报
回复
--create table a(CategoryID int identity(1,1),name char(20) null, SubCategory char(10))
--insert into a (name) values('sub11')
--insert into a (name) values('sub12')
--insert into a (name) values('sub13')
--insert into a (name) values('sub14')
--insert into a (name) values('sub21')
--insert into a (name) values('sub22')
go
create trigger t1 on a
instead of insert
as
begin
insert into a (name,SubCategory) select name,cast(CategoryID as char(10))+'wofengle' from inserted
end
--select * from a
--insert into a (name) values('sub23')
--drop trigger t1
woyaofengle 2003-07-29
  • 打赏
  • 举报
回复
create trigger t1 on table1
for insert
as
update table1 set b = a+100 from inserted where table1.a = inserted.a
愉快的登山者 2003-07-29
  • 打赏
  • 举报
回复
create trigger t1 on table1
for insert
as
update table1 set b = a from inserted where table1.a = inserted.a
愉快的登山者 2003-07-29
  • 打赏
  • 举报
回复
create trigger t1 on table1
for insert
as
update table1 set b = a from inserted where table1.id = inserted.id
pengdali 2003-07-29
  • 打赏
  • 举报
回复
CREATE TRIGGER 名 on 表
INSTEAD OF INSERT
AS
BEGIN
INSERT 表 (列1,列2,列3) SELECT 列1,列2,列1+列2-100 FROM inserted
END

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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