最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。

本篇是触发器专题,有很多触发器的问题。

1、触发器问题,插入数据时,自动更新表的数据

http://bbs.csdn.net/topics/390634682

表1有字段1,字段2
插入数据4行
字段1   字段2
101
102
101
102
我想通过触发器,直接更新字段2,实现
字段1   字段2
101     101+1
102     102+1
101     101+2
102     102+2
这样的功能,请各位帮忙看看,如何来实现,

在插入数据的时候,实现更新。


方法1,适合2005及以后的版本:

--drop table tb  create table tb (字段1 int,   字段2 int,  字段3 int,  字段4 int)   drop trigger trigger_tb  create trigger dbo.trigger_tb on tb for insert as  ;with t as ( select tb.*,        row_number() over(partition by tb.字段1 order by tb.字段4) as rownum from tb  inner join inserted i         on tb.字段1 = i.字段1            and tb.字段3 = i.字段3            and tb.字段4 = i.字段4 )  update t set 字段2 = 字段1+rownum  go   insert into tb select 101,null,            1,      1 union all select 102,null,  1,      2 union all select 101,null,  1,      3 union all select 102,null,  1,      4   --查询 select * from tb /* 字段1	字段2	字段3	字段4 101	    102	    1	    1 102	    103	    1	    2 101	    103	    1	    3 102	    104	    1	    4 */

方法2,适合2000:

--drop table tb  create table tb (字段1 int,   字段2 int,  字段3 int,  字段4 int)   --drop trigger trigger_tb  create trigger dbo.trigger_tb on tb for insert as  update tb set 字段2 = t1.字段1 + (select count(*) from tb t2                         where t2.字段1 = t1.字段1                              and t2.字段4 <= t1.字段4) from tb t1  inner join inserted i         on t1.字段1 = i.字段1            and t1.字段3 = i.字段3            and t1.字段4 = i.字段4  go   insert into tb select 101,null,            1,      1 union all select 102,null,  1,      2 union all select 101,null,  1,      3 union all select 102,null,  1,      4   --查询 select * from tb /* 字段1	字段2	字段3	字段4 101	    102	    1	    1 102	    103	    1	    2 101	    103	    1	    3 102	    104	    1	    4 */


另一个例子,SQL Server2000触发器实现一个表的更新:

--drop table mocta  create table purtb (请购单号 varchar(10),参考单号 varchar(10),备注 varchar(50))  create table mocta (工单单号 varchar(10),订单单号 varchar(10))   insert into purtb select '101','201','301' union all select '102','302','302' union all select '103','备料','备料'  insert into mocta select '201','301' union all select '202','302' go  --drop trigger trigger_purtb_insert    create trigger dbo.trigger_purtb_insert on purtb for insert as  update purtb set 备注 = isnull((select t1.订单单号                    from mocta t1                     where i.参考单号 = t1.工单单号),                   i.参考单号) from inserted i where purtb.请购单号 = i.请购单号 and                   purtb.参考单号 = i.参考单号             go   insert into purtb(请购单号,参考单号) select '104','201' union all select '105','xxx'     --查询 select * from purtb /* 请购单号	参考单号	备注 101	    201	    301 102	    302	    301 103	    备料	    备料 104	    201	    301 105	    xxx	    xxx */


2、在触发器中,当明细表插入数据时调用主表对应的数据

http://bbs.csdn.net/topics/390631301

大家好,由于金蝶ERP没法根据报价单里面的备品率自动算出备品数。所以需要建一个触发起。

触发器我是建在明细表上的,当明细表插入数据时调用主表对应的数据,关键是这时主表的数据好像也没提交到数据库,请问有什么方法可以调用到,谢谢。


我的回复:

通过了下面的实验,验证了,如果主表的事务还没有提交,但又想在触发器中用主表对应的数据,来计算备用品,是完全可以的。


因为触发器是和触发操作,在同一个事务中。
只要你的插入主表,插入明细表,在同一个事务中,那么就会和触发器也在同一个事务中,那么虽然主表操作没提交,也能在触发器中查询到这条还没有提交的主表记录。

--主表 create table t1 ( id int primary key, v varchar(20) )  --明细表 create table t2 (idd int primary key, id int references t1(id), vv varchar(20) )  insert into t1 select 1,'aa'  insert into t2 select 1,1,'xxx'  union all select 2,1,'yyy'  go   create trigger dbo.trigger_t2 on dbo.t2 for insert  as  --查询主表t1,发现刚才插入数据的事务,虽然没有提交 --但是在触发器中,完全可以查询到刚才插入到主表的数据 select * from t1  --下面可以引用t1表,也就是主表,和明细表,来计算出备品数  go   begin tran   insert into t1 select 2,'bb'  insert into t2 select 3,2,'123'   --先不提交事务 --commit /* id	v 1	aa 2	bb */